blob: 56567565bd1968fe13914e716eb1cad21d2cf398 [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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/debug.h"
39#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020040#include "mbedtls/ssl_internal.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
45 defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020055#endif
56
Paul Bakker34617722014-06-13 17:20:13 +020057/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020059 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
60}
61
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020066 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010068#else
69 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070#endif
71 return( 0 );
72}
73
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074/*
75 * Start a timer.
76 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020079{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020080 if( ssl->f_set_timer == NULL )
81 return;
82
83 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
84 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020085}
86
87/*
88 * Return -1 is timer is expired, 0 if it isn't.
89 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020091{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020092 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020093 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020094
95 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020096 {
97 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100
101 return( 0 );
102}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200105/*
106 * Double the retransmit timeout value, within the allowed range,
107 * returning -1 if the maximum value has already been reached.
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200110{
111 uint32_t new_timeout;
112
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200113 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200114 return( -1 );
115
116 new_timeout = 2 * ssl->handshake->retransmit_timeout;
117
118 /* Avoid arithmetic overflow and range overflow */
119 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200120 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200121 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200122 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200123 }
124
125 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200127 ssl->handshake->retransmit_timeout ) );
128
129 return( 0 );
130}
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200133{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200134 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200136 ssl->handshake->retransmit_timeout ) );
137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200141/*
142 * Convert max_fragment_length codes to length.
143 * RFC 6066 says:
144 * enum{
145 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
146 * } MaxFragmentLength;
147 * and we add 0 -> extension unused
148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149static unsigned int mfl_code_to_length[MBEDTLS_SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 MBEDTLS_SSL_MAX_CONTENT_LEN, /* MBEDTLS_SSL_MAX_FRAG_LEN_NONE */
152 512, /* MBEDTLS_SSL_MAX_FRAG_LEN_512 */
153 1024, /* MBEDTLS_SSL_MAX_FRAG_LEN_1024 */
154 2048, /* MBEDTLS_SSL_MAX_FRAG_LEN_2048 */
155 4096, /* MBEDTLS_SSL_MAX_FRAG_LEN_4096 */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200156};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200158
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200159#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_ssl_session_free( dst );
163 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200166 if( src->peer_cert != NULL )
167 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200168 int ret;
169
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200170 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200171 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200172 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200177 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180 dst->peer_cert = NULL;
181 return( ret );
182 }
183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200186#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187 if( src->ticket != NULL )
188 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200189 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200190 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200191 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200192
193 memcpy( dst->ticket, src->ticket, src->ticket_len );
194 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200195#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200196
197 return( 0 );
198}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200199#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
202int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200203 const unsigned char *key_enc, const unsigned char *key_dec,
204 size_t keylen,
205 const unsigned char *iv_enc, const unsigned char *iv_dec,
206 size_t ivlen,
207 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200208 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
210int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
211int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
212int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
213int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
214#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000215
Paul Bakker5121ce52009-01-03 21:22:43 +0000216/*
217 * Key material generation
218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200220static int ssl3_prf( const unsigned char *secret, size_t slen,
221 const char *label,
222 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000223 unsigned char *dstbuf, size_t dlen )
224{
225 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200226 mbedtls_md5_context md5;
227 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000228 unsigned char padding[16];
229 unsigned char sha1sum[20];
230 ((void)label);
231
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200232 mbedtls_md5_init( &md5 );
233 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200234
Paul Bakker5f70b252012-09-13 14:23:06 +0000235 /*
236 * SSLv3:
237 * block =
238 * MD5( secret + SHA1( 'A' + secret + random ) ) +
239 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
240 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
241 * ...
242 */
243 for( i = 0; i < dlen / 16; i++ )
244 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200245 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000246
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200247 mbedtls_sha1_starts( &sha1 );
248 mbedtls_sha1_update( &sha1, padding, 1 + i );
249 mbedtls_sha1_update( &sha1, secret, slen );
250 mbedtls_sha1_update( &sha1, random, rlen );
251 mbedtls_sha1_finish( &sha1, sha1sum );
Paul Bakker5f70b252012-09-13 14:23:06 +0000252
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200253 mbedtls_md5_starts( &md5 );
254 mbedtls_md5_update( &md5, secret, slen );
255 mbedtls_md5_update( &md5, sha1sum, 20 );
256 mbedtls_md5_finish( &md5, dstbuf + i * 16 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000257 }
258
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200259 mbedtls_md5_free( &md5 );
260 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_zeroize( padding, sizeof( padding ) );
263 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000264
265 return( 0 );
266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200270static int tls1_prf( const unsigned char *secret, size_t slen,
271 const char *label,
272 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000273 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000274{
Paul Bakker23986e52011-04-24 08:57:21 +0000275 size_t nb, hs;
276 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200277 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 unsigned char tmp[128];
279 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 const mbedtls_md_info_t *md_info;
281 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100282 int ret;
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
289 hs = ( slen + 1 ) / 2;
290 S1 = secret;
291 S2 = secret + slen - hs;
292
293 nb = strlen( label );
294 memcpy( tmp + 20, label, nb );
295 memcpy( tmp + 20 + nb, random, rlen );
296 nb += rlen;
297
298 /*
299 * First compute P_md5(secret,label+random)[0..dlen]
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100305 return( ret );
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
308 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
309 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 for( i = 0; i < dlen; i += 16 )
312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_md_hmac_reset ( &md_ctx );
314 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
315 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_md_hmac_reset ( &md_ctx );
318 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
319 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
322
323 for( j = 0; j < k; j++ )
324 dstbuf[i + j] = h_i[j];
325 }
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100328
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 /*
330 * XOR out with P_sha1(secret,label+random)[0..dlen]
331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100336 return( ret );
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
339 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
340 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342 for( i = 0; i < dlen; i += 20 )
343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_md_hmac_reset ( &md_ctx );
345 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
346 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_md_hmac_reset ( &md_ctx );
349 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
350 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
353
354 for( j = 0; j < k; j++ )
355 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
356 }
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_zeroize( tmp, sizeof( tmp ) );
361 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
363 return( 0 );
364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
368static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100369 const unsigned char *secret, size_t slen,
370 const char *label,
371 const unsigned char *random, size_t rlen,
372 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373{
374 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100375 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000376 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
378 const mbedtls_md_info_t *md_info;
379 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100380 int ret;
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
385 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100388
389 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000391
392 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100393 memcpy( tmp + md_len, label, nb );
394 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000395 nb += rlen;
396
397 /*
398 * Compute P_<hash>(secret, label + random)[0..dlen]
399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100401 return( ret );
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
404 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
405 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100406
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100407 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_reset ( &md_ctx );
410 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
411 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_md_hmac_reset ( &md_ctx );
414 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
415 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000416
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100417 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000418
419 for( j = 0; j < k; j++ )
420 dstbuf[i + j] = h_i[j];
421 }
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_zeroize( tmp, sizeof( tmp ) );
426 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000427
428 return( 0 );
429}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen )
436{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100438 label, random, rlen, dstbuf, dlen ) );
439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 unsigned char *dstbuf, size_t dlen )
447{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100449 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#endif /* MBEDTLS_SHA512_C */
452#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
457 defined(MBEDTLS_SSL_PROTO_TLS1_1)
458static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200459#endif
Paul Bakker380da532012-04-18 16:10:25 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_SSL_PROTO_SSL3)
462static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
463static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200464#endif
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
467static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
468static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200469#endif
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
472#if defined(MBEDTLS_SHA256_C)
473static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
474static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
475static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200476#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_SHA512_C)
479static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
480static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
481static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000486{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200487 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 unsigned char keyblk[256];
490 unsigned char *key1;
491 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100492 unsigned char *mac_enc;
493 unsigned char *mac_dec;
Hanno Becker64f0aed2017-11-09 18:39:33 +0000494 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200495 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 const mbedtls_cipher_info_t *cipher_info;
497 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_ssl_session *session = ssl->session_negotiate;
500 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
501 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100506 if( cipher_info == NULL )
507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100509 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100511 }
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100514 if( md_info == NULL )
515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100517 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100519 }
520
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000522 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_SSL_PROTO_SSL3)
525 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000526 {
Paul Bakker48916f92012-09-16 19:57:18 +0000527 handshake->tls_prf = ssl3_prf;
528 handshake->calc_verify = ssl_calc_verify_ssl;
529 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000530 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200531 else
532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
534 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000535 {
Paul Bakker48916f92012-09-16 19:57:18 +0000536 handshake->tls_prf = tls1_prf;
537 handshake->calc_verify = ssl_calc_verify_tls;
538 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000539 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200540 else
541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
543#if defined(MBEDTLS_SHA512_C)
544 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
545 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000546 {
Paul Bakker48916f92012-09-16 19:57:18 +0000547 handshake->tls_prf = tls_prf_sha384;
548 handshake->calc_verify = ssl_calc_verify_tls_sha384;
549 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000550 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000551 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_SHA256_C)
554 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000555 {
Paul Bakker48916f92012-09-16 19:57:18 +0000556 handshake->tls_prf = tls_prf_sha256;
557 handshake->calc_verify = ssl_calc_verify_tls_sha256;
558 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000559 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200560 else
561#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200566 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000567
568 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 * SSLv3:
570 * master =
571 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
572 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
573 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200574 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200575 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 * master = PRF( premaster, "master secret", randbytes )[0..47]
577 */
Paul Bakker0a597072012-09-25 21:55:46 +0000578 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000581 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
584 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200585 {
586 unsigned char session_hash[48];
587 size_t hash_len;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200590
591 ssl->handshake->calc_verify( ssl, session_hash );
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
594 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200597 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200599 {
600 hash_len = 48;
601 }
602 else
603#endif
604 hash_len = 32;
605 }
606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200608 hash_len = 36;
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200611
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100612 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
613 "extended master secret",
614 session_hash, hash_len,
615 session->master, 48 );
616 if( ret != 0 )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100619 return( ret );
620 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200621
622 }
623 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200624#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100625 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
626 "master secret",
627 handshake->randbytes, 64,
628 session->master, 48 );
629 if( ret != 0 )
630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100632 return( ret );
633 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 }
637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 /*
641 * Swap the client and server random values.
642 */
Paul Bakker48916f92012-09-16 19:57:18 +0000643 memcpy( tmp, handshake->randbytes, 64 );
644 memcpy( handshake->randbytes, tmp + 32, 32 );
645 memcpy( handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647
648 /*
649 * SSLv3:
650 * key block =
651 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
652 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
653 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
654 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
655 * ...
656 *
657 * TLSv1:
658 * key block = PRF( master, "key expansion", randbytes )
659 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100660 ret = handshake->tls_prf( session->master, 48, "key expansion",
661 handshake->randbytes, 64, keyblk, 256 );
662 if( ret != 0 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100665 return( ret );
666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
669 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
670 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
671 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
672 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 /*
677 * Determine the appropriate key, IV and MAC length.
678 */
Paul Bakker68884e32013-01-07 18:20:04 +0100679
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200680 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
683 cipher_info->mode == MBEDTLS_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200685 transform->maclen = 0;
Hanno Becker64f0aed2017-11-09 18:39:33 +0000686 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200687
Paul Bakker68884e32013-01-07 18:20:04 +0100688 transform->ivlen = 12;
689 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200690
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200691 /* Minimum length is expicit IV + tag */
692 transform->minlen = transform->ivlen - transform->fixed_ivlen
693 + ( transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100695 }
696 else
697 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200698 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
700 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200703 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100704 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200706 /* Get MAC length */
Hanno Becker64f0aed2017-11-09 18:39:33 +0000707 mac_key_len = mbedtls_md_get_size( md_info );
708 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200711 /*
712 * If HMAC is to be truncated, we shall keep the leftmost bytes,
713 * (rfc 6066 page 13 or rfc 2104 section 4),
714 * so we only need to adjust the length here.
715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Becker053b3452017-11-20 16:36:41 +0000717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Becker053b3452017-11-20 16:36:41 +0000719
720#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
721 /* Fall back to old, non-compliant version of the truncated
Hanno Beckeradb30b92017-11-21 17:20:17 +0000722 * HMAC implementation which also truncates the key (pre 2.1.10) */
Hanno Becker053b3452017-11-20 16:36:41 +0000723 mac_key_len = transform->maclen;
724#endif
725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200727
728 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100729 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200731 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200733 transform->minlen = transform->maclen;
734 else
Paul Bakker68884e32013-01-07 18:20:04 +0100735 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200736 /*
737 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100738 * 1. if EtM is in use: one block plus MAC
739 * otherwise: * first multiple of blocklen greater than maclen
740 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
743 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100744 {
745 transform->minlen = transform->maclen
746 + cipher_info->block_size;
747 }
748 else
749#endif
750 {
751 transform->minlen = transform->maclen
752 + cipher_info->block_size
753 - transform->maclen % cipher_info->block_size;
754 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
757 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
758 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200759 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100760 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
763 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
764 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200765 {
766 transform->minlen += transform->ivlen;
767 }
768 else
769#endif
770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200773 }
Paul Bakker68884e32013-01-07 18:20:04 +0100774 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 }
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000778 transform->keylen, transform->minlen, transform->ivlen,
779 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
781 /*
782 * Finally setup the cipher contexts, IVs and MAC secrets.
783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200785 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 {
Hanno Becker64f0aed2017-11-09 18:39:33 +0000787 key1 = keyblk + mac_key_len * 2;
788 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000789
Paul Bakker68884e32013-01-07 18:20:04 +0100790 mac_enc = keyblk;
Hanno Becker64f0aed2017-11-09 18:39:33 +0000791 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000793 /*
794 * This is not used in TLS v1.1.
795 */
Paul Bakker48916f92012-09-16 19:57:18 +0000796 iv_copy_len = ( transform->fixed_ivlen ) ?
797 transform->fixed_ivlen : transform->ivlen;
798 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
799 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000800 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 }
802 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_SSL_CLI_C */
804#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200805 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 {
Hanno Becker64f0aed2017-11-09 18:39:33 +0000807 key1 = keyblk + mac_key_len * 2 + transform->keylen;
808 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
Hanno Becker64f0aed2017-11-09 18:39:33 +0000810 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100811 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000812
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000813 /*
814 * This is not used in TLS v1.1.
815 */
Paul Bakker48916f92012-09-16 19:57:18 +0000816 iv_copy_len = ( transform->fixed_ivlen ) ?
817 transform->fixed_ivlen : transform->ivlen;
818 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
819 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000820 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100827 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_SSL_PROTO_SSL3)
830 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100831 {
Hanno Becker64f0aed2017-11-09 18:39:33 +0000832 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100836 }
837
Hanno Becker64f0aed2017-11-09 18:39:33 +0000838 memcpy( transform->mac_enc, mac_enc, mac_key_len );
839 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100840 }
841 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#endif /* MBEDTLS_SSL_PROTO_SSL3 */
843#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
844 defined(MBEDTLS_SSL_PROTO_TLS1_2)
845 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100846 {
Gilles Peskine823734b2018-03-19 19:06:08 +0100847 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
848 For AEAD-based ciphersuites, there is nothing to do here. */
849 if( mac_key_len != 0 )
850 {
851 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
852 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
853 }
Paul Bakker68884e32013-01-07 18:20:04 +0100854 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855 else
856#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200860 }
Paul Bakker68884e32013-01-07 18:20:04 +0100861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
863 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000864 {
865 int ret = 0;
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100870 transform->iv_enc, transform->iv_dec,
871 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100872 mac_enc, mac_dec,
Hanno Becker64f0aed2017-11-09 18:39:33 +0000873 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
876 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000877 }
878 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000880
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200881 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200882 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200885 return( ret );
886 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200887
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200888 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200889 cipher_info ) ) != 0 )
890 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200892 return( ret );
893 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200896 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200900 return( ret );
901 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200904 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200908 return( ret );
909 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_CIPHER_MODE_CBC)
912 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
915 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200918 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200919 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
922 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200925 return( ret );
926 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +0000933 // Initialize compression
934 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000936 {
Paul Bakker16770332013-10-11 09:59:44 +0200937 if( ssl->compress_buf == NULL )
938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200940 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +0200941 if( ssl->compress_buf == NULL )
942 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_SSL_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200945 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +0200946 }
947 }
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000950
Paul Bakker48916f92012-09-16 19:57:18 +0000951 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
952 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000953
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200954 if( deflateInit( &transform->ctx_deflate,
955 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000956 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
959 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000960 }
961 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +0000963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
966 return( 0 );
967}
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_SSL_PROTO_SSL3)
970void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000971{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200972 mbedtls_md5_context md5;
973 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 unsigned char pad_1[48];
975 unsigned char pad_2[48];
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +0200979 mbedtls_md5_init( &md5 );
980 mbedtls_sha1_init( &sha1 );
981
982 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
983 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Paul Bakker380da532012-04-18 16:10:25 +0000985 memset( pad_1, 0x36, 48 );
986 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200988 mbedtls_md5_update( &md5, ssl->session_negotiate->master, 48 );
989 mbedtls_md5_update( &md5, pad_1, 48 );
990 mbedtls_md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200992 mbedtls_md5_starts( &md5 );
993 mbedtls_md5_update( &md5, ssl->session_negotiate->master, 48 );
994 mbedtls_md5_update( &md5, pad_2, 48 );
995 mbedtls_md5_update( &md5, hash, 16 );
996 mbedtls_md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200998 mbedtls_sha1_update( &sha1, ssl->session_negotiate->master, 48 );
999 mbedtls_sha1_update( &sha1, pad_1, 40 );
1000 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001001
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001002 mbedtls_sha1_starts( &sha1 );
1003 mbedtls_sha1_update( &sha1, ssl->session_negotiate->master, 48 );
1004 mbedtls_sha1_update( &sha1, pad_2, 40 );
1005 mbedtls_sha1_update( &sha1, hash + 16, 20 );
1006 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001010
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001011 mbedtls_md5_free( &md5 );
1012 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001013
Paul Bakker380da532012-04-18 16:10:25 +00001014 return;
1015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1019void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001020{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001021 mbedtls_md5_context md5;
1022 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001025
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001026 mbedtls_md5_init( &md5 );
1027 mbedtls_sha1_init( &sha1 );
1028
1029 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1030 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001031
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001032 mbedtls_md5_finish( &md5, hash );
1033 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001037
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001038 mbedtls_md5_free( &md5 );
1039 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001040
Paul Bakker380da532012-04-18 16:10:25 +00001041 return;
1042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1046#if defined(MBEDTLS_SHA256_C)
1047void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001048{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001049 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001050
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001051 mbedtls_sha256_init( &sha256 );
1052
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001054
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001055 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001056 mbedtls_sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001060
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001061 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001062
Paul Bakker380da532012-04-18 16:10:25 +00001063 return;
1064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#if defined(MBEDTLS_SHA512_C)
1068void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001069{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001070 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001071
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001072 mbedtls_sha512_init( &sha512 );
1073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001075
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001076 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1077 mbedtls_sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001082 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001083
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 return;
1085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_SHA512_C */
1087#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1090int 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 +02001091{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001092 unsigned char *p = ssl->handshake->premaster;
1093 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001094 const unsigned char *psk = ssl->conf->psk;
1095 size_t psk_len = ssl->conf->psk_len;
1096
1097 /* If the psk callback was called, use its result */
1098 if( ssl->handshake->psk != NULL )
1099 {
1100 psk = ssl->handshake->psk;
1101 psk_len = ssl->handshake->psk_len;
1102 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001103
1104 /*
1105 * PMS = struct {
1106 * opaque other_secret<0..2^16-1>;
1107 * opaque psk<0..2^16-1>;
1108 * };
1109 * with "other_secret" depending on the particular key exchange
1110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1112 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001113 {
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001114 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001116
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001117 *(p++) = (unsigned char)( psk_len >> 8 );
1118 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001119
1120 if( end < p || (size_t)( end - p ) < psk_len )
1121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1122
1123 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001124 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001125 }
1126 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1128#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1129 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001130 {
1131 /*
1132 * other_secret already set by the ClientKeyExchange message,
1133 * and is 48 bytes long
1134 */
Philippe Antoinebbc79182018-05-30 09:13:21 +02001135 if( end - p < 2 )
1136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1137
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001138 *p++ = 0;
1139 *p++ = 48;
1140 p += 48;
1141 }
1142 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1144#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1145 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001146 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001147 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001148 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001149
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001150 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001152 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001153 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001156 return( ret );
1157 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001158 *(p++) = (unsigned char)( len >> 8 );
1159 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001160 p += len;
1161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001163 }
1164 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1166#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1167 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001168 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001169 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001170 size_t zlen;
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001173 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001174 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001177 return( ret );
1178 }
1179
1180 *(p++) = (unsigned char)( zlen >> 8 );
1181 *(p++) = (unsigned char)( zlen );
1182 p += zlen;
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001185 }
1186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1190 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001191 }
1192
1193 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001194 if( end - p < 2 )
1195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001196
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001197 *(p++) = (unsigned char)( psk_len >> 8 );
1198 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001199
1200 if( end < p || (size_t)( end - p ) < psk_len )
1201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1202
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001203 memcpy( p, psk, psk_len );
1204 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001205
1206 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1207
1208 return( 0 );
1209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001213/*
1214 * SSLv3.0 MAC functions
1215 */
Manuel Pégourié-Gonnard4b133e62017-12-19 10:03:46 +01001216#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001217static void ssl_mac( mbedtls_md_context_t *md_ctx,
1218 const unsigned char *secret,
1219 const unsigned char *buf, size_t len,
1220 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnard4b133e62017-12-19 10:03:46 +01001221 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001222{
1223 unsigned char header[11];
1224 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001225 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1227 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001228
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001229 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001231 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001232 else
Paul Bakker68884e32013-01-07 18:20:04 +01001233 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
1235 memcpy( header, ctr, 8 );
1236 header[ 8] = (unsigned char) type;
1237 header[ 9] = (unsigned char)( len >> 8 );
1238 header[10] = (unsigned char)( len );
1239
Paul Bakker68884e32013-01-07 18:20:04 +01001240 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_md_starts( md_ctx );
1242 mbedtls_md_update( md_ctx, secret, md_size );
1243 mbedtls_md_update( md_ctx, padding, padlen );
1244 mbedtls_md_update( md_ctx, header, 11 );
1245 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001246 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Paul Bakker68884e32013-01-07 18:20:04 +01001248 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_md_starts( md_ctx );
1250 mbedtls_md_update( md_ctx, secret, md_size );
1251 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001252 mbedtls_md_update( md_ctx, out, md_size );
1253 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1258 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1259 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001260#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001261#endif
1262
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02001263/* The function below is only used in the Lucky 13 counter-measure in
1264 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1265#if defined(SSL_SOME_MODES_USE_MAC) && \
1266 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1267 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1268 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1269/* This function makes sure every byte in the memory region is accessed
1270 * (in ascending addresses order) */
1271static void ssl_read_memory( unsigned char *p, size_t len )
1272{
1273 unsigned char acc = 0;
1274 volatile unsigned char force;
1275
1276 for( ; len != 0; p++, len-- )
1277 acc ^= *p;
1278
1279 force = acc;
1280 (void) force;
1281}
1282#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1283
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001284/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001290 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001294 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001298 }
1299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001303 ssl->out_msg, ssl->out_msglen );
1304
Hanno Beckeraede1832017-09-18 10:55:31 +01001305 if( ssl->out_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
1306 {
Hanno Becker6e052b02017-10-04 13:47:33 +01001307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1308 (unsigned) ssl->out_msglen,
Hanno Beckeraede1832017-09-18 10:55:31 +01001309 MBEDTLS_SSL_MAX_CONTENT_LEN ) );
1310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1311 }
1312
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001314 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001316#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 if( mode == MBEDTLS_MODE_STREAM ||
1318 ( mode == MBEDTLS_MODE_CBC
1319#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1320 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001321#endif
1322 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_SSL_PROTO_SSL3)
1325 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001326 {
Manuel Pégourié-Gonnard4b133e62017-12-19 10:03:46 +01001327 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001328
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001329 ssl_mac( &ssl->transform_out->md_ctx_enc,
1330 ssl->transform_out->mac_enc,
1331 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001332 ssl->out_ctr, ssl->out_msgtype,
1333 mac );
1334
1335 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001336 }
1337 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1340 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1341 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001342 {
Hanno Beckerce516ff2017-11-09 18:57:39 +00001343 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1346 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1347 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1348 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001349 ssl->out_msg, ssl->out_msglen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001350 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001352
1353 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001354 }
1355 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001356#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001360 }
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001363 ssl->out_msg + ssl->out_msglen,
1364 ssl->transform_out->maclen );
1365
1366 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001367 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001368 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001369#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001371 /*
1372 * Encrypt
1373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1375 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001377 int ret;
1378 size_t olen = 0;
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 "including %d bytes of padding",
1382 ssl->out_msglen, 0 ) );
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001385 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001386 ssl->transform_out->ivlen,
1387 ssl->out_msg, ssl->out_msglen,
1388 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001391 return( ret );
1392 }
1393
1394 if( ssl->out_msglen != olen )
1395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
Paul Bakker68884e32013-01-07 18:20:04 +01001400 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1402#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1403 if( mode == MBEDTLS_MODE_GCM ||
1404 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001405 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001406 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001407 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001408 unsigned char *enc_msg;
1409 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001410 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001412
Paul Bakkerca4ab492012-04-18 14:23:57 +00001413 memcpy( add_data, ssl->out_ctr, 8 );
1414 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001416 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001417 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1418 add_data[12] = ssl->out_msglen & 0xFF;
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakkerca4ab492012-04-18 14:23:57 +00001421 add_data, 13 );
1422
Paul Bakker68884e32013-01-07 18:20:04 +01001423 /*
1424 * Generate IV
1425 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001426 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1427 {
1428 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1430 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001431 }
1432
1433 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1434 ssl->out_ctr, 8 );
1435 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001438 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001439
Paul Bakker68884e32013-01-07 18:20:04 +01001440 /*
1441 * Fix pointer positions and message length with added IV
1442 */
1443 enc_msg = ssl->out_msg;
1444 enc_msglen = ssl->out_msglen;
1445 ssl->out_msglen += ssl->transform_out->ivlen -
1446 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker68884e32013-01-07 18:20:04 +01001449 "including %d bytes of padding",
1450 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001451
Paul Bakker68884e32013-01-07 18:20:04 +01001452 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001453 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001456 ssl->transform_out->iv_enc,
1457 ssl->transform_out->ivlen,
1458 add_data, 13,
1459 enc_msg, enc_msglen,
1460 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001461 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001464 return( ret );
1465 }
1466
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001467 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001471 }
1472
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001473 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001474 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1480#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1481 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1482 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001484 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001485 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001486 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001487
Paul Bakker48916f92012-09-16 19:57:18 +00001488 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1489 ssl->transform_out->ivlen;
1490 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 padlen = 0;
1492
1493 for( i = 0; i <= padlen; i++ )
1494 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1495
1496 ssl->out_msglen += padlen + 1;
1497
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001498 enc_msglen = ssl->out_msglen;
1499 enc_msg = ssl->out_msg;
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001502 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001503 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1504 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001507 {
1508 /*
1509 * Generate IV
1510 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001511 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001512 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001513 if( ret != 0 )
1514 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001515
Paul Bakker92be97b2013-01-02 17:30:03 +01001516 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001517 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001518
1519 /*
1520 * Fix pointer positions and message length with added IV
1521 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001522 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001523 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001524 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001529 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001530 ssl->out_msglen, ssl->transform_out->ivlen,
1531 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001534 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001535 ssl->transform_out->ivlen,
1536 enc_msg, enc_msglen,
1537 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001540 return( ret );
1541 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001542
Paul Bakkercca5b812013-08-31 17:40:26 +02001543 if( enc_msglen != olen )
1544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001547 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1550 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001551 {
1552 /*
1553 * Save IV in SSL3 and TLS1
1554 */
1555 memcpy( ssl->transform_out->iv_enc,
1556 ssl->transform_out->cipher_ctx_enc.iv,
1557 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001559#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001562 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001563 {
Hanno Becker394767c2018-01-05 16:24:22 +00001564 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1565
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001566 /*
1567 * MAC(MAC_write_key, seq_num +
1568 * TLSCipherText.type +
1569 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001570 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001571 * IV + // except for TLS 1.0
1572 * ENC(content + padding + padding_length));
1573 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001574 unsigned char pseudo_hdr[13];
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001577
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001578 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1579 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001580 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1581 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1586 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001587 ssl->out_iv, ssl->out_msglen );
Hanno Becker394767c2018-01-05 16:24:22 +00001588 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001590
Hanno Becker394767c2018-01-05 16:24:22 +00001591 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1592 ssl->transform_out->maclen );
1593
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001594 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001595 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001596 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001599 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1601 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001607 /* Make extra sure authentication was performed, exactly once */
1608 if( auth_done != 1 )
1609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001612 }
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
1616 return( 0 );
1617}
1618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001620{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001621 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001623 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001624#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001625 size_t padlen = 0, correct = 1;
1626#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001629
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001630 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001634 }
1635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001637
Paul Bakker48916f92012-09-16 19:57:18 +00001638 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001641 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 }
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1646 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001647 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001648 int ret;
1649 size_t olen = 0;
1650
Paul Bakker68884e32013-01-07 18:20:04 +01001651 padlen = 0;
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001654 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001655 ssl->transform_in->ivlen,
1656 ssl->in_msg, ssl->in_msglen,
1657 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001660 return( ret );
1661 }
1662
1663 if( ssl->in_msglen != olen )
1664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1666 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 }
Paul Bakker68884e32013-01-07 18:20:04 +01001669 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1671#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1672 if( mode == MBEDTLS_MODE_GCM ||
1673 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001674 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001675 int ret;
1676 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001677 unsigned char *dec_msg;
1678 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001679 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001680 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001682 size_t explicit_iv_len = ssl->transform_in->ivlen -
1683 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001684
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001685 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001688 "+ taglen (%d)", ssl->in_msglen,
1689 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001691 }
1692 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1693
Paul Bakker68884e32013-01-07 18:20:04 +01001694 dec_msg = ssl->in_msg;
1695 dec_msg_result = ssl->in_msg;
1696 ssl->in_msglen = dec_msglen;
1697
1698 memcpy( add_data, ssl->in_ctr, 8 );
1699 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001701 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001702 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1703 add_data[12] = ssl->in_msglen & 0xFF;
1704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001706 add_data, 13 );
1707
1708 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1709 ssl->in_iv,
1710 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001713 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001715
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001716 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001717 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001720 ssl->transform_in->iv_dec,
1721 ssl->transform_in->ivlen,
1722 add_data, 13,
1723 dec_msg, dec_msglen,
1724 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001725 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1730 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001731
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001732 return( ret );
1733 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001734 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001735
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001736 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1739 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001740 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1744#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1745 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1746 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 {
Paul Bakker45829992013-01-03 14:52:21 +01001748 /*
1749 * Decrypt and check the padding
1750 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001751 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001752 unsigned char *dec_msg;
1753 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001754 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001755 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001756 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001757
Paul Bakker5121ce52009-01-03 21:22:43 +00001758 /*
Paul Bakker45829992013-01-03 14:52:21 +01001759 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1762 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001763 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001764#endif
Paul Bakker45829992013-01-03 14:52:21 +01001765
1766 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1767 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001770 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1771 ssl->transform_in->ivlen,
1772 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001774 }
1775
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001776 dec_msglen = ssl->in_msglen;
1777 dec_msg = ssl->in_msg;
1778 dec_msg_result = ssl->in_msg;
1779
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001780 /*
1781 * Authenticate before decrypt if enabled
1782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1784 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001785 {
Hanno Beckerce516ff2017-11-09 18:57:39 +00001786 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001787 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001790
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001791 dec_msglen -= ssl->transform_in->maclen;
1792 ssl->in_msglen -= ssl->transform_in->maclen;
1793
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001794 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1795 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1796 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1797 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1802 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001803 ssl->in_iv, ssl->in_msglen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001804 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001808 ssl->transform_in->maclen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001809 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001810 ssl->transform_in->maclen );
1811
Hanno Beckerce516ff2017-11-09 18:57:39 +00001812 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1813 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001818 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001819 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001822
1823 /*
1824 * Check length sanity
1825 */
1826 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001829 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001831 }
1832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001834 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001835 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001838 {
Paul Bakker48916f92012-09-16 19:57:18 +00001839 dec_msglen -= ssl->transform_in->ivlen;
1840 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001841
Paul Bakker48916f92012-09-16 19:57:18 +00001842 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001843 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001844 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001848 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001849 ssl->transform_in->ivlen,
1850 dec_msg, dec_msglen,
1851 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001854 return( ret );
1855 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001856
Paul Bakkercca5b812013-08-31 17:40:26 +02001857 if( dec_msglen != olen )
1858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1860 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001861 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1864 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001865 {
1866 /*
1867 * Save IV in SSL3 and TLS1
1868 */
1869 memcpy( ssl->transform_in->iv_dec,
1870 ssl->transform_in->cipher_ctx_dec.iv,
1871 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001873#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
1875 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001876
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001877 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001878 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880#if defined(MBEDTLS_SSL_DEBUG_ALL)
1881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01001882 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001883#endif
Paul Bakker45829992013-01-03 14:52:21 +01001884 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001885 correct = 0;
1886 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#if defined(MBEDTLS_SSL_PROTO_SSL3)
1889 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 {
Paul Bakker48916f92012-09-16 19:57:18 +00001891 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893#if defined(MBEDTLS_SSL_DEBUG_ALL)
1894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001896 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001897#endif
Paul Bakker45829992013-01-03 14:52:21 +01001898 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 }
1900 }
1901 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1903#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1904 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1905 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 {
1907 /*
Paul Bakker45829992013-01-03 14:52:21 +01001908 * TLSv1+: always check the padding up to the first failure
1909 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001911 size_t pad_count = 0, real_count = 1;
Angus Gratton1226dd72018-06-19 15:57:50 +10001912 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001913
Paul Bakker956c9e02013-12-19 14:42:28 +01001914 /*
1915 * Padding is guaranteed to be incorrect if:
Angus Gratton1226dd72018-06-19 15:57:50 +10001916 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001917 *
Angus Gratton1226dd72018-06-19 15:57:50 +10001918 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001919 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001920 *
1921 * In both cases we reset padding_idx to a safe value (0) to
1922 * prevent out-of-buffer reads.
1923 */
Angus Gratton1226dd72018-06-19 15:57:50 +10001924 correct &= ( padlen <= ssl->in_msglen );
1925 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001926 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001927
1928 padding_idx *= correct;
1929
Angus Gratton1226dd72018-06-19 15:57:50 +10001930 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001931 {
Angus Gratton1226dd72018-06-19 15:57:50 +10001932 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001933 pad_count += real_count *
1934 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1935 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001936
1937 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001940 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001942#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001943 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001945 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1947 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001951 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001952
1953 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001955 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1957 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Manuel Pégourié-Gonnard671f9322018-07-10 11:15:36 +02001963#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard671f9322018-07-10 11:15:36 +02001966#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
1968 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001969 * Authenticate if not done yet.
1970 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001972#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001973 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001974 {
Hanno Beckerce516ff2017-11-09 18:57:39 +00001975 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001976
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001979 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1980 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_SSL_PROTO_SSL3)
1983 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001984 {
1985 ssl_mac( &ssl->transform_in->md_ctx_dec,
1986 ssl->transform_in->mac_dec,
1987 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001988 ssl->in_ctr, ssl->in_msgtype,
1989 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001990 }
1991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1993#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1994 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1995 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001996 {
1997 /*
1998 * Process MAC and always update for padlen afterwards to make
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02001999 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002000 *
2001 * Known timing attacks:
2002 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2003 *
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02002004 * To compensate for different timings for the MAC calculation
2005 * depending on how much padding was removed (which is determined
2006 * by padlen), process extra_run more blocks through the hash
2007 * function.
2008 *
2009 * The formula in the paper is
2010 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2011 * where L1 is the size of the header plus the decrypted message
2012 * plus CBC padding and L2 is the size of the header plus the
2013 * decrypted message. This is for an underlying hash function
2014 * with 64-byte blocks.
2015 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2016 * correctly. We round down instead of up, so -56 is the correct
2017 * value for our calculations instead of -55.
2018 *
2019 * Repeat the formula rather than defining a block_size variable.
2020 * This avoids requiring division by a variable at runtime
2021 * (which would be marginally less efficient and would require
2022 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002023 */
2024 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002025
2026 /*
2027 * The next two sizes are the minimum and maximum values of
2028 * in_msglen over all padlen values.
2029 *
2030 * They're independent of padlen, since we previously did
2031 * in_msglen -= padlen.
2032 *
2033 * Note that max_len + maclen is never more than the buffer
2034 * length, as we previously did in_msglen -= maclen too.
2035 */
2036 const size_t max_len = ssl->in_msglen + padlen;
2037 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2038
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02002039 switch( ssl->transform_in->ciphersuite_info->mac )
2040 {
2041#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2042 defined(MBEDTLS_SHA256_C)
2043 case MBEDTLS_MD_MD5:
2044 case MBEDTLS_MD_SHA1:
2045 case MBEDTLS_MD_SHA256:
2046 /* 8 bytes of message size, 64-byte compression blocks */
2047 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2048 ( 13 + ssl->in_msglen + 8 ) / 64;
2049 break;
2050#endif
2051#if defined(MBEDTLS_SHA512_C)
2052 case MBEDTLS_MD_SHA384:
2053 /* 16 bytes of message size, 128-byte compression blocks */
2054 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2055 ( 13 + ssl->in_msglen + 16 ) / 128;
2056 break;
2057#endif
2058 default:
2059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2061 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002062
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002063 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2066 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2067 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2068 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002069 ssl->in_msglen );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002070 /* Make sure we access everything even when padlen > 0. This
2071 * makes the synchronisation requirements for just-in-time
2072 * Prime+Probe attacks much tighter and hopefully impractical. */
2073 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00002074 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002075
2076 /* Call mbedtls_md_process at least once due to cache attacks
2077 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002078 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002082
2083 /* Make sure we access all the memory that could contain the MAC,
2084 * before we check it in the next code block. This makes the
2085 * synchronisation requirements for just-in-time Prime+Probe
2086 * attacks much tighter and hopefully impractical. */
2087 ssl_read_memory( ssl->in_msg + min_len,
2088 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002089 }
2090 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2092 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002098#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Beckerce516ff2017-11-09 18:57:39 +00002099 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2100 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2101 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
Hanno Beckerce516ff2017-11-09 18:57:39 +00002104 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2105 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#if defined(MBEDTLS_SSL_DEBUG_ALL)
2108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002109#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002110 correct = 0;
2111 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002112 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002113 }
Hanno Becker3aab4cc2018-10-17 14:43:14 +01002114
2115 /*
2116 * Finally check the correct flag
2117 */
2118 if( correct == 0 )
2119 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002120#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121
2122 /* Make extra sure authentication was performed, exactly once */
2123 if( auth_done != 1 )
2124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2126 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002127 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
2129 if( ssl->in_msglen == 0 )
2130 {
Angus Gratton485b3932018-06-19 15:58:22 +10002131#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2132 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2133 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2134 {
2135 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2138 }
2139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2140
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 ssl->nb_zero++;
2142
2143 /*
2144 * Three or more empty messages may be a DoS attack
2145 * (excessive CPU consumption).
2146 */
2147 if( ssl->nb_zero > 3 )
2148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
2153 }
2154 else
2155 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002159 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002160 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002161 }
2162 else
2163#endif
2164 {
2165 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2166 if( ++ssl->in_ctr[i - 1] != 0 )
2167 break;
2168
2169 /* The loop goes to its end iff the counter is wrapping */
2170 if( i == ssl_ep_len( ssl ) )
2171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2173 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002174 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002175 }
2176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
2179 return( 0 );
2180}
2181
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002182#undef MAC_NONE
2183#undef MAC_PLAINTEXT
2184#undef MAC_CIPHERTEXT
2185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002187/*
2188 * Compression/decompression functions
2189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002191{
2192 int ret;
2193 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002194 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002195 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002196 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002199
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002200 if( len_pre == 0 )
2201 return( 0 );
2202
Paul Bakker2770fbd2012-07-03 13:30:23 +00002203 memcpy( msg_pre, ssl->out_msg, len_pre );
2204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002206 ssl->out_msglen ) );
2207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002209 ssl->out_msg, ssl->out_msglen );
2210
Paul Bakker48916f92012-09-16 19:57:18 +00002211 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2212 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2213 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002214 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002215
Paul Bakker48916f92012-09-16 19:57:18 +00002216 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002217 if( ret != Z_OK )
2218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2220 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002221 }
2222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekbb666142018-04-23 08:29:36 -04002224 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002227 ssl->out_msglen ) );
2228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002230 ssl->out_msg, ssl->out_msglen );
2231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002233
2234 return( 0 );
2235}
2236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002238{
2239 int ret;
2240 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek078014a2018-04-24 06:32:44 -04002241 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002242 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002243 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002246
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002247 if( len_pre == 0 )
2248 return( 0 );
2249
Paul Bakker2770fbd2012-07-03 13:30:23 +00002250 memcpy( msg_pre, ssl->in_msg, len_pre );
2251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002253 ssl->in_msglen ) );
2254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002256 ssl->in_msg, ssl->in_msglen );
2257
Paul Bakker48916f92012-09-16 19:57:18 +00002258 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2259 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2260 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002261 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek078014a2018-04-24 06:32:44 -04002262 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002263
Paul Bakker48916f92012-09-16 19:57:18 +00002264 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002265 if( ret != Z_OK )
2266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2268 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002269 }
2270
Andrzej Kurekbb666142018-04-23 08:29:36 -04002271 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek078014a2018-04-24 06:32:44 -04002272 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002275 ssl->in_msglen ) );
2276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002278 ssl->in_msg, ssl->in_msglen );
2279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002281
2282 return( 0 );
2283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2287static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#if defined(MBEDTLS_SSL_PROTO_DTLS)
2290static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002291{
2292 /* If renegotiation is not enforced, retransmit until we would reach max
2293 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002294 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002295 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002296 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002297 unsigned char doublings = 1;
2298
2299 while( ratio != 0 )
2300 {
2301 ++doublings;
2302 ratio >>= 1;
2303 }
2304
2305 if( ++ssl->renego_records_seen > doublings )
2306 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002308 return( 0 );
2309 }
2310 }
2311
2312 return( ssl_write_hello_request( ssl ) );
2313}
2314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002316
Paul Bakker5121ce52009-01-03 21:22:43 +00002317/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002318 * Fill the input message buffer by appending data to it.
2319 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002320 *
2321 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2322 * available (from this read and/or a previous one). Otherwise, an error code
2323 * is returned (possibly EOF or WANT_READ).
2324 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002325 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2326 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2327 * since we always read a whole datagram at once.
2328 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002329 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002330 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002333{
Paul Bakker23986e52011-04-24 08:57:21 +00002334 int ret;
2335 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002339 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002342 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002344 }
2345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002350 }
2351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002355 uint32_t timeout;
2356
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002357 /* Just to be sure */
2358 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2359 {
2360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2361 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2363 }
2364
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002365 /*
2366 * The point is, we need to always read a full datagram at once, so we
2367 * sometimes read more then requested, and handle the additional data.
2368 * It could be the rest of the current record (while fetching the
2369 * header) and/or some other records in the same datagram.
2370 */
2371
2372 /*
2373 * Move to the next record in the already read datagram if applicable
2374 */
2375 if( ssl->next_record_offset != 0 )
2376 {
2377 if( ssl->in_left < ssl->next_record_offset )
2378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002381 }
2382
2383 ssl->in_left -= ssl->next_record_offset;
2384
2385 if( ssl->in_left != 0 )
2386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002388 ssl->next_record_offset ) );
2389 memmove( ssl->in_hdr,
2390 ssl->in_hdr + ssl->next_record_offset,
2391 ssl->in_left );
2392 }
2393
2394 ssl->next_record_offset = 0;
2395 }
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002399
2400 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002401 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002402 */
2403 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002406 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002407 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002408
2409 /*
2410 * A record can't be split accross datagrams. If we need to read but
2411 * are not at the beginning of a new record, the caller did something
2412 * wrong.
2413 */
2414 if( ssl->in_left != 0 )
2415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002418 }
2419
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002420 /*
2421 * Don't even try to read if time's out already.
2422 * This avoids by-passing the timer when repeatedly receiving messages
2423 * that will end up being dropped.
2424 */
2425 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002426 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002427 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002432 timeout = ssl->handshake->retransmit_timeout;
2433 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002434 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002437
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002438 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002439 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2440 timeout );
2441 else
2442 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002445
2446 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002448 }
2449
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002450 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002453 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002456 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002457 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002460 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002461 }
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002466 return( ret );
2467 }
2468
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002469 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002472 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002474 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002475 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002478 return( ret );
2479 }
2480
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002481 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002482 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002484 }
2485
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 if( ret < 0 )
2487 return( ret );
2488
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002489 ssl->in_left = ret;
2490 }
2491 else
2492#endif
2493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002495 ssl->in_left, nb_want ) );
2496
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002497 while( ssl->in_left < nb_want )
2498 {
2499 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002500
2501 if( ssl_check_timer( ssl ) != 0 )
2502 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2503 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002504 {
2505 if( ssl->f_recv_timeout != NULL )
2506 {
2507 ret = ssl->f_recv_timeout( ssl->p_bio,
2508 ssl->in_hdr + ssl->in_left, len,
2509 ssl->conf->read_timeout );
2510 }
2511 else
2512 {
2513 ret = ssl->f_recv( ssl->p_bio,
2514 ssl->in_hdr + ssl->in_left, len );
2515 }
2516 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002519 ssl->in_left, nb_want ) );
2520 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002521
2522 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002524
2525 if( ret < 0 )
2526 return( ret );
2527
mohammad1603f72e51f2018-03-28 23:44:39 -07002528 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad160389c12ec2018-03-19 07:15:50 -07002529 {
Jaeden Ameroee6c8222018-04-03 12:07:19 +01002530 MBEDTLS_SSL_DEBUG_MSG( 1,
2531 ( "f_recv returned %d bytes but only %lu were requested",
mohammad1603ad2908c2018-04-02 07:30:32 -07002532 ret, (unsigned long)len ) );
mohammad160389c12ec2018-03-19 07:15:50 -07002533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2534 }
2535
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002536 ssl->in_left += ret;
2537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 }
2539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
2542 return( 0 );
2543}
2544
2545/*
2546 * Flush any data not yet written
2547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002549{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002550 int ret;
2551 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002555 if( ssl->f_send == NULL )
2556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002558 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002560 }
2561
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002562 /* Avoid incrementing counter if data is flushed */
2563 if( ssl->out_left == 0 )
2564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002566 return( 0 );
2567 }
2568
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 while( ssl->out_left > 0 )
2570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2572 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002575 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002576 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
2580 if( ret <= 0 )
2581 return( ret );
2582
mohammad1603f72e51f2018-03-28 23:44:39 -07002583 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad1603f65add42018-02-22 04:29:04 -08002584 {
Jaeden Ameroee6c8222018-04-03 12:07:19 +01002585 MBEDTLS_SSL_DEBUG_MSG( 1,
2586 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad1603ad2908c2018-04-02 07:30:32 -07002587 ret, (unsigned long)ssl->out_left ) );
mohammad1603f65add42018-02-22 04:29:04 -08002588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2589 }
2590
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 ssl->out_left -= ret;
2592 }
2593
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002594 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002595 if( ++ssl->out_ctr[i - 1] != 0 )
2596 break;
2597
2598 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002599 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2602 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002603 }
2604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607 return( 0 );
2608}
2609
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002610/*
2611 * Functions to handle the DTLS retransmission state machine
2612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002614/*
2615 * Append current handshake message to current outgoing flight
2616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002620
2621 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002622 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002623 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002626 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002627 }
2628
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002629 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002630 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002633 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002634 }
2635
2636 /* Copy current handshake message with headers */
2637 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2638 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002639 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002640 msg->next = NULL;
2641
2642 /* Append to the current flight */
2643 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002644 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002645 else
2646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002648 while( cur->next != NULL )
2649 cur = cur->next;
2650 cur->next = msg;
2651 }
2652
2653 return( 0 );
2654}
2655
2656/*
2657 * Free the current flight of handshake messages
2658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002660{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_ssl_flight_item *cur = flight;
2662 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002663
2664 while( cur != NULL )
2665 {
2666 next = cur->next;
2667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 mbedtls_free( cur->p );
2669 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002670
2671 cur = next;
2672 }
2673}
2674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2676static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002677#endif
2678
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002679/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002680 * Swap transform_out and out_ctr with the alternative ones
2681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002685 unsigned char tmp_out_ctr[8];
2686
2687 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002690 return;
2691 }
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002694
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002695 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002696 tmp_transform = ssl->transform_out;
2697 ssl->transform_out = ssl->handshake->alt_transform_out;
2698 ssl->handshake->alt_transform_out = tmp_transform;
2699
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002700 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002701 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2702 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2703 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002704
2705 /* Adjust to the newly activated transform */
2706 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002708 {
2709 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2710 ssl->transform_out->fixed_ivlen;
2711 }
2712 else
2713 ssl->out_msg = ssl->out_iv;
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2716 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2721 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002722 }
2723 }
2724#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002725}
2726
2727/*
2728 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002729 *
2730 * Need to remember the current message in case flush_output returns
2731 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002732 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002741
2742 ssl->handshake->cur_msg = ssl->handshake->flight;
2743 ssl_swap_epochs( ssl );
2744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002746 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002747
2748 while( ssl->handshake->cur_msg != NULL )
2749 {
2750 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002752
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002753 /* Swap epochs before sending Finished: we can't do it after
2754 * sending ChangeCipherSpec, in case write returns WANT_READ.
2755 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2757 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002758 {
2759 ssl_swap_epochs( ssl );
2760 }
2761
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002762 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002763 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002764 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002765
2766 ssl->handshake->cur_msg = cur->next;
2767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002773 return( ret );
2774 }
2775 }
2776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2778 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002779 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002782 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2783 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002786
2787 return( 0 );
2788}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002789
2790/*
2791 * To be called when the last message of an incoming flight is received.
2792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002794{
2795 /* We won't need to resend that one any more */
2796 ssl_flight_free( ssl->handshake->flight );
2797 ssl->handshake->flight = NULL;
2798 ssl->handshake->cur_msg = NULL;
2799
2800 /* The next incoming flight will start with this msg_seq */
2801 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2802
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002803 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002804 ssl_set_timer( ssl, 0 );
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2807 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002810 }
2811 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002813}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002814
2815/*
2816 * To be called when the last message of an outgoing flight is send.
2817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002819{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002820 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002821 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2824 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002827 }
2828 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002830}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002832
Paul Bakker5121ce52009-01-03 21:22:43 +00002833/*
2834 * Record layer functions
2835 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002836
2837/*
2838 * Write current record.
2839 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842{
Paul Bakker05ef8352012-05-08 09:17:57 +00002843 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002844 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002849 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002850 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002852 {
2853 ; /* Skip special handshake treatment when resending */
2854 }
2855 else
2856#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 {
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002859 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST &&
2860 ssl->handshake == NULL )
2861 {
2862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2864 }
2865
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2867 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2868 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2869
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002870 /*
2871 * DTLS has additional fields in the Handshake layer,
2872 * between the length field and the actual payload:
2873 * uint16 message_seq;
2874 * uint24 fragment_offset;
2875 * uint24 fragment_length;
2876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002878 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002879 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002880 /* Make room for the additional DTLS fields */
Hanno Becker0983dc42017-09-18 10:55:54 +01002881 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
2882 {
2883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2884 "size %u, maximum %u",
2885 (unsigned) ( ssl->in_hslen - 4 ),
2886 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
2887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2888 }
2889
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002890 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002891 ssl->out_msglen += 8;
2892 len += 8;
2893
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002894 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002896 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002897 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2898 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2899 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002900 }
2901 else
2902 {
2903 ssl->out_msg[4] = 0;
2904 ssl->out_msg[5] = 0;
2905 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002906
2907 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2908 memset( ssl->out_msg + 6, 0x00, 3 );
2909 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002914 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 }
2916
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002917 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002919 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002920 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
2922 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
2923 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002924 {
2925 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002928 return( ret );
2929 }
2930 }
2931#endif
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002934 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936 {
2937 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940 return( ret );
2941 }
2942
2943 len = ssl->out_msglen;
2944 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2948 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 ret = mbedtls_ssl_hw_record_write( ssl );
2953 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2956 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002957 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002958
2959 if( ret == 0 )
2960 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002961 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002963 if( !done )
2964 {
2965 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002967 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002968
2969 ssl->out_len[0] = (unsigned char)( len >> 8 );
2970 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002971
Paul Bakker48916f92012-09-16 19:57:18 +00002972 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002973 {
2974 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002977 return( ret );
2978 }
2979
2980 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002981 ssl->out_len[0] = (unsigned char)( len >> 8 );
2982 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002983 }
2984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00002988 "version = [%d:%d], msglen = %d",
2989 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002990 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
2993 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 }
2995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999 return( ret );
3000 }
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
3004 return( 0 );
3005}
3006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003008/*
3009 * Mark bits in bitmask (used for DTLS HS reassembly)
3010 */
3011static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3012{
3013 unsigned int start_bits, end_bits;
3014
3015 start_bits = 8 - ( offset % 8 );
3016 if( start_bits != 8 )
3017 {
3018 size_t first_byte_idx = offset / 8;
3019
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003020 /* Special case */
3021 if( len <= start_bits )
3022 {
3023 for( ; len != 0; len-- )
3024 mask[first_byte_idx] |= 1 << ( start_bits - len );
3025
3026 /* Avoid potential issues with offset or len becoming invalid */
3027 return;
3028 }
3029
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003030 offset += start_bits; /* Now offset % 8 == 0 */
3031 len -= start_bits;
3032
3033 for( ; start_bits != 0; start_bits-- )
3034 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3035 }
3036
3037 end_bits = len % 8;
3038 if( end_bits != 0 )
3039 {
3040 size_t last_byte_idx = ( offset + len ) / 8;
3041
3042 len -= end_bits; /* Now len % 8 == 0 */
3043
3044 for( ; end_bits != 0; end_bits-- )
3045 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3046 }
3047
3048 memset( mask + offset / 8, 0xFF, len / 8 );
3049}
3050
3051/*
3052 * Check that bitmask is full
3053 */
3054static int ssl_bitmask_check( unsigned char *mask, size_t len )
3055{
3056 size_t i;
3057
3058 for( i = 0; i < len / 8; i++ )
3059 if( mask[i] != 0xFF )
3060 return( -1 );
3061
3062 for( i = 0; i < len % 8; i++ )
3063 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3064 return( -1 );
3065
3066 return( 0 );
3067}
3068
3069/*
3070 * Reassemble fragmented DTLS handshake messages.
3071 *
3072 * Use a temporary buffer for reassembly, divided in two parts:
3073 * - the first holds the reassembled message (including handshake header),
3074 * - the second holds a bitmask indicating which parts of the message
3075 * (excluding headers) have been received so far.
3076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003078{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003079 unsigned char *msg, *bitmask;
3080 size_t frag_len, frag_off;
3081 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3082
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003083 if( ssl->handshake == NULL )
3084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3086 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003087 }
3088
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003089 /*
3090 * For first fragment, check size and allocate buffer
3091 */
3092 if( ssl->handshake->hs_msg == NULL )
3093 {
3094 size_t alloc_len;
3095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003097 msg_len ) );
3098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3102 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003103 }
3104
3105 /* The bitmask needs one bit per byte of message excluding header */
3106 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3107
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003108 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003109 if( ssl->handshake->hs_msg == NULL )
3110 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003113 }
3114
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003115 /* Prepare final header: copy msg_type, length and message_seq,
3116 * then add standardised fragment_offset and fragment_length */
3117 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3118 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3119 memcpy( ssl->handshake->hs_msg + 9,
3120 ssl->handshake->hs_msg + 1, 3 );
3121 }
3122 else
3123 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003124 /* Make sure msg_type and length are consistent */
3125 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3128 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003129 }
3130 }
3131
3132 msg = ssl->handshake->hs_msg + 12;
3133 bitmask = msg + msg_len;
3134
3135 /*
3136 * Check and copy current fragment
3137 */
3138 frag_off = ( ssl->in_msg[6] << 16 ) |
3139 ( ssl->in_msg[7] << 8 ) |
3140 ssl->in_msg[8];
3141 frag_len = ( ssl->in_msg[9] << 16 ) |
3142 ( ssl->in_msg[10] << 8 ) |
3143 ssl->in_msg[11];
3144
3145 if( frag_off + frag_len > msg_len )
3146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003148 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003150 }
3151
3152 if( frag_len + 12 > ssl->in_msglen )
3153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003155 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003157 }
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003160 frag_off, frag_len ) );
3161
3162 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3163 ssl_bitmask_set( bitmask, frag_off, frag_len );
3164
3165 /*
3166 * Do we have the complete message by now?
3167 * If yes, finalize it, else ask to read the next record.
3168 */
3169 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003172 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003173 }
3174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003176
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003177 if( frag_len + 12 < ssl->in_msglen )
3178 {
3179 /*
3180 * We'got more handshake messages in the same record.
3181 * This case is not handled now because no know implementation does
3182 * that and it's hard to test, so we prefer to fail cleanly for now.
3183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003186 }
3187
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003188 if( ssl->in_left > ssl->next_record_offset )
3189 {
3190 /*
3191 * We've got more data in the buffer after the current record,
3192 * that we don't want to overwrite. Move it before writing the
3193 * reassembled message, and adjust in_left and next_record_offset.
3194 */
3195 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3196 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3197 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3198
3199 /* First compute and check new lengths */
3200 ssl->next_record_offset = new_remain - ssl->in_hdr;
3201 ssl->in_left = ssl->next_record_offset + remain_len;
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003204 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3207 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003208 }
3209
3210 memmove( new_remain, cur_remain, remain_len );
3211 }
3212
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003213 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003216 ssl->handshake->hs_msg = NULL;
3217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003219 ssl->in_msg, ssl->in_hslen );
3220
3221 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225static int ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003230 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003232 }
3233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003235 ( ssl->in_msg[1] << 16 ) |
3236 ( ssl->in_msg[2] << 8 ) |
3237 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003240 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003241 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003245 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003246 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003247 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003248
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003249 /* ssl->handshake is NULL when receiving ClientHello for renego */
3250 if( ssl->handshake != NULL &&
3251 recv_msg_seq != ssl->handshake->in_msg_seq )
3252 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003253 /* Retransmit only on last message from previous flight, to avoid
3254 * too many retransmissions.
3255 * Besides, No sane server ever retransmits HelloVerifyRequest */
3256 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003260 "message_seq = %d, start_of_flight = %d",
3261 recv_msg_seq,
3262 ssl->handshake->in_flight_start_seq ) );
3263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003267 return( ret );
3268 }
3269 }
3270 else
3271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003273 "message_seq = %d, expected = %d",
3274 recv_msg_seq,
3275 ssl->handshake->in_msg_seq ) );
3276 }
3277
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003278 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003279 }
3280 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003281
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003282 /* Reassemble if current message is fragmented or reassembly is
3283 * already in progress */
3284 if( ssl->in_msglen < ssl->in_hslen ||
3285 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3286 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3287 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003290
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003291 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003294 return( ret );
3295 }
3296 }
3297 }
3298 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003300 /* With TLS we don't handle fragmentation (for now) */
3301 if( ssl->in_msglen < ssl->in_hslen )
3302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3304 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003305 }
3306
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003307 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3308 ssl->handshake != NULL )
3309 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003310 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003311 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003312
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003313 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003315 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003316 ssl->handshake != NULL )
3317 {
3318 ssl->handshake->in_msg_seq++;
3319 }
3320#endif
3321
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003322 return( 0 );
3323}
3324
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003325/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003326 * DTLS anti-replay: RFC 6347 4.1.2.6
3327 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003328 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3329 * Bit n is set iff record number in_window_top - n has been seen.
3330 *
3331 * Usually, in_window_top is the last record number seen and the lsb of
3332 * in_window is set. The only exception is the initial state (record number 0
3333 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3336static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003337{
3338 ssl->in_window_top = 0;
3339 ssl->in_window = 0;
3340}
3341
3342static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3343{
3344 return( ( (uint64_t) buf[0] << 40 ) |
3345 ( (uint64_t) buf[1] << 32 ) |
3346 ( (uint64_t) buf[2] << 24 ) |
3347 ( (uint64_t) buf[3] << 16 ) |
3348 ( (uint64_t) buf[4] << 8 ) |
3349 ( (uint64_t) buf[5] ) );
3350}
3351
3352/*
3353 * Return 0 if sequence number is acceptable, -1 otherwise
3354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003356{
3357 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3358 uint64_t bit;
3359
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003360 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003361 return( 0 );
3362
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003363 if( rec_seqnum > ssl->in_window_top )
3364 return( 0 );
3365
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003366 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003367
3368 if( bit >= 64 )
3369 return( -1 );
3370
3371 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3372 return( -1 );
3373
3374 return( 0 );
3375}
3376
3377/*
3378 * Update replay window on new validated record
3379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003381{
3382 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3383
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003384 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003385 return;
3386
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003387 if( rec_seqnum > ssl->in_window_top )
3388 {
3389 /* Update window_top and the contents of the window */
3390 uint64_t shift = rec_seqnum - ssl->in_window_top;
3391
3392 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003393 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003394 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003395 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003396 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003397 ssl->in_window |= 1;
3398 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003399
3400 ssl->in_window_top = rec_seqnum;
3401 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003402 else
3403 {
3404 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003405 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003406
3407 if( bit < 64 ) /* Always true, but be extra sure */
3408 ssl->in_window |= (uint64_t) 1 << bit;
3409 }
3410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003412
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003413#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003414/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003415static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3416
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003417/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003418 * Without any SSL context, check if a datagram looks like a ClientHello with
3419 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003420 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003421 *
3422 * - if cookie is valid, return 0
3423 * - if ClientHello looks superficially valid but cookie is not,
3424 * fill obuf and set olen, then
3425 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3426 * - otherwise return a specific error code
3427 */
3428static int ssl_check_dtls_clihlo_cookie(
3429 mbedtls_ssl_cookie_write_t *f_cookie_write,
3430 mbedtls_ssl_cookie_check_t *f_cookie_check,
3431 void *p_cookie,
3432 const unsigned char *cli_id, size_t cli_id_len,
3433 const unsigned char *in, size_t in_len,
3434 unsigned char *obuf, size_t buf_len, size_t *olen )
3435{
3436 size_t sid_len, cookie_len;
3437 unsigned char *p;
3438
3439 if( f_cookie_write == NULL || f_cookie_check == NULL )
3440 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3441
3442 /*
3443 * Structure of ClientHello with record and handshake headers,
3444 * and expected values. We don't need to check a lot, more checks will be
3445 * done when actually parsing the ClientHello - skipping those checks
3446 * avoids code duplication and does not make cookie forging any easier.
3447 *
3448 * 0-0 ContentType type; copied, must be handshake
3449 * 1-2 ProtocolVersion version; copied
3450 * 3-4 uint16 epoch; copied, must be 0
3451 * 5-10 uint48 sequence_number; copied
3452 * 11-12 uint16 length; (ignored)
3453 *
3454 * 13-13 HandshakeType msg_type; (ignored)
3455 * 14-16 uint24 length; (ignored)
3456 * 17-18 uint16 message_seq; copied
3457 * 19-21 uint24 fragment_offset; copied, must be 0
3458 * 22-24 uint24 fragment_length; (ignored)
3459 *
3460 * 25-26 ProtocolVersion client_version; (ignored)
3461 * 27-58 Random random; (ignored)
3462 * 59-xx SessionID session_id; 1 byte len + sid_len content
3463 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3464 * ...
3465 *
3466 * Minimum length is 61 bytes.
3467 */
3468 if( in_len < 61 ||
3469 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3470 in[3] != 0 || in[4] != 0 ||
3471 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3472 {
3473 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3474 }
3475
3476 sid_len = in[59];
3477 if( sid_len > in_len - 61 )
3478 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3479
3480 cookie_len = in[60 + sid_len];
3481 if( cookie_len > in_len - 60 )
3482 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3483
3484 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3485 cli_id, cli_id_len ) == 0 )
3486 {
3487 /* Valid cookie */
3488 return( 0 );
3489 }
3490
3491 /*
3492 * If we get here, we've got an invalid cookie, let's prepare HVR.
3493 *
3494 * 0-0 ContentType type; copied
3495 * 1-2 ProtocolVersion version; copied
3496 * 3-4 uint16 epoch; copied
3497 * 5-10 uint48 sequence_number; copied
3498 * 11-12 uint16 length; olen - 13
3499 *
3500 * 13-13 HandshakeType msg_type; hello_verify_request
3501 * 14-16 uint24 length; olen - 25
3502 * 17-18 uint16 message_seq; copied
3503 * 19-21 uint24 fragment_offset; copied
3504 * 22-24 uint24 fragment_length; olen - 25
3505 *
3506 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3507 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3508 *
3509 * Minimum length is 28.
3510 */
3511 if( buf_len < 28 )
3512 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3513
3514 /* Copy most fields and adapt others */
3515 memcpy( obuf, in, 25 );
3516 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3517 obuf[25] = 0xfe;
3518 obuf[26] = 0xff;
3519
3520 /* Generate and write actual cookie */
3521 p = obuf + 28;
3522 if( f_cookie_write( p_cookie,
3523 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3524 {
3525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3526 }
3527
3528 *olen = p - obuf;
3529
3530 /* Go back and fill length fields */
3531 obuf[27] = (unsigned char)( *olen - 28 );
3532
3533 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3534 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3535 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3536
3537 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3538 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3539
3540 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3541}
3542
3543/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003544 * Handle possible client reconnect with the same UDP quadruplet
3545 * (RFC 6347 Section 4.2.8).
3546 *
3547 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3548 * that looks like a ClientHello.
3549 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003550 * - if the input looks like a ClientHello without cookies,
3551 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003552 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003553 * - if the input looks like a ClientHello with a valid cookie,
3554 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003555 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003556 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003557 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003558 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003559 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3560 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003561 */
3562static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3563{
3564 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003565 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003566
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003567 ret = ssl_check_dtls_clihlo_cookie(
3568 ssl->conf->f_cookie_write,
3569 ssl->conf->f_cookie_check,
3570 ssl->conf->p_cookie,
3571 ssl->cli_id, ssl->cli_id_len,
3572 ssl->in_buf, ssl->in_left,
3573 ssl->out_buf, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003574
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003575 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3576
3577 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003578 {
Brian J Murraye7f8dc32016-11-06 04:45:15 -08003579 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003580 * If the error is permanent we'll catch it later,
3581 * if it's not, then hopefully it'll work next time. */
3582 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3583
3584 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003585 }
3586
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003587 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003588 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003589 /* Got a valid cookie, partially reset context */
3590 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3591 {
3592 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3593 return( ret );
3594 }
3595
3596 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003597 }
3598
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003599 return( ret );
3600}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003601#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003602
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003603/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003604 * ContentType type;
3605 * ProtocolVersion version;
3606 * uint16 epoch; // DTLS only
3607 * uint48 sequence_number; // DTLS only
3608 * uint16 length;
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003609 *
3610 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butchere103aa82015-12-16 01:51:01 +00003611 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003612 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3613 *
3614 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butchere103aa82015-12-16 01:51:01 +00003615 * 1. proceed with the record if this function returns 0
3616 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3617 * 3. return CLIENT_RECONNECT if this function returns that value
3618 * 4. drop the whole datagram if this function returns anything else.
3619 * Point 2 is needed when the peer is resending, and we have already received
3620 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003623{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003624 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 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 +02003627
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003629 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003630 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003633 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003634 ssl->in_msgtype,
3635 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003636
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003637 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3639 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3640 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3641 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003644
Andres Amaya Garcia1042d862017-07-05 13:26:34 +01003645#if defined(MBEDTLS_SSL_PROTO_DTLS)
3646 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3647 * Section 4.1.2.7 */
3648 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3649#endif /* MBEDTLS_SSL_PROTO_DTLS */
3650 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3651 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003654 }
3655
3656 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003657 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3660 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 }
3662
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003663 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3666 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 }
3668
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003669 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003671 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3674 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003675 }
3676
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003677 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003678 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003680 if( ssl->in_msglen < 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3684 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 }
3686 }
3687 else
3688 {
Paul Bakker48916f92012-09-16 19:57:18 +00003689 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3692 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003693 }
3694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#if defined(MBEDTLS_SSL_PROTO_SSL3)
3696 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3697 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3700 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003701 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3704 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 /*
3706 * TLS encrypted messages can have up to 256 bytes of padding
3707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003709 ssl->in_msglen > ssl->transform_in->minlen +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3713 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003714 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003715#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 }
3717
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003718 /*
3719 * DTLS-related tests done last, because most of them may result in
3720 * silently dropping the record (but not the whole datagram), and we only
3721 * want to consider that after ensuring that the "basic" fields (type,
3722 * version, length) are sane.
3723 */
3724#if defined(MBEDTLS_SSL_PROTO_DTLS)
3725 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3726 {
3727 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3728
3729 /* Drop unexpected ChangeCipherSpec messages */
3730 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3731 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3732 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3733 {
3734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3735 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3736 }
3737
3738 /* Drop unexpected ApplicationData records,
3739 * except at the beginning of renegotiations */
3740 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3741 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3742#if defined(MBEDTLS_SSL_RENEGOTIATION)
3743 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3744 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3745#endif
3746 )
3747 {
3748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3749 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3750 }
3751
3752 /* Check epoch (and sequence number) with DTLS */
3753 if( rec_epoch != ssl->in_epoch )
3754 {
3755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3756 "expected %d, received %d",
3757 ssl->in_epoch, rec_epoch ) );
3758
3759#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3760 /*
3761 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3762 * access the first byte of record content (handshake type), as we
3763 * have an active transform (possibly iv_len != 0), so use the
3764 * fact that the record header len is 13 instead.
3765 */
3766 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3767 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3768 rec_epoch == 0 &&
3769 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3770 ssl->in_left > 13 &&
3771 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3772 {
3773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3774 "from the same port" ) );
3775 return( ssl_handle_possible_reconnect( ssl ) );
3776 }
3777 else
3778#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3779 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3780 }
3781
3782#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3783 /* Replay detection only works for the current epoch */
3784 if( rec_epoch == ssl->in_epoch &&
3785 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3786 {
3787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3788 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3789 }
3790#endif
3791 }
3792#endif /* MBEDTLS_SSL_PROTO_DTLS */
3793
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003794 return( 0 );
3795}
Paul Bakker5121ce52009-01-03 21:22:43 +00003796
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003797/*
3798 * If applicable, decrypt (and decompress) record content
3799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003801{
3802 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3805 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3808 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003812 ret = mbedtls_ssl_hw_record_read( ssl );
3813 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3816 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003817 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003818
3819 if( ret == 0 )
3820 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003821 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003823 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003824 {
3825 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003828 return( ret );
3829 }
3830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003832 ssl->in_msg, ssl->in_msglen );
3833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3837 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003838 }
3839 }
3840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003842 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003844 {
3845 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003848 return( ret );
3849 }
3850
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003851 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3852 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003853 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003860 }
3861#endif
3862
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003863 return( 0 );
3864}
3865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003867
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003868/*
3869 * Read a record.
3870 *
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02003871 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3872 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3873 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003876{
3877 int ret;
3878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003880
Hanno Becker704f4932017-06-08 13:08:45 +01003881 if( ssl->keep_current_message == 1 )
3882 {
3883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
3884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3885 ssl->keep_current_message = 0;
3886
3887 return( 0 );
3888 }
3889
Hanno Becker6a582e82017-06-08 13:38:05 +01003890 /*
3891 * Step A
3892 *
3893 * Consume last content-layer message and potentially
3894 * update in_msglen which keeps track of the contents'
3895 * consumption state.
3896 *
3897 * (1) Handshake messages:
3898 * Remove last handshake message, move content
3899 * and adapt in_msglen.
3900 *
3901 * (2) Alert messages:
3902 * Consume whole record content, in_msglen = 0.
3903 *
3904 * NOTE: This needs to be fixed, since like for
3905 * handshake messages it is allowed to have
3906 * multiple alerts witin a single record.
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003907 * Internal reference IOTSSL-1321.
Hanno Becker6a582e82017-06-08 13:38:05 +01003908 *
3909 * (3) Change cipher spec:
3910 * Consume whole record content, in_msglen = 0.
3911 *
3912 * (4) Application data:
3913 * Don't do anything - the record layer provides
3914 * the application data as a stream transport
3915 * and consumes through mbedtls_ssl_read only.
3916 *
3917 */
3918
3919 /* Case (1): Handshake messages */
3920
3921 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003922 {
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003923 if( ssl->in_offt != NULL )
3924 {
3925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3926 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3927 }
3928
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003929 /*
3930 * Get next Handshake message in the current record
3931 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003932
Hanno Becker6a582e82017-06-08 13:38:05 +01003933 /* Notes:
3934 * (1) in_hslen is *NOT* necessarily the size of the
3935 * current handshake content: If DTLS handshake
3936 * fragmentation is used, that's the fragment
3937 * size instead. Using the total handshake message
3938 * size here is FAULTY and should be changed at
3939 * some point. Internal reference IOTSSL-1414.
3940 * (2) While it doesn't seem to cause problems, one
3941 * has to be very careful not to assume that in_hslen
3942 * is always <= in_msglen in a sensible communication.
3943 * Again, it's wrong for DTLS handshake fragmentation.
3944 * The following check is therefore mandatory, and
3945 * should not be treated as a silently corrected assertion.
3946 */
3947 if( ssl->in_hslen < ssl->in_msglen )
3948 {
3949 ssl->in_msglen -= ssl->in_hslen;
3950 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3951 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003952
Hanno Becker6a582e82017-06-08 13:38:05 +01003953 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
3954 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003955
Hanno Becker6a582e82017-06-08 13:38:05 +01003956 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3957 return( ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003958
Hanno Becker6a582e82017-06-08 13:38:05 +01003959 return( 0 );
3960 }
3961 else
3962 {
3963 ssl->in_msglen = 0;
3964 }
3965
3966 ssl->in_hslen = 0;
3967 }
3968 /* Case (4): Application data */
3969 else if( ssl->in_offt != NULL )
3970 {
3971 return( 0 );
3972 }
3973 /* Everything else (CCS & Alerts) */
3974 else
3975 {
3976 ssl->in_msglen = 0;
3977 }
3978
3979 /*
3980 * Step B
3981 *
3982 * Fetch and decode new record if current one is fully consumed.
3983 *
3984 */
3985
3986 if( ssl->in_msglen > 0 )
3987 {
3988 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003989 return( 0 );
3990 }
3991
Hanno Becker6a582e82017-06-08 13:38:05 +01003992 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003993
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003994read_record_header:
Hanno Becker6a582e82017-06-08 13:38:05 +01003995
3996 /* Current record either fully processed or to be discarded. */
3997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004001 return( ret );
4002 }
4003
4004 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004007 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4008 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004009 {
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01004010 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4011 {
4012 /* Skip unexpected record (but not whole datagram) */
4013 ssl->next_record_offset = ssl->in_msglen
4014 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004015
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01004016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4017 "(header)" ) );
4018 }
4019 else
4020 {
4021 /* Skip invalid record and the rest of the datagram */
4022 ssl->next_record_offset = 0;
4023 ssl->in_left = 0;
4024
4025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4026 "(header)" ) );
4027 }
4028
4029 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004030 goto read_record_header;
4031 }
4032#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004033 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004034 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004035
4036 /*
4037 * Read and optionally decrypt the message contents
4038 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4040 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004043 return( ret );
4044 }
4045
4046 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004048 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004050 else
4051#endif
4052 ssl->in_left = 0;
4053
4054 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004057 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004058 {
4059 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4061 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004062 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004063 /* Except when waiting for Finished as a bad mac here
4064 * probably means something went wrong in the handshake
4065 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4066 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4067 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4068 {
4069#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4070 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4071 {
4072 mbedtls_ssl_send_alert_message( ssl,
4073 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4074 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4075 }
4076#endif
4077 return( ret );
4078 }
4079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004081 if( ssl->conf->badmac_limit != 0 &&
4082 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4085 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004086 }
4087#endif
4088
Hanno Becker6a582e82017-06-08 13:38:05 +01004089 /* As above, invalid records cause
4090 * dismissal of the whole datagram. */
4091
4092 ssl->next_record_offset = 0;
4093 ssl->in_left = 0;
4094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004096 goto read_record_header;
4097 }
4098
4099 return( ret );
4100 }
4101 else
4102#endif
4103 {
4104 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4106 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 mbedtls_ssl_send_alert_message( ssl,
4109 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4110 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004111 }
4112#endif
4113 return( ret );
4114 }
4115 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004116
4117 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004118 * When we sent the last flight of the handshake, we MUST respond to a
4119 * retransmit of the peer's previous flight with a retransmit. (In
4120 * practice, only the Finished message will make it, other messages
4121 * including CCS use the old transform so they're dropped as invalid.)
4122 *
4123 * If the record we received is not a handshake message, however, it
4124 * means the peer received our last flight so we can clean up
4125 * handshake info.
4126 *
4127 * This check needs to be done before prepare_handshake() due to an edge
4128 * case: if the client immediately requests renegotiation, this
4129 * finishes the current handshake first, avoiding the new ClientHello
4130 * being mistaken for an ancient message in the current handshake.
4131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004133 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004134 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4138 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004145 return( ret );
4146 }
4147
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004148 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004149 }
4150 else
4151 {
4152 ssl_handshake_wrapup_free_hs_transform( ssl );
4153 }
4154 }
4155#endif
4156
4157 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004158 * Handle particular types of records
4159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004161 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004162 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
4163 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004164 }
4165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004167 {
Angus Grattonfd1c5e82018-06-20 15:43:50 +10004168 if( ssl->in_msglen != 2 )
4169 {
4170 /* Note: Standard allows for more than one 2 byte alert
4171 to be packed in a single message, but Mbed TLS doesn't
4172 currently support this. */
4173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4174 ssl->in_msglen ) );
4175 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4176 }
4177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004179 ssl->in_msg[0], ssl->in_msg[1] ) );
4180
4181 /*
Simon Butcher94c5e3c2015-10-27 16:09:03 +00004182 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004187 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004188 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004189 }
4190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4192 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4195 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004196 }
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02004197
4198#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4199 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4200 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4201 {
4202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4203 /* Will be handled when trying to parse ServerHello */
4204 return( 0 );
4205 }
4206#endif
4207
4208#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4209 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4210 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4211 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4212 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4213 {
4214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4215 /* Will be handled in mbedtls_ssl_parse_certificate() */
4216 return( 0 );
4217 }
4218#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4219
4220 /* Silently ignore: fetch new message */
4221 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00004222 }
4223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004225
4226 return( 0 );
4227}
4228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004230{
4231 int ret;
4232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4234 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4235 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004236 {
4237 return( ret );
4238 }
4239
4240 return( 0 );
4241}
4242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004244 unsigned char level,
4245 unsigned char message )
4246{
4247 int ret;
4248
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004249 if( ssl == NULL || ssl->conf == NULL )
4250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004255 ssl->out_msglen = 2;
4256 ssl->out_msg[0] = level;
4257 ssl->out_msg[1] = message;
4258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004262 return( ret );
4263 }
4264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004266
4267 return( 0 );
4268}
4269
Paul Bakker5121ce52009-01-03 21:22:43 +00004270/*
4271 * Handshake functions
4272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4274 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4275 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4276 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4277 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4278 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4279 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4280int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004281{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4287 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004291 ssl->state++;
4292 return( 0 );
4293 }
4294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004297}
4298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4306 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4307 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004310 ssl->state++;
4311 return( 0 );
4312 }
4313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4315 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004316}
4317#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004321 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322 const mbedtls_x509_crt *crt;
4323 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4328 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4329 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004332 ssl->state++;
4333 return( 0 );
4334 }
4335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004337 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 {
4339 if( ssl->client_auth == 0 )
4340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004342 ssl->state++;
4343 return( 0 );
4344 }
4345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 /*
4348 * If using SSLv3 and got no cert, send an Alert message
4349 * (otherwise an empty Certificate message will be sent).
4350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4352 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 {
4354 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4356 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4357 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 goto write_msg;
4361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004363 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364#endif /* MBEDTLS_SSL_CLI_C */
4365#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004366 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004368 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4371 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 }
4373 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004377
4378 /*
4379 * 0 . 0 handshake type
4380 * 1 . 3 handshake length
4381 * 4 . 6 length of all certs
4382 * 7 . 9 length of cert. 1
4383 * 10 . n-1 peer certificate
4384 * n . n+2 length of cert. 2
4385 * n+3 . ... upper level cert, etc.
4386 */
4387 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004389
Paul Bakker29087132010-03-21 21:03:34 +00004390 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004391 {
4392 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4396 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4397 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004398 }
4399
4400 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4401 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4402 ssl->out_msg[i + 2] = (unsigned char)( n );
4403
4404 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4405 i += n; crt = crt->next;
4406 }
4407
4408 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4409 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4410 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4411
4412 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4414 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004415
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004416#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004417write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004418#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004419
4420 ssl->state++;
4421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004425 return( ret );
4426 }
4427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004429
Paul Bakkered27a042013-04-18 22:46:23 +02004430 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004431}
4432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004433int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004434{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004436 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004438 int authmode = ssl->conf->authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4443 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4444 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004447 ssl->state++;
4448 return( 0 );
4449 }
4450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004452 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004453 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4454 {
4455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4456 ssl->state++;
4457 return( 0 );
4458 }
4459
4460#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4461 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4462 authmode = ssl->handshake->sni_authmode;
4463#endif
4464
4465 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4466 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004467 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004468 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004470 ssl->state++;
4471 return( 0 );
4472 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004473#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 return( ret );
4479 }
4480
4481 ssl->state++;
4482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483#if defined(MBEDTLS_SSL_SRV_C)
4484#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004485 /*
4486 * Check if the client sent an empty certificate
4487 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004488 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004491 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4493 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4494 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004497
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004498 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004499 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004500 return( 0 );
4501 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004503 }
4504 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4508 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004509 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4513 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4514 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4515 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004518
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004519 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004520 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004522 else
4523 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004524 }
4525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4527 MBEDTLS_SSL_PROTO_TLS1_2 */
4528#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4533 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004534 }
4535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4537 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4540 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004541 }
4542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004544
Paul Bakker5121ce52009-01-03 21:22:43 +00004545 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004546 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004547 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004548 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004549
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004550 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4554 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 }
4556
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004557 /* In case we tried to reuse a session but it failed */
4558 if( ssl->session_negotiate->peer_cert != NULL )
4559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4561 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004562 }
4563
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004564 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004566 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 sizeof( mbedtls_x509_crt ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004569 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 }
4571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004573
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004574 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004575
4576 while( i < ssl->in_hslen )
4577 {
Philippe Antoinebbc79182018-05-30 09:13:21 +02004578 if ( i + 3 > ssl->in_hslen ) {
4579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4580 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4581 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4582 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004584 if( ssl->in_msg[i] != 0 )
4585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4587 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004588 }
4589
4590 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4591 | (unsigned int) ssl->in_msg[i + 2];
4592 i += 3;
4593
4594 if( n < 128 || i + n > ssl->in_hslen )
4595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4597 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004598 }
4599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004601 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004602 if( ret != 0 )
4603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004605 return( ret );
4606 }
4607
4608 i += n;
4609 }
4610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004612
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004613 /*
4614 * On client, make sure the server cert doesn't change during renego to
4615 * avoid "triple handshake" attack: https://secure-resumption.com/
4616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004618 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004620 {
4621 if( ssl->session->peer_cert == NULL )
4622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
4624 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004625 }
4626
4627 if( ssl->session->peer_cert->raw.len !=
4628 ssl->session_negotiate->peer_cert->raw.len ||
4629 memcmp( ssl->session->peer_cert->raw.p,
4630 ssl->session_negotiate->peer_cert->raw.p,
4631 ssl->session->peer_cert->raw.len ) != 0 )
4632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
4634 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004635 }
4636 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004638
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004639 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004641 mbedtls_x509_crt *ca_chain;
4642 mbedtls_x509_crl *ca_crl;
4643
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004644#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004645 if( ssl->handshake->sni_ca_chain != NULL )
4646 {
4647 ca_chain = ssl->handshake->sni_ca_chain;
4648 ca_crl = ssl->handshake->sni_ca_crl;
4649 }
4650 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004651#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004652 {
4653 ca_chain = ssl->conf->ca_chain;
4654 ca_crl = ssl->conf->ca_crl;
4655 }
4656
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004657 /*
4658 * Main check: verify certificate
4659 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004660 ret = mbedtls_x509_crt_verify_with_profile(
4661 ssl->session_negotiate->peer_cert,
4662 ca_chain, ca_crl,
4663 ssl->conf->cert_profile,
4664 ssl->hostname,
4665 &ssl->session_negotiate->verify_result,
4666 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004667
4668 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004671 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004672
4673 /*
4674 * Secondary checks: always done, but change 'ret' only if it was 0
4675 */
4676
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004677#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004679 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004680
4681 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004683 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004684 {
Hanno Beckera3929ba2017-05-08 16:31:14 +01004685 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004688 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004690 }
4691 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004692#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Beckera3929ba2017-05-08 16:31:14 +01004695 ciphersuite_info,
4696 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004697 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004700 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004702 }
4703
Hanno Beckera3929ba2017-05-08 16:31:14 +01004704 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4705 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4706 * with details encoded in the verification flags. All other kinds
4707 * of error codes, including those from the user provided f_vrfy
4708 * functions, are treated as fatal and lead to a failure of
4709 * ssl_parse_certificate even if verification was optional. */
4710 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4711 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4712 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4713 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004714 ret = 0;
Hanno Beckera3929ba2017-05-08 16:31:14 +01004715 }
4716
4717 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4718 {
4719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4720 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4721 }
4722
Hanno Becker61c0c702017-05-15 16:05:15 +01004723#if defined(MBEDTLS_DEBUG_C)
4724 if( ssl->session_negotiate->verify_result != 0 )
4725 {
4726 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4727 ssl->session_negotiate->verify_result ) );
4728 }
4729 else
4730 {
4731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4732 }
4733#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 }
4735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004737
4738 return( ret );
4739}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004740#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
4741 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
4742 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
4743 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4744 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4745 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4746 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004749{
4750 int ret;
4751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004755 ssl->out_msglen = 1;
4756 ssl->out_msg[0] = 1;
4757
Paul Bakker5121ce52009-01-03 21:22:43 +00004758 ssl->state++;
4759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004763 return( ret );
4764 }
4765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004767
4768 return( 0 );
4769}
4770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004772{
4773 int ret;
4774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 return( ret );
4781 }
4782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4786 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004787 }
4788
4789 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4792 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004793 }
4794
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004795 /*
4796 * Switch to our negotiated transform and session parameters for inbound
4797 * data.
4798 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004800 ssl->transform_in = ssl->transform_negotiate;
4801 ssl->session_in = ssl->session_negotiate;
4802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004804 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004807 ssl_dtls_replay_reset( ssl );
4808#endif
4809
4810 /* Increment epoch */
4811 if( ++ssl->in_epoch == 0 )
4812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4814 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004815 }
4816 }
4817 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004819 memset( ssl->in_ctr, 0, 8 );
4820
4821 /*
4822 * Set the in_msg pointer to the correct location based on IV length
4823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004825 {
4826 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4827 ssl->transform_negotiate->fixed_ivlen;
4828 }
4829 else
4830 ssl->in_msg = ssl->in_iv;
4831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4833 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4838 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004839 }
4840 }
4841#endif
4842
Paul Bakker5121ce52009-01-03 21:22:43 +00004843 ssl->state++;
4844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004846
4847 return( 0 );
4848}
4849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
4851 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004852{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004853 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4856 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4857 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004858 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004859 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4862#if defined(MBEDTLS_SHA512_C)
4863 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004864 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4865 else
4866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867#if defined(MBEDTLS_SHA256_C)
4868 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004869 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004870 else
4871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004875 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004876 }
Paul Bakker380da532012-04-18 16:10:25 +00004877}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004881#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4882 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4883 mbedtls_md5_starts( &ssl->handshake->fin_md5 );
4884 mbedtls_sha1_starts( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4887#if defined(MBEDTLS_SHA256_C)
4888 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004889#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890#if defined(MBEDTLS_SHA512_C)
4891 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004892#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004894}
4895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004897 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4900 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4901 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4902 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4905#if defined(MBEDTLS_SHA256_C)
4906 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908#if defined(MBEDTLS_SHA512_C)
4909 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004912}
4913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4915 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4916static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004917 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004918{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4920 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004921}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004922#endif
Paul Bakker380da532012-04-18 16:10:25 +00004923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4925#if defined(MBEDTLS_SHA256_C)
4926static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004927 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004928{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004930}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004931#endif
Paul Bakker380da532012-04-18 16:10:25 +00004932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933#if defined(MBEDTLS_SHA512_C)
4934static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004935 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004936{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004938}
Paul Bakker769075d2012-11-24 11:26:46 +01004939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004943static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004945{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004946 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004947 mbedtls_md5_context md5;
4948 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004949
Paul Bakker5121ce52009-01-03 21:22:43 +00004950 unsigned char padbuf[48];
4951 unsigned char md5sum[16];
4952 unsigned char sha1sum[20];
4953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004955 if( !session )
4956 session = ssl->session;
4957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004959
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004960 mbedtls_md5_init( &md5 );
4961 mbedtls_sha1_init( &sha1 );
4962
4963 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
4964 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004965
4966 /*
4967 * SSLv3:
4968 * hash =
4969 * MD5( master + pad2 +
4970 * MD5( handshake + sender + master + pad1 ) )
4971 * + SHA1( master + pad2 +
4972 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004973 */
4974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004975#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004976 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4977 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004978#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004981 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4982 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004983#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02004986 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004987
Paul Bakker1ef83d62012-04-11 12:09:53 +00004988 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004989
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004990 mbedtls_md5_update( &md5, (const unsigned char *) sender, 4 );
4991 mbedtls_md5_update( &md5, session->master, 48 );
4992 mbedtls_md5_update( &md5, padbuf, 48 );
4993 mbedtls_md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004994
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004995 mbedtls_sha1_update( &sha1, (const unsigned char *) sender, 4 );
4996 mbedtls_sha1_update( &sha1, session->master, 48 );
4997 mbedtls_sha1_update( &sha1, padbuf, 40 );
4998 mbedtls_sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004999
Paul Bakker1ef83d62012-04-11 12:09:53 +00005000 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005001
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005002 mbedtls_md5_starts( &md5 );
5003 mbedtls_md5_update( &md5, session->master, 48 );
5004 mbedtls_md5_update( &md5, padbuf, 48 );
5005 mbedtls_md5_update( &md5, md5sum, 16 );
5006 mbedtls_md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005007
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005008 mbedtls_sha1_starts( &sha1 );
5009 mbedtls_sha1_update( &sha1, session->master, 48 );
5010 mbedtls_sha1_update( &sha1, padbuf , 40 );
5011 mbedtls_sha1_update( &sha1, sha1sum, 20 );
5012 mbedtls_sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005015
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005016 mbedtls_md5_free( &md5 );
5017 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5020 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5021 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005024}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005027#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005028static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005030{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005031 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005032 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005033 mbedtls_md5_context md5;
5034 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005035 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005037 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005038 if( !session )
5039 session = ssl->session;
5040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005042
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005043 mbedtls_md5_init( &md5 );
5044 mbedtls_sha1_init( &sha1 );
5045
5046 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5047 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005048
Paul Bakker1ef83d62012-04-11 12:09:53 +00005049 /*
5050 * TLSv1:
5051 * hash = PRF( master, finished_label,
5052 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5053 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005056 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5057 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005058#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005061 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5062 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005063#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005066 ? "client finished"
5067 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005068
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005069 mbedtls_md5_finish( &md5, padbuf );
5070 mbedtls_sha1_finish( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005071
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005072 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005073 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005076
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005077 mbedtls_md5_free( &md5 );
5078 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5087#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005088static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005090{
5091 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005092 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005093 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005094 unsigned char padbuf[32];
5095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005097 if( !session )
5098 session = ssl->session;
5099
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005100 mbedtls_sha256_init( &sha256 );
5101
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005103
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005104 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005105
5106 /*
5107 * TLSv1.2:
5108 * hash = PRF( master, finished_label,
5109 * Hash( handshake ) )[0.11]
5110 */
5111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112#if !defined(MBEDTLS_SHA256_ALT)
5113 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005114 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005115#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005118 ? "client finished"
5119 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005120
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005121 mbedtls_sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005122
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005123 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005124 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005127
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005128 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005137static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005139{
5140 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005141 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005142 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005143 unsigned char padbuf[48];
5144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005146 if( !session )
5147 session = ssl->session;
5148
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005149 mbedtls_sha512_init( &sha512 );
5150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005152
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005153 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005154
5155 /*
5156 * TLSv1.2:
5157 * hash = PRF( master, finished_label,
5158 * Hash( handshake ) )[0.11]
5159 */
5160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005162 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5163 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005164#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005167 ? "client finished"
5168 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005169
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005170 mbedtls_sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005171
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005172 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005173 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005176
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005177 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005183#endif /* MBEDTLS_SHA512_C */
5184#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005189
5190 /*
5191 * Free our handshake params
5192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193 mbedtls_ssl_handshake_free( ssl->handshake );
5194 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005195 ssl->handshake = NULL;
5196
5197 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005198 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005199 */
5200 if( ssl->transform )
5201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 mbedtls_ssl_transform_free( ssl->transform );
5203 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005204 }
5205 ssl->transform = ssl->transform_negotiate;
5206 ssl->transform_negotiate = NULL;
5207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005209}
5210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005211void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005212{
5213 int resume = ssl->handshake->resume;
5214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217#if defined(MBEDTLS_SSL_RENEGOTIATION)
5218 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005221 ssl->renego_records_seen = 0;
5222 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005223#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005224
5225 /*
5226 * Free the previous session and switch in the current one
5227 */
Paul Bakker0a597072012-09-25 21:55:46 +00005228 if( ssl->session )
5229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005231 /* RFC 7366 3.1: keep the EtM state */
5232 ssl->session_negotiate->encrypt_then_mac =
5233 ssl->session->encrypt_then_mac;
5234#endif
5235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236 mbedtls_ssl_session_free( ssl->session );
5237 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005238 }
5239 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005240 ssl->session_negotiate = NULL;
5241
Paul Bakker0a597072012-09-25 21:55:46 +00005242 /*
5243 * Add cache entry
5244 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005245 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005246 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005247 resume == 0 )
5248 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005249 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005251 }
Paul Bakker0a597072012-09-25 21:55:46 +00005252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005254 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005255 ssl->handshake->flight != NULL )
5256 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005257 /* Cancel handshake timer */
5258 ssl_set_timer( ssl, 0 );
5259
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005260 /* Keep last flight around in case we need to resend it:
5261 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005263 }
5264 else
5265#endif
5266 ssl_handshake_wrapup_free_hs_transform( ssl );
5267
Paul Bakker48916f92012-09-16 19:57:18 +00005268 ssl->state++;
5269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005271}
5272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005274{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005275 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005278
Paul Bakker92be97b2013-01-02 17:30:03 +01005279 /*
5280 * Set the out_msg pointer to the correct location based on IV length
5281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005283 {
5284 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5285 ssl->transform_negotiate->fixed_ivlen;
5286 }
5287 else
5288 ssl->out_msg = ssl->out_iv;
5289
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005290 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005294#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005295 ssl->verify_data_len = hash_len;
5296 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005297#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005298
Paul Bakker5121ce52009-01-03 21:22:43 +00005299 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5301 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005302
5303 /*
5304 * In case of session resuming, invert the client and server
5305 * ChangeCipherSpec messages order.
5306 */
Paul Bakker0a597072012-09-25 21:55:46 +00005307 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005310 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005314 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005315 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005317 }
5318 else
5319 ssl->state++;
5320
Paul Bakker48916f92012-09-16 19:57:18 +00005321 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005322 * Switch to our negotiated transform and session parameters for outbound
5323 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005328 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005329 {
5330 unsigned char i;
5331
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005332 /* Remember current epoch settings for resending */
5333 ssl->handshake->alt_transform_out = ssl->transform_out;
5334 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5335
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005336 /* Set sequence_number to zero */
5337 memset( ssl->out_ctr + 2, 0, 6 );
5338
5339 /* Increment epoch */
5340 for( i = 2; i > 0; i-- )
5341 if( ++ssl->out_ctr[i - 1] != 0 )
5342 break;
5343
5344 /* The loop goes to its end iff the counter is wrapping */
5345 if( i == 0 )
5346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5348 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005349 }
5350 }
5351 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005353 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005354
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005355 ssl->transform_out = ssl->transform_negotiate;
5356 ssl->session_out = ssl->session_negotiate;
5357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005358#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5359 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5364 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005365 }
5366 }
5367#endif
5368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005370 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005372#endif
5373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005377 return( ret );
5378 }
5379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005381
5382 return( 0 );
5383}
5384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005386#define SSL_MAX_HASH_LEN 36
5387#else
5388#define SSL_MAX_HASH_LEN 12
5389#endif
5390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005392{
Paul Bakker23986e52011-04-24 08:57:21 +00005393 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005394 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005395 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005398
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005399 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005404 return( ret );
5405 }
5406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5410 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005411 }
5412
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005413 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414#if defined(MBEDTLS_SSL_PROTO_SSL3)
5415 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005416 hash_len = 36;
5417 else
5418#endif
5419 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5422 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5425 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 }
5427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005429 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5432 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005433 }
5434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005436 ssl->verify_data_len = hash_len;
5437 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005438#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005439
Paul Bakker0a597072012-09-25 21:55:46 +00005440 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005443 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005445#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005446#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005447 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005449#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 }
5451 else
5452 ssl->state++;
5453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005455 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005457#endif
5458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005460
5461 return( 0 );
5462}
5463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5469 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5470 mbedtls_md5_init( &handshake->fin_md5 );
5471 mbedtls_sha1_init( &handshake->fin_sha1 );
5472 mbedtls_md5_starts( &handshake->fin_md5 );
5473 mbedtls_sha1_starts( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005474#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5476#if defined(MBEDTLS_SHA256_C)
5477 mbedtls_sha256_init( &handshake->fin_sha256 );
5478 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480#if defined(MBEDTLS_SHA512_C)
5481 mbedtls_sha512_init( &handshake->fin_sha512 );
5482 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005485
5486 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01005487
5488#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5489 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5490 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5491#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493#if defined(MBEDTLS_DHM_C)
5494 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005495#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496#if defined(MBEDTLS_ECDH_C)
5497 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005498#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005499
5500#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5501 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5502#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005503}
5504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005507 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5510 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 mbedtls_md_init( &transform->md_ctx_enc );
5513 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005514}
5515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005518 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005519}
5520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005522{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005523 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005524 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005526 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005528 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005530
5531 /*
5532 * Either the pointers are now NULL or cleared properly and can be freed.
5533 * Now allocate missing structures.
5534 */
5535 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005536 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005537 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005538 }
Paul Bakker48916f92012-09-16 19:57:18 +00005539
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005540 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005541 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005542 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005543 }
Paul Bakker48916f92012-09-16 19:57:18 +00005544
Paul Bakker82788fb2014-10-20 13:59:19 +02005545 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005546 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005547 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005548 }
Paul Bakker48916f92012-09-16 19:57:18 +00005549
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005550 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005551 if( ssl->handshake == NULL ||
5552 ssl->transform_negotiate == NULL ||
5553 ssl->session_negotiate == NULL )
5554 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 mbedtls_free( ssl->handshake );
5558 mbedtls_free( ssl->transform_negotiate );
5559 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005560
5561 ssl->handshake = NULL;
5562 ssl->transform_negotiate = NULL;
5563 ssl->session_negotiate = NULL;
5564
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005565 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005566 }
5567
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005568 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005570 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005571 ssl_handshake_params_init( ssl->handshake );
5572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5575 {
5576 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005577
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005578 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5579 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5580 else
5581 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005582
5583 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005584 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005585#endif
5586
Paul Bakker48916f92012-09-16 19:57:18 +00005587 return( 0 );
5588}
5589
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005590#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005591/* Dummy cookie callbacks for defaults */
5592static int ssl_cookie_write_dummy( void *ctx,
5593 unsigned char **p, unsigned char *end,
5594 const unsigned char *cli_id, size_t cli_id_len )
5595{
5596 ((void) ctx);
5597 ((void) p);
5598 ((void) end);
5599 ((void) cli_id);
5600 ((void) cli_id_len);
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005603}
5604
5605static int ssl_cookie_check_dummy( void *ctx,
5606 const unsigned char *cookie, size_t cookie_len,
5607 const unsigned char *cli_id, size_t cli_id_len )
5608{
5609 ((void) ctx);
5610 ((void) cookie);
5611 ((void) cookie_len);
5612 ((void) cli_id);
5613 ((void) cli_id_len);
5614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005616}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005617#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005618
Paul Bakker5121ce52009-01-03 21:22:43 +00005619/*
5620 * Initialize an SSL context
5621 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005622void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5623{
5624 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5625}
5626
5627/*
5628 * Setup an SSL context
5629 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005630int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005631 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005632{
Paul Bakker48916f92012-09-16 19:57:18 +00005633 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005634 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005635
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005636 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005637
5638 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005639 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005640 */
k-stachowiak4772a1f2018-07-09 10:43:37 +02005641 ssl->in_buf = NULL;
5642 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005643 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5644 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005645 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak83f9fba2018-07-31 17:13:26 +02005647 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiak4772a1f2018-07-09 10:43:37 +02005648 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005649 }
5650
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005651#if defined(MBEDTLS_SSL_PROTO_DTLS)
5652 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5653 {
5654 ssl->out_hdr = ssl->out_buf;
5655 ssl->out_ctr = ssl->out_buf + 3;
5656 ssl->out_len = ssl->out_buf + 11;
5657 ssl->out_iv = ssl->out_buf + 13;
5658 ssl->out_msg = ssl->out_buf + 13;
5659
5660 ssl->in_hdr = ssl->in_buf;
5661 ssl->in_ctr = ssl->in_buf + 3;
5662 ssl->in_len = ssl->in_buf + 11;
5663 ssl->in_iv = ssl->in_buf + 13;
5664 ssl->in_msg = ssl->in_buf + 13;
5665 }
5666 else
5667#endif
5668 {
5669 ssl->out_ctr = ssl->out_buf;
5670 ssl->out_hdr = ssl->out_buf + 8;
5671 ssl->out_len = ssl->out_buf + 11;
5672 ssl->out_iv = ssl->out_buf + 13;
5673 ssl->out_msg = ssl->out_buf + 13;
5674
5675 ssl->in_ctr = ssl->in_buf;
5676 ssl->in_hdr = ssl->in_buf + 8;
5677 ssl->in_len = ssl->in_buf + 11;
5678 ssl->in_iv = ssl->in_buf + 13;
5679 ssl->in_msg = ssl->in_buf + 13;
5680 }
5681
Paul Bakker48916f92012-09-16 19:57:18 +00005682 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiak4772a1f2018-07-09 10:43:37 +02005683 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005684
5685 return( 0 );
k-stachowiak4772a1f2018-07-09 10:43:37 +02005686
5687error:
5688 mbedtls_free( ssl->in_buf );
5689 mbedtls_free( ssl->out_buf );
5690
5691 ssl->conf = NULL;
5692
5693 ssl->in_buf = NULL;
5694 ssl->out_buf = NULL;
5695
5696 ssl->in_hdr = NULL;
5697 ssl->in_ctr = NULL;
5698 ssl->in_len = NULL;
5699 ssl->in_iv = NULL;
5700 ssl->in_msg = NULL;
5701
5702 ssl->out_hdr = NULL;
5703 ssl->out_ctr = NULL;
5704 ssl->out_len = NULL;
5705 ssl->out_iv = NULL;
5706 ssl->out_msg = NULL;
5707
k-stachowiak83f9fba2018-07-31 17:13:26 +02005708 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005709}
5710
5711/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005712 * Reset an initialized and used SSL context for re-use while retaining
5713 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005714 *
5715 * If partial is non-zero, keep data in the input buffer and client ID.
5716 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00005717 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005718static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005719{
Paul Bakker48916f92012-09-16 19:57:18 +00005720 int ret;
5721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005723
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005724 /* Cancel any possibly running timer */
5725 ssl_set_timer( ssl, 0 );
5726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_RENEGOTIATION)
5728 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005729 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005730
5731 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
5733 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005734#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005736
Paul Bakker7eb013f2011-10-06 12:37:39 +00005737 ssl->in_offt = NULL;
5738
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005739 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005740 ssl->in_msgtype = 0;
5741 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005742 if( partial == 0 )
5743 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005745 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005746 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005749 ssl_dtls_replay_reset( ssl );
5750#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005751
5752 ssl->in_hslen = 0;
5753 ssl->nb_zero = 0;
Hanno Becker704f4932017-06-08 13:08:45 +01005754 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005755
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005756 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005757 ssl->out_msgtype = 0;
5758 ssl->out_msglen = 0;
5759 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
5761 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005762 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005763#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005764
Paul Bakker48916f92012-09-16 19:57:18 +00005765 ssl->transform_in = NULL;
5766 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005767
Hanno Becker3328d8c2018-08-13 16:35:15 +01005768 ssl->session_in = NULL;
5769 ssl->session_out = NULL;
5770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005772 if( partial == 0 )
5773 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5776 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
5779 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
5782 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005783 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005784 }
5785#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005786
Paul Bakker48916f92012-09-16 19:57:18 +00005787 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 mbedtls_ssl_transform_free( ssl->transform );
5790 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005791 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005792 }
Paul Bakker48916f92012-09-16 19:57:18 +00005793
Paul Bakkerc0463502013-02-14 11:19:38 +01005794 if( ssl->session )
5795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796 mbedtls_ssl_session_free( ssl->session );
5797 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005798 ssl->session = NULL;
5799 }
5800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005802 ssl->alpn_chosen = NULL;
5803#endif
5804
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005805#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005806 if( partial == 0 )
5807 {
5808 mbedtls_free( ssl->cli_id );
5809 ssl->cli_id = NULL;
5810 ssl->cli_id_len = 0;
5811 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005812#endif
5813
Paul Bakker48916f92012-09-16 19:57:18 +00005814 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5815 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005816
5817 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005818}
5819
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005820/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005821 * Reset an initialized and used SSL context for re-use while retaining
5822 * all application-set variables, function pointers and data.
5823 */
5824int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
5825{
5826 return( ssl_session_reset_int( ssl, 0 ) );
5827}
5828
5829/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 * SSL set accessors
5831 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005832void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00005833{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005834 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00005835}
5836
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02005837void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005838{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005839 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005840}
5841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005843void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005844{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005845 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005846}
5847#endif
5848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005850void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005851{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005852 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005853}
5854#endif
5855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005857void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005858{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005859 conf->hs_timeout_min = min;
5860 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005861}
5862#endif
5863
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005864void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00005865{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005866 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00005867}
5868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005870void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005871 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005872 void *p_vrfy )
5873{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005874 conf->f_vrfy = f_vrfy;
5875 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005878
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005879void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005880 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005881 void *p_rng )
5882{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01005883 conf->f_rng = f_rng;
5884 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00005885}
5886
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005887void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02005888 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 void *p_dbg )
5890{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005891 conf->f_dbg = f_dbg;
5892 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00005893}
5894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005895void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005896 void *p_bio,
5897 int (*f_send)(void *, const unsigned char *, size_t),
5898 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005899 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005900{
5901 ssl->p_bio = p_bio;
5902 ssl->f_send = f_send;
5903 ssl->f_recv = f_recv;
5904 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005905}
5906
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005907void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005908{
5909 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005910}
5911
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005912void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
5913 void *p_timer,
5914 void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms),
5915 int (*f_get_timer)(void *) )
5916{
5917 ssl->p_timer = p_timer;
5918 ssl->f_set_timer = f_set_timer;
5919 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005920
5921 /* Make sure we start with no timer running */
5922 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005923}
5924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005926void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005927 void *p_cache,
5928 int (*f_get_cache)(void *, mbedtls_ssl_session *),
5929 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005930{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005931 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005932 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005933 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937#if defined(MBEDTLS_SSL_CLI_C)
5938int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005939{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005940 int ret;
5941
5942 if( ssl == NULL ||
5943 session == NULL ||
5944 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005945 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005948 }
5949
5950 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5951 return( ret );
5952
Paul Bakker0a597072012-09-25 21:55:46 +00005953 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005954
5955 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005957#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005958
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005959void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005960 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005961{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005962 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
5963 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
5964 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
5965 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005966}
5967
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005968void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005969 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005970 int major, int minor )
5971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005973 return;
5974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005976 return;
5977
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005978 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005979}
5980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005982void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01005983 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005984{
5985 conf->cert_profile = profile;
5986}
5987
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005988/* Append a new keycert entry to a (possibly empty) list */
5989static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
5990 mbedtls_x509_crt *cert,
5991 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005992{
niisato8ba6ff52018-06-25 19:05:48 +09005993 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005994
niisato8ba6ff52018-06-25 19:05:48 +09005995 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
5996 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005997 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005998
niisato8ba6ff52018-06-25 19:05:48 +09005999 new_cert->cert = cert;
6000 new_cert->key = key;
6001 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006002
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006003 /* Update head is the list was null, else add to the end */
6004 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006005 {
niisato8ba6ff52018-06-25 19:05:48 +09006006 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006007 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006008 else
6009 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006010 mbedtls_ssl_key_cert *cur = *head;
6011 while( cur->next != NULL )
6012 cur = cur->next;
niisato8ba6ff52018-06-25 19:05:48 +09006013 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006014 }
6015
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006016 return( 0 );
6017}
6018
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006019int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006020 mbedtls_x509_crt *own_cert,
6021 mbedtls_pk_context *pk_key )
6022{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006023 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006024}
6025
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006026void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006027 mbedtls_x509_crt *ca_chain,
6028 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006029{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006030 conf->ca_chain = ca_chain;
6031 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006032}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006034
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006035#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6036int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6037 mbedtls_x509_crt *own_cert,
6038 mbedtls_pk_context *pk_key )
6039{
6040 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6041 own_cert, pk_key ) );
6042}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006043
6044void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6045 mbedtls_x509_crt *ca_chain,
6046 mbedtls_x509_crl *ca_crl )
6047{
6048 ssl->handshake->sni_ca_chain = ca_chain;
6049 ssl->handshake->sni_ca_crl = ca_crl;
6050}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006051
6052void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6053 int authmode )
6054{
6055 ssl->handshake->sni_authmode = authmode;
6056}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006057#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006060int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006061 const unsigned char *psk, size_t psk_len,
6062 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006063{
Paul Bakker6db455e2013-09-18 17:29:31 +02006064 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006069
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006070 /* Identity len will be encoded on two bytes */
6071 if( ( psk_identity_len >> 16 ) != 0 ||
6072 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6073 {
6074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6075 }
6076
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006077 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006078 {
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006079 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006080
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006081 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnardffb81802015-10-20 19:56:45 +02006082 conf->psk = NULL;
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006083 conf->psk_len = 0;
6084 }
6085 if( conf->psk_identity != NULL )
6086 {
6087 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnardffb81802015-10-20 19:56:45 +02006088 conf->psk_identity = NULL;
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006089 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006090 }
6091
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006092 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6093 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006094 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006095 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006096 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006097 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006098 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006099 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006100 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006101
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006102 conf->psk_len = psk_len;
6103 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006104
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006105 memcpy( conf->psk, psk, conf->psk_len );
6106 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006107
6108 return( 0 );
6109}
6110
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006111int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6112 const unsigned char *psk, size_t psk_len )
6113{
6114 if( psk == NULL || ssl->handshake == NULL )
6115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6116
6117 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6119
6120 if( ssl->handshake->psk != NULL )
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006121 {
6122 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006123 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006124 ssl->handshake->psk_len = 0;
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006125 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006126
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006127 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006128 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006129
6130 ssl->handshake->psk_len = psk_len;
6131 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6132
6133 return( 0 );
6134}
6135
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006136void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006138 size_t),
6139 void *p_psk )
6140{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006141 conf->f_psk = f_psk;
6142 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006145
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006146#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006147int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006148{
6149 int ret;
6150
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006151 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6152 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6153 {
6154 mbedtls_mpi_free( &conf->dhm_P );
6155 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006156 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006157 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006158
6159 return( 0 );
6160}
6161
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006162int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006163{
6164 int ret;
6165
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006166 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6167 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6168 {
6169 mbedtls_mpi_free( &conf->dhm_P );
6170 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006171 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006172 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006173
6174 return( 0 );
6175}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006176#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006177
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006178#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6179/*
6180 * Set the minimum length for Diffie-Hellman parameters
6181 */
6182void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6183 unsigned int bitlen )
6184{
6185 conf->dhm_min_bitlen = bitlen;
6186}
6187#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6188
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02006189#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006190/*
6191 * Set allowed/preferred hashes for handshake signatures
6192 */
6193void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6194 const int *hashes )
6195{
6196 conf->sig_hashes = hashes;
6197}
Hanno Becker593b0d32017-04-07 13:25:49 +01006198#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006199
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006200#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006201/*
6202 * Set the allowed elliptic curves
6203 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006204void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006205 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006206{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006207 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006208}
Hanno Becker593b0d32017-04-07 13:25:49 +01006209#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006210
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006211#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006213{
Hanno Becker593b0d32017-04-07 13:25:49 +01006214 /* Initialize to suppress unnecessary compiler warning */
6215 size_t hostname_len = 0;
6216
6217 /* Check if new hostname is valid before
6218 * making any change to current one */
6219
6220 if( hostname != NULL )
6221 {
6222 hostname_len = strlen( hostname );
6223
6224 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6226 }
6227
6228 /* Now it's clear that we will overwrite the old hostname,
6229 * so we can free it safely */
6230
6231 if( ssl->hostname != NULL )
6232 {
6233 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6234 mbedtls_free( ssl->hostname );
6235 }
6236
6237 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006238
Paul Bakker5121ce52009-01-03 21:22:43 +00006239 if( hostname == NULL )
Hanno Becker593b0d32017-04-07 13:25:49 +01006240 {
6241 ssl->hostname = NULL;
6242 }
6243 else
6244 {
6245 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006246
Hanno Becker593b0d32017-04-07 13:25:49 +01006247 if( ssl->hostname == NULL )
6248 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006249
Hanno Becker593b0d32017-04-07 13:25:49 +01006250 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006251
Hanno Becker593b0d32017-04-07 13:25:49 +01006252 ssl->hostname[hostname_len] = '\0';
6253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006254
6255 return( 0 );
6256}
Hanno Beckerc7845e52017-04-07 13:02:16 +01006257#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006258
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006259#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006260void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006262 const unsigned char *, size_t),
6263 void *p_sni )
6264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006265 conf->f_sni = f_sni;
6266 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006267}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006271int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006272{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006273 size_t cur_len, tot_len;
6274 const char **p;
6275
6276 /*
Brian J Murraye7f8dc32016-11-06 04:45:15 -08006277 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6278 * MUST NOT be truncated."
6279 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006280 */
6281 tot_len = 0;
6282 for( p = protos; *p != NULL; p++ )
6283 {
6284 cur_len = strlen( *p );
6285 tot_len += cur_len;
6286
6287 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006289 }
6290
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006291 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006292
6293 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006294}
6295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006297{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006298 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006299}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006301
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006302void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006303{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006304 conf->max_major_ver = major;
6305 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006306}
6307
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006308void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006309{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006310 conf->min_major_ver = major;
6311 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006312}
6313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006315void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006316{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006317 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006318}
6319#endif
6320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006322void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006323{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006324 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006325}
6326#endif
6327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006329void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006330{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006331 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006332}
6333#endif
6334
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006335#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006336void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006337{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006338 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006339}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006340#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006343int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6346 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006349 }
6350
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006351 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006352
6353 return( 0 );
6354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006358void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006359{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006360 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006365void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006366{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006367 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006368}
6369#endif
6370
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006371void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006373 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006374}
6375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006377void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006378{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006379 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006380}
6381
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006382void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006383{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006384 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006385}
6386
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006387void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006388 const unsigned char period[8] )
6389{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006390 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006392#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006395#if defined(MBEDTLS_SSL_CLI_C)
6396void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006397{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006398 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006399}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006400#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006401
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006402#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006403void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6404 mbedtls_ssl_ticket_write_t *f_ticket_write,
6405 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6406 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006407{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006408 conf->f_ticket_write = f_ticket_write;
6409 conf->f_ticket_parse = f_ticket_parse;
6410 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006411}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006414
Paul Bakker5121ce52009-01-03 21:22:43 +00006415/*
6416 * SSL get accessors
6417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006419{
6420 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6421}
6422
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006423uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006424{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006425 if( ssl->session != NULL )
6426 return( ssl->session->verify_result );
6427
6428 if( ssl->session_negotiate != NULL )
6429 return( ssl->session_negotiate->verify_result );
6430
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006431 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006432}
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006435{
Paul Bakker926c8e42013-03-06 10:23:34 +01006436 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006437 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006440}
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006446 {
6447 switch( ssl->minor_ver )
6448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006450 return( "DTLSv1.0" );
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006453 return( "DTLSv1.2" );
6454
6455 default:
6456 return( "unknown (DTLS)" );
6457 }
6458 }
6459#endif
6460
Paul Bakker43ca69c2011-01-15 17:35:19 +00006461 switch( ssl->minor_ver )
6462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006464 return( "SSLv3.0" );
6465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006467 return( "TLSv1.0" );
6468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006470 return( "TLSv1.1" );
6471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006473 return( "TLSv1.2" );
6474
Paul Bakker43ca69c2011-01-15 17:35:19 +00006475 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006476 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006477 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006478}
6479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006481{
Hanno Becker42d267b2018-08-17 15:28:19 +01006482 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker07eb7ca2018-08-03 09:40:07 +01006484 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006485
Hanno Becker3328d8c2018-08-13 16:35:15 +01006486 if( transform == NULL )
6487 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489#if defined(MBEDTLS_ZLIB_SUPPORT)
6490 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6491 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006492#endif
6493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 case MBEDTLS_MODE_GCM:
6497 case MBEDTLS_MODE_CCM:
6498 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006499 transform_expansion = transform->minlen;
6500 break;
6501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 case MBEDTLS_MODE_CBC:
Hanno Becker07eb7ca2018-08-03 09:40:07 +01006503
6504 block_size = mbedtls_cipher_get_block_size(
6505 &transform->cipher_ctx_enc );
6506
Hanno Becker42d267b2018-08-17 15:28:19 +01006507 /* Expansion due to the addition of the MAC. */
6508 transform_expansion += transform->maclen;
6509
6510 /* Expansion due to the addition of CBC padding;
6511 * Theoretically up to 256 bytes, but we never use
6512 * more than the block size of the underlying cipher. */
6513 transform_expansion += block_size;
6514
6515 /* For TLS 1.1 or higher, an explicit IV is added
6516 * after the record header. */
Hanno Becker07eb7ca2018-08-03 09:40:07 +01006517#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6518 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker42d267b2018-08-17 15:28:19 +01006519 transform_expansion += block_size;
Hanno Becker07eb7ca2018-08-03 09:40:07 +01006520#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker42d267b2018-08-17 15:28:19 +01006521
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006522 break;
6523
6524 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006527 }
6528
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006529 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006530}
6531
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006532#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6533size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6534{
6535 size_t max_len;
6536
6537 /*
6538 * Assume mfl_code is correct since it was checked when set
6539 */
6540 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6541
6542 /*
6543 * Check if a smaller max length was negotiated
6544 */
6545 if( ssl->session_out != NULL &&
6546 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6547 {
6548 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6549 }
6550
6551 return max_len;
6552}
6553#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555#if defined(MBEDTLS_X509_CRT_PARSE_C)
6556const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006557{
6558 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006559 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006560
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006561 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565#if defined(MBEDTLS_SSL_CLI_C)
6566int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006567{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006568 if( ssl == NULL ||
6569 dst == NULL ||
6570 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006571 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006574 }
6575
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006576 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006579
Paul Bakker5121ce52009-01-03 21:22:43 +00006580/*
Paul Bakker1961b702013-01-25 14:49:24 +01006581 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006583int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006584{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006585 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006586
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006587 if( ssl == NULL || ssl->conf == NULL )
6588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006597#endif
6598
Paul Bakker1961b702013-01-25 14:49:24 +01006599 return( ret );
6600}
6601
6602/*
6603 * Perform the SSL handshake
6604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006606{
6607 int ret = 0;
6608
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006609 if( ssl == NULL || ssl->conf == NULL )
6610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006617
6618 if( ret != 0 )
6619 break;
6620 }
6621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006623
6624 return( ret );
6625}
6626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006627#if defined(MBEDTLS_SSL_RENEGOTIATION)
6628#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006629/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006630 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006633{
6634 int ret;
6635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006637
6638 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6640 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006645 return( ret );
6646 }
6647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006649
6650 return( 0 );
6651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006653
6654/*
6655 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 * - any side: calling mbedtls_ssl_renegotiate(),
6657 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
6658 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02006659 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006660 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006664{
6665 int ret;
6666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006668
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006669 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6670 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006671
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006672 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
6673 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006677 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006678 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006679 ssl->handshake->out_msg_seq = 1;
6680 else
6681 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006682 }
6683#endif
6684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
6686 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00006687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006691 return( ret );
6692 }
6693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006695
6696 return( 0 );
6697}
6698
6699/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006700 * Renegotiate current connection on client,
6701 * or request renegotiation on server
6702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006704{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006706
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006707 if( ssl == NULL || ssl->conf == NULL )
6708 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006711 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006712 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6715 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006718
6719 /* Did we already try/start sending HelloRequest? */
6720 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006722
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006723 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006724 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006728 /*
6729 * On client, either start the renegotiation process or,
6730 * if already in progress, continue the handshake
6731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006732 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6735 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006736
6737 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006740 return( ret );
6741 }
6742 }
6743 else
6744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006747 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006748 return( ret );
6749 }
6750 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006752
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006753 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006754}
6755
6756/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006757 * Check record counters and renegotiate if they're above the limit.
6758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006760{
Andres AG7fa66d42016-12-15 17:01:16 +00006761 size_t ep_len = ssl_ep_len( ssl );
6762 int in_ctr_cmp;
6763 int out_ctr_cmp;
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
6766 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006767 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006768 {
6769 return( 0 );
6770 }
6771
Andres AG7fa66d42016-12-15 17:01:16 +00006772 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
6773 ssl->conf->renego_period + ep_len, 8 - ep_len );
6774 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
6775 ssl->conf->renego_period + ep_len, 8 - ep_len );
6776
6777 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006778 {
6779 return( 0 );
6780 }
6781
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006786
6787/*
6788 * Receive application data decrypted from the SSL layer
6789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006791{
Hanno Becker6a582e82017-06-08 13:38:05 +01006792 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006793 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006794
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006795 if( ssl == NULL || ssl->conf == NULL )
6796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006801 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006804 return( ret );
6805
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006806 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006810 return( ret );
6811 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006812 }
6813#endif
6814
Hanno Becker6a582e82017-06-08 13:38:05 +01006815 /*
6816 * Check if renegotiation is necessary and/or handshake is
6817 * in process. If yes, perform/continue, and fall through
6818 * if an unexpected packet is received while the client
6819 * is waiting for the ServerHello.
6820 *
6821 * (There is no equivalent to the last condition on
6822 * the server-side as it is not treated as within
6823 * a handshake while waiting for the ClientHello
6824 * after a renegotiation request.)
6825 */
6826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker6a582e82017-06-08 13:38:05 +01006828 ret = ssl_check_ctr_renegotiate( ssl );
6829 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6830 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006833 return( ret );
6834 }
6835#endif
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker6a582e82017-06-08 13:38:05 +01006840 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6841 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 return( ret );
6845 }
6846 }
6847
6848 if( ssl->in_offt == NULL )
6849 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006850 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006851 if( ssl->f_get_timer != NULL &&
6852 ssl->f_get_timer( ssl->p_timer ) == -1 )
6853 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006854 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006855 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006856
Hanno Becker6a582e82017-06-08 13:38:05 +01006857 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006858 {
Hanno Becker6a582e82017-06-08 13:38:05 +01006859 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
6860 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006861
Hanno Becker6a582e82017-06-08 13:38:05 +01006862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6863 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006864 }
6865
6866 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006868 {
6869 /*
6870 * OpenSSL sends empty messages to randomize the IV
6871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00006875 return( 0 );
6876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006878 return( ret );
6879 }
6880 }
6881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00006883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006887 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
6889 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006892
6893 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006895 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006896 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006897#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006899 }
Hanno Becker6a582e82017-06-08 13:38:05 +01006900#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006901
Hanno Becker6a582e82017-06-08 13:38:05 +01006902#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006903 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006907
6908 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006911 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006912#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00006914 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006915#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006916
Hanno Becker3cd07be2017-10-24 11:49:19 +01006917#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckere454d732017-10-24 11:47:37 +01006918 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
6919 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
6920 ssl->conf->allow_legacy_renegotiation ==
6921 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
6922 {
6923 /* DTLS clients need to know renego is server-initiated */
6924#if defined(MBEDTLS_SSL_PROTO_DTLS)
6925 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6926 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6927 {
6928 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
6929 }
6930#endif
6931 ret = ssl_start_renegotiation( ssl );
6932 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6933 ret != 0 )
6934 {
6935 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6936 return( ret );
6937 }
6938 }
6939 else
Hanno Becker3cd07be2017-10-24 11:49:19 +01006940#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00006941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944#if defined(MBEDTLS_SSL_PROTO_SSL3)
6945 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006946 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006947 /*
6948 * SSLv3 does not have a "no_renegotiation" alert
6949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006951 return( ret );
6952 }
6953 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6955#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6956 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6957 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6960 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
6961 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006962 {
6963 return( ret );
6964 }
Paul Bakker48916f92012-09-16 19:57:18 +00006965 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006966 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
6968 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6971 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006972 }
Paul Bakker48916f92012-09-16 19:57:18 +00006973 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006974
Hanno Becker6a582e82017-06-08 13:38:05 +01006975 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006976 }
Hanno Becker3cd07be2017-10-24 11:49:19 +01006977#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006979 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006980 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006981 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006982 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006985 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006987 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006988 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
6993 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006996 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006997 }
6998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7002 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003 }
7004
7005 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007006
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007007 /* We're going to return something now, cancel timer,
7008 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007010 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007011
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007012#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007013 /* If we requested renego but received AppData, resend HelloRequest.
7014 * Do it now, after setting in_offt, to avoid taking this branch
7015 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007016#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007017 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007019 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007020 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007023 return( ret );
7024 }
7025 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02007027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007028 }
7029
7030 n = ( len < ssl->in_msglen )
7031 ? len : ssl->in_msglen;
7032
7033 memcpy( buf, ssl->in_offt, n );
7034 ssl->in_msglen -= n;
7035
7036 if( ssl->in_msglen == 0 )
Hanno Beckercc019082017-06-09 10:51:37 +01007037 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007038 /* all bytes consumed */
7039 ssl->in_offt = NULL;
Hanno Beckercc019082017-06-09 10:51:37 +01007040 ssl->keep_current_message = 0;
7041 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007042 else
7043 /* more data available */
7044 ssl->in_offt += n;
7045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007047
Paul Bakker23986e52011-04-24 08:57:21 +00007048 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007049}
7050
7051/*
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007052 * Send application data to be encrypted by the SSL layer, taking care of max
7053 * fragment length and buffer size.
7054 *
7055 * According to RFC 5246 Section 6.2.1:
7056 *
7057 * Zero-length fragments of Application data MAY be sent as they are
7058 * potentially useful as a traffic analysis countermeasure.
7059 *
7060 * Therefore, it is possible that the input message length is 0 and the
7061 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007062 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007063static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007064 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007065{
Paul Bakker23986e52011-04-24 08:57:21 +00007066 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007068 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florina3604112017-07-22 09:01:44 +02007069#else
7070 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7071#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007072 if( len > max_len )
7073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007075 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007078 "maximum fragment length: %d > %d",
7079 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007081 }
7082 else
7083#endif
7084 len = max_len;
7085 }
Paul Bakker887bd502011-06-08 13:10:54 +00007086
Paul Bakker5121ce52009-01-03 21:22:43 +00007087 if( ssl->out_left != 0 )
7088 {
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007089 /*
7090 * The user has previously tried to send the data and
7091 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7092 * written. In this case, we expect the high-level write function
7093 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007098 return( ret );
7099 }
7100 }
Paul Bakker887bd502011-06-08 13:10:54 +00007101 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007102 {
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007103 /*
7104 * The user is trying to send a message the first time, so we need to
7105 * copy the data into the internal buffers and setup the data structure
7106 * to keep track of partial writes
7107 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007108 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007110 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007115 return( ret );
7116 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007117 }
7118
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007119 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007120}
7121
7122/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007123 * Write application data, doing 1/n-1 splitting if necessary.
7124 *
7125 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007126 * then the caller will call us again with the same arguments, so
Hanno Beckere298c8b2017-09-18 14:58:11 +01007127 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007130static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007131 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007132{
7133 int ret;
7134
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007135 if( ssl->conf->cbc_record_splitting ==
7136 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007137 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7139 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7140 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007141 {
7142 return( ssl_write_real( ssl, buf, len ) );
7143 }
7144
7145 if( ssl->split_done == 0 )
7146 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007147 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007148 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007149 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007150 }
7151
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007152 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7153 return( ret );
7154 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007155
7156 return( ret + 1 );
7157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007159
7160/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007161 * Write application data (public-facing wrapper)
7162 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007163int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007164{
7165 int ret;
7166
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007168
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007169 if( ssl == NULL || ssl->conf == NULL )
7170 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7171
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007172#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007173 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7174 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007175 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007176 return( ret );
7177 }
7178#endif
7179
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007180 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007181 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007182 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007183 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007185 return( ret );
7186 }
7187 }
7188
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007189#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007190 ret = ssl_write_split( ssl, buf, len );
7191#else
7192 ret = ssl_write_real( ssl, buf, len );
7193#endif
7194
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007196
7197 return( ret );
7198}
7199
7200/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007201 * Notify the peer that the connection is being closed
7202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007204{
7205 int ret;
7206
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007207 if( ssl == NULL || ssl->conf == NULL )
7208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007211
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007212 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7218 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7219 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007222 return( ret );
7223 }
7224 }
7225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007227
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007228 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007229}
7230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007232{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007233 if( transform == NULL )
7234 return;
7235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007237 deflateEnd( &transform->ctx_deflate );
7238 inflateEnd( &transform->ctx_inflate );
7239#endif
7240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7242 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 mbedtls_md_free( &transform->md_ctx_enc );
7245 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007248}
7249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250#if defined(MBEDTLS_X509_CRT_PARSE_C)
7251static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007254
7255 while( cur != NULL )
7256 {
7257 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007259 cur = next;
7260 }
7261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007265{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007266 if( handshake == NULL )
7267 return;
7268
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007269#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7270 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7271 mbedtls_md5_free( &handshake->fin_md5 );
7272 mbedtls_sha1_free( &handshake->fin_sha1 );
7273#endif
7274#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7275#if defined(MBEDTLS_SHA256_C)
7276 mbedtls_sha256_free( &handshake->fin_sha256 );
7277#endif
7278#if defined(MBEDTLS_SHA512_C)
7279 mbedtls_sha512_free( &handshake->fin_sha512 );
7280#endif
7281#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283#if defined(MBEDTLS_DHM_C)
7284 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007285#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286#if defined(MBEDTLS_ECDH_C)
7287 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007288#endif
7289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02007291 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007293#endif
7294
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007295#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7296 if( handshake->psk != NULL )
7297 {
7298 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7299 mbedtls_free( handshake->psk );
7300 }
7301#endif
7302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7304 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007305 /*
7306 * Free only the linked list wrapper, not the keys themselves
7307 * since the belong to the SNI callback
7308 */
7309 if( handshake->sni_key_cert != NULL )
7310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007312
7313 while( cur != NULL )
7314 {
7315 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007317 cur = next;
7318 }
7319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_SSL_PROTO_DTLS)
7323 mbedtls_free( handshake->verify_cookie );
7324 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007325 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007326#endif
7327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007328 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007329}
7330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007332{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007333 if( session == NULL )
7334 return;
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007337 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339 mbedtls_x509_crt_free( session->peer_cert );
7340 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007341 }
Paul Bakkered27a042013-04-18 22:46:23 +02007342#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007343
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007344#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007346#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007349}
7350
Paul Bakker5121ce52009-01-03 21:22:43 +00007351/*
7352 * Free an SSL context
7353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007355{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007356 if( ssl == NULL )
7357 return;
7358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007361 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7364 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007365 }
7366
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007367 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7370 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007371 }
7372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007374 if( ssl->compress_buf != NULL )
7375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7377 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007378 }
7379#endif
7380
Paul Bakker48916f92012-09-16 19:57:18 +00007381 if( ssl->transform )
7382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 mbedtls_ssl_transform_free( ssl->transform );
7384 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007385 }
7386
7387 if( ssl->handshake )
7388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 mbedtls_ssl_handshake_free( ssl->handshake );
7390 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7391 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 mbedtls_free( ssl->handshake );
7394 mbedtls_free( ssl->transform_negotiate );
7395 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007396 }
7397
Paul Bakkerc0463502013-02-14 11:19:38 +01007398 if( ssl->session )
7399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 mbedtls_ssl_session_free( ssl->session );
7401 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007402 }
7403
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007404#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007405 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007406 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007407 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007409 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007410#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7413 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7416 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007417 }
7418#endif
7419
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007420#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007422#endif
7423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007425
Paul Bakker86f04f42013-02-14 11:20:09 +01007426 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007428}
7429
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007430/*
7431 * Initialze mbedtls_ssl_config
7432 */
7433void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7434{
7435 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7436}
7437
Simon Butcherc941b6c2015-12-28 01:29:10 +00007438#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007439static int ssl_preset_default_hashes[] = {
7440#if defined(MBEDTLS_SHA512_C)
7441 MBEDTLS_MD_SHA512,
7442 MBEDTLS_MD_SHA384,
7443#endif
7444#if defined(MBEDTLS_SHA256_C)
7445 MBEDTLS_MD_SHA256,
7446 MBEDTLS_MD_SHA224,
7447#endif
Gilles Peskine7344e1b2017-05-12 13:16:40 +02007448#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007449 MBEDTLS_MD_SHA1,
7450#endif
7451 MBEDTLS_MD_NONE
7452};
Simon Butcherc941b6c2015-12-28 01:29:10 +00007453#endif
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007454
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007455static int ssl_preset_suiteb_ciphersuites[] = {
7456 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7457 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7458 0
7459};
7460
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007461#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007462static int ssl_preset_suiteb_hashes[] = {
7463 MBEDTLS_MD_SHA256,
7464 MBEDTLS_MD_SHA384,
7465 MBEDTLS_MD_NONE
7466};
7467#endif
7468
7469#if defined(MBEDTLS_ECP_C)
7470static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
7471 MBEDTLS_ECP_DP_SECP256R1,
7472 MBEDTLS_ECP_DP_SECP384R1,
7473 MBEDTLS_ECP_DP_NONE
7474};
7475#endif
7476
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007477/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007478 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007479 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007480int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007481 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007482{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007483#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007484 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007485#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007486
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007487 /* Use the functions here so that they are covered in tests,
7488 * but otherwise access member directly for efficiency */
7489 mbedtls_ssl_conf_endpoint( conf, endpoint );
7490 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007491
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007492 /*
7493 * Things that are common to all presets
7494 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007495#if defined(MBEDTLS_SSL_CLI_C)
7496 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7497 {
7498 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7499#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7500 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7501#endif
7502 }
7503#endif
7504
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007505#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007506 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007507#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007508
7509#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7510 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7511#endif
7512
7513#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7514 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7515#endif
7516
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007517#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7518 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7519#endif
7520
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007521#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007522 conf->f_cookie_write = ssl_cookie_write_dummy;
7523 conf->f_cookie_check = ssl_cookie_check_dummy;
7524#endif
7525
7526#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7527 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7528#endif
7529
7530#if defined(MBEDTLS_SSL_PROTO_DTLS)
7531 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7532 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7533#endif
7534
7535#if defined(MBEDTLS_SSL_RENEGOTIATION)
7536 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG7fa66d42016-12-15 17:01:16 +00007537 memset( conf->renego_period, 0x00, 2 );
7538 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007539#endif
7540
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007541#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7542 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7543 {
7544 if( ( ret = mbedtls_ssl_conf_dh_param( conf,
Hanno Becker80e0d462017-10-13 16:51:54 +01007545 MBEDTLS_DHM_RFC3526_MODP_2048_P,
7546 MBEDTLS_DHM_RFC3526_MODP_2048_G ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007547 {
7548 return( ret );
7549 }
7550 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007551#endif
7552
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007553 /*
7554 * Preset-specific defaults
7555 */
7556 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007557 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007558 /*
7559 * NSA Suite B
7560 */
7561 case MBEDTLS_SSL_PRESET_SUITEB:
7562 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7563 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7564 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7565 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7566
7567 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7568 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7569 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7570 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7571 ssl_preset_suiteb_ciphersuites;
7572
7573#if defined(MBEDTLS_X509_CRT_PARSE_C)
7574 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007575#endif
7576
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007577#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007578 conf->sig_hashes = ssl_preset_suiteb_hashes;
7579#endif
7580
7581#if defined(MBEDTLS_ECP_C)
7582 conf->curve_list = ssl_preset_suiteb_curves;
7583#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007584 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007585
7586 /*
7587 * Default
7588 */
7589 default:
Ron Eldor1ac9aa72017-05-28 10:46:38 +03007590 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7591 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7592 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7593 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7594 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7595 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
7596 MBEDTLS_SSL_MIN_MINOR_VERSION :
7597 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007598 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7599 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7600
7601#if defined(MBEDTLS_SSL_PROTO_DTLS)
7602 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7603 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
7604#endif
7605
7606 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7607 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7608 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7609 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7610 mbedtls_ssl_list_ciphersuites();
7611
7612#if defined(MBEDTLS_X509_CRT_PARSE_C)
7613 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7614#endif
7615
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007616#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007617 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007618#endif
7619
7620#if defined(MBEDTLS_ECP_C)
7621 conf->curve_list = mbedtls_ecp_grp_id_list();
7622#endif
7623
7624#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7625 conf->dhm_min_bitlen = 1024;
7626#endif
7627 }
7628
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007629 return( 0 );
7630}
7631
7632/*
7633 * Free mbedtls_ssl_config
7634 */
7635void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
7636{
7637#if defined(MBEDTLS_DHM_C)
7638 mbedtls_mpi_free( &conf->dhm_P );
7639 mbedtls_mpi_free( &conf->dhm_G );
7640#endif
7641
7642#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7643 if( conf->psk != NULL )
7644 {
7645 mbedtls_zeroize( conf->psk, conf->psk_len );
7646 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
7647 mbedtls_free( conf->psk );
7648 mbedtls_free( conf->psk_identity );
7649 conf->psk_len = 0;
7650 conf->psk_identity_len = 0;
7651 }
7652#endif
7653
7654#if defined(MBEDTLS_X509_CRT_PARSE_C)
7655 ssl_key_cert_free( conf->key_cert );
7656#endif
7657
7658 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
7659}
7660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007662/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#if defined(MBEDTLS_RSA_C)
7668 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
7669 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007670#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671#if defined(MBEDTLS_ECDSA_C)
7672 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
7673 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007674#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007676}
7677
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007678unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
7679{
7680 switch( type ) {
7681 case MBEDTLS_PK_RSA:
7682 return( MBEDTLS_SSL_SIG_RSA );
7683 case MBEDTLS_PK_ECDSA:
7684 case MBEDTLS_PK_ECKEY:
7685 return( MBEDTLS_SSL_SIG_ECDSA );
7686 default:
7687 return( MBEDTLS_SSL_SIG_ANON );
7688 }
7689}
7690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007692{
7693 switch( sig )
7694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695#if defined(MBEDTLS_RSA_C)
7696 case MBEDTLS_SSL_SIG_RSA:
7697 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699#if defined(MBEDTLS_ECDSA_C)
7700 case MBEDTLS_SSL_SIG_ECDSA:
7701 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007702#endif
7703 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007705 }
7706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#endif /* MBEDTLS_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007708
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007709#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7710 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7711
7712/* Find an entry in a signature-hash set matching a given hash algorithm. */
7713mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7714 mbedtls_pk_type_t sig_alg )
7715{
7716 switch( sig_alg )
7717 {
7718 case MBEDTLS_PK_RSA:
7719 return( set->rsa );
7720 case MBEDTLS_PK_ECDSA:
7721 return( set->ecdsa );
7722 default:
7723 return( MBEDTLS_MD_NONE );
7724 }
7725}
7726
7727/* Add a signature-hash-pair to a signature-hash set */
7728void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7729 mbedtls_pk_type_t sig_alg,
7730 mbedtls_md_type_t md_alg )
7731{
7732 switch( sig_alg )
7733 {
7734 case MBEDTLS_PK_RSA:
7735 if( set->rsa == MBEDTLS_MD_NONE )
7736 set->rsa = md_alg;
7737 break;
7738
7739 case MBEDTLS_PK_ECDSA:
7740 if( set->ecdsa == MBEDTLS_MD_NONE )
7741 set->ecdsa = md_alg;
7742 break;
7743
7744 default:
7745 break;
7746 }
7747}
7748
7749/* Allow exactly one hash algorithm for each signature. */
7750void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7751 mbedtls_md_type_t md_alg )
7752{
7753 set->rsa = md_alg;
7754 set->ecdsa = md_alg;
7755}
7756
7757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
7758 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
7759
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007760/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007761 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007764{
7765 switch( hash )
7766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#if defined(MBEDTLS_MD5_C)
7768 case MBEDTLS_SSL_HASH_MD5:
7769 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#if defined(MBEDTLS_SHA1_C)
7772 case MBEDTLS_SSL_HASH_SHA1:
7773 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007774#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775#if defined(MBEDTLS_SHA256_C)
7776 case MBEDTLS_SSL_HASH_SHA224:
7777 return( MBEDTLS_MD_SHA224 );
7778 case MBEDTLS_SSL_HASH_SHA256:
7779 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781#if defined(MBEDTLS_SHA512_C)
7782 case MBEDTLS_SSL_HASH_SHA384:
7783 return( MBEDTLS_MD_SHA384 );
7784 case MBEDTLS_SSL_HASH_SHA512:
7785 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007786#endif
7787 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007789 }
7790}
7791
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007792/*
7793 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7794 */
7795unsigned char mbedtls_ssl_hash_from_md_alg( int md )
7796{
7797 switch( md )
7798 {
7799#if defined(MBEDTLS_MD5_C)
7800 case MBEDTLS_MD_MD5:
7801 return( MBEDTLS_SSL_HASH_MD5 );
7802#endif
7803#if defined(MBEDTLS_SHA1_C)
7804 case MBEDTLS_MD_SHA1:
7805 return( MBEDTLS_SSL_HASH_SHA1 );
7806#endif
7807#if defined(MBEDTLS_SHA256_C)
7808 case MBEDTLS_MD_SHA224:
7809 return( MBEDTLS_SSL_HASH_SHA224 );
7810 case MBEDTLS_MD_SHA256:
7811 return( MBEDTLS_SSL_HASH_SHA256 );
7812#endif
7813#if defined(MBEDTLS_SHA512_C)
7814 case MBEDTLS_MD_SHA384:
7815 return( MBEDTLS_SSL_HASH_SHA384 );
7816 case MBEDTLS_MD_SHA512:
7817 return( MBEDTLS_SSL_HASH_SHA512 );
7818#endif
7819 default:
7820 return( MBEDTLS_SSL_HASH_NONE );
7821 }
7822}
7823
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007824#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007825/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007826 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007827 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007828 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007829int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007830{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007832
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007833 if( ssl->conf->curve_list == NULL )
7834 return( -1 );
7835
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007836 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007837 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007838 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007839
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007840 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007841}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007842#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007843
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007844#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007845/*
7846 * Check if a hash proposed by the peer is in our list.
7847 * Return 0 if we're willing to use it, -1 otherwise.
7848 */
7849int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
7850 mbedtls_md_type_t md )
7851{
7852 const int *cur;
7853
7854 if( ssl->conf->sig_hashes == NULL )
7855 return( -1 );
7856
7857 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
7858 if( *cur == (int) md )
7859 return( 0 );
7860
7861 return( -1 );
7862}
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007863#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_X509_CRT_PARSE_C)
7866int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
7867 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007868 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007869 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007870{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007871 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007873 int usage = 0;
7874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007876 const char *ext_oid;
7877 size_t ext_len;
7878#endif
7879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7881 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007882 ((void) cert);
7883 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007884 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007885#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
7888 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007889 {
7890 /* Server part of the key exchange */
7891 switch( ciphersuite->key_exchange )
7892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 case MBEDTLS_KEY_EXCHANGE_RSA:
7894 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007895 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007896 break;
7897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7899 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7900 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7901 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007902 break;
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7905 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007906 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007907 break;
7908
7909 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 case MBEDTLS_KEY_EXCHANGE_NONE:
7911 case MBEDTLS_KEY_EXCHANGE_PSK:
7912 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7913 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007914 usage = 0;
7915 }
7916 }
7917 else
7918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7920 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007921 }
7922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007924 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007925 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007926 ret = -1;
7927 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007928#else
7929 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
7933 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 ext_oid = MBEDTLS_OID_SERVER_AUTH;
7936 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007937 }
7938 else
7939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
7941 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007942 }
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007945 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007946 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007947 ret = -1;
7948 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007950
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007951 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007954
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007955/*
7956 * Convert version numbers to/from wire format
7957 * and, for DTLS, to/from TLS equivalent.
7958 *
7959 * For TLS this is the identity.
Brian J Murraye7f8dc32016-11-06 04:45:15 -08007960 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007961 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
7962 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
7963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007965 unsigned char ver[2] )
7966{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967#if defined(MBEDTLS_SSL_PROTO_DTLS)
7968 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007971 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7972
7973 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
7974 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
7975 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007976 else
7977#else
7978 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007979#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007980 {
7981 ver[0] = (unsigned char) major;
7982 ver[1] = (unsigned char) minor;
7983 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007984}
7985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007987 const unsigned char ver[2] )
7988{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989#if defined(MBEDTLS_SSL_PROTO_DTLS)
7990 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007991 {
7992 *major = 255 - ver[0] + 2;
7993 *minor = 255 - ver[1] + 1;
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007996 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7997 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007998 else
7999#else
8000 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008001#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008002 {
8003 *major = ver[0];
8004 *minor = ver[1];
8005 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008006}
8007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008#endif /* MBEDTLS_SSL_TLS_C */