blob: d1e699ce44b5a29a3c421dae81b1496b8ca29970 [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
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010057/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010063#else
64 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065#endif
66 return( 0 );
67}
68
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020069/*
70 * Start a timer.
71 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020075 if( ssl->f_set_timer == NULL )
76 return;
77
78 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
79 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080}
81
82/*
83 * Return -1 is timer is expired, 0 if it isn't.
84 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020088 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020089
90 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020091 {
92 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020093 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020094 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095
96 return( 0 );
97}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098
Hanno Becker5aa4e2c2018-08-06 09:26:08 +010099static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
100 mbedtls_ssl_transform *transform );
101static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
102 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100103
104#define SSL_DONT_FORCE_FLUSH 0
105#define SSL_FORCE_FLUSH 1
106
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200107#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100108
109static uint16_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
110{
111 uint16_t mtu = ssl->conf->mtu;
112
113 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
114 return( (int) mtu );
115
116 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
117}
118
Hanno Becker67bc7c32018-08-06 11:33:50 +0100119static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
120{
121 size_t const bytes_written = ssl->out_left;
122 uint16_t const mtu = ssl_get_maximum_datagram_size( ssl );
123
124 /* Double-check that the write-index hasn't gone
125 * past what we can transmit in a single datagram. */
126 if( bytes_written > (size_t) mtu )
127 {
128 /* Should never happen... */
129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
130 }
131
132 return( (int) ( mtu - bytes_written ) );
133}
134
135static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
136{
137 int ret;
138 size_t remaining, expansion;
139 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
140
141#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
142 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
143
144 if( max_len > mfl )
145 max_len = mfl;
146#endif
147
148 ret = ssl_get_remaining_space_in_datagram( ssl );
149 if( ret < 0 )
150 return( ret );
151 remaining = (size_t) ret;
152
153 ret = mbedtls_ssl_get_record_expansion( ssl );
154 if( ret < 0 )
155 return( ret );
156 expansion = (size_t) ret;
157
158 if( remaining <= expansion )
159 return( 0 );
160
161 remaining -= expansion;
162 if( remaining >= max_len )
163 remaining = max_len;
164
165 return( (int) remaining );
166}
167
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200168/*
169 * Double the retransmit timeout value, within the allowed range,
170 * returning -1 if the maximum value has already been reached.
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200173{
174 uint32_t new_timeout;
175
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200176 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200177 return( -1 );
178
179 new_timeout = 2 * ssl->handshake->retransmit_timeout;
180
181 /* Avoid arithmetic overflow and range overflow */
182 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200183 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200184 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200185 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200186 }
187
188 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200190 ssl->handshake->retransmit_timeout ) );
191
192 return( 0 );
193}
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200196{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200197 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200199 ssl->handshake->retransmit_timeout ) );
200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200204/*
205 * Convert max_fragment_length codes to length.
206 * RFC 6066 says:
207 * enum{
208 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
209 * } MaxFragmentLength;
210 * and we add 0 -> extension unused
211 */
Angus Grattond8213d02016-05-25 20:56:48 +1000212static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200213{
Angus Grattond8213d02016-05-25 20:56:48 +1000214 switch( mfl )
215 {
216 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
217 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
218 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
219 return 512;
220 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
221 return 1024;
222 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
223 return 2048;
224 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
225 return 4096;
226 default:
227 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
228 }
229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200231
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200232#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_ssl_session_free( dst );
236 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200239 if( src->peer_cert != NULL )
240 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200241 int ret;
242
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200243 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200244 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200245 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200250 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200253 dst->peer_cert = NULL;
254 return( ret );
255 }
256 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200259#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 if( src->ticket != NULL )
261 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200262 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200263 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200264 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200265
266 memcpy( dst->ticket, src->ticket, src->ticket_len );
267 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200268#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269
270 return( 0 );
271}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200272#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
275int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200276 const unsigned char *key_enc, const unsigned char *key_dec,
277 size_t keylen,
278 const unsigned char *iv_enc, const unsigned char *iv_dec,
279 size_t ivlen,
280 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200281 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
283int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
284int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
285int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
286int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
287#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289/*
290 * Key material generation
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200293static int ssl3_prf( const unsigned char *secret, size_t slen,
294 const char *label,
295 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000296 unsigned char *dstbuf, size_t dlen )
297{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100298 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000299 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200300 mbedtls_md5_context md5;
301 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000302 unsigned char padding[16];
303 unsigned char sha1sum[20];
304 ((void)label);
305
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200306 mbedtls_md5_init( &md5 );
307 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200308
Paul Bakker5f70b252012-09-13 14:23:06 +0000309 /*
310 * SSLv3:
311 * block =
312 * MD5( secret + SHA1( 'A' + secret + random ) ) +
313 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
314 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
315 * ...
316 */
317 for( i = 0; i < dlen / 16; i++ )
318 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200319 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000320
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100321 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100322 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100323 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100324 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100325 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100326 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100327 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100328 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100329 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100330 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000331
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100332 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100333 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100334 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100335 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100336 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100337 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100338 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100339 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000340 }
341
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100342exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200343 mbedtls_md5_free( &md5 );
344 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000345
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500346 mbedtls_platform_zeroize( padding, sizeof( padding ) );
347 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000348
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100349 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200354static int tls1_prf( const unsigned char *secret, size_t slen,
355 const char *label,
356 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000357 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358{
Paul Bakker23986e52011-04-24 08:57:21 +0000359 size_t nb, hs;
360 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200361 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char tmp[128];
363 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 const mbedtls_md_info_t *md_info;
365 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100366 int ret;
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
373 hs = ( slen + 1 ) / 2;
374 S1 = secret;
375 S2 = secret + slen - hs;
376
377 nb = strlen( label );
378 memcpy( tmp + 20, label, nb );
379 memcpy( tmp + 20 + nb, random, rlen );
380 nb += rlen;
381
382 /*
383 * First compute P_md5(secret,label+random)[0..dlen]
384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100389 return( ret );
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
392 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
393 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
395 for( i = 0; i < dlen; i += 16 )
396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_md_hmac_reset ( &md_ctx );
398 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
399 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_md_hmac_reset ( &md_ctx );
402 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
403 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
406
407 for( j = 0; j < k; j++ )
408 dstbuf[i + j] = h_i[j];
409 }
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100412
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 /*
414 * XOR out with P_sha1(secret,label+random)[0..dlen]
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100420 return( ret );
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
423 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
424 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 for( i = 0; i < dlen; i += 20 )
427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_md_hmac_reset ( &md_ctx );
429 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
430 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_md_hmac_reset ( &md_ctx );
433 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
434 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
437
438 for( j = 0; j < k; j++ )
439 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
440 }
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100443
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500444 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
445 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447 return( 0 );
448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
452static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100453 const unsigned char *secret, size_t slen,
454 const char *label,
455 const unsigned char *random, size_t rlen,
456 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457{
458 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100459 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000460 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
462 const mbedtls_md_info_t *md_info;
463 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100464 int ret;
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100472
473 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000475
476 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100477 memcpy( tmp + md_len, label, nb );
478 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000479 nb += rlen;
480
481 /*
482 * Compute P_<hash>(secret, label + random)[0..dlen]
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100485 return( ret );
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
488 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
489 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100490
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100491 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_md_hmac_reset ( &md_ctx );
494 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
495 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_md_hmac_reset ( &md_ctx );
498 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
499 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000500
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100501 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000502
503 for( j = 0; j < k; j++ )
504 dstbuf[i + j] = h_i[j];
505 }
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100508
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500509 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
510 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000511
512 return( 0 );
513}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100516static int tls_prf_sha256( const unsigned char *secret, size_t slen,
517 const char *label,
518 const unsigned char *random, size_t rlen,
519 unsigned char *dstbuf, size_t dlen )
520{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100522 label, random, rlen, dstbuf, dlen ) );
523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200527static int tls_prf_sha384( const unsigned char *secret, size_t slen,
528 const char *label,
529 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000530 unsigned char *dstbuf, size_t dlen )
531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100533 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#endif /* MBEDTLS_SHA512_C */
536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
541 defined(MBEDTLS_SSL_PROTO_TLS1_1)
542static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200543#endif
Paul Bakker380da532012-04-18 16:10:25 +0000544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_SSL_PROTO_SSL3)
546static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
547static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200548#endif
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
551static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
552static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200553#endif
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
556#if defined(MBEDTLS_SHA256_C)
557static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
558static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
559static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200560#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_SHA512_C)
563static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
564static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
565static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000570{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200571 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 unsigned char keyblk[256];
574 unsigned char *key1;
575 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100576 unsigned char *mac_enc;
577 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000578 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200579 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 const mbedtls_cipher_info_t *cipher_info;
581 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_ssl_session *session = ssl->session_negotiate;
584 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
585 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100590 if( cipher_info == NULL )
591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100593 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100598 if( md_info == NULL )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100601 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100603 }
604
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000606 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_SSL_PROTO_SSL3)
609 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000610 {
Paul Bakker48916f92012-09-16 19:57:18 +0000611 handshake->tls_prf = ssl3_prf;
612 handshake->calc_verify = ssl_calc_verify_ssl;
613 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000614 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200615 else
616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
618 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000619 {
Paul Bakker48916f92012-09-16 19:57:18 +0000620 handshake->tls_prf = tls1_prf;
621 handshake->calc_verify = ssl_calc_verify_tls;
622 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000623 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200624 else
625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
627#if defined(MBEDTLS_SHA512_C)
628 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
629 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000630 {
Paul Bakker48916f92012-09-16 19:57:18 +0000631 handshake->tls_prf = tls_prf_sha384;
632 handshake->calc_verify = ssl_calc_verify_tls_sha384;
633 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000634 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000635 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_SHA256_C)
638 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000639 {
Paul Bakker48916f92012-09-16 19:57:18 +0000640 handshake->tls_prf = tls_prf_sha256;
641 handshake->calc_verify = ssl_calc_verify_tls_sha256;
642 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000643 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200644 else
645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200650 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000651
652 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 * SSLv3:
654 * master =
655 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
656 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
657 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200658 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200659 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 * master = PRF( premaster, "master secret", randbytes )[0..47]
661 */
Paul Bakker0a597072012-09-25 21:55:46 +0000662 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000665 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
668 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200669 {
670 unsigned char session_hash[48];
671 size_t hash_len;
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200674
675 ssl->handshake->calc_verify( ssl, session_hash );
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
678 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200681 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200683 {
684 hash_len = 48;
685 }
686 else
687#endif
688 hash_len = 32;
689 }
690 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200692 hash_len = 36;
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200695
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100696 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
697 "extended master secret",
698 session_hash, hash_len,
699 session->master, 48 );
700 if( ret != 0 )
701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100703 return( ret );
704 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200705
706 }
707 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200708#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100709 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
710 "master secret",
711 handshake->randbytes, 64,
712 session->master, 48 );
713 if( ret != 0 )
714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100716 return( ret );
717 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200718
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500719 mbedtls_platform_zeroize( handshake->premaster,
720 sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 }
722 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 /*
726 * Swap the client and server random values.
727 */
Paul Bakker48916f92012-09-16 19:57:18 +0000728 memcpy( tmp, handshake->randbytes, 64 );
729 memcpy( handshake->randbytes, tmp + 32, 32 );
730 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500731 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
733 /*
734 * SSLv3:
735 * key block =
736 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
737 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
738 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
739 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
740 * ...
741 *
742 * TLSv1:
743 * key block = PRF( master, "key expansion", randbytes )
744 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100745 ret = handshake->tls_prf( session->master, 48, "key expansion",
746 handshake->randbytes, 64, keyblk, 256 );
747 if( ret != 0 )
748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100750 return( ret );
751 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
754 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
755 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
756 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
757 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500759 mbedtls_platform_zeroize( handshake->randbytes,
760 sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
762 /*
763 * Determine the appropriate key, IV and MAC length.
764 */
Paul Bakker68884e32013-01-07 18:20:04 +0100765
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200766 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200769 cipher_info->mode == MBEDTLS_MODE_CCM ||
770 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000771 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200772 size_t taglen, explicit_ivlen;
773
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200774 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000775 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200776
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200777 /* All modes haves 96-bit IVs;
778 * GCM and CCM has 4 implicit and 8 explicit bytes
779 * ChachaPoly has all 12 bytes implicit
780 */
Paul Bakker68884e32013-01-07 18:20:04 +0100781 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200782 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
783 transform->fixed_ivlen = 12;
784 else
785 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200786
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200787 /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */
788 taglen = transform->ciphersuite_info->flags &
789 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
790
791
792 /* Minimum length of encrypted record */
793 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
794 transform->minlen = explicit_ivlen + taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100795 }
796 else
797 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200798 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
800 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200803 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100804 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000805
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200806 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000807 mac_key_len = mbedtls_md_get_size( md_info );
808 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200811 /*
812 * If HMAC is to be truncated, we shall keep the leftmost bytes,
813 * (rfc 6066 page 13 or rfc 2104 section 4),
814 * so we only need to adjust the length here.
815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000819
820#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
821 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000822 * HMAC implementation which also truncates the key
823 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000824 mac_key_len = transform->maclen;
825#endif
826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200828
829 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100830 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200832 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200834 transform->minlen = transform->maclen;
835 else
Paul Bakker68884e32013-01-07 18:20:04 +0100836 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200837 /*
838 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100839 * 1. if EtM is in use: one block plus MAC
840 * otherwise: * first multiple of blocklen greater than maclen
841 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200842 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
844 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100845 {
846 transform->minlen = transform->maclen
847 + cipher_info->block_size;
848 }
849 else
850#endif
851 {
852 transform->minlen = transform->maclen
853 + cipher_info->block_size
854 - transform->maclen % cipher_info->block_size;
855 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
858 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
859 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200860 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100861 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
864 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
865 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200866 {
867 transform->minlen += transform->ivlen;
868 }
869 else
870#endif
871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200874 }
Paul Bakker68884e32013-01-07 18:20:04 +0100875 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 }
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000879 transform->keylen, transform->minlen, transform->ivlen,
880 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
882 /*
883 * Finally setup the cipher contexts, IVs and MAC secrets.
884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000888 key1 = keyblk + mac_key_len * 2;
889 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000890
Paul Bakker68884e32013-01-07 18:20:04 +0100891 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000892 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000894 /*
895 * This is not used in TLS v1.1.
896 */
Paul Bakker48916f92012-09-16 19:57:18 +0000897 iv_copy_len = ( transform->fixed_ivlen ) ?
898 transform->fixed_ivlen : transform->ivlen;
899 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
900 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000901 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000902 }
903 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#endif /* MBEDTLS_SSL_CLI_C */
905#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200906 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000907 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000908 key1 = keyblk + mac_key_len * 2 + transform->keylen;
909 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Hanno Becker81c7b182017-11-09 18:39:33 +0000911 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100912 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000914 /*
915 * This is not used in TLS v1.1.
916 */
Paul Bakker48916f92012-09-16 19:57:18 +0000917 iv_copy_len = ( transform->fixed_ivlen ) ?
918 transform->fixed_ivlen : transform->ivlen;
919 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
920 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000921 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100928 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#if defined(MBEDTLS_SSL_PROTO_SSL3)
931 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100932 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000933 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100937 }
938
Hanno Becker81c7b182017-11-09 18:39:33 +0000939 memcpy( transform->mac_enc, mac_enc, mac_key_len );
940 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100941 }
942 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#endif /* MBEDTLS_SSL_PROTO_SSL3 */
944#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
945 defined(MBEDTLS_SSL_PROTO_TLS1_2)
946 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100947 {
Gilles Peskine039fd122018-03-19 19:06:08 +0100948 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
949 For AEAD-based ciphersuites, there is nothing to do here. */
950 if( mac_key_len != 0 )
951 {
952 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
953 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
954 }
Paul Bakker68884e32013-01-07 18:20:04 +0100955 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200956 else
957#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200961 }
Paul Bakker68884e32013-01-07 18:20:04 +0100962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
964 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000965 {
966 int ret = 0;
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100971 transform->iv_enc, transform->iv_dec,
972 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100973 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +0000974 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
977 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000978 }
979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000981
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200982#if defined(MBEDTLS_SSL_EXPORT_KEYS)
983 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100984 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +0200985 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
986 session->master, keyblk,
Hanno Becker81c7b182017-11-09 18:39:33 +0000987 mac_key_len, transform->keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +0100988 iv_copy_len );
989 }
990#endif
991
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200992 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200993 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200995 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200996 return( ret );
997 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200998
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200999 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001000 cipher_info ) ) != 0 )
1001 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001003 return( ret );
1004 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001007 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001011 return( ret );
1012 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001015 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001019 return( ret );
1020 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_CIPHER_MODE_CBC)
1023 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1026 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001029 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001030 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1033 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001036 return( ret );
1037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001041 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001044 // Initialize compression
1045 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001047 {
Paul Bakker16770332013-10-11 09:59:44 +02001048 if( ssl->compress_buf == NULL )
1049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001051 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001052 if( ssl->compress_buf == NULL )
1053 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001055 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001056 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +02001057 }
1058 }
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001061
Paul Bakker48916f92012-09-16 19:57:18 +00001062 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1063 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001064
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001065 if( deflateInit( &transform->ctx_deflate,
1066 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001067 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1070 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001071 }
1072 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
1077 return( 0 );
1078}
1079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#if defined(MBEDTLS_SSL_PROTO_SSL3)
1081void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001082{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001083 mbedtls_md5_context md5;
1084 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 unsigned char pad_1[48];
1086 unsigned char pad_2[48];
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001090 mbedtls_md5_init( &md5 );
1091 mbedtls_sha1_init( &sha1 );
1092
1093 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1094 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Paul Bakker380da532012-04-18 16:10:25 +00001096 memset( pad_1, 0x36, 48 );
1097 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001099 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1100 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1101 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001103 mbedtls_md5_starts_ret( &md5 );
1104 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1105 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1106 mbedtls_md5_update_ret( &md5, hash, 16 );
1107 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001109 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1110 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1111 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001112
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001113 mbedtls_sha1_starts_ret( &sha1 );
1114 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1115 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1116 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1117 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001121
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001122 mbedtls_md5_free( &md5 );
1123 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001124
Paul Bakker380da532012-04-18 16:10:25 +00001125 return;
1126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1130void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001131{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001132 mbedtls_md5_context md5;
1133 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001136
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001137 mbedtls_md5_init( &md5 );
1138 mbedtls_sha1_init( &sha1 );
1139
1140 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1141 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001142
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001143 mbedtls_md5_finish_ret( &md5, hash );
1144 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001148
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001149 mbedtls_md5_free( &md5 );
1150 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001151
Paul Bakker380da532012-04-18 16:10:25 +00001152 return;
1153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1157#if defined(MBEDTLS_SHA256_C)
1158void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001159{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001160 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001161
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001162 mbedtls_sha256_init( &sha256 );
1163
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001165
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001166 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001167 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001171
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001172 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001173
Paul Bakker380da532012-04-18 16:10:25 +00001174 return;
1175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#if defined(MBEDTLS_SHA512_C)
1179void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001180{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001181 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001182
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001183 mbedtls_sha512_init( &sha512 );
1184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001186
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001187 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001188 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001193 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001194
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 return;
1196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#endif /* MBEDTLS_SHA512_C */
1198#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1201int 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 +02001202{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001203 unsigned char *p = ssl->handshake->premaster;
1204 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001205 const unsigned char *psk = ssl->conf->psk;
1206 size_t psk_len = ssl->conf->psk_len;
1207
1208 /* If the psk callback was called, use its result */
1209 if( ssl->handshake->psk != NULL )
1210 {
1211 psk = ssl->handshake->psk;
1212 psk_len = ssl->handshake->psk_len;
1213 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001214
1215 /*
1216 * PMS = struct {
1217 * opaque other_secret<0..2^16-1>;
1218 * opaque psk<0..2^16-1>;
1219 * };
1220 * with "other_secret" depending on the particular key exchange
1221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1223 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001224 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001225 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001227
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001228 *(p++) = (unsigned char)( psk_len >> 8 );
1229 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001230
1231 if( end < p || (size_t)( end - p ) < psk_len )
1232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1233
1234 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001235 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001236 }
1237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1239#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1240 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001241 {
1242 /*
1243 * other_secret already set by the ClientKeyExchange message,
1244 * and is 48 bytes long
1245 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001246 if( end - p < 2 )
1247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1248
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001249 *p++ = 0;
1250 *p++ = 48;
1251 p += 48;
1252 }
1253 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1255#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1256 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001257 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001258 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001259 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001260
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001261 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001263 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001264 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001267 return( ret );
1268 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001269 *(p++) = (unsigned char)( len >> 8 );
1270 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001271 p += len;
1272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001274 }
1275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1277#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1278 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001279 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001280 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001281 size_t zlen;
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001284 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001285 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001288 return( ret );
1289 }
1290
1291 *(p++) = (unsigned char)( zlen >> 8 );
1292 *(p++) = (unsigned char)( zlen );
1293 p += zlen;
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001296 }
1297 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001302 }
1303
1304 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001305 if( end - p < 2 )
1306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001307
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001308 *(p++) = (unsigned char)( psk_len >> 8 );
1309 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001310
1311 if( end < p || (size_t)( end - p ) < psk_len )
1312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1313
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001314 memcpy( p, psk, psk_len );
1315 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001316
1317 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1318
1319 return( 0 );
1320}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001324/*
1325 * SSLv3.0 MAC functions
1326 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001327#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001328static void ssl_mac( mbedtls_md_context_t *md_ctx,
1329 const unsigned char *secret,
1330 const unsigned char *buf, size_t len,
1331 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001332 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001333{
1334 unsigned char header[11];
1335 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001336 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1338 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001339
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001340 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001342 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001343 else
Paul Bakker68884e32013-01-07 18:20:04 +01001344 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
1346 memcpy( header, ctr, 8 );
1347 header[ 8] = (unsigned char) type;
1348 header[ 9] = (unsigned char)( len >> 8 );
1349 header[10] = (unsigned char)( len );
1350
Paul Bakker68884e32013-01-07 18:20:04 +01001351 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 mbedtls_md_starts( md_ctx );
1353 mbedtls_md_update( md_ctx, secret, md_size );
1354 mbedtls_md_update( md_ctx, padding, padlen );
1355 mbedtls_md_update( md_ctx, header, 11 );
1356 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001357 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358
Paul Bakker68884e32013-01-07 18:20:04 +01001359 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_md_starts( md_ctx );
1361 mbedtls_md_update( md_ctx, secret, md_size );
1362 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001363 mbedtls_md_update( md_ctx, out, md_size );
1364 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001365}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1369 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001370 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001371#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001372#endif
1373
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001374/* The function below is only used in the Lucky 13 counter-measure in
1375 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1376#if defined(SSL_SOME_MODES_USE_MAC) && \
1377 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1378 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1379 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1380/* This function makes sure every byte in the memory region is accessed
1381 * (in ascending addresses order) */
1382static void ssl_read_memory( unsigned char *p, size_t len )
1383{
1384 unsigned char acc = 0;
1385 volatile unsigned char force;
1386
1387 for( ; len != 0; p++, len-- )
1388 acc ^= *p;
1389
1390 force = acc;
1391 (void) force;
1392}
1393#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1394
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001395/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001396 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001401 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001405 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001409 }
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001414 ssl->out_msg, ssl->out_msglen );
1415
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001417 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001419#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 if( mode == MBEDTLS_MODE_STREAM ||
1421 ( mode == MBEDTLS_MODE_CBC
1422#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1423 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001424#endif
1425 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_SSL_PROTO_SSL3)
1428 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001429 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001430 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001431
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001432 ssl_mac( &ssl->transform_out->md_ctx_enc,
1433 ssl->transform_out->mac_enc,
1434 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001435 ssl->out_ctr, ssl->out_msgtype,
1436 mac );
1437
1438 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001439 }
1440 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001441#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1443 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1444 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001445 {
Hanno Becker992b6872017-11-09 18:57:39 +00001446 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1449 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1450 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1451 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001452 ssl->out_msg, ssl->out_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001453 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Becker992b6872017-11-09 18:57:39 +00001455
1456 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001457 }
1458 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001459#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001463 }
1464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001466 ssl->out_msg + ssl->out_msglen,
1467 ssl->transform_out->maclen );
1468
1469 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001470 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001471 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001472#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001474 /*
1475 * Encrypt
1476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1478 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001480 int ret;
1481 size_t olen = 0;
1482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 "including %d bytes of padding",
1485 ssl->out_msglen, 0 ) );
1486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001488 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001489 ssl->transform_out->ivlen,
1490 ssl->out_msg, ssl->out_msglen,
1491 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001494 return( ret );
1495 }
1496
1497 if( ssl->out_msglen != olen )
1498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001501 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 }
Paul Bakker68884e32013-01-07 18:20:04 +01001503 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001505#if defined(MBEDTLS_GCM_C) || \
1506 defined(MBEDTLS_CCM_C) || \
1507 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001509 mode == MBEDTLS_MODE_CCM ||
1510 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001511 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001512 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001513 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001514 unsigned char *enc_msg;
1515 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001516 unsigned char iv[12];
1517 mbedtls_ssl_transform *transform = ssl->transform_out;
1518 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001520 size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001521
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001522 /*
1523 * Prepare additional authenticated data
1524 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001525 memcpy( add_data, ssl->out_ctr, 8 );
1526 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001528 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001529 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1530 add_data[12] = ssl->out_msglen & 0xFF;
1531
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001532 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001533
Paul Bakker68884e32013-01-07 18:20:04 +01001534 /*
1535 * Generate IV
1536 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001537 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1538 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001539 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001540 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1541 memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 );
1542 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1543
1544 }
1545 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1546 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001547 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001548 unsigned char i;
1549
1550 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
1551
1552 for( i = 0; i < 8; i++ )
1553 iv[i+4] ^= ssl->out_ctr[i];
1554 }
1555 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001556 {
1557 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001560 }
1561
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001562 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
1563 iv, transform->ivlen );
1564 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
1565 ssl->out_iv, explicit_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001566
Paul Bakker68884e32013-01-07 18:20:04 +01001567 /*
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001568 * Fix message length with added IV
Paul Bakker68884e32013-01-07 18:20:04 +01001569 */
1570 enc_msg = ssl->out_msg;
1571 enc_msglen = ssl->out_msglen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001572 ssl->out_msglen += explicit_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001575 "including 0 bytes of padding",
1576 ssl->out_msglen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001577
Paul Bakker68884e32013-01-07 18:20:04 +01001578 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001579 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001580 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001581 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
1582 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001583 add_data, 13,
1584 enc_msg, enc_msglen,
1585 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001586 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001589 return( ret );
1590 }
1591
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001592 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001596 }
1597
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001598 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001599 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1605#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001606 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001609 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001610 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001611 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001612
Paul Bakker48916f92012-09-16 19:57:18 +00001613 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1614 ssl->transform_out->ivlen;
1615 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 padlen = 0;
1617
1618 for( i = 0; i <= padlen; i++ )
1619 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1620
1621 ssl->out_msglen += padlen + 1;
1622
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001623 enc_msglen = ssl->out_msglen;
1624 enc_msg = ssl->out_msg;
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001627 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001628 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1629 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001632 {
1633 /*
1634 * Generate IV
1635 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001636 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001637 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001638 if( ret != 0 )
1639 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001640
Paul Bakker92be97b2013-01-02 17:30:03 +01001641 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001642 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001643
1644 /*
1645 * Fix pointer positions and message length with added IV
1646 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001647 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001649 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001654 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001655 ssl->out_msglen, ssl->transform_out->ivlen,
1656 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001659 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001660 ssl->transform_out->ivlen,
1661 enc_msg, enc_msglen,
1662 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001665 return( ret );
1666 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001667
Paul Bakkercca5b812013-08-31 17:40:26 +02001668 if( enc_msglen != olen )
1669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001672 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1675 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001676 {
1677 /*
1678 * Save IV in SSL3 and TLS1
1679 */
1680 memcpy( ssl->transform_out->iv_enc,
1681 ssl->transform_out->cipher_ctx_enc.iv,
1682 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001684#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001687 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001688 {
1689 /*
1690 * MAC(MAC_write_key, seq_num +
1691 * TLSCipherText.type +
1692 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001693 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001694 * IV + // except for TLS 1.0
1695 * ENC(content + padding + padding_length));
1696 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001697 unsigned char pseudo_hdr[13];
1698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001700
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001701 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1702 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001703 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1704 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1709 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001710 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001712 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001714
1715 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001716 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001719 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001720 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001722 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001726 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001728 /* Make extra sure authentication was performed, exactly once */
1729 if( auth_done != 1 )
1730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001733 }
1734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
1737 return( 0 );
1738}
1739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001741{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001743 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001744#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001745 size_t padlen = 0, correct = 1;
1746#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001750 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1753 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001754 }
1755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001757
Paul Bakker48916f92012-09-16 19:57:18 +00001758 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001761 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 }
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1766 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001767 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001768 int ret;
1769 size_t olen = 0;
1770
Paul Bakker68884e32013-01-07 18:20:04 +01001771 padlen = 0;
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001774 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001775 ssl->transform_in->ivlen,
1776 ssl->in_msg, ssl->in_msglen,
1777 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001780 return( ret );
1781 }
1782
1783 if( ssl->in_msglen != olen )
1784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001787 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 }
Paul Bakker68884e32013-01-07 18:20:04 +01001789 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001791#if defined(MBEDTLS_GCM_C) || \
1792 defined(MBEDTLS_CCM_C) || \
1793 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001795 mode == MBEDTLS_MODE_CCM ||
1796 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001797 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001798 int ret;
1799 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001800 unsigned char *dec_msg;
1801 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001802 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001803 unsigned char iv[12];
1804 mbedtls_ssl_transform *transform = ssl->transform_in;
1805 unsigned char taglen = transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001807 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001808
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001809 /*
1810 * Compute and update sizes
1811 */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001812 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001815 "+ taglen (%d)", ssl->in_msglen,
1816 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001818 }
1819 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1820
Paul Bakker68884e32013-01-07 18:20:04 +01001821 dec_msg = ssl->in_msg;
1822 dec_msg_result = ssl->in_msg;
1823 ssl->in_msglen = dec_msglen;
1824
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001825 /*
1826 * Prepare additional authenticated data
1827 */
Paul Bakker68884e32013-01-07 18:20:04 +01001828 memcpy( add_data, ssl->in_ctr, 8 );
1829 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001831 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001832 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1833 add_data[12] = ssl->in_msglen & 0xFF;
1834
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001835 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 );
Paul Bakker68884e32013-01-07 18:20:04 +01001836
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001837 /*
1838 * Prepare IV
1839 */
1840 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
1841 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001842 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001843 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1844 memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01001845
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001846 }
1847 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
1848 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02001849 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001850 unsigned char i;
1851
1852 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
1853
1854 for( i = 0; i < 8; i++ )
1855 iv[i+4] ^= ssl->in_ctr[i];
1856 }
1857 else
1858 {
1859 /* Reminder if we ever add an AEAD mode with a different size */
1860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1861 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1862 }
1863
1864 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001866
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001867 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001868 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001871 iv, transform->ivlen,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001872 add_data, 13,
1873 dec_msg, dec_msglen,
1874 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001875 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1880 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001881
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001882 return( ret );
1883 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001884 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001885
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001886 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1889 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001890 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001891 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1894#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001895 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 {
Paul Bakker45829992013-01-03 14:52:21 +01001898 /*
1899 * Decrypt and check the padding
1900 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001901 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001902 unsigned char *dec_msg;
1903 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001904 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001905 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001906 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001907
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 /*
Paul Bakker45829992013-01-03 14:52:21 +01001909 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1912 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001913 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001914#endif
Paul Bakker45829992013-01-03 14:52:21 +01001915
1916 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1917 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001920 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1921 ssl->transform_in->ivlen,
1922 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001924 }
1925
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001926 dec_msglen = ssl->in_msglen;
1927 dec_msg = ssl->in_msg;
1928 dec_msg_result = ssl->in_msg;
1929
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001930 /*
1931 * Authenticate before decrypt if enabled
1932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1934 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001935 {
Hanno Becker992b6872017-11-09 18:57:39 +00001936 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001937 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001940
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001941 dec_msglen -= ssl->transform_in->maclen;
1942 ssl->in_msglen -= ssl->transform_in->maclen;
1943
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001944 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1945 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1946 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1947 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1952 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001953 ssl->in_iv, ssl->in_msglen );
Hanno Becker992b6872017-11-09 18:57:39 +00001954 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001958 ssl->transform_in->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001959 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001960 ssl->transform_in->maclen );
1961
Hanno Becker992b6872017-11-09 18:57:39 +00001962 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1963 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001968 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001969 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001970 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001972
1973 /*
1974 * Check length sanity
1975 */
1976 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001979 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001981 }
1982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001984 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001985 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001986 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001988 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001989 unsigned char i;
Paul Bakker48916f92012-09-16 19:57:18 +00001990 dec_msglen -= ssl->transform_in->ivlen;
1991 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001992
Paul Bakker48916f92012-09-16 19:57:18 +00001993 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001994 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001995 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001999 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02002000 ssl->transform_in->ivlen,
2001 dec_msg, dec_msglen,
2002 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002005 return( ret );
2006 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002007
Paul Bakkercca5b812013-08-31 17:40:26 +02002008 if( dec_msglen != olen )
2009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002012 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
2015 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002016 {
2017 /*
2018 * Save IV in SSL3 and TLS1
2019 */
2020 memcpy( ssl->transform_in->iv_dec,
2021 ssl->transform_in->cipher_ctx_dec.iv,
2022 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
2026 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002027
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002028 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002029 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01002030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#if defined(MBEDTLS_SSL_DEBUG_ALL)
2032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01002033 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002034#endif
Paul Bakker45829992013-01-03 14:52:21 +01002035 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01002036 correct = 0;
2037 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_SSL_PROTO_SSL3)
2040 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 {
Paul Bakker48916f92012-09-16 19:57:18 +00002042 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_DEBUG_ALL)
2045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00002046 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00002047 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002048#endif
Paul Bakker45829992013-01-03 14:52:21 +01002049 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 }
2051 }
2052 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2054#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2055 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2056 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 {
2058 /*
Paul Bakker45829992013-01-03 14:52:21 +01002059 * TLSv1+: always check the padding up to the first failure
2060 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002062 size_t pad_count = 0, real_count = 1;
Angus Grattonb512bc12018-06-19 15:57:50 +10002063 size_t padding_idx = ssl->in_msglen - padlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 size_t i;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002065
Paul Bakker956c9e02013-12-19 14:42:28 +01002066 /*
2067 * Padding is guaranteed to be incorrect if:
Angus Grattonb512bc12018-06-19 15:57:50 +10002068 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01002069 *
Angus Grattonb512bc12018-06-19 15:57:50 +10002070 * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002071 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01002072 *
2073 * In both cases we reset padding_idx to a safe value (0) to
2074 * prevent out-of-buffer reads.
2075 */
Angus Grattonb512bc12018-06-19 15:57:50 +10002076 correct &= ( padlen <= ssl->in_msglen );
2077 correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02002078 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01002079
2080 padding_idx *= correct;
2081
Angus Grattonb512bc12018-06-19 15:57:50 +10002082 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002083 {
Angus Grattonb512bc12018-06-19 15:57:50 +10002084 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002085 pad_count += real_count *
2086 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
2087 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002088
2089 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01002090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002092 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002094#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002095 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2099 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002103 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002104
2105 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002107 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002109 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002113 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002114
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002115#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002118#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
2120 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002121 * Authenticate if not done yet.
2122 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002124#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002125 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002126 {
Hanno Becker992b6872017-11-09 18:57:39 +00002127 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002128
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002129 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002131 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2132 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#if defined(MBEDTLS_SSL_PROTO_SSL3)
2135 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002136 {
2137 ssl_mac( &ssl->transform_in->md_ctx_dec,
2138 ssl->transform_in->mac_dec,
2139 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002140 ssl->in_ctr, ssl->in_msgtype,
2141 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002142 }
2143 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2145#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2146 defined(MBEDTLS_SSL_PROTO_TLS1_2)
2147 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002148 {
2149 /*
2150 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002151 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002152 *
2153 * Known timing attacks:
2154 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2155 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002156 * To compensate for different timings for the MAC calculation
2157 * depending on how much padding was removed (which is determined
2158 * by padlen), process extra_run more blocks through the hash
2159 * function.
2160 *
2161 * The formula in the paper is
2162 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2163 * where L1 is the size of the header plus the decrypted message
2164 * plus CBC padding and L2 is the size of the header plus the
2165 * decrypted message. This is for an underlying hash function
2166 * with 64-byte blocks.
2167 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2168 * correctly. We round down instead of up, so -56 is the correct
2169 * value for our calculations instead of -55.
2170 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002171 * Repeat the formula rather than defining a block_size variable.
2172 * This avoids requiring division by a variable at runtime
2173 * (which would be marginally less efficient and would require
2174 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002175 */
2176 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002177
2178 /*
2179 * The next two sizes are the minimum and maximum values of
2180 * in_msglen over all padlen values.
2181 *
2182 * They're independent of padlen, since we previously did
2183 * in_msglen -= padlen.
2184 *
2185 * Note that max_len + maclen is never more than the buffer
2186 * length, as we previously did in_msglen -= maclen too.
2187 */
2188 const size_t max_len = ssl->in_msglen + padlen;
2189 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2190
Gilles Peskine20b44082018-05-29 14:06:49 +02002191 switch( ssl->transform_in->ciphersuite_info->mac )
2192 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002193#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2194 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002195 case MBEDTLS_MD_MD5:
2196 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002197 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002198 /* 8 bytes of message size, 64-byte compression blocks */
2199 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2200 ( 13 + ssl->in_msglen + 8 ) / 64;
2201 break;
2202#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002203#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002204 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002205 /* 16 bytes of message size, 128-byte compression blocks */
2206 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2207 ( 13 + ssl->in_msglen + 16 ) / 128;
2208 break;
2209#endif
2210 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2213 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002214
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002215 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2218 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2219 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2220 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002221 ssl->in_msglen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002222 /* Make sure we access everything even when padlen > 0. This
2223 * makes the synchronisation requirements for just-in-time
2224 * Prime+Probe attacks much tighter and hopefully impractical. */
2225 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Becker992b6872017-11-09 18:57:39 +00002226 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002227
2228 /* Call mbedtls_md_process at least once due to cache attacks
2229 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002230 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002234
2235 /* Make sure we access all the memory that could contain the MAC,
2236 * before we check it in the next code block. This makes the
2237 * synchronisation requirements for just-in-time Prime+Probe
2238 * attacks much tighter and hopefully impractical. */
2239 ssl_read_memory( ssl->in_msg + min_len,
2240 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 }
2242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2244 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002248 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002250#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker992b6872017-11-09 18:57:39 +00002251 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2252 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2253 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002254#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002255
Hanno Becker992b6872017-11-09 18:57:39 +00002256 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2257 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259#if defined(MBEDTLS_SSL_DEBUG_ALL)
2260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002261#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002262 correct = 0;
2263 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002264 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 /*
2267 * Finally check the correct flag
2268 */
2269 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002271 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002272#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002273
2274 /* Make extra sure authentication was performed, exactly once */
2275 if( auth_done != 1 )
2276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2278 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002279 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
2281 if( ssl->in_msglen == 0 )
2282 {
Angus Gratton34817922018-06-19 15:58:22 +10002283#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2284 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2285 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2286 {
2287 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2289 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2290 }
2291#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2292
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 ssl->nb_zero++;
2294
2295 /*
2296 * Three or more empty messages may be a DoS attack
2297 * (excessive CPU consumption).
2298 */
2299 if( ssl->nb_zero > 3 )
2300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 }
2305 }
2306 else
2307 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002310 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002311 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002312 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002313 }
2314 else
2315#endif
2316 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002317 unsigned char i;
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002318 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2319 if( ++ssl->in_ctr[i - 1] != 0 )
2320 break;
2321
2322 /* The loop goes to its end iff the counter is wrapping */
2323 if( i == ssl_ep_len( ssl ) )
2324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2326 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002327 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002328 }
2329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
2332 return( 0 );
2333}
2334
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002335#undef MAC_NONE
2336#undef MAC_PLAINTEXT
2337#undef MAC_CIPHERTEXT
2338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002340/*
2341 * Compression/decompression functions
2342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002344{
2345 int ret;
2346 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002347 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002349 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002352
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002353 if( len_pre == 0 )
2354 return( 0 );
2355
Paul Bakker2770fbd2012-07-03 13:30:23 +00002356 memcpy( msg_pre, ssl->out_msg, len_pre );
2357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002359 ssl->out_msglen ) );
2360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002362 ssl->out_msg, ssl->out_msglen );
2363
Paul Bakker48916f92012-09-16 19:57:18 +00002364 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2365 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2366 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002367 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002368
Paul Bakker48916f92012-09-16 19:57:18 +00002369 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002370 if( ret != Z_OK )
2371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2373 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002374 }
2375
Angus Grattond8213d02016-05-25 20:56:48 +10002376 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002377 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002380 ssl->out_msglen ) );
2381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002383 ssl->out_msg, ssl->out_msglen );
2384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002386
2387 return( 0 );
2388}
2389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002391{
2392 int ret;
2393 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002394 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002395 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002396 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002399
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002400 if( len_pre == 0 )
2401 return( 0 );
2402
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403 memcpy( msg_pre, ssl->in_msg, len_pre );
2404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002406 ssl->in_msglen ) );
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002409 ssl->in_msg, ssl->in_msglen );
2410
Paul Bakker48916f92012-09-16 19:57:18 +00002411 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2412 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2413 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002414 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002415 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002416
Paul Bakker48916f92012-09-16 19:57:18 +00002417 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418 if( ret != Z_OK )
2419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2421 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002422 }
2423
Angus Grattond8213d02016-05-25 20:56:48 +10002424 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002425 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002428 ssl->in_msglen ) );
2429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002431 ssl->in_msg, ssl->in_msglen );
2432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002434
2435 return( 0 );
2436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2440static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#if defined(MBEDTLS_SSL_PROTO_DTLS)
2443static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002444{
2445 /* If renegotiation is not enforced, retransmit until we would reach max
2446 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002447 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002448 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002449 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002450 unsigned char doublings = 1;
2451
2452 while( ratio != 0 )
2453 {
2454 ++doublings;
2455 ratio >>= 1;
2456 }
2457
2458 if( ++ssl->renego_records_seen > doublings )
2459 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002461 return( 0 );
2462 }
2463 }
2464
2465 return( ssl_write_hello_request( ssl ) );
2466}
2467#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002469
Paul Bakker5121ce52009-01-03 21:22:43 +00002470/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002471 * Fill the input message buffer by appending data to it.
2472 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002473 *
2474 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2475 * available (from this read and/or a previous one). Otherwise, an error code
2476 * is returned (possibly EOF or WANT_READ).
2477 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002478 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2479 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2480 * since we always read a whole datagram at once.
2481 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002482 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002483 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486{
Paul Bakker23986e52011-04-24 08:57:21 +00002487 int ret;
2488 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002492 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002495 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002497 }
2498
Angus Grattond8213d02016-05-25 20:56:48 +10002499 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002503 }
2504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002506 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002508 uint32_t timeout;
2509
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002510 /* Just to be sure */
2511 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2512 {
2513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2514 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2516 }
2517
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002518 /*
2519 * The point is, we need to always read a full datagram at once, so we
2520 * sometimes read more then requested, and handle the additional data.
2521 * It could be the rest of the current record (while fetching the
2522 * header) and/or some other records in the same datagram.
2523 */
2524
2525 /*
2526 * Move to the next record in the already read datagram if applicable
2527 */
2528 if( ssl->next_record_offset != 0 )
2529 {
2530 if( ssl->in_left < ssl->next_record_offset )
2531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002534 }
2535
2536 ssl->in_left -= ssl->next_record_offset;
2537
2538 if( ssl->in_left != 0 )
2539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002541 ssl->next_record_offset ) );
2542 memmove( ssl->in_hdr,
2543 ssl->in_hdr + ssl->next_record_offset,
2544 ssl->in_left );
2545 }
2546
2547 ssl->next_record_offset = 0;
2548 }
2549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002552
2553 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002554 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002555 */
2556 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002559 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002560 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002561
2562 /*
2563 * A record can't be split accross datagrams. If we need to read but
2564 * are not at the beginning of a new record, the caller did something
2565 * wrong.
2566 */
2567 if( ssl->in_left != 0 )
2568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002571 }
2572
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002573 /*
2574 * Don't even try to read if time's out already.
2575 * This avoids by-passing the timer when repeatedly receiving messages
2576 * that will end up being dropped.
2577 */
2578 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002579 {
2580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002581 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002582 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002583 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002584 {
Angus Grattond8213d02016-05-25 20:56:48 +10002585 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002588 timeout = ssl->handshake->retransmit_timeout;
2589 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002590 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002593
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002594 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002595 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2596 timeout );
2597 else
2598 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002601
2602 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002604 }
2605
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002606 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002609 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002612 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002613 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002616 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002617 }
2618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002622 return( ret );
2623 }
2624
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002625 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002628 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002630 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002631 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002634 return( ret );
2635 }
2636
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002637 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002640 }
2641
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 if( ret < 0 )
2643 return( ret );
2644
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002645 ssl->in_left = ret;
2646 }
2647 else
2648#endif
2649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002651 ssl->in_left, nb_want ) );
2652
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002653 while( ssl->in_left < nb_want )
2654 {
2655 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002656
2657 if( ssl_check_timer( ssl ) != 0 )
2658 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2659 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002660 {
2661 if( ssl->f_recv_timeout != NULL )
2662 {
2663 ret = ssl->f_recv_timeout( ssl->p_bio,
2664 ssl->in_hdr + ssl->in_left, len,
2665 ssl->conf->read_timeout );
2666 }
2667 else
2668 {
2669 ret = ssl->f_recv( ssl->p_bio,
2670 ssl->in_hdr + ssl->in_left, len );
2671 }
2672 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002675 ssl->in_left, nb_want ) );
2676 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002677
2678 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002680
2681 if( ret < 0 )
2682 return( ret );
2683
mohammad160352aecb92018-03-28 23:41:40 -07002684 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002685 {
Darryl Green11999bb2018-03-13 15:22:58 +00002686 MBEDTLS_SSL_DEBUG_MSG( 1,
2687 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07002688 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2690 }
2691
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002692 ssl->in_left += ret;
2693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 }
2695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
2698 return( 0 );
2699}
2700
2701/*
2702 * Flush any data not yet written
2703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002706 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01002707 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002711 if( ssl->f_send == NULL )
2712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002714 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002716 }
2717
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002718 /* Avoid incrementing counter if data is flushed */
2719 if( ssl->out_left == 0 )
2720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002722 return( 0 );
2723 }
2724
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 while( ssl->out_left > 0 )
2726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2728 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729
Hanno Becker2b1e3542018-08-06 11:19:13 +01002730 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002731 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
2735 if( ret <= 0 )
2736 return( ret );
2737
mohammad160352aecb92018-03-28 23:41:40 -07002738 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002739 {
Darryl Green11999bb2018-03-13 15:22:58 +00002740 MBEDTLS_SSL_DEBUG_MSG( 1,
2741 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07002742 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2744 }
2745
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 ssl->out_left -= ret;
2747 }
2748
Hanno Becker2b1e3542018-08-06 11:19:13 +01002749#if defined(MBEDTLS_SSL_PROTO_DTLS)
2750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2751 {
2752 ssl->out_hdr = ssl->out_buf;
2753 }
2754 else
2755#endif
2756 {
2757 ssl->out_hdr = ssl->out_buf + 8;
2758 }
2759 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
2763 return( 0 );
2764}
2765
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002766/*
2767 * Functions to handle the DTLS retransmission state machine
2768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002770/*
2771 * Append current handshake message to current outgoing flight
2772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2777 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2778 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002779
2780 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002781 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002782 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002785 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002786 }
2787
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002788 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002789 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002792 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002793 }
2794
2795 /* Copy current handshake message with headers */
2796 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2797 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002798 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002799 msg->next = NULL;
2800
2801 /* Append to the current flight */
2802 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002803 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002804 else
2805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002807 while( cur->next != NULL )
2808 cur = cur->next;
2809 cur->next = msg;
2810 }
2811
Hanno Becker3b235902018-08-06 09:54:53 +01002812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002813 return( 0 );
2814}
2815
2816/*
2817 * Free the current flight of handshake messages
2818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002820{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 mbedtls_ssl_flight_item *cur = flight;
2822 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002823
2824 while( cur != NULL )
2825 {
2826 next = cur->next;
2827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 mbedtls_free( cur->p );
2829 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002830
2831 cur = next;
2832 }
2833}
2834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2836static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002837#endif
2838
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002839/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002840 * Swap transform_out and out_ctr with the alternative ones
2841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002843{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002845 unsigned char tmp_out_ctr[8];
2846
2847 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002850 return;
2851 }
2852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002854
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002855 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002856 tmp_transform = ssl->transform_out;
2857 ssl->transform_out = ssl->handshake->alt_transform_out;
2858 ssl->handshake->alt_transform_out = tmp_transform;
2859
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002860 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002861 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2862 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002863 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002864
2865 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01002866 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2869 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2874 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002875 }
2876 }
2877#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002878}
2879
2880/*
2881 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002882 */
2883int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2884{
2885 int ret = 0;
2886
2887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2888
2889 ret = mbedtls_ssl_flight_transmit( ssl );
2890
2891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2892
2893 return( ret );
2894}
2895
2896/*
2897 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002898 *
2899 * Need to remember the current message in case flush_output returns
2900 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002901 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002902 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002903int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002904{
Hanno Becker67bc7c32018-08-06 11:33:50 +01002905 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002909 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002911
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002912 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002913 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002914 ssl_swap_epochs( ssl );
2915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002917 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002918
2919 while( ssl->handshake->cur_msg != NULL )
2920 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002921 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002922 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002923
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002924 /* Swap epochs before sending Finished: we can't do it after
2925 * sending ChangeCipherSpec, in case write returns WANT_READ.
2926 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker67bc7c32018-08-06 11:33:50 +01002928 cur->p[0] == MBEDTLS_SSL_HS_FINISHED &&
2929 ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002930 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002932 ssl_swap_epochs( ssl );
2933 }
2934
Hanno Becker67bc7c32018-08-06 11:33:50 +01002935 ret = ssl_get_remaining_payload_in_datagram( ssl );
2936 if( ret < 0 )
2937 return( ret );
2938 max_frag_len = (size_t) ret;
2939
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002940 /* CCS is copied as is, while HS messages may need fragmentation */
2941 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2942 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002943 if( max_frag_len == 0 )
2944 {
2945 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2946 return( ret );
2947
2948 continue;
2949 }
2950
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002951 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002952 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002953 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002954
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002955 /* Update position inside current message */
2956 ssl->handshake->cur_msg_p += cur->len;
2957 }
2958 else
2959 {
2960 const unsigned char * const p = ssl->handshake->cur_msg_p;
2961 const size_t hs_len = cur->len - 12;
2962 const size_t frag_off = p - ( cur->p + 12 );
2963 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002964 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002965
Hanno Becker67bc7c32018-08-06 11:33:50 +01002966 if( max_frag_len < 12 )
2967 {
2968 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2969 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
2970 {
2971 ssl_swap_epochs( ssl );
2972 }
2973
2974 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2975 return( ret );
2976
2977 continue;
2978 }
2979 max_hs_frag_len = max_frag_len - 12;
2980
2981 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2982 max_hs_frag_len : rem_len;
2983
2984 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002985 {
2986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002987 (unsigned) cur_hs_frag_len,
2988 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002989 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002990
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002991 /* Messages are stored with handshake headers as if not fragmented,
2992 * copy beginning of headers then fill fragmentation fields.
2993 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2994 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002995
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002996 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2997 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2998 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2999
Hanno Becker67bc7c32018-08-06 11:33:50 +01003000 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3001 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3002 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003003
3004 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3005
Hanno Becker67bc7c32018-08-06 11:33:50 +01003006 /* Copy the handshame message content and set records fields */
3007 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3008 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003009 ssl->out_msgtype = cur->type;
3010
3011 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003012 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003013 }
3014
3015 /* If done with the current message move to the next one if any */
3016 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3017 {
3018 if( cur->next != NULL )
3019 {
3020 ssl->handshake->cur_msg = cur->next;
3021 ssl->handshake->cur_msg_p = cur->next->p + 12;
3022 }
3023 else
3024 {
3025 ssl->handshake->cur_msg = NULL;
3026 ssl->handshake->cur_msg_p = NULL;
3027 }
3028 }
3029
3030 /* Actually send the message out */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003031 if( ( ret = mbedtls_ssl_write_record( ssl,
3032 SSL_DONT_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003035 return( ret );
3036 }
3037 }
3038
Hanno Becker67bc7c32018-08-06 11:33:50 +01003039 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3040 return( ret );
3041
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003042 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3044 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003045 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003048 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3049 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003050
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003052
3053 return( 0 );
3054}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003055
3056/*
3057 * To be called when the last message of an incoming flight is received.
3058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003060{
3061 /* We won't need to resend that one any more */
3062 ssl_flight_free( ssl->handshake->flight );
3063 ssl->handshake->flight = NULL;
3064 ssl->handshake->cur_msg = NULL;
3065
3066 /* The next incoming flight will start with this msg_seq */
3067 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3068
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003069 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003070 ssl_set_timer( ssl, 0 );
3071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3073 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003076 }
3077 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003079}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003080
3081/*
3082 * To be called when the last message of an outgoing flight is send.
3083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003085{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003086 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003087 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3090 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003093 }
3094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003098
Paul Bakker5121ce52009-01-03 21:22:43 +00003099/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003100 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003102
3103/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003104 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003105 *
3106 * - fill in handshake headers
3107 * - update handshake checksum
3108 * - DTLS: save message for resending
3109 * - then pass to the record layer
3110 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003111 * DTLS: except for HelloRequest, messages are only queued, and will only be
3112 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003113 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003114 * Inputs:
3115 * - ssl->out_msglen: 4 + actual handshake message len
3116 * (4 is the size of handshake headers for TLS)
3117 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3118 * - ssl->out_msg + 4: the handshake message body
3119 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003120 * Ouputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003121 * - ssl->out_msglen: the length of the record contents
3122 * (including handshake headers but excluding record headers)
3123 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003124 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003125int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003126{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003127 int ret;
3128 const size_t hs_len = ssl->out_msglen - 4;
3129 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3132
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003133 /*
3134 * Sanity checks
3135 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003136 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3137 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3138 {
3139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3141 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003143 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3144 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST &&
3145 ssl->handshake == NULL )
3146 {
3147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3149 }
3150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003153 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003155 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003158 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003159#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003160
Hanno Beckerb50a2532018-08-06 11:52:54 +01003161 /* Double-check that we did not exceed the bounds
3162 * of the outgoing record buffer.
3163 * This should never fail as the various message
3164 * writing functions must obey the bounds of the
3165 * outgoing record buffer, but better be safe.
3166 *
3167 * Note: We deliberately do not check for the MTU or MFL here.
3168 */
3169 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3170 {
3171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3172 "size %u, maximum %u",
3173 (unsigned) ssl->out_msglen,
3174 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3176 }
3177
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003178 /*
3179 * Fill handshake headers
3180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003183 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3184 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3185 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003187 /*
3188 * DTLS has additional fields in the Handshake layer,
3189 * between the length field and the actual payload:
3190 * uint16 message_seq;
3191 * uint24 fragment_offset;
3192 * uint24 fragment_length;
3193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003195 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003196 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003197 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003198 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003199 {
3200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3201 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003202 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003203 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3205 }
3206
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003207 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003208 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003209
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003210 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003211 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003212 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003213 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3214 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3215 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003216 }
3217 else
3218 {
3219 ssl->out_msg[4] = 0;
3220 ssl->out_msg[5] = 0;
3221 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003222
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003223 /* Handshake hashes are computed without fragmentation,
3224 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003225 memset( ssl->out_msg + 6, 0x00, 3 );
3226 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003229
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003230 /* Update running hashes of hanshake messages seen */
3231 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3232 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233 }
3234
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003235 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003238 hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003239 {
3240 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003243 return( ret );
3244 }
3245 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003246 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003247#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003248 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003249 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003250 {
3251 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3252 return( ret );
3253 }
3254 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003255
3256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3257
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003258 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003259}
3260
3261/*
3262 * Record layer functions
3263 */
3264
3265/*
3266 * Write current record.
3267 *
3268 * Uses:
3269 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3270 * - ssl->out_msglen: length of the record content (excl headers)
3271 * - ssl->out_msg: record content
3272 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003273int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003274{
3275 int ret, done = 0;
3276 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003277 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003278
3279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
3280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003282 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003284 {
3285 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003288 return( ret );
3289 }
3290
3291 len = ssl->out_msglen;
3292 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3296 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 ret = mbedtls_ssl_hw_record_write( ssl );
3301 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3304 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003305 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003306
3307 if( ret == 0 )
3308 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003311 if( !done )
3312 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003313 unsigned i;
3314 size_t protected_record_size;
3315
Paul Bakker05ef8352012-05-08 09:17:57 +00003316 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003318 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003319
Hanno Becker19859472018-08-06 09:40:20 +01003320 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003321 ssl->out_len[0] = (unsigned char)( len >> 8 );
3322 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003323
Paul Bakker48916f92012-09-16 19:57:18 +00003324 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003325 {
3326 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
3327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003329 return( ret );
3330 }
3331
3332 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003333 ssl->out_len[0] = (unsigned char)( len >> 8 );
3334 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003335 }
3336
Hanno Becker2b1e3542018-08-06 11:19:13 +01003337 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
3338
3339#if defined(MBEDTLS_SSL_PROTO_DTLS)
3340 /* In case of DTLS, double-check that we don't exceed
3341 * the remaining space in the datagram. */
3342 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3343 {
3344 ret = ssl_get_maximum_datagram_size( ssl );
3345 if( ret < 0 )
3346 return( ret );
3347
3348 if( protected_record_size > (size_t) ret )
3349 {
3350 /* Should never happen */
3351 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3352 }
3353 }
3354#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Becker2b1e3542018-08-06 11:19:13 +01003357 "version = [%d:%d], msglen = %d",
3358 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len ) );
3359
Paul Bakker05ef8352012-05-08 09:17:57 +00003360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Becker2b1e3542018-08-06 11:19:13 +01003362 ssl->out_hdr, protected_record_size );
3363
3364 ssl->out_left += protected_record_size;
3365 ssl->out_hdr += protected_record_size;
3366 ssl_update_out_pointers( ssl, ssl->transform_out );
3367
Hanno Becker04484622018-08-06 09:49:38 +01003368 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3369 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3370 break;
3371
3372 /* The loop goes to its end iff the counter is wrapping */
3373 if( i == ssl_ep_len( ssl ) )
3374 {
3375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3376 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 }
3379
Hanno Becker67bc7c32018-08-06 11:33:50 +01003380#if defined(MBEDTLS_SSL_PROTO_DTLS)
3381 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3382 {
3383 size_t remaining = ssl_get_remaining_payload_in_datagram( ssl );
3384 if( remaining == 0 )
3385 flush = SSL_FORCE_FLUSH;
3386 else
3387 {
3388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Stil %u bytes available in current datagram", (unsigned) remaining ) );
3389 }
3390 }
3391#endif /* MBEDTLS_SSL_PROTO_DTLS */
3392
3393 if( ( flush == SSL_FORCE_FLUSH ) &&
3394 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397 return( ret );
3398 }
3399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
3402 return( 0 );
3403}
3404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003406/*
3407 * Mark bits in bitmask (used for DTLS HS reassembly)
3408 */
3409static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3410{
3411 unsigned int start_bits, end_bits;
3412
3413 start_bits = 8 - ( offset % 8 );
3414 if( start_bits != 8 )
3415 {
3416 size_t first_byte_idx = offset / 8;
3417
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003418 /* Special case */
3419 if( len <= start_bits )
3420 {
3421 for( ; len != 0; len-- )
3422 mask[first_byte_idx] |= 1 << ( start_bits - len );
3423
3424 /* Avoid potential issues with offset or len becoming invalid */
3425 return;
3426 }
3427
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003428 offset += start_bits; /* Now offset % 8 == 0 */
3429 len -= start_bits;
3430
3431 for( ; start_bits != 0; start_bits-- )
3432 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3433 }
3434
3435 end_bits = len % 8;
3436 if( end_bits != 0 )
3437 {
3438 size_t last_byte_idx = ( offset + len ) / 8;
3439
3440 len -= end_bits; /* Now len % 8 == 0 */
3441
3442 for( ; end_bits != 0; end_bits-- )
3443 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3444 }
3445
3446 memset( mask + offset / 8, 0xFF, len / 8 );
3447}
3448
3449/*
3450 * Check that bitmask is full
3451 */
3452static int ssl_bitmask_check( unsigned char *mask, size_t len )
3453{
3454 size_t i;
3455
3456 for( i = 0; i < len / 8; i++ )
3457 if( mask[i] != 0xFF )
3458 return( -1 );
3459
3460 for( i = 0; i < len % 8; i++ )
3461 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3462 return( -1 );
3463
3464 return( 0 );
3465}
3466
3467/*
3468 * Reassemble fragmented DTLS handshake messages.
3469 *
3470 * Use a temporary buffer for reassembly, divided in two parts:
3471 * - the first holds the reassembled message (including handshake header),
3472 * - the second holds a bitmask indicating which parts of the message
3473 * (excluding headers) have been received so far.
3474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003476{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003477 unsigned char *msg, *bitmask;
3478 size_t frag_len, frag_off;
3479 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3480
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003481 if( ssl->handshake == NULL )
3482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3484 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003485 }
3486
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003487 /*
3488 * For first fragment, check size and allocate buffer
3489 */
3490 if( ssl->handshake->hs_msg == NULL )
3491 {
3492 size_t alloc_len;
3493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003495 msg_len ) );
3496
Angus Grattond8213d02016-05-25 20:56:48 +10003497 if( ssl->in_hslen > MBEDTLS_SSL_IN_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3500 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003501 }
3502
3503 /* The bitmask needs one bit per byte of message excluding header */
3504 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3505
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003506 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003507 if( ssl->handshake->hs_msg == NULL )
3508 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003510 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003511 }
3512
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003513 /* Prepare final header: copy msg_type, length and message_seq,
3514 * then add standardised fragment_offset and fragment_length */
3515 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3516 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3517 memcpy( ssl->handshake->hs_msg + 9,
3518 ssl->handshake->hs_msg + 1, 3 );
3519 }
3520 else
3521 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003522 /* Make sure msg_type and length are consistent */
3523 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3526 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003527 }
3528 }
3529
3530 msg = ssl->handshake->hs_msg + 12;
3531 bitmask = msg + msg_len;
3532
3533 /*
3534 * Check and copy current fragment
3535 */
3536 frag_off = ( ssl->in_msg[6] << 16 ) |
3537 ( ssl->in_msg[7] << 8 ) |
3538 ssl->in_msg[8];
3539 frag_len = ( ssl->in_msg[9] << 16 ) |
3540 ( ssl->in_msg[10] << 8 ) |
3541 ssl->in_msg[11];
3542
3543 if( frag_off + frag_len > msg_len )
3544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003546 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003548 }
3549
3550 if( frag_len + 12 > ssl->in_msglen )
3551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003553 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003555 }
3556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003558 frag_off, frag_len ) );
3559
3560 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3561 ssl_bitmask_set( bitmask, frag_off, frag_len );
3562
3563 /*
3564 * Do we have the complete message by now?
3565 * If yes, finalize it, else ask to read the next record.
3566 */
3567 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01003570 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003571 }
3572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003574
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003575 if( frag_len + 12 < ssl->in_msglen )
3576 {
3577 /*
3578 * We'got more handshake messages in the same record.
3579 * This case is not handled now because no know implementation does
3580 * that and it's hard to test, so we prefer to fail cleanly for now.
3581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3583 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003584 }
3585
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003586 if( ssl->in_left > ssl->next_record_offset )
3587 {
3588 /*
3589 * We've got more data in the buffer after the current record,
3590 * that we don't want to overwrite. Move it before writing the
3591 * reassembled message, and adjust in_left and next_record_offset.
3592 */
3593 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3594 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3595 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3596
3597 /* First compute and check new lengths */
3598 ssl->next_record_offset = new_remain - ssl->in_hdr;
3599 ssl->in_left = ssl->next_record_offset + remain_len;
3600
Angus Grattond8213d02016-05-25 20:56:48 +10003601 if( ssl->in_left > MBEDTLS_SSL_IN_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003602 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3605 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003606 }
3607
3608 memmove( new_remain, cur_remain, remain_len );
3609 }
3610
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003611 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003614 ssl->handshake->hs_msg = NULL;
3615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003617 ssl->in_msg, ssl->in_hslen );
3618
3619 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003622
Simon Butcher99000142016-10-13 17:21:01 +01003623int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003628 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003630 }
3631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003633 ( ssl->in_msg[1] << 16 ) |
3634 ( ssl->in_msg[2] << 8 ) |
3635 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003638 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003639 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003642 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003643 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003644 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003645 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003646
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003647 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003648 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3649 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3650 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3651 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003652 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003653 /* Retransmit only on last message from previous flight, to avoid
3654 * too many retransmissions.
3655 * Besides, No sane server ever retransmits HelloVerifyRequest */
3656 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003660 "message_seq = %d, start_of_flight = %d",
3661 recv_msg_seq,
3662 ssl->handshake->in_flight_start_seq ) );
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003667 return( ret );
3668 }
3669 }
3670 else
3671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003673 "message_seq = %d, expected = %d",
3674 recv_msg_seq,
3675 ssl->handshake->in_msg_seq ) );
3676 }
3677
Hanno Becker90333da2017-10-10 11:27:13 +01003678 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003679 }
3680 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003681
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003682 /* Reassemble if current message is fragmented or reassembly is
3683 * already in progress */
3684 if( ssl->in_msglen < ssl->in_hslen ||
3685 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3686 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3687 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003690
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003691 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003693 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003694 return( ret );
3695 }
3696 }
3697 }
3698 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003700 /* With TLS we don't handle fragmentation (for now) */
3701 if( ssl->in_msglen < ssl->in_hslen )
3702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3704 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003705 }
3706
Simon Butcher99000142016-10-13 17:21:01 +01003707 return( 0 );
3708}
3709
3710void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3711{
3712
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003713 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3714 ssl->handshake != NULL )
3715 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003716 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003717 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003718
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003719 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003721 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003722 ssl->handshake != NULL )
3723 {
3724 ssl->handshake->in_msg_seq++;
3725 }
3726#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003727}
3728
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003729/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003730 * DTLS anti-replay: RFC 6347 4.1.2.6
3731 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003732 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3733 * Bit n is set iff record number in_window_top - n has been seen.
3734 *
3735 * Usually, in_window_top is the last record number seen and the lsb of
3736 * in_window is set. The only exception is the initial state (record number 0
3737 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3740static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003741{
3742 ssl->in_window_top = 0;
3743 ssl->in_window = 0;
3744}
3745
3746static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3747{
3748 return( ( (uint64_t) buf[0] << 40 ) |
3749 ( (uint64_t) buf[1] << 32 ) |
3750 ( (uint64_t) buf[2] << 24 ) |
3751 ( (uint64_t) buf[3] << 16 ) |
3752 ( (uint64_t) buf[4] << 8 ) |
3753 ( (uint64_t) buf[5] ) );
3754}
3755
3756/*
3757 * Return 0 if sequence number is acceptable, -1 otherwise
3758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003760{
3761 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3762 uint64_t bit;
3763
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003764 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003765 return( 0 );
3766
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003767 if( rec_seqnum > ssl->in_window_top )
3768 return( 0 );
3769
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003770 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003771
3772 if( bit >= 64 )
3773 return( -1 );
3774
3775 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3776 return( -1 );
3777
3778 return( 0 );
3779}
3780
3781/*
3782 * Update replay window on new validated record
3783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003785{
3786 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3787
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003788 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003789 return;
3790
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003791 if( rec_seqnum > ssl->in_window_top )
3792 {
3793 /* Update window_top and the contents of the window */
3794 uint64_t shift = rec_seqnum - ssl->in_window_top;
3795
3796 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003797 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003798 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003799 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003800 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003801 ssl->in_window |= 1;
3802 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003803
3804 ssl->in_window_top = rec_seqnum;
3805 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003806 else
3807 {
3808 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003809 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003810
3811 if( bit < 64 ) /* Always true, but be extra sure */
3812 ssl->in_window |= (uint64_t) 1 << bit;
3813 }
3814}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003816
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003817#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003818/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003819static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3820
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003821/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003822 * Without any SSL context, check if a datagram looks like a ClientHello with
3823 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003824 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003825 *
3826 * - if cookie is valid, return 0
3827 * - if ClientHello looks superficially valid but cookie is not,
3828 * fill obuf and set olen, then
3829 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3830 * - otherwise return a specific error code
3831 */
3832static int ssl_check_dtls_clihlo_cookie(
3833 mbedtls_ssl_cookie_write_t *f_cookie_write,
3834 mbedtls_ssl_cookie_check_t *f_cookie_check,
3835 void *p_cookie,
3836 const unsigned char *cli_id, size_t cli_id_len,
3837 const unsigned char *in, size_t in_len,
3838 unsigned char *obuf, size_t buf_len, size_t *olen )
3839{
3840 size_t sid_len, cookie_len;
3841 unsigned char *p;
3842
3843 if( f_cookie_write == NULL || f_cookie_check == NULL )
3844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3845
3846 /*
3847 * Structure of ClientHello with record and handshake headers,
3848 * and expected values. We don't need to check a lot, more checks will be
3849 * done when actually parsing the ClientHello - skipping those checks
3850 * avoids code duplication and does not make cookie forging any easier.
3851 *
3852 * 0-0 ContentType type; copied, must be handshake
3853 * 1-2 ProtocolVersion version; copied
3854 * 3-4 uint16 epoch; copied, must be 0
3855 * 5-10 uint48 sequence_number; copied
3856 * 11-12 uint16 length; (ignored)
3857 *
3858 * 13-13 HandshakeType msg_type; (ignored)
3859 * 14-16 uint24 length; (ignored)
3860 * 17-18 uint16 message_seq; copied
3861 * 19-21 uint24 fragment_offset; copied, must be 0
3862 * 22-24 uint24 fragment_length; (ignored)
3863 *
3864 * 25-26 ProtocolVersion client_version; (ignored)
3865 * 27-58 Random random; (ignored)
3866 * 59-xx SessionID session_id; 1 byte len + sid_len content
3867 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3868 * ...
3869 *
3870 * Minimum length is 61 bytes.
3871 */
3872 if( in_len < 61 ||
3873 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3874 in[3] != 0 || in[4] != 0 ||
3875 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3876 {
3877 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3878 }
3879
3880 sid_len = in[59];
3881 if( sid_len > in_len - 61 )
3882 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3883
3884 cookie_len = in[60 + sid_len];
3885 if( cookie_len > in_len - 60 )
3886 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3887
3888 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3889 cli_id, cli_id_len ) == 0 )
3890 {
3891 /* Valid cookie */
3892 return( 0 );
3893 }
3894
3895 /*
3896 * If we get here, we've got an invalid cookie, let's prepare HVR.
3897 *
3898 * 0-0 ContentType type; copied
3899 * 1-2 ProtocolVersion version; copied
3900 * 3-4 uint16 epoch; copied
3901 * 5-10 uint48 sequence_number; copied
3902 * 11-12 uint16 length; olen - 13
3903 *
3904 * 13-13 HandshakeType msg_type; hello_verify_request
3905 * 14-16 uint24 length; olen - 25
3906 * 17-18 uint16 message_seq; copied
3907 * 19-21 uint24 fragment_offset; copied
3908 * 22-24 uint24 fragment_length; olen - 25
3909 *
3910 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3911 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3912 *
3913 * Minimum length is 28.
3914 */
3915 if( buf_len < 28 )
3916 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3917
3918 /* Copy most fields and adapt others */
3919 memcpy( obuf, in, 25 );
3920 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3921 obuf[25] = 0xfe;
3922 obuf[26] = 0xff;
3923
3924 /* Generate and write actual cookie */
3925 p = obuf + 28;
3926 if( f_cookie_write( p_cookie,
3927 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3928 {
3929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3930 }
3931
3932 *olen = p - obuf;
3933
3934 /* Go back and fill length fields */
3935 obuf[27] = (unsigned char)( *olen - 28 );
3936
3937 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3938 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3939 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3940
3941 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3942 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3943
3944 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3945}
3946
3947/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003948 * Handle possible client reconnect with the same UDP quadruplet
3949 * (RFC 6347 Section 4.2.8).
3950 *
3951 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3952 * that looks like a ClientHello.
3953 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003954 * - if the input looks like a ClientHello without cookies,
3955 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003956 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003957 * - if the input looks like a ClientHello with a valid cookie,
3958 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003959 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003960 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003961 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003962 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003963 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3964 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003965 */
3966static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3967{
3968 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003969 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003970
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003971 ret = ssl_check_dtls_clihlo_cookie(
3972 ssl->conf->f_cookie_write,
3973 ssl->conf->f_cookie_check,
3974 ssl->conf->p_cookie,
3975 ssl->cli_id, ssl->cli_id_len,
3976 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003977 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003978
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003979 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3980
3981 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003982 {
Brian J Murray1903fb32016-11-06 04:45:15 -08003983 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003984 * If the error is permanent we'll catch it later,
3985 * if it's not, then hopefully it'll work next time. */
3986 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3987
3988 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003989 }
3990
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003991 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003992 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003993 /* Got a valid cookie, partially reset context */
3994 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3995 {
3996 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3997 return( ret );
3998 }
3999
4000 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004001 }
4002
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004003 return( ret );
4004}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004005#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004006
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004007/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004008 * ContentType type;
4009 * ProtocolVersion version;
4010 * uint16 epoch; // DTLS only
4011 * uint48 sequence_number; // DTLS only
4012 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004013 *
4014 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004015 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004016 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4017 *
4018 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004019 * 1. proceed with the record if this function returns 0
4020 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4021 * 3. return CLIENT_RECONNECT if this function return that value
4022 * 4. drop the whole datagram if this function returns anything else.
4023 * Point 2 is needed when the peer is resending, and we have already received
4024 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004026static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004027{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004028 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 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 +02004031
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004033 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004034 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004037 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004038 ssl->in_msgtype,
4039 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004041 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4043 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4044 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4045 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004048
4049#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004050 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4051 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004052 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4053#endif /* MBEDTLS_SSL_PROTO_DTLS */
4054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4055 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004058 }
4059
4060 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004061 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4064 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004065 }
4066
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004067 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4070 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004071 }
4072
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004073 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004074 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004075 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4078 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004079 }
4080
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004081 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004082 * DTLS-related tests.
4083 * Check epoch before checking length constraint because
4084 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4085 * message gets duplicated before the corresponding Finished message,
4086 * the second ChangeCipherSpec should be discarded because it belongs
4087 * to an old epoch, but not because its length is shorter than
4088 * the minimum record length for packets using the new record transform.
4089 * Note that these two kinds of failures are handled differently,
4090 * as an unexpected record is silently skipped but an invalid
4091 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004092 */
4093#if defined(MBEDTLS_SSL_PROTO_DTLS)
4094 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4095 {
4096 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4097
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004098 /* Check epoch (and sequence number) with DTLS */
4099 if( rec_epoch != ssl->in_epoch )
4100 {
4101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4102 "expected %d, received %d",
4103 ssl->in_epoch, rec_epoch ) );
4104
4105#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4106 /*
4107 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4108 * access the first byte of record content (handshake type), as we
4109 * have an active transform (possibly iv_len != 0), so use the
4110 * fact that the record header len is 13 instead.
4111 */
4112 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4113 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4114 rec_epoch == 0 &&
4115 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4116 ssl->in_left > 13 &&
4117 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4118 {
4119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4120 "from the same port" ) );
4121 return( ssl_handle_possible_reconnect( ssl ) );
4122 }
4123 else
4124#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
4125 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4126 }
4127
4128#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4129 /* Replay detection only works for the current epoch */
4130 if( rec_epoch == ssl->in_epoch &&
4131 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4132 {
4133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4134 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4135 }
4136#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004137
4138 /* Drop unexpected ChangeCipherSpec messages */
4139 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4140 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4141 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4142 {
4143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
4144 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4145 }
4146
4147 /* Drop unexpected ApplicationData records,
4148 * except at the beginning of renegotiations */
4149 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4150 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4151#if defined(MBEDTLS_SSL_RENEGOTIATION)
4152 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4153 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4154#endif
4155 )
4156 {
4157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4158 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4159 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004160 }
4161#endif /* MBEDTLS_SSL_PROTO_DTLS */
4162
Hanno Becker52c6dc62017-05-26 16:07:36 +01004163
4164 /* Check length against bounds of the current transform and version */
4165 if( ssl->transform_in == NULL )
4166 {
4167 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004168 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004169 {
4170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4172 }
4173 }
4174 else
4175 {
4176 if( ssl->in_msglen < ssl->transform_in->minlen )
4177 {
4178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4179 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4180 }
4181
4182#if defined(MBEDTLS_SSL_PROTO_SSL3)
4183 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004184 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004185 {
4186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4187 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4188 }
4189#endif
4190#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4191 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4192 /*
4193 * TLS encrypted messages can have up to 256 bytes of padding
4194 */
4195 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4196 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004197 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004198 {
4199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4200 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4201 }
4202#endif
4203 }
4204
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004205 return( 0 );
4206}
Paul Bakker5121ce52009-01-03 21:22:43 +00004207
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004208/*
4209 * If applicable, decrypt (and decompress) record content
4210 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004212{
4213 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
4216 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4219 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 ret = mbedtls_ssl_hw_record_read( ssl );
4224 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4227 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004228 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004229
4230 if( ret == 0 )
4231 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004234 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004235 {
4236 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
4237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004238 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004239 return( ret );
4240 }
4241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 ssl->in_msg, ssl->in_msglen );
4244
Angus Grattond8213d02016-05-25 20:56:48 +10004245 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4248 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004249 }
4250 }
4251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004253 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004255 {
4256 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004259 return( ret );
4260 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004261 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004265 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004268 }
4269#endif
4270
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004271 return( 0 );
4272}
4273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004275
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004276/*
4277 * Read a record.
4278 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004279 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4280 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4281 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004284{
4285 int ret;
4286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004288
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004289 if( ssl->keep_current_message == 0 )
4290 {
4291 do {
Simon Butcher99000142016-10-13 17:21:01 +01004292
Hanno Becker90333da2017-10-10 11:27:13 +01004293 do ret = mbedtls_ssl_read_record_layer( ssl );
4294 while( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4295
4296 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004297 {
4298 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_read_record_layer" ), ret );
4299 return( ret );
4300 }
4301
4302 ret = mbedtls_ssl_handle_message_type( ssl );
4303
Hanno Becker90333da2017-10-10 11:27:13 +01004304 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4305 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004306
4307 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004308 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004309 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004310 return( ret );
4311 }
4312
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004313 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
4314 {
4315 mbedtls_ssl_update_handshake_status( ssl );
4316 }
Simon Butcher99000142016-10-13 17:21:01 +01004317 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004318 else
Simon Butcher99000142016-10-13 17:21:01 +01004319 {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= reuse previously read message" ) );
4321 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004322 }
4323
4324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4325
4326 return( 0 );
4327}
4328
4329int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl )
4330{
4331 int ret;
4332
Hanno Becker4a810fb2017-05-24 16:27:30 +01004333 /*
4334 * Step A
4335 *
4336 * Consume last content-layer message and potentially
4337 * update in_msglen which keeps track of the contents'
4338 * consumption state.
4339 *
4340 * (1) Handshake messages:
4341 * Remove last handshake message, move content
4342 * and adapt in_msglen.
4343 *
4344 * (2) Alert messages:
4345 * Consume whole record content, in_msglen = 0.
4346 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004347 * (3) Change cipher spec:
4348 * Consume whole record content, in_msglen = 0.
4349 *
4350 * (4) Application data:
4351 * Don't do anything - the record layer provides
4352 * the application data as a stream transport
4353 * and consumes through mbedtls_ssl_read only.
4354 *
4355 */
4356
4357 /* Case (1): Handshake messages */
4358 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004359 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004360 /* Hard assertion to be sure that no application data
4361 * is in flight, as corrupting ssl->in_msglen during
4362 * ssl->in_offt != NULL is fatal. */
4363 if( ssl->in_offt != NULL )
4364 {
4365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4367 }
4368
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004369 /*
4370 * Get next Handshake message in the current record
4371 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004372
Hanno Becker4a810fb2017-05-24 16:27:30 +01004373 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004374 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004375 * current handshake content: If DTLS handshake
4376 * fragmentation is used, that's the fragment
4377 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004378 * size here is faulty and should be changed at
4379 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004380 * (2) While it doesn't seem to cause problems, one
4381 * has to be very careful not to assume that in_hslen
4382 * is always <= in_msglen in a sensible communication.
4383 * Again, it's wrong for DTLS handshake fragmentation.
4384 * The following check is therefore mandatory, and
4385 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004386 * Additionally, ssl->in_hslen might be arbitrarily out of
4387 * bounds after handling a DTLS message with an unexpected
4388 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004389 */
4390 if( ssl->in_hslen < ssl->in_msglen )
4391 {
4392 ssl->in_msglen -= ssl->in_hslen;
4393 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4394 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004395
Hanno Becker4a810fb2017-05-24 16:27:30 +01004396 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4397 ssl->in_msg, ssl->in_msglen );
4398 }
4399 else
4400 {
4401 ssl->in_msglen = 0;
4402 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004403
Hanno Becker4a810fb2017-05-24 16:27:30 +01004404 ssl->in_hslen = 0;
4405 }
4406 /* Case (4): Application data */
4407 else if( ssl->in_offt != NULL )
4408 {
4409 return( 0 );
4410 }
4411 /* Everything else (CCS & Alerts) */
4412 else
4413 {
4414 ssl->in_msglen = 0;
4415 }
4416
4417 /*
4418 * Step B
4419 *
4420 * Fetch and decode new record if current one is fully consumed.
4421 *
4422 */
4423
4424 if( ssl->in_msglen > 0 )
4425 {
4426 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004427 return( 0 );
4428 }
4429
Hanno Becker4a810fb2017-05-24 16:27:30 +01004430 /* Current record either fully processed or to be discarded. */
4431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004435 return( ret );
4436 }
4437
4438 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004441 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4442 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004443 {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004444 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4445 {
4446 /* Skip unexpected record (but not whole datagram) */
4447 ssl->next_record_offset = ssl->in_msglen
4448 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004449
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4451 "(header)" ) );
4452 }
4453 else
4454 {
4455 /* Skip invalid record and the rest of the datagram */
4456 ssl->next_record_offset = 0;
4457 ssl->in_left = 0;
4458
4459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4460 "(header)" ) );
4461 }
4462
4463 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004464 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004465 }
4466#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004467 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004468 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004469
4470 /*
4471 * Read and optionally decrypt the message contents
4472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4474 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004477 return( ret );
4478 }
4479
4480 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004482 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01004485 if( ssl->next_record_offset < ssl->in_left )
4486 {
4487 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4488 }
4489 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004490 else
4491#endif
4492 ssl->in_left = 0;
4493
4494 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004497 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004498 {
4499 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4501 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004502 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004503 /* Except when waiting for Finished as a bad mac here
4504 * probably means something went wrong in the handshake
4505 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4506 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4507 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4508 {
4509#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4510 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4511 {
4512 mbedtls_ssl_send_alert_message( ssl,
4513 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4514 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4515 }
4516#endif
4517 return( ret );
4518 }
4519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004521 if( ssl->conf->badmac_limit != 0 &&
4522 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4525 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004526 }
4527#endif
4528
Hanno Becker4a810fb2017-05-24 16:27:30 +01004529 /* As above, invalid records cause
4530 * dismissal of the whole datagram. */
4531
4532 ssl->next_record_offset = 0;
4533 ssl->in_left = 0;
4534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004536 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004537 }
4538
4539 return( ret );
4540 }
4541 else
4542#endif
4543 {
4544 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4546 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 mbedtls_ssl_send_alert_message( ssl,
4549 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4550 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004551 }
4552#endif
4553 return( ret );
4554 }
4555 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004556
Simon Butcher99000142016-10-13 17:21:01 +01004557 return( 0 );
4558}
4559
4560int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4561{
4562 int ret;
4563
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004564 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004565 * Handle particular types of records
4566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004568 {
Simon Butcher99000142016-10-13 17:21:01 +01004569 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4570 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004571 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 }
4574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004577 if( ssl->in_msglen != 2 )
4578 {
4579 /* Note: Standard allows for more than one 2 byte alert
4580 to be packed in a single message, but Mbed TLS doesn't
4581 currently support this. */
4582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4583 ssl->in_msglen ) );
4584 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4585 }
4586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004588 ssl->in_msg[0], ssl->in_msg[1] ) );
4589
4590 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004591 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004596 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004598 }
4599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4601 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4604 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004605 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004606
4607#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4608 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4609 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4610 {
Hanno Becker90333da2017-10-10 11:27:13 +01004611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004612 /* Will be handled when trying to parse ServerHello */
4613 return( 0 );
4614 }
4615#endif
4616
4617#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4618 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4619 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4620 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4621 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4622 {
4623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4624 /* Will be handled in mbedtls_ssl_parse_certificate() */
4625 return( 0 );
4626 }
4627#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4628
4629 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004630 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 }
4632
Hanno Beckerc76c6192017-06-06 10:03:17 +01004633#if defined(MBEDTLS_SSL_PROTO_DTLS)
4634 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4635 ssl->handshake != NULL &&
4636 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4637 {
4638 ssl_handshake_wrapup_free_hs_transform( ssl );
4639 }
4640#endif
4641
Paul Bakker5121ce52009-01-03 21:22:43 +00004642 return( 0 );
4643}
4644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004646{
4647 int ret;
4648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4650 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4651 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004652 {
4653 return( ret );
4654 }
4655
4656 return( 0 );
4657}
4658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004660 unsigned char level,
4661 unsigned char message )
4662{
4663 int ret;
4664
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004665 if( ssl == NULL || ssl->conf == NULL )
4666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004672 ssl->out_msglen = 2;
4673 ssl->out_msg[0] = level;
4674 ssl->out_msg[1] = message;
4675
Hanno Becker67bc7c32018-08-06 11:33:50 +01004676 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004678 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004679 return( ret );
4680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004682
4683 return( 0 );
4684}
4685
Paul Bakker5121ce52009-01-03 21:22:43 +00004686/*
4687 * Handshake functions
4688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4690 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4691 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4692 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4693 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4694 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4695 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02004696/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4704 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004705 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4706 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004709 ssl->state++;
4710 return( 0 );
4711 }
4712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4714 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004715}
4716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4724 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004725 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4726 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004729 ssl->state++;
4730 return( 0 );
4731 }
4732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004735}
Gilles Peskinef9828522017-05-03 12:28:43 +02004736
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004737#else
Gilles Peskinef9828522017-05-03 12:28:43 +02004738/* Some certificate support -> implement write and parse */
4739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004741{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004743 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 const mbedtls_x509_crt *crt;
4745 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004748
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 Bakker48f7a5d2013-04-19 14:30:58 +02004753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004755 ssl->state++;
4756 return( 0 );
4757 }
4758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004760 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 {
4762 if( ssl->client_auth == 0 )
4763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004765 ssl->state++;
4766 return( 0 );
4767 }
4768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004770 /*
4771 * If using SSLv3 and got no cert, send an Alert message
4772 * (otherwise an empty Certificate message will be sent).
4773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4775 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004776 {
4777 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4779 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4780 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004783 goto write_msg;
4784 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004786 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787#endif /* MBEDTLS_SSL_CLI_C */
4788#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004789 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4794 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 }
4796 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004797#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004800
4801 /*
4802 * 0 . 0 handshake type
4803 * 1 . 3 handshake length
4804 * 4 . 6 length of all certs
4805 * 7 . 9 length of cert. 1
4806 * 10 . n-1 peer certificate
4807 * n . n+2 length of cert. 2
4808 * n+3 . ... upper level cert, etc.
4809 */
4810 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004812
Paul Bakker29087132010-03-21 21:03:34 +00004813 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 {
4815 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10004816 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10004819 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 }
4822
4823 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4824 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4825 ssl->out_msg[i + 2] = (unsigned char)( n );
4826
4827 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4828 i += n; crt = crt->next;
4829 }
4830
4831 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4832 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4833 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4834
4835 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4837 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004838
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004839#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004840write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004842
4843 ssl->state++;
4844
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004845 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 return( ret );
4849 }
4850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004852
Paul Bakkered27a042013-04-18 22:46:23 +02004853 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004854}
4855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004857{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004859 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004861 int authmode = ssl->conf->authmode;
Gilles Peskine064a85c2017-05-10 10:46:40 +02004862 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00004863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4867 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02004868 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
4869 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004872 ssl->state++;
4873 return( 0 );
4874 }
4875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004876#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004877 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004878 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4879 {
4880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4881 ssl->state++;
4882 return( 0 );
4883 }
4884
4885#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4886 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4887 authmode = ssl->handshake->sni_authmode;
4888#endif
4889
4890 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4891 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004893 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004895 ssl->state++;
4896 return( 0 );
4897 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004902 /* mbedtls_ssl_read_record may have sent an alert already. We
4903 let it decide whether to alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 return( ret );
4906 }
4907
4908 ssl->state++;
4909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910#if defined(MBEDTLS_SSL_SRV_C)
4911#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 /*
4913 * Check if the client sent an empty certificate
4914 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004915 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004917 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004918 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4920 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4921 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004924
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004925 /* The client was asked for a certificate but didn't send
4926 one. The client should know what's going on, so we
4927 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004928 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004929 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004930 return( 0 );
4931 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 }
4934 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4938 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004939 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4943 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4944 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4945 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004948
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004949 /* The client was asked for a certificate but didn't send
4950 one. The client should know what's going on, so we
4951 don't send an alert. */
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004952 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004953 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004954 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004955 else
4956 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004957 }
4958 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4960 MBEDTLS_SSL_PROTO_TLS1_2 */
4961#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004966 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4967 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004969 }
4970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4972 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004975 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4976 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004977 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004978 }
4979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004981
Paul Bakker5121ce52009-01-03 21:22:43 +00004982 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004984 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004985 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004986
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004987 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004991 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4992 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004994 }
4995
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004996 /* In case we tried to reuse a session but it failed */
4997 if( ssl->session_negotiate->peer_cert != NULL )
4998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
5000 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02005001 }
5002
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005003 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007 sizeof( mbedtls_x509_crt ) ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005008 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5009 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005010 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005011 }
5012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005014
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00005015 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00005016
5017 while( i < ssl->in_hslen )
5018 {
Philippe Antoine747fd532018-05-30 09:13:21 +02005019 if ( i + 3 > ssl->in_hslen ) {
5020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
5021 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
5023 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
5024 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005025 if( ssl->in_msg[i] != 0 )
5026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005028 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5029 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005031 }
5032
5033 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
5034 | (unsigned int) ssl->in_msg[i + 2];
5035 i += 3;
5036
5037 if( n < 128 || i + n > ssl->in_hslen )
5038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005040 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5041 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005042 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005043 }
5044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005045 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02005046 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005047 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00005048 {
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005049 case 0: /*ok*/
5050 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
5051 /* Ignore certificate with an unknown algorithm: maybe a
5052 prior certificate was already trusted. */
5053 break;
5054
5055 case MBEDTLS_ERR_X509_ALLOC_FAILED:
5056 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
5057 goto crt_parse_der_failed;
5058
5059 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
5060 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5061 goto crt_parse_der_failed;
5062
5063 default:
5064 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5065 crt_parse_der_failed:
5066 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005068 return( ret );
5069 }
5070
5071 i += n;
5072 }
5073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00005075
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005076 /*
5077 * On client, make sure the server cert doesn't change during renego to
5078 * avoid "triple handshake" attack: https://secure-resumption.com/
5079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005081 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005083 {
5084 if( ssl->session->peer_cert == NULL )
5085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005087 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5088 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005090 }
5091
5092 if( ssl->session->peer_cert->raw.len !=
5093 ssl->session_negotiate->peer_cert->raw.len ||
5094 memcmp( ssl->session->peer_cert->raw.p,
5095 ssl->session_negotiate->peer_cert->raw.p,
5096 ssl->session->peer_cert->raw.len ) != 0 )
5097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005099 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5100 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005102 }
5103 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01005105
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005106 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005107 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005108 mbedtls_x509_crt *ca_chain;
5109 mbedtls_x509_crl *ca_crl;
5110
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005111#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005112 if( ssl->handshake->sni_ca_chain != NULL )
5113 {
5114 ca_chain = ssl->handshake->sni_ca_chain;
5115 ca_crl = ssl->handshake->sni_ca_crl;
5116 }
5117 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005118#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005119 {
5120 ca_chain = ssl->conf->ca_chain;
5121 ca_crl = ssl->conf->ca_crl;
5122 }
5123
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005124 /*
5125 * Main check: verify certificate
5126 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005127 ret = mbedtls_x509_crt_verify_with_profile(
5128 ssl->session_negotiate->peer_cert,
5129 ca_chain, ca_crl,
5130 ssl->conf->cert_profile,
5131 ssl->hostname,
5132 &ssl->session_negotiate->verify_result,
5133 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00005134
5135 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005138 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005139
5140 /*
5141 * Secondary checks: always done, but change 'ret' only if it was 0
5142 */
5143
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005144#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005147
5148 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005150 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005151 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005152 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
5153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005155 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005157 }
5158 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005159#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005162 ciphersuite_info,
5163 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005164 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005167 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005169 }
5170
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005171 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
5172 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
5173 * with details encoded in the verification flags. All other kinds
5174 * of error codes, including those from the user provided f_vrfy
5175 * functions, are treated as fatal and lead to a failure of
5176 * ssl_parse_certificate even if verification was optional. */
5177 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
5178 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
5179 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
5180 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005181 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005182 }
5183
5184 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
5185 {
5186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
5187 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
5188 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005189
5190 if( ret != 0 )
5191 {
5192 /* The certificate may have been rejected for several reasons.
5193 Pick one and send the corresponding alert. Which alert to send
5194 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005195 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
5196 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
5197 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
5198 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
5199 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
5200 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5201 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
5202 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5203 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
5204 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5205 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
5206 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5207 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
5208 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
5209 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
5210 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
5211 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
5212 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
5213 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
5214 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02005215 else
5216 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005217 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5218 alert );
5219 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01005220
Hanno Beckere6706e62017-05-15 16:05:15 +01005221#if defined(MBEDTLS_DEBUG_C)
5222 if( ssl->session_negotiate->verify_result != 0 )
5223 {
5224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
5225 ssl->session_negotiate->verify_result ) );
5226 }
5227 else
5228 {
5229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
5230 }
5231#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005232 }
5233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005235
5236 return( ret );
5237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
5239 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
5240 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
5241 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
5242 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
5243 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
5244 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005247{
5248 int ret;
5249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005253 ssl->out_msglen = 1;
5254 ssl->out_msg[0] = 1;
5255
Paul Bakker5121ce52009-01-03 21:22:43 +00005256 ssl->state++;
5257
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005258 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005259 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005261 return( ret );
5262 }
5263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005265
5266 return( 0 );
5267}
5268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005270{
5271 int ret;
5272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005278 return( ret );
5279 }
5280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005284 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5285 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005287 }
5288
5289 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
5290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005292 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5293 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00005295 }
5296
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005297 /*
5298 * Switch to our negotiated transform and session parameters for inbound
5299 * data.
5300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005302 ssl->transform_in = ssl->transform_negotiate;
5303 ssl->session_in = ssl->session_negotiate;
5304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005306 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005309 ssl_dtls_replay_reset( ssl );
5310#endif
5311
5312 /* Increment epoch */
5313 if( ++ssl->in_epoch == 0 )
5314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005316 /* This is highly unlikely to happen for legitimate reasons, so
5317 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005319 }
5320 }
5321 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005322#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005323 memset( ssl->in_ctr, 0, 8 );
5324
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005325 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5328 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005333 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5334 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005336 }
5337 }
5338#endif
5339
Paul Bakker5121ce52009-01-03 21:22:43 +00005340 ssl->state++;
5341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005343
5344 return( 0 );
5345}
5346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
5348 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00005349{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02005350 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01005351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5353 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5354 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00005355 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00005356 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005357#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5359#if defined(MBEDTLS_SHA512_C)
5360 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005361 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
5362 else
5363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364#if defined(MBEDTLS_SHA256_C)
5365 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00005366 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005367 else
5368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005372 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005373 }
Paul Bakker380da532012-04-18 16:10:25 +00005374}
Paul Bakkerf7abd422013-04-16 13:15:56 +02005375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005377{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5379 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005380 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
5381 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005382#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5384#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005385 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005388 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02005391}
5392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005394 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5397 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005398 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5399 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005400#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5402#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005403 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005406 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01005407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005409}
5410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5412 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5413static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005414 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005415{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005416 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
5417 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005418}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005419#endif
Paul Bakker380da532012-04-18 16:10:25 +00005420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5422#if defined(MBEDTLS_SHA256_C)
5423static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005424 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005425{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005426 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005427}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005428#endif
Paul Bakker380da532012-04-18 16:10:25 +00005429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005430#if defined(MBEDTLS_SHA512_C)
5431static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005432 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00005433{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005434 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00005435}
Paul Bakker769075d2012-11-24 11:26:46 +01005436#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00005438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005439#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005440static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442{
Paul Bakker3c2122f2013-06-24 19:03:14 +02005443 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005444 mbedtls_md5_context md5;
5445 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005446
Paul Bakker5121ce52009-01-03 21:22:43 +00005447 unsigned char padbuf[48];
5448 unsigned char md5sum[16];
5449 unsigned char sha1sum[20];
5450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005452 if( !session )
5453 session = ssl->session;
5454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005456
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005457 mbedtls_md5_init( &md5 );
5458 mbedtls_sha1_init( &sha1 );
5459
5460 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5461 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005462
5463 /*
5464 * SSLv3:
5465 * hash =
5466 * MD5( master + pad2 +
5467 * MD5( handshake + sender + master + pad1 ) )
5468 * + SHA1( master + pad2 +
5469 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 */
5471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005473 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5474 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005475#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005478 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5479 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005480#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005482 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02005483 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00005484
Paul Bakker1ef83d62012-04-11 12:09:53 +00005485 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005487 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
5488 mbedtls_md5_update_ret( &md5, session->master, 48 );
5489 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5490 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005492 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
5493 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5494 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
5495 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005496
Paul Bakker1ef83d62012-04-11 12:09:53 +00005497 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005498
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005499 mbedtls_md5_starts_ret( &md5 );
5500 mbedtls_md5_update_ret( &md5, session->master, 48 );
5501 mbedtls_md5_update_ret( &md5, padbuf, 48 );
5502 mbedtls_md5_update_ret( &md5, md5sum, 16 );
5503 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005504
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005505 mbedtls_sha1_starts_ret( &sha1 );
5506 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
5507 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
5508 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
5509 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005513 mbedtls_md5_free( &md5 );
5514 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005515
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005516 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
5517 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
5518 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005525static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005527{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005528 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005529 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005530 mbedtls_md5_context md5;
5531 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005532 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005535 if( !session )
5536 session = ssl->session;
5537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005539
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005540 mbedtls_md5_init( &md5 );
5541 mbedtls_sha1_init( &sha1 );
5542
5543 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5544 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005545
Paul Bakker1ef83d62012-04-11 12:09:53 +00005546 /*
5547 * TLSv1:
5548 * hash = PRF( master, finished_label,
5549 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5550 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005553 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5554 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005555#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005558 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5559 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005560#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005563 ? "client finished"
5564 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005565
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005566 mbedtls_md5_finish_ret( &md5, padbuf );
5567 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005568
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005569 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005570 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005573
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005574 mbedtls_md5_free( &md5 );
5575 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005576
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005577 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5584#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005585static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005587{
5588 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005589 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005590 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005591 unsigned char padbuf[32];
5592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005594 if( !session )
5595 session = ssl->session;
5596
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005597 mbedtls_sha256_init( &sha256 );
5598
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005600
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005601 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005602
5603 /*
5604 * TLSv1.2:
5605 * hash = PRF( master, finished_label,
5606 * Hash( handshake ) )[0.11]
5607 */
5608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609#if !defined(MBEDTLS_SHA256_ALT)
5610 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005611 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005612#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005615 ? "client finished"
5616 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005617
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005618 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005619
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005620 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005621 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005624
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005625 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005626
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005627 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005630}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005634static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005636{
5637 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005638 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005639 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005640 unsigned char padbuf[48];
5641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005643 if( !session )
5644 session = ssl->session;
5645
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005646 mbedtls_sha512_init( &sha512 );
5647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005649
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005650 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005651
5652 /*
5653 * TLSv1.2:
5654 * hash = PRF( master, finished_label,
5655 * Hash( handshake ) )[0.11]
5656 */
5657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005659 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5660 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005661#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005664 ? "client finished"
5665 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005666
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005667 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005668
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005669 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005670 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005673
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005674 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005675
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005676 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005679}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680#endif /* MBEDTLS_SHA512_C */
5681#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005684{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005686
5687 /*
5688 * Free our handshake params
5689 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02005690 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005692 ssl->handshake = NULL;
5693
5694 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005695 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005696 */
5697 if( ssl->transform )
5698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 mbedtls_ssl_transform_free( ssl->transform );
5700 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005701 }
5702 ssl->transform = ssl->transform_negotiate;
5703 ssl->transform_negotiate = NULL;
5704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005706}
5707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005709{
5710 int resume = ssl->handshake->resume;
5711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714#if defined(MBEDTLS_SSL_RENEGOTIATION)
5715 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005718 ssl->renego_records_seen = 0;
5719 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005720#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005721
5722 /*
5723 * Free the previous session and switch in the current one
5724 */
Paul Bakker0a597072012-09-25 21:55:46 +00005725 if( ssl->session )
5726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005728 /* RFC 7366 3.1: keep the EtM state */
5729 ssl->session_negotiate->encrypt_then_mac =
5730 ssl->session->encrypt_then_mac;
5731#endif
5732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 mbedtls_ssl_session_free( ssl->session );
5734 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005735 }
5736 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005737 ssl->session_negotiate = NULL;
5738
Paul Bakker0a597072012-09-25 21:55:46 +00005739 /*
5740 * Add cache entry
5741 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005742 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005743 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005744 resume == 0 )
5745 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005746 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005748 }
Paul Bakker0a597072012-09-25 21:55:46 +00005749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005751 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005752 ssl->handshake->flight != NULL )
5753 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005754 /* Cancel handshake timer */
5755 ssl_set_timer( ssl, 0 );
5756
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005757 /* Keep last flight around in case we need to resend it:
5758 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005760 }
5761 else
5762#endif
5763 ssl_handshake_wrapup_free_hs_transform( ssl );
5764
Paul Bakker48916f92012-09-16 19:57:18 +00005765 ssl->state++;
5766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005767 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005768}
5769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005771{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005772 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005775
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005776 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01005777
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005778 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005779
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01005780 /*
5781 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
5782 * may define some other value. Currently (early 2016), no defined
5783 * ciphersuite does this (and this is unlikely to change as activity has
5784 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
5785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005788#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005789 ssl->verify_data_len = hash_len;
5790 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005791#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005792
Paul Bakker5121ce52009-01-03 21:22:43 +00005793 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5795 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005796
5797 /*
5798 * In case of session resuming, invert the client and server
5799 * ChangeCipherSpec messages order.
5800 */
Paul Bakker0a597072012-09-25 21:55:46 +00005801 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005804 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005808 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005809 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005810#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005811 }
5812 else
5813 ssl->state++;
5814
Paul Bakker48916f92012-09-16 19:57:18 +00005815 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005816 * Switch to our negotiated transform and session parameters for outbound
5817 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005822 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005823 {
5824 unsigned char i;
5825
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005826 /* Remember current epoch settings for resending */
5827 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01005828 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005829
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005830 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01005831 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005832
5833 /* Increment epoch */
5834 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01005835 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005836 break;
5837
5838 /* The loop goes to its end iff the counter is wrapping */
5839 if( i == 0 )
5840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5842 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005843 }
5844 }
5845 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005846#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01005847 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005848
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005849 ssl->transform_out = ssl->transform_negotiate;
5850 ssl->session_out = ssl->session_negotiate;
5851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005852#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5853 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5858 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005859 }
5860 }
5861#endif
5862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005864 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005866#endif
5867
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005868 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005869 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005871 return( ret );
5872 }
5873
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005874#if defined(MBEDTLS_SSL_PROTO_DTLS)
5875 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5876 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
5877 {
5878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
5879 return( ret );
5880 }
5881#endif
5882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005884
5885 return( 0 );
5886}
5887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005889#define SSL_MAX_HASH_LEN 36
5890#else
5891#define SSL_MAX_HASH_LEN 12
5892#endif
5893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005895{
Paul Bakker23986e52011-04-24 08:57:21 +00005896 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005897 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005898 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005901
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005902 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005904 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005907 return( ret );
5908 }
5909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005913 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5914 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 }
5917
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005918 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919#if defined(MBEDTLS_SSL_PROTO_SSL3)
5920 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005921 hash_len = 36;
5922 else
5923#endif
5924 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5927 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005930 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5931 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933 }
5934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005936 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005939 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5940 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 }
5943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005945 ssl->verify_data_len = hash_len;
5946 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005947#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005948
Paul Bakker0a597072012-09-25 21:55:46 +00005949 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005952 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005956 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005958#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005959 }
5960 else
5961 ssl->state++;
5962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005964 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005966#endif
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005969
5970 return( 0 );
5971}
5972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005974{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5978 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5979 mbedtls_md5_init( &handshake->fin_md5 );
5980 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005981 mbedtls_md5_starts_ret( &handshake->fin_md5 );
5982 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5985#if defined(MBEDTLS_SHA256_C)
5986 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005987 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005988#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005989#if defined(MBEDTLS_SHA512_C)
5990 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01005991 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005993#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005994
5995 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005996
5997#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5998 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5999 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
6000#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002#if defined(MBEDTLS_DHM_C)
6003 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005#if defined(MBEDTLS_ECDH_C)
6006 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006007#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006008#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006009 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006010#if defined(MBEDTLS_SSL_CLI_C)
6011 handshake->ecjpake_cache = NULL;
6012 handshake->ecjpake_cache_len = 0;
6013#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006014#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006015
6016#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6017 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
6018#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006019}
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 mbedtls_cipher_init( &transform->cipher_ctx_enc );
6026 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02006027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 mbedtls_md_init( &transform->md_ctx_enc );
6029 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006030}
6031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006035}
6036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006038{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006039 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00006040 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006042 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006044 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02006045 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006046
6047 /*
6048 * Either the pointers are now NULL or cleared properly and can be freed.
6049 * Now allocate missing structures.
6050 */
6051 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006052 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006053 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006054 }
Paul Bakker48916f92012-09-16 19:57:18 +00006055
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006056 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006057 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006058 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006059 }
Paul Bakker48916f92012-09-16 19:57:18 +00006060
Paul Bakker82788fb2014-10-20 13:59:19 +02006061 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006062 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006063 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02006064 }
Paul Bakker48916f92012-09-16 19:57:18 +00006065
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006066 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00006067 if( ssl->handshake == NULL ||
6068 ssl->transform_negotiate == NULL ||
6069 ssl->session_negotiate == NULL )
6070 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02006071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 mbedtls_free( ssl->handshake );
6074 mbedtls_free( ssl->transform_negotiate );
6075 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006076
6077 ssl->handshake = NULL;
6078 ssl->transform_negotiate = NULL;
6079 ssl->session_negotiate = NULL;
6080
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006081 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00006082 }
6083
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006084 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006086 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02006087 ssl_handshake_params_init( ssl->handshake );
6088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6091 {
6092 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006093
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006094 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6095 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
6096 else
6097 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006098
6099 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02006100 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02006101#endif
6102
Paul Bakker48916f92012-09-16 19:57:18 +00006103 return( 0 );
6104}
6105
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006106#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006107/* Dummy cookie callbacks for defaults */
6108static int ssl_cookie_write_dummy( void *ctx,
6109 unsigned char **p, unsigned char *end,
6110 const unsigned char *cli_id, size_t cli_id_len )
6111{
6112 ((void) ctx);
6113 ((void) p);
6114 ((void) end);
6115 ((void) cli_id);
6116 ((void) cli_id_len);
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006119}
6120
6121static int ssl_cookie_check_dummy( void *ctx,
6122 const unsigned char *cookie, size_t cookie_len,
6123 const unsigned char *cli_id, size_t cli_id_len )
6124{
6125 ((void) ctx);
6126 ((void) cookie);
6127 ((void) cookie_len);
6128 ((void) cli_id);
6129 ((void) cli_id_len);
6130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006132}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006133#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02006134
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006135/* Once ssl->out_hdr as the address of the beginning of the
6136 * next outgoing record is set, deduce the other pointers.
6137 *
6138 * Note: For TLS, we save the implicit record sequence number
6139 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
6140 * and the caller has to make sure there's space for this.
6141 */
6142
6143static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
6144 mbedtls_ssl_transform *transform )
6145{
6146#if defined(MBEDTLS_SSL_PROTO_DTLS)
6147 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6148 {
6149 ssl->out_ctr = ssl->out_hdr + 3;
6150 ssl->out_len = ssl->out_hdr + 11;
6151 ssl->out_iv = ssl->out_hdr + 13;
6152 }
6153 else
6154#endif
6155 {
6156 ssl->out_ctr = ssl->out_hdr - 8;
6157 ssl->out_len = ssl->out_hdr + 3;
6158 ssl->out_iv = ssl->out_hdr + 5;
6159 }
6160
6161 /* Adjust out_msg to make space for explicit IV, if used. */
6162 if( transform != NULL &&
6163 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6164 {
6165 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
6166 }
6167 else
6168 ssl->out_msg = ssl->out_iv;
6169}
6170
6171/* Once ssl->in_hdr as the address of the beginning of the
6172 * next incoming record is set, deduce the other pointers.
6173 *
6174 * Note: For TLS, we save the implicit record sequence number
6175 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
6176 * and the caller has to make sure there's space for this.
6177 */
6178
6179static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
6180 mbedtls_ssl_transform *transform )
6181{
6182#if defined(MBEDTLS_SSL_PROTO_DTLS)
6183 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6184 {
6185 ssl->in_ctr = ssl->in_hdr + 3;
6186 ssl->in_len = ssl->in_hdr + 11;
6187 ssl->in_iv = ssl->in_hdr + 13;
6188 }
6189 else
6190#endif
6191 {
6192 ssl->in_ctr = ssl->in_hdr - 8;
6193 ssl->in_len = ssl->in_hdr + 3;
6194 ssl->in_iv = ssl->in_hdr + 5;
6195 }
6196
6197 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
6198 if( transform != NULL &&
6199 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
6200 {
6201 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
6202 }
6203 else
6204 ssl->in_msg = ssl->in_iv;
6205}
6206
Paul Bakker5121ce52009-01-03 21:22:43 +00006207/*
6208 * Initialize an SSL context
6209 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02006210void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
6211{
6212 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
6213}
6214
6215/*
6216 * Setup an SSL context
6217 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006218int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02006219 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00006220{
Paul Bakker48916f92012-09-16 19:57:18 +00006221 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006222
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02006223 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00006224
6225 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01006226 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00006227 */
Angus Grattond8213d02016-05-25 20:56:48 +10006228 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
6229 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 {
Angus Grattond8213d02016-05-25 20:56:48 +10006231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
6232 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6233 }
6234
6235 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
6236 if( ssl->out_buf == NULL )
6237 {
6238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006240 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006241 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 }
6243
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006244 /* Set the incoming and outgoing record pointers. */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006245#if defined(MBEDTLS_SSL_PROTO_DTLS)
6246 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6247 {
6248 ssl->out_hdr = ssl->out_buf;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006249 ssl->in_hdr = ssl->in_buf;
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006250 }
6251 else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006252#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006253 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006254 ssl->out_hdr = ssl->out_buf + 8;
6255 ssl->in_hdr = ssl->in_buf + 8;
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02006256 }
6257
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01006258 /* Derive other internal pointers. */
6259 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
6260 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
6261
Paul Bakker48916f92012-09-16 19:57:18 +00006262 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6263 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006264
6265 return( 0 );
6266}
6267
6268/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00006269 * Reset an initialized and used SSL context for re-use while retaining
6270 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006271 *
6272 * If partial is non-zero, keep data in the input buffer and client ID.
6273 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00006274 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006275static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00006276{
Paul Bakker48916f92012-09-16 19:57:18 +00006277 int ret;
6278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006280
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006281 /* Cancel any possibly running timer */
6282 ssl_set_timer( ssl, 0 );
6283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284#if defined(MBEDTLS_SSL_RENEGOTIATION)
6285 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006286 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006287
6288 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
6290 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00006293
Paul Bakker7eb013f2011-10-06 12:37:39 +00006294 ssl->in_offt = NULL;
6295
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006296 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006297 ssl->in_msgtype = 0;
6298 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006299 if( partial == 0 )
6300 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006302 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006303 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02006304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02006306 ssl_dtls_replay_reset( ssl );
6307#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006308
6309 ssl->in_hslen = 0;
6310 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01006311
6312 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006313
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006314 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006315 ssl->out_msgtype = 0;
6316 ssl->out_msglen = 0;
6317 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
6319 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006320 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006321#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00006322
Hanno Becker19859472018-08-06 09:40:20 +01006323 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
6324
Paul Bakker48916f92012-09-16 19:57:18 +00006325 ssl->transform_in = NULL;
6326 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00006327
Hanno Becker78640902018-08-13 16:35:15 +01006328 ssl->session_in = NULL;
6329 ssl->session_out = NULL;
6330
Angus Grattond8213d02016-05-25 20:56:48 +10006331 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006332 if( partial == 0 )
Angus Grattond8213d02016-05-25 20:56:48 +10006333 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00006334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6336 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00006337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
6339 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
6342 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006343 }
Paul Bakker05ef8352012-05-08 09:17:57 +00006344 }
6345#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00006346
Paul Bakker48916f92012-09-16 19:57:18 +00006347 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00006348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 mbedtls_ssl_transform_free( ssl->transform );
6350 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006351 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00006352 }
Paul Bakker48916f92012-09-16 19:57:18 +00006353
Paul Bakkerc0463502013-02-14 11:19:38 +01006354 if( ssl->session )
6355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 mbedtls_ssl_session_free( ssl->session );
6357 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006358 ssl->session = NULL;
6359 }
6360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006362 ssl->alpn_chosen = NULL;
6363#endif
6364
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006365#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006366 if( partial == 0 )
6367 {
6368 mbedtls_free( ssl->cli_id );
6369 ssl->cli_id = NULL;
6370 ssl->cli_id_len = 0;
6371 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006372#endif
6373
Paul Bakker48916f92012-09-16 19:57:18 +00006374 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6375 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00006376
6377 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00006378}
6379
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006380/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02006381 * Reset an initialized and used SSL context for re-use while retaining
6382 * all application-set variables, function pointers and data.
6383 */
6384int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
6385{
6386 return( ssl_session_reset_int( ssl, 0 ) );
6387}
6388
6389/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006390 * SSL set accessors
6391 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006392void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00006393{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006394 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00006395}
6396
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006397void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006398{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006399 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01006400}
6401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006403void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006404{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006405 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02006406}
6407#endif
6408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006410void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006411{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006412 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006413}
6414#endif
6415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006417void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006418{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006419 conf->hs_timeout_min = min;
6420 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02006421}
6422#endif
6423
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006424void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00006425{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006426 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00006427}
6428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006430void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006431 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006432 void *p_vrfy )
6433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006434 conf->f_vrfy = f_vrfy;
6435 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00006438
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006439void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00006440 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00006441 void *p_rng )
6442{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01006443 conf->f_rng = f_rng;
6444 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00006445}
6446
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006447void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02006448 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00006449 void *p_dbg )
6450{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006451 conf->f_dbg = f_dbg;
6452 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00006453}
6454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006456 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00006457 mbedtls_ssl_send_t *f_send,
6458 mbedtls_ssl_recv_t *f_recv,
6459 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006460{
6461 ssl->p_bio = p_bio;
6462 ssl->f_send = f_send;
6463 ssl->f_recv = f_recv;
6464 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006465}
6466
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006467void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01006468{
6469 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02006470}
6471
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006472void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
6473 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00006474 mbedtls_ssl_set_timer_t *f_set_timer,
6475 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006476{
6477 ssl->p_timer = p_timer;
6478 ssl->f_set_timer = f_set_timer;
6479 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006480
6481 /* Make sure we start with no timer running */
6482 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02006483}
6484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006486void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006487 void *p_cache,
6488 int (*f_get_cache)(void *, mbedtls_ssl_session *),
6489 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006490{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01006491 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006492 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006493 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00006494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497#if defined(MBEDTLS_SSL_CLI_C)
6498int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00006499{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006500 int ret;
6501
6502 if( ssl == NULL ||
6503 session == NULL ||
6504 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006505 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006508 }
6509
6510 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
6511 return( ret );
6512
Paul Bakker0a597072012-09-25 21:55:46 +00006513 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006514
6515 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006518
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006519void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006520 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00006521{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006522 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
6523 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
6524 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
6525 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006526}
6527
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006528void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006529 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006530 int major, int minor )
6531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006533 return;
6534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02006536 return;
6537
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006538 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00006539}
6540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006542void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01006543 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006544{
6545 conf->cert_profile = profile;
6546}
6547
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006548/* Append a new keycert entry to a (possibly empty) list */
6549static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
6550 mbedtls_x509_crt *cert,
6551 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006552{
niisato8ee24222018-06-25 19:05:48 +09006553 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006554
niisato8ee24222018-06-25 19:05:48 +09006555 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
6556 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006557 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006558
niisato8ee24222018-06-25 19:05:48 +09006559 new_cert->cert = cert;
6560 new_cert->key = key;
6561 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006562
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006563 /* Update head is the list was null, else add to the end */
6564 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006565 {
niisato8ee24222018-06-25 19:05:48 +09006566 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006567 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006568 else
6569 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006570 mbedtls_ssl_key_cert *cur = *head;
6571 while( cur->next != NULL )
6572 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09006573 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006574 }
6575
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006576 return( 0 );
6577}
6578
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006579int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006580 mbedtls_x509_crt *own_cert,
6581 mbedtls_pk_context *pk_key )
6582{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006583 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006584}
6585
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006586void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006587 mbedtls_x509_crt *ca_chain,
6588 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006589{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006590 conf->ca_chain = ca_chain;
6591 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006592}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006594
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006595#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6596int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6597 mbedtls_x509_crt *own_cert,
6598 mbedtls_pk_context *pk_key )
6599{
6600 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6601 own_cert, pk_key ) );
6602}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006603
6604void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6605 mbedtls_x509_crt *ca_chain,
6606 mbedtls_x509_crl *ca_crl )
6607{
6608 ssl->handshake->sni_ca_chain = ca_chain;
6609 ssl->handshake->sni_ca_crl = ca_crl;
6610}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006611
6612void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6613 int authmode )
6614{
6615 ssl->handshake->sni_authmode = authmode;
6616}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006617#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6618
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006619#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006620/*
6621 * Set EC J-PAKE password for current handshake
6622 */
6623int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
6624 const unsigned char *pw,
6625 size_t pw_len )
6626{
6627 mbedtls_ecjpake_role role;
6628
Janos Follath8eb64132016-06-03 15:40:57 +01006629 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6631
6632 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6633 role = MBEDTLS_ECJPAKE_SERVER;
6634 else
6635 role = MBEDTLS_ECJPAKE_CLIENT;
6636
6637 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
6638 role,
6639 MBEDTLS_MD_SHA256,
6640 MBEDTLS_ECP_DP_SECP256R1,
6641 pw, pw_len ) );
6642}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006643#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02006644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006646int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006647 const unsigned char *psk, size_t psk_len,
6648 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006649{
Paul Bakker6db455e2013-09-18 17:29:31 +02006650 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006655
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006656 /* Identity len will be encoded on two bytes */
6657 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10006658 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006659 {
6660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6661 }
6662
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006663 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006664 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006665 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006666
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006667 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006668 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006669 conf->psk_len = 0;
6670 }
6671 if( conf->psk_identity != NULL )
6672 {
6673 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02006674 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006675 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006676 }
6677
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006678 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6679 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006680 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006681 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006682 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006683 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006684 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006685 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006686 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006687
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006688 conf->psk_len = psk_len;
6689 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006690
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006691 memcpy( conf->psk, psk, conf->psk_len );
6692 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006693
6694 return( 0 );
6695}
6696
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006697int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6698 const unsigned char *psk, size_t psk_len )
6699{
6700 if( psk == NULL || ssl->handshake == NULL )
6701 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6702
6703 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6705
6706 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006707 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006708 mbedtls_platform_zeroize( ssl->handshake->psk,
6709 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006710 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01006711 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01006712 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006713
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006714 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006715 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006716
6717 ssl->handshake->psk_len = psk_len;
6718 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6719
6720 return( 0 );
6721}
6722
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006723void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006725 size_t),
6726 void *p_psk )
6727{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006728 conf->f_psk = f_psk;
6729 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006730}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006732
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006733#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01006734
6735#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006736int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006737{
6738 int ret;
6739
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006740 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6741 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6742 {
6743 mbedtls_mpi_free( &conf->dhm_P );
6744 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006745 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006746 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006747
6748 return( 0 );
6749}
Hanno Becker470a8c42017-10-04 15:28:46 +01006750#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006751
Hanno Beckera90658f2017-10-04 15:29:08 +01006752int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
6753 const unsigned char *dhm_P, size_t P_len,
6754 const unsigned char *dhm_G, size_t G_len )
6755{
6756 int ret;
6757
6758 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
6759 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
6760 {
6761 mbedtls_mpi_free( &conf->dhm_P );
6762 mbedtls_mpi_free( &conf->dhm_G );
6763 return( ret );
6764 }
6765
6766 return( 0 );
6767}
Paul Bakker5121ce52009-01-03 21:22:43 +00006768
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006769int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006770{
6771 int ret;
6772
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006773 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6774 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6775 {
6776 mbedtls_mpi_free( &conf->dhm_P );
6777 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006778 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006779 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006780
6781 return( 0 );
6782}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006783#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006784
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006785#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6786/*
6787 * Set the minimum length for Diffie-Hellman parameters
6788 */
6789void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6790 unsigned int bitlen )
6791{
6792 conf->dhm_min_bitlen = bitlen;
6793}
6794#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6795
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02006796#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006797/*
6798 * Set allowed/preferred hashes for handshake signatures
6799 */
6800void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6801 const int *hashes )
6802{
6803 conf->sig_hashes = hashes;
6804}
Hanno Becker947194e2017-04-07 13:25:49 +01006805#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006806
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006807#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006808/*
6809 * Set the allowed elliptic curves
6810 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006811void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006812 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006813{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006814 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006815}
Hanno Becker947194e2017-04-07 13:25:49 +01006816#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006817
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006818#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006820{
Hanno Becker947194e2017-04-07 13:25:49 +01006821 /* Initialize to suppress unnecessary compiler warning */
6822 size_t hostname_len = 0;
6823
6824 /* Check if new hostname is valid before
6825 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01006826 if( hostname != NULL )
6827 {
6828 hostname_len = strlen( hostname );
6829
6830 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6832 }
6833
6834 /* Now it's clear that we will overwrite the old hostname,
6835 * so we can free it safely */
6836
6837 if( ssl->hostname != NULL )
6838 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006839 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01006840 mbedtls_free( ssl->hostname );
6841 }
6842
6843 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006844
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01006846 {
6847 ssl->hostname = NULL;
6848 }
6849 else
6850 {
6851 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01006852 if( ssl->hostname == NULL )
6853 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006854
Hanno Becker947194e2017-04-07 13:25:49 +01006855 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006856
Hanno Becker947194e2017-04-07 13:25:49 +01006857 ssl->hostname[hostname_len] = '\0';
6858 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006859
6860 return( 0 );
6861}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01006862#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006863
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006864#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006865void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006867 const unsigned char *, size_t),
6868 void *p_sni )
6869{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006870 conf->f_sni = f_sni;
6871 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006876int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006877{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006878 size_t cur_len, tot_len;
6879 const char **p;
6880
6881 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08006882 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6883 * MUST NOT be truncated."
6884 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006885 */
6886 tot_len = 0;
6887 for( p = protos; *p != NULL; p++ )
6888 {
6889 cur_len = strlen( *p );
6890 tot_len += cur_len;
6891
6892 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006894 }
6895
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006896 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006897
6898 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006899}
6900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006902{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006903 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006906
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006907void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006908{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006909 conf->max_major_ver = major;
6910 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006911}
6912
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006913void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006915 conf->min_major_ver = major;
6916 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006917}
6918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006920void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006921{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006922 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006923}
6924#endif
6925
Janos Follath088ce432017-04-10 12:42:31 +01006926#if defined(MBEDTLS_SSL_SRV_C)
6927void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
6928 char cert_req_ca_list )
6929{
6930 conf->cert_req_ca_list = cert_req_ca_list;
6931}
6932#endif
6933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006935void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006936{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006937 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006938}
6939#endif
6940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006942void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006943{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006944 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006945}
6946#endif
6947
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006948#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006949void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006950{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006951 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006952}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006953#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006954
Manuel Pégourié-Gonnard0b1d9b22017-09-21 13:15:27 +02006955#if defined(MBEDTLS_SSL_PROTO_DTLS)
6956void mbedtls_ssl_conf_mtu( mbedtls_ssl_config *conf, uint16_t mtu )
6957{
6958 conf->mtu = mtu;
6959}
6960#endif
6961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006963int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10006966 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006969 }
6970
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006971 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006972
6973 return( 0 );
6974}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006978void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006979{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006980 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006985void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006986{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006987 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006988}
6989#endif
6990
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006991void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006992{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006993 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006994}
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006997void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006998{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006999 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007000}
7001
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007002void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007003{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007004 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007005}
7006
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007007void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007008 const unsigned char period[8] )
7009{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007010 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01007011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007015#if defined(MBEDTLS_SSL_CLI_C)
7016void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007017{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01007018 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007019}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007020#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02007021
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007022#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007023void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
7024 mbedtls_ssl_ticket_write_t *f_ticket_write,
7025 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
7026 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02007027{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02007028 conf->f_ticket_write = f_ticket_write;
7029 conf->f_ticket_parse = f_ticket_parse;
7030 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02007031}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02007034
Robert Cragie4feb7ae2015-10-02 13:33:37 +01007035#if defined(MBEDTLS_SSL_EXPORT_KEYS)
7036void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
7037 mbedtls_ssl_export_keys_t *f_export_keys,
7038 void *p_export_keys )
7039{
7040 conf->f_export_keys = f_export_keys;
7041 conf->p_export_keys = p_export_keys;
7042}
7043#endif
7044
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007045#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007046void mbedtls_ssl_conf_async_private_cb(
7047 mbedtls_ssl_config *conf,
7048 mbedtls_ssl_async_sign_t *f_async_sign,
7049 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
7050 mbedtls_ssl_async_resume_t *f_async_resume,
7051 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007052 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007053{
7054 conf->f_async_sign_start = f_async_sign;
7055 conf->f_async_decrypt_start = f_async_decrypt;
7056 conf->f_async_resume = f_async_resume;
7057 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007058 conf->p_async_config_data = async_config_data;
7059}
7060
Gilles Peskine8f97af72018-04-26 11:46:10 +02007061void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
7062{
7063 return( conf->p_async_config_data );
7064}
7065
Gilles Peskine1febfef2018-04-30 11:54:39 +02007066void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007067{
7068 if( ssl->handshake == NULL )
7069 return( NULL );
7070 else
7071 return( ssl->handshake->user_async_ctx );
7072}
7073
Gilles Peskine1febfef2018-04-30 11:54:39 +02007074void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02007075 void *ctx )
7076{
7077 if( ssl->handshake != NULL )
7078 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007079}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02007080#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01007081
Paul Bakker5121ce52009-01-03 21:22:43 +00007082/*
7083 * SSL get accessors
7084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007086{
7087 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
7088}
7089
Hanno Becker8b170a02017-10-10 11:51:19 +01007090int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
7091{
7092 /*
7093 * Case A: We're currently holding back
7094 * a message for further processing.
7095 */
7096
7097 if( ssl->keep_current_message == 1 )
7098 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007100 return( 1 );
7101 }
7102
7103 /*
7104 * Case B: Further records are pending in the current datagram.
7105 */
7106
7107#if defined(MBEDTLS_SSL_PROTO_DTLS)
7108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7109 ssl->in_left > ssl->next_record_offset )
7110 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007112 return( 1 );
7113 }
7114#endif /* MBEDTLS_SSL_PROTO_DTLS */
7115
7116 /*
7117 * Case C: A handshake message is being processed.
7118 */
7119
Hanno Becker8b170a02017-10-10 11:51:19 +01007120 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
7121 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007123 return( 1 );
7124 }
7125
7126 /*
7127 * Case D: An application data message is being processed
7128 */
7129 if( ssl->in_offt != NULL )
7130 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01007131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01007132 return( 1 );
7133 }
7134
7135 /*
7136 * In all other cases, the rest of the message can be dropped.
7137 * As in ssl_read_record_layer, this needs to be adapted if
7138 * we implement support for multiple alerts in single records.
7139 */
7140
7141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
7142 return( 0 );
7143}
7144
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007145uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007146{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00007147 if( ssl->session != NULL )
7148 return( ssl->session->verify_result );
7149
7150 if( ssl->session_negotiate != NULL )
7151 return( ssl->session_negotiate->verify_result );
7152
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02007153 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00007154}
7155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00007157{
Paul Bakker926c8e42013-03-06 10:23:34 +01007158 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007159 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01007160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00007162}
7163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00007165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007167 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007168 {
7169 switch( ssl->minor_ver )
7170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007172 return( "DTLSv1.0" );
7173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007175 return( "DTLSv1.2" );
7176
7177 default:
7178 return( "unknown (DTLS)" );
7179 }
7180 }
7181#endif
7182
Paul Bakker43ca69c2011-01-15 17:35:19 +00007183 switch( ssl->minor_ver )
7184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007186 return( "SSLv3.0" );
7187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007189 return( "TLSv1.0" );
7190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00007192 return( "TLSv1.1" );
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00007195 return( "TLSv1.2" );
7196
Paul Bakker43ca69c2011-01-15 17:35:19 +00007197 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01007198 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00007199 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00007200}
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007203{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007204 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01007206 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007207
Hanno Becker78640902018-08-13 16:35:15 +01007208 if( transform == NULL )
7209 return( (int) mbedtls_ssl_hdr_len( ssl ) );
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211#if defined(MBEDTLS_ZLIB_SUPPORT)
7212 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
7213 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007214 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007215#endif
7216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 case MBEDTLS_MODE_GCM:
7220 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007221 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007223 transform_expansion = transform->minlen;
7224 break;
7225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01007227
7228 block_size = mbedtls_cipher_get_block_size(
7229 &transform->cipher_ctx_enc );
7230
7231#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
7232 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7233 {
7234 /* Expansion due to addition of
7235 * - MAC
7236 * - CBC padding (theoretically up to 256 bytes, but
7237 * we never use more than block_size)
7238 * - explicit IV
7239 */
7240 transform_expansion = transform->maclen + 2 * block_size;
7241 }
7242 else
7243#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
7244 {
7245 /* No explicit IV prior to TLS 1.1. */
7246 transform_expansion = transform->maclen + block_size;
7247 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007248 break;
7249
7250 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007253 }
7254
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02007255 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02007256}
7257
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007258#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7259size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
7260{
7261 size_t max_len;
7262
7263 /*
7264 * Assume mfl_code is correct since it was checked when set
7265 */
Angus Grattond8213d02016-05-25 20:56:48 +10007266 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007267
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007268 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007269 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10007270 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007271 {
Angus Grattond8213d02016-05-25 20:56:48 +10007272 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007273 }
7274
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02007275 /* During a handshake, use the value being negotiated */
7276 if( ssl->session_negotiate != NULL &&
7277 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
7278 {
7279 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
7280 }
7281
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007282 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007283}
7284#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
7285
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007286int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
7287{
7288 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
7289
7290#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
7291 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
7292
7293 if( max_len > mfl )
7294 max_len = mfl;
7295#endif
7296
7297#if defined(MBEDTLS_SSL_PROTO_DTLS)
7298 if( ssl->conf->mtu != 0 )
7299 {
7300 const size_t mtu = ssl->conf->mtu;
7301 const int ret = mbedtls_ssl_get_record_expansion( ssl );
7302 const size_t overhead = (size_t) ret;
7303
7304 if( ret < 0 )
7305 return( ret );
7306
7307 if( mtu <= overhead )
7308 {
7309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
7310 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
7311 }
7312
7313 if( max_len > mtu - overhead )
7314 max_len = mtu - overhead;
7315 }
7316#endif
7317
7318 return( (int) max_len );
7319}
7320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321#if defined(MBEDTLS_X509_CRT_PARSE_C)
7322const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00007323{
7324 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007325 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007326
Paul Bakkerd8bb8262014-06-17 14:06:49 +02007327 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00007328}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00007330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331#if defined(MBEDTLS_SSL_CLI_C)
7332int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007333{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007334 if( ssl == NULL ||
7335 dst == NULL ||
7336 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007337 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007340 }
7341
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007342 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007343}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02007345
Paul Bakker5121ce52009-01-03 21:22:43 +00007346/*
Paul Bakker1961b702013-01-25 14:49:24 +01007347 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00007348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007353 if( ssl == NULL || ssl->conf == NULL )
7354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007361 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007363#endif
7364
Paul Bakker1961b702013-01-25 14:49:24 +01007365 return( ret );
7366}
7367
7368/*
7369 * Perform the SSL handshake
7370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01007372{
7373 int ret = 0;
7374
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007375 if( ssl == NULL || ssl->conf == NULL )
7376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01007379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01007381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01007383
7384 if( ret != 0 )
7385 break;
7386 }
7387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007389
7390 return( ret );
7391}
7392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_SSL_RENEGOTIATION)
7394#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007395/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007396 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00007397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007399{
7400 int ret;
7401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007403
7404 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7406 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007407
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007408 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007409 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007411 return( ret );
7412 }
7413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007415
7416 return( 0 );
7417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007419
7420/*
7421 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 * - any side: calling mbedtls_ssl_renegotiate(),
7423 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
7424 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02007425 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007426 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007430{
7431 int ret;
7432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007434
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007435 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7436 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007437
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007438 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
7439 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007441 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007443 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007444 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02007445 ssl->handshake->out_msg_seq = 1;
7446 else
7447 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02007448 }
7449#endif
7450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
7452 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00007453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00007457 return( ret );
7458 }
7459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007461
7462 return( 0 );
7463}
7464
7465/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007466 * Renegotiate current connection on client,
7467 * or request renegotiation on server
7468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007470{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007472
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007473 if( ssl == NULL || ssl->conf == NULL )
7474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007477 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007478 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007484
7485 /* Did we already try/start sending HelloRequest? */
7486 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02007488
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007489 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007490 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007494 /*
7495 * On client, either start the renegotiation process or,
7496 * if already in progress, continue the handshake
7497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
7501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007502
7503 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
7504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007506 return( ret );
7507 }
7508 }
7509 else
7510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007514 return( ret );
7515 }
7516 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01007518
Paul Bakker37ce0ff2013-10-31 14:32:04 +01007519 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01007520}
7521
7522/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007523 * Check record counters and renegotiate if they're above the limit.
7524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007526{
Andres AG2196c7f2016-12-15 17:01:16 +00007527 size_t ep_len = ssl_ep_len( ssl );
7528 int in_ctr_cmp;
7529 int out_ctr_cmp;
7530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
7532 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007533 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007534 {
7535 return( 0 );
7536 }
7537
Andres AG2196c7f2016-12-15 17:01:16 +00007538 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
7539 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01007540 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00007541 ssl->conf->renego_period + ep_len, 8 - ep_len );
7542
7543 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007544 {
7545 return( 0 );
7546 }
7547
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02007548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007552
7553/*
7554 * Receive application data decrypted from the SSL layer
7555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007557{
Hanno Becker4a810fb2017-05-24 16:27:30 +01007558 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00007559 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00007560
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007561 if( ssl == NULL || ssl->conf == NULL )
7562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007567 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007570 return( ret );
7571
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007572 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007574 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007575 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007576 return( ret );
7577 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007578 }
7579#endif
7580
Hanno Becker4a810fb2017-05-24 16:27:30 +01007581 /*
7582 * Check if renegotiation is necessary and/or handshake is
7583 * in process. If yes, perform/continue, and fall through
7584 * if an unexpected packet is received while the client
7585 * is waiting for the ServerHello.
7586 *
7587 * (There is no equivalent to the last condition on
7588 * the server-side as it is not treated as within
7589 * a handshake while waiting for the ClientHello
7590 * after a renegotiation request.)
7591 */
7592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007594 ret = ssl_check_ctr_renegotiate( ssl );
7595 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7596 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01007599 return( ret );
7600 }
7601#endif
7602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01007606 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7607 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007610 return( ret );
7611 }
7612 }
7613
Hanno Beckere41158b2017-10-23 13:30:32 +01007614 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01007615 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007616 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007617 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007618 if( ssl->f_get_timer != NULL &&
7619 ssl->f_get_timer( ssl->p_timer ) == -1 )
7620 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007621 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02007622 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007623
Hanno Becker4a810fb2017-05-24 16:27:30 +01007624 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007625 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007626 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
7627 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00007628
Hanno Becker4a810fb2017-05-24 16:27:30 +01007629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
7630 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007631 }
7632
7633 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007635 {
7636 /*
7637 * OpenSSL sends empty messages to randomize the IV
7638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00007642 return( 0 );
7643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007645 return( ret );
7646 }
7647 }
7648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00007650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007652
Hanno Becker4a810fb2017-05-24 16:27:30 +01007653 /*
7654 * - For client-side, expect SERVER_HELLO_REQUEST.
7655 * - For server-side, expect CLIENT_HELLO.
7656 * - Fail (TLS) or silently drop record (DTLS) in other cases.
7657 */
7658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007660 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01007662 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00007663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007665
7666 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007668 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007669 {
7670 continue;
7671 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007672#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007674 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007675#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007676
Hanno Becker4a810fb2017-05-24 16:27:30 +01007677#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007678 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007682
7683 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007685 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01007686 {
7687 continue;
7688 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02007689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00007691 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01007692#endif /* MBEDTLS_SSL_SRV_C */
7693
Hanno Becker21df7f92017-10-17 11:03:26 +01007694#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01007695 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007696 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
7697 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
7698 ssl->conf->allow_legacy_renegotiation ==
7699 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
7700 {
7701 /*
7702 * Accept renegotiation request
7703 */
Paul Bakker48916f92012-09-16 19:57:18 +00007704
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01007705 /* DTLS clients need to know renego is server-initiated */
7706#if defined(MBEDTLS_SSL_PROTO_DTLS)
7707 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7708 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7709 {
7710 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
7711 }
7712#endif
7713 ret = ssl_start_renegotiation( ssl );
7714 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
7715 ret != 0 )
7716 {
7717 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
7718 return( ret );
7719 }
7720 }
7721 else
Hanno Becker21df7f92017-10-17 11:03:26 +01007722#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00007723 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01007724 /*
7725 * Refuse renegotiation
7726 */
7727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730#if defined(MBEDTLS_SSL_PROTO_SSL3)
7731 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00007732 {
Gilles Peskine92e44262017-05-10 17:27:49 +02007733 /* SSLv3 does not have a "no_renegotiation" warning, so
7734 we send a fatal alert and abort the connection. */
7735 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7736 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
7737 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007738 }
7739 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7741#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7742 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7743 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7746 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7747 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00007748 {
7749 return( ret );
7750 }
Paul Bakker48916f92012-09-16 19:57:18 +00007751 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007752 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
7754 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02007755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
7757 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02007758 }
Paul Bakker48916f92012-09-16 19:57:18 +00007759 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007760
Hanno Becker90333da2017-10-10 11:27:13 +01007761 /* At this point, we don't know whether the renegotiation has been
7762 * completed or not. The cases to consider are the following:
7763 * 1) The renegotiation is complete. In this case, no new record
7764 * has been read yet.
7765 * 2) The renegotiation is incomplete because the client received
7766 * an application data record while awaiting the ServerHello.
7767 * 3) The renegotiation is incomplete because the client received
7768 * a non-handshake, non-application data message while awaiting
7769 * the ServerHello.
7770 * In each of these case, looping will be the proper action:
7771 * - For 1), the next iteration will read a new record and check
7772 * if it's application data.
7773 * - For 2), the loop condition isn't satisfied as application data
7774 * is present, hence continue is the same as break
7775 * - For 3), the loop condition is satisfied and read_record
7776 * will re-deliver the message that was held back by the client
7777 * when expecting the ServerHello.
7778 */
7779 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00007780 }
Hanno Becker21df7f92017-10-17 11:03:26 +01007781#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007783 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007784 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007785 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007786 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007789 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007791 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02007792 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01007793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
7797 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01007800 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02007801 }
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7806 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007807 }
7808
7809 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007810
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007811 /* We're going to return something now, cancel timer,
7812 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007814 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007815
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007816#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007817 /* If we requested renego but received AppData, resend HelloRequest.
7818 * Do it now, after setting in_offt, to avoid taking this branch
7819 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007821 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007823 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007824 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007827 return( ret );
7828 }
7829 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01007831#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00007832 }
7833
7834 n = ( len < ssl->in_msglen )
7835 ? len : ssl->in_msglen;
7836
7837 memcpy( buf, ssl->in_offt, n );
7838 ssl->in_msglen -= n;
7839
7840 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01007841 {
7842 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01007844 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01007847 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007848 /* more data available */
7849 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01007850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007853
Paul Bakker23986e52011-04-24 08:57:21 +00007854 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007855}
7856
7857/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007858 * Send application data to be encrypted by the SSL layer, taking care of max
7859 * fragment length and buffer size.
7860 *
7861 * According to RFC 5246 Section 6.2.1:
7862 *
7863 * Zero-length fragments of Application data MAY be sent as they are
7864 * potentially useful as a traffic analysis countermeasure.
7865 *
7866 * Therefore, it is possible that the input message length is 0 and the
7867 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007868 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007869static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007870 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007871{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02007872 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
7873 const size_t max_len = (size_t) ret;
7874
7875 if( ret < 0 )
7876 {
7877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
7878 return( ret );
7879 }
7880
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007881 if( len > max_len )
7882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007887 "maximum fragment length: %d > %d",
7888 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007890 }
7891 else
7892#endif
7893 len = max_len;
7894 }
Paul Bakker887bd502011-06-08 13:10:54 +00007895
Paul Bakker5121ce52009-01-03 21:22:43 +00007896 if( ssl->out_left != 0 )
7897 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007898 /*
7899 * The user has previously tried to send the data and
7900 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7901 * written. In this case, we expect the high-level write function
7902 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007907 return( ret );
7908 }
7909 }
Paul Bakker887bd502011-06-08 13:10:54 +00007910 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007911 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01007912 /*
7913 * The user is trying to send a message the first time, so we need to
7914 * copy the data into the internal buffers and setup the data structure
7915 * to keep track of partial writes
7916 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007917 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007919 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007920
Hanno Becker67bc7c32018-08-06 11:33:50 +01007921 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007924 return( ret );
7925 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007926 }
7927
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007928 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007929}
7930
7931/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007932 * Write application data, doing 1/n-1 splitting if necessary.
7933 *
7934 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007935 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01007936 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007939static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007940 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007941{
7942 int ret;
7943
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007944 if( ssl->conf->cbc_record_splitting ==
7945 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007946 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7948 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7949 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007950 {
7951 return( ssl_write_real( ssl, buf, len ) );
7952 }
7953
7954 if( ssl->split_done == 0 )
7955 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007956 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007957 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007958 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007959 }
7960
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007961 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7962 return( ret );
7963 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007964
7965 return( ret + 1 );
7966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007968
7969/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007970 * Write application data (public-facing wrapper)
7971 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007972int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007973{
7974 int ret;
7975
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007977
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007978 if( ssl == NULL || ssl->conf == NULL )
7979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7980
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007981#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007982 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7983 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007984 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007985 return( ret );
7986 }
7987#endif
7988
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007989 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007990 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007991 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007992 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007994 return( ret );
7995 }
7996 }
7997
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007998#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007999 ret = ssl_write_split( ssl, buf, len );
8000#else
8001 ret = ssl_write_real( ssl, buf, len );
8002#endif
8003
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02008004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02008005
8006 return( ret );
8007}
8008
8009/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008010 * Notify the peer that the connection is being closed
8011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008013{
8014 int ret;
8015
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02008016 if( ssl == NULL || ssl->conf == NULL )
8017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008020
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008021 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00008025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
8027 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
8028 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008031 return( ret );
8032 }
8033 }
8034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008036
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02008037 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008038}
8039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00008041{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008042 if( transform == NULL )
8043 return;
8044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00008046 deflateEnd( &transform->ctx_deflate );
8047 inflateEnd( &transform->ctx_inflate );
8048#endif
8049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050 mbedtls_cipher_free( &transform->cipher_ctx_enc );
8051 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02008052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053 mbedtls_md_free( &transform->md_ctx_enc );
8054 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02008055
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008056 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008057}
8058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008059#if defined(MBEDTLS_X509_CRT_PARSE_C)
8060static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008063
8064 while( cur != NULL )
8065 {
8066 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008068 cur = next;
8069 }
8070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008072
Gilles Peskine9b562d52018-04-25 20:32:43 +02008073void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008074{
Gilles Peskine9b562d52018-04-25 20:32:43 +02008075 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
8076
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008077 if( handshake == NULL )
8078 return;
8079
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008080#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
8081 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
8082 {
Gilles Peskine8f97af72018-04-26 11:46:10 +02008083 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008084 handshake->async_in_progress = 0;
8085 }
8086#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
8087
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02008088#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8089 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8090 mbedtls_md5_free( &handshake->fin_md5 );
8091 mbedtls_sha1_free( &handshake->fin_sha1 );
8092#endif
8093#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8094#if defined(MBEDTLS_SHA256_C)
8095 mbedtls_sha256_free( &handshake->fin_sha256 );
8096#endif
8097#if defined(MBEDTLS_SHA512_C)
8098 mbedtls_sha512_free( &handshake->fin_sha512 );
8099#endif
8100#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102#if defined(MBEDTLS_DHM_C)
8103 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00008104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105#if defined(MBEDTLS_ECDH_C)
8106 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02008107#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008108#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008109 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008110#if defined(MBEDTLS_SSL_CLI_C)
8111 mbedtls_free( handshake->ecjpake_cache );
8112 handshake->ecjpake_cache = NULL;
8113 handshake->ecjpake_cache_len = 0;
8114#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008115#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02008116
Janos Follath4ae5c292016-02-10 11:27:43 +00008117#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
8118 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02008119 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02008121#endif
8122
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008123#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8124 if( handshake->psk != NULL )
8125 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008126 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008127 mbedtls_free( handshake->psk );
8128 }
8129#endif
8130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008131#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8132 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008133 /*
8134 * Free only the linked list wrapper, not the keys themselves
8135 * since the belong to the SNI callback
8136 */
8137 if( handshake->sni_key_cert != NULL )
8138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008140
8141 while( cur != NULL )
8142 {
8143 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02008145 cur = next;
8146 }
8147 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02008149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150#if defined(MBEDTLS_SSL_PROTO_DTLS)
8151 mbedtls_free( handshake->verify_cookie );
8152 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02008153 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02008154#endif
8155
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008156 mbedtls_platform_zeroize( handshake,
8157 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008158}
8159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008160void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00008161{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008162 if( session == NULL )
8163 return;
8164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00008166 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00008167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168 mbedtls_x509_crt_free( session->peer_cert );
8169 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00008170 }
Paul Bakkered27a042013-04-18 22:46:23 +02008171#endif
Paul Bakker0a597072012-09-25 21:55:46 +00008172
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008173#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02008175#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02008176
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008177 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008178}
8179
Paul Bakker5121ce52009-01-03 21:22:43 +00008180/*
8181 * Free an SSL context
8182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008184{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008185 if( ssl == NULL )
8186 return;
8187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008189
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008190 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008191 {
Angus Grattond8213d02016-05-25 20:56:48 +10008192 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008194 }
8195
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01008196 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008197 {
Angus Grattond8213d02016-05-25 20:56:48 +10008198 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00008200 }
8201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02008203 if( ssl->compress_buf != NULL )
8204 {
Angus Grattond8213d02016-05-25 20:56:48 +10008205 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02008207 }
8208#endif
8209
Paul Bakker48916f92012-09-16 19:57:18 +00008210 if( ssl->transform )
8211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 mbedtls_ssl_transform_free( ssl->transform );
8213 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008214 }
8215
8216 if( ssl->handshake )
8217 {
Gilles Peskine9b562d52018-04-25 20:32:43 +02008218 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219 mbedtls_ssl_transform_free( ssl->transform_negotiate );
8220 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 mbedtls_free( ssl->handshake );
8223 mbedtls_free( ssl->transform_negotiate );
8224 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00008225 }
8226
Paul Bakkerc0463502013-02-14 11:19:38 +01008227 if( ssl->session )
8228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008229 mbedtls_ssl_session_free( ssl->session );
8230 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008231 }
8232
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02008233#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02008234 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008235 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008236 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00008238 }
Paul Bakker0be444a2013-08-27 21:55:01 +02008239#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008241#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8242 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
8245 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00008246 }
8247#endif
8248
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008249#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008251#endif
8252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00008254
Paul Bakker86f04f42013-02-14 11:20:09 +01008255 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008256 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008257}
8258
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008259/*
8260 * Initialze mbedtls_ssl_config
8261 */
8262void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
8263{
8264 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
8265}
8266
Simon Butcherc97b6972015-12-27 23:48:17 +00008267#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008268static int ssl_preset_default_hashes[] = {
8269#if defined(MBEDTLS_SHA512_C)
8270 MBEDTLS_MD_SHA512,
8271 MBEDTLS_MD_SHA384,
8272#endif
8273#if defined(MBEDTLS_SHA256_C)
8274 MBEDTLS_MD_SHA256,
8275 MBEDTLS_MD_SHA224,
8276#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02008277#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008278 MBEDTLS_MD_SHA1,
8279#endif
8280 MBEDTLS_MD_NONE
8281};
Simon Butcherc97b6972015-12-27 23:48:17 +00008282#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008283
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008284static int ssl_preset_suiteb_ciphersuites[] = {
8285 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
8286 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
8287 0
8288};
8289
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008290#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008291static int ssl_preset_suiteb_hashes[] = {
8292 MBEDTLS_MD_SHA256,
8293 MBEDTLS_MD_SHA384,
8294 MBEDTLS_MD_NONE
8295};
8296#endif
8297
8298#if defined(MBEDTLS_ECP_C)
8299static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
8300 MBEDTLS_ECP_DP_SECP256R1,
8301 MBEDTLS_ECP_DP_SECP384R1,
8302 MBEDTLS_ECP_DP_NONE
8303};
8304#endif
8305
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008306/*
Tillmann Karras588ad502015-09-25 04:27:22 +02008307 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008308 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008309int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008310 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008311{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008312#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008313 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02008314#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008315
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02008316 /* Use the functions here so that they are covered in tests,
8317 * but otherwise access member directly for efficiency */
8318 mbedtls_ssl_conf_endpoint( conf, endpoint );
8319 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008320
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008321 /*
8322 * Things that are common to all presets
8323 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008324#if defined(MBEDTLS_SSL_CLI_C)
8325 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
8326 {
8327 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
8328#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8329 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
8330#endif
8331 }
8332#endif
8333
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008334#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008335 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008336#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008337
8338#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8339 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
8340#endif
8341
8342#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
8343 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
8344#endif
8345
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008346#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8347 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
8348#endif
8349
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008350#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008351 conf->f_cookie_write = ssl_cookie_write_dummy;
8352 conf->f_cookie_check = ssl_cookie_check_dummy;
8353#endif
8354
8355#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
8356 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
8357#endif
8358
Janos Follath088ce432017-04-10 12:42:31 +01008359#if defined(MBEDTLS_SSL_SRV_C)
8360 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
8361#endif
8362
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008363#if defined(MBEDTLS_SSL_PROTO_DTLS)
8364 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
8365 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
8366#endif
8367
8368#if defined(MBEDTLS_SSL_RENEGOTIATION)
8369 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +00008370 memset( conf->renego_period, 0x00, 2 );
8371 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008372#endif
8373
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008374#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
8375 if( endpoint == MBEDTLS_SSL_IS_SERVER )
8376 {
Hanno Becker00d0a682017-10-04 13:14:29 +01008377 const unsigned char dhm_p[] =
8378 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
8379 const unsigned char dhm_g[] =
8380 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
8381
Hanno Beckera90658f2017-10-04 15:29:08 +01008382 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
8383 dhm_p, sizeof( dhm_p ),
8384 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008385 {
8386 return( ret );
8387 }
8388 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008389#endif
8390
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008391 /*
8392 * Preset-specific defaults
8393 */
8394 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008395 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008396 /*
8397 * NSA Suite B
8398 */
8399 case MBEDTLS_SSL_PRESET_SUITEB:
8400 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
8401 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
8402 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8403 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8404
8405 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8406 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8407 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8408 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8409 ssl_preset_suiteb_ciphersuites;
8410
8411#if defined(MBEDTLS_X509_CRT_PARSE_C)
8412 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008413#endif
8414
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008415#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008416 conf->sig_hashes = ssl_preset_suiteb_hashes;
8417#endif
8418
8419#if defined(MBEDTLS_ECP_C)
8420 conf->curve_list = ssl_preset_suiteb_curves;
8421#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02008422 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008423
8424 /*
8425 * Default
8426 */
8427 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +03008428 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
8429 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
8430 MBEDTLS_SSL_MIN_MAJOR_VERSION :
8431 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
8432 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
8433 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
8434 MBEDTLS_SSL_MIN_MINOR_VERSION :
8435 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008436 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
8437 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
8438
8439#if defined(MBEDTLS_SSL_PROTO_DTLS)
8440 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8441 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
8442#endif
8443
8444 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
8445 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
8446 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
8447 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
8448 mbedtls_ssl_list_ciphersuites();
8449
8450#if defined(MBEDTLS_X509_CRT_PARSE_C)
8451 conf->cert_profile = &mbedtls_x509_crt_profile_default;
8452#endif
8453
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008454#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01008455 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02008456#endif
8457
8458#if defined(MBEDTLS_ECP_C)
8459 conf->curve_list = mbedtls_ecp_grp_id_list();
8460#endif
8461
8462#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8463 conf->dhm_min_bitlen = 1024;
8464#endif
8465 }
8466
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008467 return( 0 );
8468}
8469
8470/*
8471 * Free mbedtls_ssl_config
8472 */
8473void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
8474{
8475#if defined(MBEDTLS_DHM_C)
8476 mbedtls_mpi_free( &conf->dhm_P );
8477 mbedtls_mpi_free( &conf->dhm_G );
8478#endif
8479
8480#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
8481 if( conf->psk != NULL )
8482 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008483 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008484 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +00008485 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008486 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09008487 }
8488
8489 if( conf->psk_identity != NULL )
8490 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008491 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +09008492 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +00008493 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008494 conf->psk_identity_len = 0;
8495 }
8496#endif
8497
8498#if defined(MBEDTLS_X509_CRT_PARSE_C)
8499 ssl_key_cert_free( conf->key_cert );
8500#endif
8501
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008502 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02008503}
8504
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008505#if defined(MBEDTLS_PK_C) && \
8506 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008507/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008511{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512#if defined(MBEDTLS_RSA_C)
8513 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
8514 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008515#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516#if defined(MBEDTLS_ECDSA_C)
8517 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
8518 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02008521}
8522
Hanno Becker7e5437a2017-04-28 17:15:26 +01008523unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
8524{
8525 switch( type ) {
8526 case MBEDTLS_PK_RSA:
8527 return( MBEDTLS_SSL_SIG_RSA );
8528 case MBEDTLS_PK_ECDSA:
8529 case MBEDTLS_PK_ECKEY:
8530 return( MBEDTLS_SSL_SIG_ECDSA );
8531 default:
8532 return( MBEDTLS_SSL_SIG_ANON );
8533 }
8534}
8535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008536mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008537{
8538 switch( sig )
8539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#if defined(MBEDTLS_RSA_C)
8541 case MBEDTLS_SSL_SIG_RSA:
8542 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008543#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#if defined(MBEDTLS_ECDSA_C)
8545 case MBEDTLS_SSL_SIG_ECDSA:
8546 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008547#endif
8548 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008550 }
8551}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02008552#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008553
Hanno Becker7e5437a2017-04-28 17:15:26 +01008554#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8555 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8556
8557/* Find an entry in a signature-hash set matching a given hash algorithm. */
8558mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
8559 mbedtls_pk_type_t sig_alg )
8560{
8561 switch( sig_alg )
8562 {
8563 case MBEDTLS_PK_RSA:
8564 return( set->rsa );
8565 case MBEDTLS_PK_ECDSA:
8566 return( set->ecdsa );
8567 default:
8568 return( MBEDTLS_MD_NONE );
8569 }
8570}
8571
8572/* Add a signature-hash-pair to a signature-hash set */
8573void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
8574 mbedtls_pk_type_t sig_alg,
8575 mbedtls_md_type_t md_alg )
8576{
8577 switch( sig_alg )
8578 {
8579 case MBEDTLS_PK_RSA:
8580 if( set->rsa == MBEDTLS_MD_NONE )
8581 set->rsa = md_alg;
8582 break;
8583
8584 case MBEDTLS_PK_ECDSA:
8585 if( set->ecdsa == MBEDTLS_MD_NONE )
8586 set->ecdsa = md_alg;
8587 break;
8588
8589 default:
8590 break;
8591 }
8592}
8593
8594/* Allow exactly one hash algorithm for each signature. */
8595void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
8596 mbedtls_md_type_t md_alg )
8597{
8598 set->rsa = md_alg;
8599 set->ecdsa = md_alg;
8600}
8601
8602#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
8603 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
8604
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008605/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008606 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02008607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008609{
8610 switch( hash )
8611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612#if defined(MBEDTLS_MD5_C)
8613 case MBEDTLS_SSL_HASH_MD5:
8614 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616#if defined(MBEDTLS_SHA1_C)
8617 case MBEDTLS_SSL_HASH_SHA1:
8618 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008619#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620#if defined(MBEDTLS_SHA256_C)
8621 case MBEDTLS_SSL_HASH_SHA224:
8622 return( MBEDTLS_MD_SHA224 );
8623 case MBEDTLS_SSL_HASH_SHA256:
8624 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008625#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626#if defined(MBEDTLS_SHA512_C)
8627 case MBEDTLS_SSL_HASH_SHA384:
8628 return( MBEDTLS_MD_SHA384 );
8629 case MBEDTLS_SSL_HASH_SHA512:
8630 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008631#endif
8632 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02008634 }
8635}
8636
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008637/*
8638 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
8639 */
8640unsigned char mbedtls_ssl_hash_from_md_alg( int md )
8641{
8642 switch( md )
8643 {
8644#if defined(MBEDTLS_MD5_C)
8645 case MBEDTLS_MD_MD5:
8646 return( MBEDTLS_SSL_HASH_MD5 );
8647#endif
8648#if defined(MBEDTLS_SHA1_C)
8649 case MBEDTLS_MD_SHA1:
8650 return( MBEDTLS_SSL_HASH_SHA1 );
8651#endif
8652#if defined(MBEDTLS_SHA256_C)
8653 case MBEDTLS_MD_SHA224:
8654 return( MBEDTLS_SSL_HASH_SHA224 );
8655 case MBEDTLS_MD_SHA256:
8656 return( MBEDTLS_SSL_HASH_SHA256 );
8657#endif
8658#if defined(MBEDTLS_SHA512_C)
8659 case MBEDTLS_MD_SHA384:
8660 return( MBEDTLS_SSL_HASH_SHA384 );
8661 case MBEDTLS_MD_SHA512:
8662 return( MBEDTLS_SSL_HASH_SHA512 );
8663#endif
8664 default:
8665 return( MBEDTLS_SSL_HASH_NONE );
8666 }
8667}
8668
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008669#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008670/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008671 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008672 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008673 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008674int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008677
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008678 if( ssl->conf->curve_list == NULL )
8679 return( -1 );
8680
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008681 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008682 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008683 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008684
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02008685 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01008686}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008687#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008688
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008689#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008690/*
8691 * Check if a hash proposed by the peer is in our list.
8692 * Return 0 if we're willing to use it, -1 otherwise.
8693 */
8694int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
8695 mbedtls_md_type_t md )
8696{
8697 const int *cur;
8698
8699 if( ssl->conf->sig_hashes == NULL )
8700 return( -1 );
8701
8702 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
8703 if( *cur == (int) md )
8704 return( 0 );
8705
8706 return( -1 );
8707}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008708#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02008709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008710#if defined(MBEDTLS_X509_CRT_PARSE_C)
8711int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
8712 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008713 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008714 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008715{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008716 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008718 int usage = 0;
8719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008721 const char *ext_oid;
8722 size_t ext_len;
8723#endif
8724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
8726 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008727 ((void) cert);
8728 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008729 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008730#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
8733 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008734 {
8735 /* Server part of the key exchange */
8736 switch( ciphersuite->key_exchange )
8737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008738 case MBEDTLS_KEY_EXCHANGE_RSA:
8739 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008740 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008741 break;
8742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8744 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8745 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8746 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008747 break;
8748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8750 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008751 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008752 break;
8753
8754 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755 case MBEDTLS_KEY_EXCHANGE_NONE:
8756 case MBEDTLS_KEY_EXCHANGE_PSK:
8757 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
8758 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02008759 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008760 usage = 0;
8761 }
8762 }
8763 else
8764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008765 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
8766 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008767 }
8768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008770 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008771 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008772 ret = -1;
8773 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008774#else
8775 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
8779 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008781 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8782 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008783 }
8784 else
8785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8787 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008788 }
8789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008791 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01008792 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008793 ret = -1;
8794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008795#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02008796
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01008797 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02008798}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02008800
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008801/*
8802 * Convert version numbers to/from wire format
8803 * and, for DTLS, to/from TLS equivalent.
8804 *
8805 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08008806 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008807 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
8808 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
8809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008811 unsigned char ver[2] )
8812{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008813#if defined(MBEDTLS_SSL_PROTO_DTLS)
8814 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008817 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8818
8819 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
8820 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
8821 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008822 else
8823#else
8824 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008825#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008826 {
8827 ver[0] = (unsigned char) major;
8828 ver[1] = (unsigned char) minor;
8829 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008830}
8831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008833 const unsigned char ver[2] )
8834{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835#if defined(MBEDTLS_SSL_PROTO_DTLS)
8836 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008837 {
8838 *major = 255 - ver[0] + 2;
8839 *minor = 255 - ver[1] + 1;
8840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008841 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008842 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
8843 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008844 else
8845#else
8846 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008847#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008848 {
8849 *major = ver[0];
8850 *minor = ver[1];
8851 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008852}
8853
Simon Butcher99000142016-10-13 17:21:01 +01008854int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
8855{
8856#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8857 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
8858 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8859
8860 switch( md )
8861 {
8862#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
8863#if defined(MBEDTLS_MD5_C)
8864 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01008865 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01008866#endif
8867#if defined(MBEDTLS_SHA1_C)
8868 case MBEDTLS_SSL_HASH_SHA1:
8869 ssl->handshake->calc_verify = ssl_calc_verify_tls;
8870 break;
8871#endif
8872#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
8873#if defined(MBEDTLS_SHA512_C)
8874 case MBEDTLS_SSL_HASH_SHA384:
8875 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
8876 break;
8877#endif
8878#if defined(MBEDTLS_SHA256_C)
8879 case MBEDTLS_SSL_HASH_SHA256:
8880 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
8881 break;
8882#endif
8883 default:
8884 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8885 }
8886
8887 return 0;
8888#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
8889 (void) ssl;
8890 (void) md;
8891
8892 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
8893#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8894}
8895
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008896#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8897 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8898int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
8899 unsigned char *output,
8900 unsigned char *data, size_t data_len )
8901{
8902 int ret = 0;
8903 mbedtls_md5_context mbedtls_md5;
8904 mbedtls_sha1_context mbedtls_sha1;
8905
8906 mbedtls_md5_init( &mbedtls_md5 );
8907 mbedtls_sha1_init( &mbedtls_sha1 );
8908
8909 /*
8910 * digitally-signed struct {
8911 * opaque md5_hash[16];
8912 * opaque sha_hash[20];
8913 * };
8914 *
8915 * md5_hash
8916 * MD5(ClientHello.random + ServerHello.random
8917 * + ServerParams);
8918 * sha_hash
8919 * SHA(ClientHello.random + ServerHello.random
8920 * + ServerParams);
8921 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008922 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008923 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008925 goto exit;
8926 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008927 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008928 ssl->handshake->randbytes, 64 ) ) != 0 )
8929 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008931 goto exit;
8932 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008933 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008934 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008936 goto exit;
8937 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008938 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008939 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008941 goto exit;
8942 }
8943
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008944 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008945 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008947 goto exit;
8948 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008949 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008950 ssl->handshake->randbytes, 64 ) ) != 0 )
8951 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008953 goto exit;
8954 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008955 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008956 data_len ) ) != 0 )
8957 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008959 goto exit;
8960 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008961 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008962 output + 16 ) ) != 0 )
8963 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008965 goto exit;
8966 }
8967
8968exit:
8969 mbedtls_md5_free( &mbedtls_md5 );
8970 mbedtls_sha1_free( &mbedtls_sha1 );
8971
8972 if( ret != 0 )
8973 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8974 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
8975
8976 return( ret );
8977
8978}
8979#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
8980 MBEDTLS_SSL_PROTO_TLS1_1 */
8981
8982#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
8983 defined(MBEDTLS_SSL_PROTO_TLS1_2)
8984int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +02008985 unsigned char *hash, size_t *hashlen,
8986 unsigned char *data, size_t data_len,
8987 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008988{
8989 int ret = 0;
8990 mbedtls_md_context_t ctx;
8991 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +02008992 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01008993
8994 mbedtls_md_init( &ctx );
8995
8996 /*
8997 * digitally-signed struct {
8998 * opaque client_random[32];
8999 * opaque server_random[32];
9000 * ServerDHParams params;
9001 * };
9002 */
9003 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
9004 {
9005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
9006 goto exit;
9007 }
9008 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
9009 {
9010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
9011 goto exit;
9012 }
9013 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
9014 {
9015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9016 goto exit;
9017 }
9018 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
9019 {
9020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
9021 goto exit;
9022 }
Gilles Peskineca1d7422018-04-24 11:53:22 +02009023 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01009024 {
9025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
9026 goto exit;
9027 }
9028
9029exit:
9030 mbedtls_md_free( &ctx );
9031
9032 if( ret != 0 )
9033 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9034 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
9035
9036 return( ret );
9037}
9038#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
9039 MBEDTLS_SSL_PROTO_TLS1_2 */
9040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041#endif /* MBEDTLS_SSL_TLS_C */