blob: 3ab482fa97db43465be4742e0e80da296ad8b93d [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 {
1564 /*
1565 * MAC(MAC_write_key, seq_num +
1566 * TLSCipherText.type +
1567 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001568 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001569 * IV + // except for TLS 1.0
1570 * ENC(content + padding + padding_length));
1571 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001572 unsigned char pseudo_hdr[13];
1573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001575
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001576 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1577 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001578 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1579 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1584 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001585 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001587 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001589
1590 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001591 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001594 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001595 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1597 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001602
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001603 /* Make extra sure authentication was performed, exactly once */
1604 if( auth_done != 1 )
1605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001608 }
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
1612 return( 0 );
1613}
1614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001617 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001619 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001620#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001621 size_t padlen = 0, correct = 1;
1622#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001626 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001630 }
1631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001633
Paul Bakker48916f92012-09-16 19:57:18 +00001634 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001637 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 }
1640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1642 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001643 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001644 int ret;
1645 size_t olen = 0;
1646
Paul Bakker68884e32013-01-07 18:20:04 +01001647 padlen = 0;
1648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001650 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001651 ssl->transform_in->ivlen,
1652 ssl->in_msg, ssl->in_msglen,
1653 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001656 return( ret );
1657 }
1658
1659 if( ssl->in_msglen != olen )
1660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 }
Paul Bakker68884e32013-01-07 18:20:04 +01001665 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1667#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1668 if( mode == MBEDTLS_MODE_GCM ||
1669 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001670 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001671 int ret;
1672 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001673 unsigned char *dec_msg;
1674 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001675 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001676 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001678 size_t explicit_iv_len = ssl->transform_in->ivlen -
1679 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001680
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001681 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001684 "+ taglen (%d)", ssl->in_msglen,
1685 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001687 }
1688 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1689
Paul Bakker68884e32013-01-07 18:20:04 +01001690 dec_msg = ssl->in_msg;
1691 dec_msg_result = ssl->in_msg;
1692 ssl->in_msglen = dec_msglen;
1693
1694 memcpy( add_data, ssl->in_ctr, 8 );
1695 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001697 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001698 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1699 add_data[12] = ssl->in_msglen & 0xFF;
1700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001702 add_data, 13 );
1703
1704 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1705 ssl->in_iv,
1706 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001709 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001711
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001712 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001713 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001716 ssl->transform_in->iv_dec,
1717 ssl->transform_in->ivlen,
1718 add_data, 13,
1719 dec_msg, dec_msglen,
1720 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001721 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1726 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001727
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001728 return( ret );
1729 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001730 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001731
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001732 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001736 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001737 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001738 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1740#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1741 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1742 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 {
Paul Bakker45829992013-01-03 14:52:21 +01001744 /*
1745 * Decrypt and check the padding
1746 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001747 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001748 unsigned char *dec_msg;
1749 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001750 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001752 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001753
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 /*
Paul Bakker45829992013-01-03 14:52:21 +01001755 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1758 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001759 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001760#endif
Paul Bakker45829992013-01-03 14:52:21 +01001761
1762 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1763 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001766 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1767 ssl->transform_in->ivlen,
1768 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001770 }
1771
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001772 dec_msglen = ssl->in_msglen;
1773 dec_msg = ssl->in_msg;
1774 dec_msg_result = ssl->in_msg;
1775
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001776 /*
1777 * Authenticate before decrypt if enabled
1778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1780 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001781 {
Hanno Beckerce516ff2017-11-09 18:57:39 +00001782 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001783 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001786
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001787 dec_msglen -= ssl->transform_in->maclen;
1788 ssl->in_msglen -= ssl->transform_in->maclen;
1789
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001790 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1791 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1792 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1793 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1798 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001799 ssl->in_iv, ssl->in_msglen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001800 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001804 ssl->transform_in->maclen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001805 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001806 ssl->transform_in->maclen );
1807
Hanno Beckerce516ff2017-11-09 18:57:39 +00001808 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1809 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001814 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001815 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001818
1819 /*
1820 * Check length sanity
1821 */
1822 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001825 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001827 }
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001830 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001831 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001832 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001834 {
Paul Bakker48916f92012-09-16 19:57:18 +00001835 dec_msglen -= ssl->transform_in->ivlen;
1836 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001837
Paul Bakker48916f92012-09-16 19:57:18 +00001838 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001839 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001840 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001844 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001845 ssl->transform_in->ivlen,
1846 dec_msg, dec_msglen,
1847 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001850 return( ret );
1851 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001852
Paul Bakkercca5b812013-08-31 17:40:26 +02001853 if( dec_msglen != olen )
1854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001857 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1860 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001861 {
1862 /*
1863 * Save IV in SSL3 and TLS1
1864 */
1865 memcpy( ssl->transform_in->iv_dec,
1866 ssl->transform_in->cipher_ctx_dec.iv,
1867 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001869#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001872
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001873 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001874 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876#if defined(MBEDTLS_SSL_DEBUG_ALL)
1877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01001878 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001879#endif
Paul Bakker45829992013-01-03 14:52:21 +01001880 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001881 correct = 0;
1882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884#if defined(MBEDTLS_SSL_PROTO_SSL3)
1885 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 {
Paul Bakker48916f92012-09-16 19:57:18 +00001887 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#if defined(MBEDTLS_SSL_DEBUG_ALL)
1890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001892 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001893#endif
Paul Bakker45829992013-01-03 14:52:21 +01001894 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 }
1896 }
1897 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1899#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1900 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1901 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 {
1903 /*
Paul Bakker45829992013-01-03 14:52:21 +01001904 * TLSv1+: always check the padding up to the first failure
1905 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001907 size_t pad_count = 0, real_count = 1;
Angus Gratton1226dd72018-06-19 15:57:50 +10001908 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001909
Paul Bakker956c9e02013-12-19 14:42:28 +01001910 /*
1911 * Padding is guaranteed to be incorrect if:
Angus Gratton1226dd72018-06-19 15:57:50 +10001912 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001913 *
Angus Gratton1226dd72018-06-19 15:57:50 +10001914 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001915 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001916 *
1917 * In both cases we reset padding_idx to a safe value (0) to
1918 * prevent out-of-buffer reads.
1919 */
Angus Gratton1226dd72018-06-19 15:57:50 +10001920 correct &= ( padlen <= ssl->in_msglen );
1921 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001922 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001923
1924 padding_idx *= correct;
1925
Angus Gratton1226dd72018-06-19 15:57:50 +10001926 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001927 {
Angus Gratton1226dd72018-06-19 15:57:50 +10001928 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001929 pad_count += real_count *
1930 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1931 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001932
1933 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001936 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001938#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001939 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001941 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1943 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001947 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001948
1949 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001951 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1953 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001957 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Manuel Pégourié-Gonnard671f9322018-07-10 11:15:36 +02001959#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard671f9322018-07-10 11:15:36 +02001962#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001963
1964 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001965 * Authenticate if not done yet.
1966 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001968#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001969 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 {
Hanno Beckerce516ff2017-11-09 18:57:39 +00001971 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001972
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001973 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001974
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001975 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1976 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_SSL_PROTO_SSL3)
1979 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001980 {
1981 ssl_mac( &ssl->transform_in->md_ctx_dec,
1982 ssl->transform_in->mac_dec,
1983 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001984 ssl->in_ctr, ssl->in_msgtype,
1985 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001986 }
1987 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1989#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1990 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1991 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001992 {
1993 /*
1994 * Process MAC and always update for padlen afterwards to make
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02001995 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001996 *
1997 * Known timing attacks:
1998 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1999 *
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02002000 * To compensate for different timings for the MAC calculation
2001 * depending on how much padding was removed (which is determined
2002 * by padlen), process extra_run more blocks through the hash
2003 * function.
2004 *
2005 * The formula in the paper is
2006 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2007 * where L1 is the size of the header plus the decrypted message
2008 * plus CBC padding and L2 is the size of the header plus the
2009 * decrypted message. This is for an underlying hash function
2010 * with 64-byte blocks.
2011 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2012 * correctly. We round down instead of up, so -56 is the correct
2013 * value for our calculations instead of -55.
2014 *
2015 * Repeat the formula rather than defining a block_size variable.
2016 * This avoids requiring division by a variable at runtime
2017 * (which would be marginally less efficient and would require
2018 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002019 */
2020 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002021
2022 /*
2023 * The next two sizes are the minimum and maximum values of
2024 * in_msglen over all padlen values.
2025 *
2026 * They're independent of padlen, since we previously did
2027 * in_msglen -= padlen.
2028 *
2029 * Note that max_len + maclen is never more than the buffer
2030 * length, as we previously did in_msglen -= maclen too.
2031 */
2032 const size_t max_len = ssl->in_msglen + padlen;
2033 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2034
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02002035 switch( ssl->transform_in->ciphersuite_info->mac )
2036 {
2037#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2038 defined(MBEDTLS_SHA256_C)
2039 case MBEDTLS_MD_MD5:
2040 case MBEDTLS_MD_SHA1:
2041 case MBEDTLS_MD_SHA256:
2042 /* 8 bytes of message size, 64-byte compression blocks */
2043 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2044 ( 13 + ssl->in_msglen + 8 ) / 64;
2045 break;
2046#endif
2047#if defined(MBEDTLS_SHA512_C)
2048 case MBEDTLS_MD_SHA384:
2049 /* 16 bytes of message size, 128-byte compression blocks */
2050 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2051 ( 13 + ssl->in_msglen + 16 ) / 128;
2052 break;
2053#endif
2054 default:
2055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2057 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002058
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002059 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2062 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2063 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2064 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002065 ssl->in_msglen );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002066 /* Make sure we access everything even when padlen > 0. This
2067 * makes the synchronisation requirements for just-in-time
2068 * Prime+Probe attacks much tighter and hopefully impractical. */
2069 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00002070 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002071
2072 /* Call mbedtls_md_process at least once due to cache attacks
2073 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002074 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002078
2079 /* Make sure we access all the memory that could contain the MAC,
2080 * before we check it in the next code block. This makes the
2081 * synchronisation requirements for just-in-time Prime+Probe
2082 * attacks much tighter and hopefully impractical. */
2083 ssl_read_memory( ssl->in_msg + min_len,
2084 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002085 }
2086 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2088 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2091 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002092 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002094#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Beckerce516ff2017-11-09 18:57:39 +00002095 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2096 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2097 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002098#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Hanno Beckerce516ff2017-11-09 18:57:39 +00002100 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2101 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_SSL_DEBUG_ALL)
2104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002105#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002106 correct = 0;
2107 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002108 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002109
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002110 /*
2111 * Finally check the correct flag
2112 */
2113 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002115 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002116#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002117
2118 /* Make extra sure authentication was performed, exactly once */
2119 if( auth_done != 1 )
2120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002124
2125 if( ssl->in_msglen == 0 )
2126 {
Angus Gratton485b3932018-06-19 15:58:22 +10002127#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2128 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2129 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2130 {
2131 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2133 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2134 }
2135#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2136
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 ssl->nb_zero++;
2138
2139 /*
2140 * Three or more empty messages may be a DoS attack
2141 * (excessive CPU consumption).
2142 */
2143 if( ssl->nb_zero > 3 )
2144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 }
2149 }
2150 else
2151 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002155 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002156 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002157 }
2158 else
2159#endif
2160 {
2161 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2162 if( ++ssl->in_ctr[i - 1] != 0 )
2163 break;
2164
2165 /* The loop goes to its end iff the counter is wrapping */
2166 if( i == ssl_ep_len( ssl ) )
2167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2169 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002170 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002171 }
2172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002174
2175 return( 0 );
2176}
2177
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002178#undef MAC_NONE
2179#undef MAC_PLAINTEXT
2180#undef MAC_CIPHERTEXT
2181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002183/*
2184 * Compression/decompression functions
2185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002187{
2188 int ret;
2189 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002190 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002191 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002192 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002195
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002196 if( len_pre == 0 )
2197 return( 0 );
2198
Paul Bakker2770fbd2012-07-03 13:30:23 +00002199 memcpy( msg_pre, ssl->out_msg, len_pre );
2200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002202 ssl->out_msglen ) );
2203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002205 ssl->out_msg, ssl->out_msglen );
2206
Paul Bakker48916f92012-09-16 19:57:18 +00002207 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2208 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2209 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002210 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002211
Paul Bakker48916f92012-09-16 19:57:18 +00002212 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002213 if( ret != Z_OK )
2214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2216 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002217 }
2218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekbb666142018-04-23 08:29:36 -04002220 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002223 ssl->out_msglen ) );
2224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002226 ssl->out_msg, ssl->out_msglen );
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002229
2230 return( 0 );
2231}
2232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002234{
2235 int ret;
2236 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek078014a2018-04-24 06:32:44 -04002237 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002238 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002239 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002242
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002243 if( len_pre == 0 )
2244 return( 0 );
2245
Paul Bakker2770fbd2012-07-03 13:30:23 +00002246 memcpy( msg_pre, ssl->in_msg, len_pre );
2247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002249 ssl->in_msglen ) );
2250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002252 ssl->in_msg, ssl->in_msglen );
2253
Paul Bakker48916f92012-09-16 19:57:18 +00002254 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2255 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2256 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002257 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek078014a2018-04-24 06:32:44 -04002258 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002259
Paul Bakker48916f92012-09-16 19:57:18 +00002260 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002261 if( ret != Z_OK )
2262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2264 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002265 }
2266
Andrzej Kurekbb666142018-04-23 08:29:36 -04002267 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek078014a2018-04-24 06:32:44 -04002268 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002271 ssl->in_msglen ) );
2272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002274 ssl->in_msg, ssl->in_msglen );
2275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002277
2278 return( 0 );
2279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2283static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_SSL_PROTO_DTLS)
2286static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002287{
2288 /* If renegotiation is not enforced, retransmit until we would reach max
2289 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002290 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002291 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002292 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002293 unsigned char doublings = 1;
2294
2295 while( ratio != 0 )
2296 {
2297 ++doublings;
2298 ratio >>= 1;
2299 }
2300
2301 if( ++ssl->renego_records_seen > doublings )
2302 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002304 return( 0 );
2305 }
2306 }
2307
2308 return( ssl_write_hello_request( ssl ) );
2309}
2310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002312
Paul Bakker5121ce52009-01-03 21:22:43 +00002313/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002314 * Fill the input message buffer by appending data to it.
2315 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002316 *
2317 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2318 * available (from this read and/or a previous one). Otherwise, an error code
2319 * is returned (possibly EOF or WANT_READ).
2320 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002321 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2322 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2323 * since we always read a whole datagram at once.
2324 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002325 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002326 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002329{
Paul Bakker23986e52011-04-24 08:57:21 +00002330 int ret;
2331 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002335 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002338 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002340 }
2341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002346 }
2347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002349 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002351 uint32_t timeout;
2352
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002353 /* Just to be sure */
2354 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2355 {
2356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2357 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2359 }
2360
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002361 /*
2362 * The point is, we need to always read a full datagram at once, so we
2363 * sometimes read more then requested, and handle the additional data.
2364 * It could be the rest of the current record (while fetching the
2365 * header) and/or some other records in the same datagram.
2366 */
2367
2368 /*
2369 * Move to the next record in the already read datagram if applicable
2370 */
2371 if( ssl->next_record_offset != 0 )
2372 {
2373 if( ssl->in_left < ssl->next_record_offset )
2374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002377 }
2378
2379 ssl->in_left -= ssl->next_record_offset;
2380
2381 if( ssl->in_left != 0 )
2382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002384 ssl->next_record_offset ) );
2385 memmove( ssl->in_hdr,
2386 ssl->in_hdr + ssl->next_record_offset,
2387 ssl->in_left );
2388 }
2389
2390 ssl->next_record_offset = 0;
2391 }
2392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002395
2396 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002397 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002398 */
2399 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002402 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002403 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002404
2405 /*
2406 * A record can't be split accross datagrams. If we need to read but
2407 * are not at the beginning of a new record, the caller did something
2408 * wrong.
2409 */
2410 if( ssl->in_left != 0 )
2411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002414 }
2415
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002416 /*
2417 * Don't even try to read if time's out already.
2418 * This avoids by-passing the timer when repeatedly receiving messages
2419 * that will end up being dropped.
2420 */
2421 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002422 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002423 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002428 timeout = ssl->handshake->retransmit_timeout;
2429 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002430 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002433
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002434 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002435 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2436 timeout );
2437 else
2438 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002441
2442 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002444 }
2445
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002446 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002449 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002452 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002453 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002456 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002457 }
2458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002462 return( ret );
2463 }
2464
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002465 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002468 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002470 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002471 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002474 return( ret );
2475 }
2476
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002477 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002478 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002480 }
2481
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 if( ret < 0 )
2483 return( ret );
2484
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002485 ssl->in_left = ret;
2486 }
2487 else
2488#endif
2489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002491 ssl->in_left, nb_want ) );
2492
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002493 while( ssl->in_left < nb_want )
2494 {
2495 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002496
2497 if( ssl_check_timer( ssl ) != 0 )
2498 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2499 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002500 {
2501 if( ssl->f_recv_timeout != NULL )
2502 {
2503 ret = ssl->f_recv_timeout( ssl->p_bio,
2504 ssl->in_hdr + ssl->in_left, len,
2505 ssl->conf->read_timeout );
2506 }
2507 else
2508 {
2509 ret = ssl->f_recv( ssl->p_bio,
2510 ssl->in_hdr + ssl->in_left, len );
2511 }
2512 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002515 ssl->in_left, nb_want ) );
2516 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002517
2518 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002520
2521 if( ret < 0 )
2522 return( ret );
2523
mohammad1603f72e51f2018-03-28 23:44:39 -07002524 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad160389c12ec2018-03-19 07:15:50 -07002525 {
Jaeden Ameroee6c8222018-04-03 12:07:19 +01002526 MBEDTLS_SSL_DEBUG_MSG( 1,
2527 ( "f_recv returned %d bytes but only %lu were requested",
mohammad1603ad2908c2018-04-02 07:30:32 -07002528 ret, (unsigned long)len ) );
mohammad160389c12ec2018-03-19 07:15:50 -07002529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2530 }
2531
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002532 ssl->in_left += ret;
2533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 }
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
2538 return( 0 );
2539}
2540
2541/*
2542 * Flush any data not yet written
2543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002545{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002546 int ret;
2547 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002551 if( ssl->f_send == NULL )
2552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002554 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002556 }
2557
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002558 /* Avoid incrementing counter if data is flushed */
2559 if( ssl->out_left == 0 )
2560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002562 return( 0 );
2563 }
2564
Paul Bakker5121ce52009-01-03 21:22:43 +00002565 while( ssl->out_left > 0 )
2566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2568 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002571 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002572 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
2576 if( ret <= 0 )
2577 return( ret );
2578
mohammad1603f72e51f2018-03-28 23:44:39 -07002579 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad1603f65add42018-02-22 04:29:04 -08002580 {
Jaeden Ameroee6c8222018-04-03 12:07:19 +01002581 MBEDTLS_SSL_DEBUG_MSG( 1,
2582 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad1603ad2908c2018-04-02 07:30:32 -07002583 ret, (unsigned long)ssl->out_left ) );
mohammad1603f65add42018-02-22 04:29:04 -08002584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2585 }
2586
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 ssl->out_left -= ret;
2588 }
2589
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002590 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002591 if( ++ssl->out_ctr[i - 1] != 0 )
2592 break;
2593
2594 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002595 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2598 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002599 }
2600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
2603 return( 0 );
2604}
2605
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002606/*
2607 * Functions to handle the DTLS retransmission state machine
2608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002610/*
2611 * Append current handshake message to current outgoing flight
2612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002616
2617 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002618 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002619 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002622 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002623 }
2624
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002625 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002626 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002629 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002630 }
2631
2632 /* Copy current handshake message with headers */
2633 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2634 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002635 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002636 msg->next = NULL;
2637
2638 /* Append to the current flight */
2639 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002640 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002641 else
2642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002644 while( cur->next != NULL )
2645 cur = cur->next;
2646 cur->next = msg;
2647 }
2648
2649 return( 0 );
2650}
2651
2652/*
2653 * Free the current flight of handshake messages
2654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 mbedtls_ssl_flight_item *cur = flight;
2658 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002659
2660 while( cur != NULL )
2661 {
2662 next = cur->next;
2663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 mbedtls_free( cur->p );
2665 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002666
2667 cur = next;
2668 }
2669}
2670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2672static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002673#endif
2674
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002675/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002676 * Swap transform_out and out_ctr with the alternative ones
2677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002679{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002681 unsigned char tmp_out_ctr[8];
2682
2683 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002686 return;
2687 }
2688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002690
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002691 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002692 tmp_transform = ssl->transform_out;
2693 ssl->transform_out = ssl->handshake->alt_transform_out;
2694 ssl->handshake->alt_transform_out = tmp_transform;
2695
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002696 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002697 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2698 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2699 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002700
2701 /* Adjust to the newly activated transform */
2702 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002704 {
2705 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2706 ssl->transform_out->fixed_ivlen;
2707 }
2708 else
2709 ssl->out_msg = ssl->out_iv;
2710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2712 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2717 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002718 }
2719 }
2720#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002721}
2722
2723/*
2724 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002725 *
2726 * Need to remember the current message in case flush_output returns
2727 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002728 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002737
2738 ssl->handshake->cur_msg = ssl->handshake->flight;
2739 ssl_swap_epochs( ssl );
2740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002742 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002743
2744 while( ssl->handshake->cur_msg != NULL )
2745 {
2746 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002748
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002749 /* Swap epochs before sending Finished: we can't do it after
2750 * sending ChangeCipherSpec, in case write returns WANT_READ.
2751 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2753 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002754 {
2755 ssl_swap_epochs( ssl );
2756 }
2757
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002758 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002759 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002760 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002761
2762 ssl->handshake->cur_msg = cur->next;
2763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002769 return( ret );
2770 }
2771 }
2772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2774 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002775 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002778 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2779 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002782
2783 return( 0 );
2784}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002785
2786/*
2787 * To be called when the last message of an incoming flight is received.
2788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002790{
2791 /* We won't need to resend that one any more */
2792 ssl_flight_free( ssl->handshake->flight );
2793 ssl->handshake->flight = NULL;
2794 ssl->handshake->cur_msg = NULL;
2795
2796 /* The next incoming flight will start with this msg_seq */
2797 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2798
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002799 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002800 ssl_set_timer( ssl, 0 );
2801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2803 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002806 }
2807 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002809}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002810
2811/*
2812 * To be called when the last message of an outgoing flight is send.
2813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002815{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002816 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002817 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2820 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002823 }
2824 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002828
Paul Bakker5121ce52009-01-03 21:22:43 +00002829/*
2830 * Record layer functions
2831 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002832
2833/*
2834 * Write current record.
2835 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002838{
Paul Bakker05ef8352012-05-08 09:17:57 +00002839 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002840 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002846 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002848 {
2849 ; /* Skip special handshake treatment when resending */
2850 }
2851 else
2852#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002854 {
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002855 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST &&
2856 ssl->handshake == NULL )
2857 {
2858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2860 }
2861
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2863 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2864 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2865
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002866 /*
2867 * DTLS has additional fields in the Handshake layer,
2868 * between the length field and the actual payload:
2869 * uint16 message_seq;
2870 * uint24 fragment_offset;
2871 * uint24 fragment_length;
2872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002874 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002875 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002876 /* Make room for the additional DTLS fields */
Hanno Becker0983dc42017-09-18 10:55:54 +01002877 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
2878 {
2879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2880 "size %u, maximum %u",
2881 (unsigned) ( ssl->in_hslen - 4 ),
2882 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
2883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2884 }
2885
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002886 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002887 ssl->out_msglen += 8;
2888 len += 8;
2889
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002890 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002892 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002893 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2894 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2895 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002896 }
2897 else
2898 {
2899 ssl->out_msg[4] = 0;
2900 ssl->out_msg[5] = 0;
2901 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002902
2903 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2904 memset( ssl->out_msg + 6, 0x00, 3 );
2905 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002906 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002910 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 }
2912
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002913 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002915 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002916 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
2918 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
2919 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002920 {
2921 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002924 return( ret );
2925 }
2926 }
2927#endif
2928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002930 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932 {
2933 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936 return( ret );
2937 }
2938
2939 len = ssl->out_msglen;
2940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2944 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 ret = mbedtls_ssl_hw_record_write( ssl );
2949 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2952 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002953 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002954
2955 if( ret == 0 )
2956 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002957 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002959 if( !done )
2960 {
2961 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002963 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002964
2965 ssl->out_len[0] = (unsigned char)( len >> 8 );
2966 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002967
Paul Bakker48916f92012-09-16 19:57:18 +00002968 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002969 {
2970 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002973 return( ret );
2974 }
2975
2976 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002977 ssl->out_len[0] = (unsigned char)( len >> 8 );
2978 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002979 }
2980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00002984 "version = [%d:%d], msglen = %d",
2985 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002986 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
2989 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 }
2991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 return( ret );
2996 }
2997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
3000 return( 0 );
3001}
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003004/*
3005 * Mark bits in bitmask (used for DTLS HS reassembly)
3006 */
3007static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3008{
3009 unsigned int start_bits, end_bits;
3010
3011 start_bits = 8 - ( offset % 8 );
3012 if( start_bits != 8 )
3013 {
3014 size_t first_byte_idx = offset / 8;
3015
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003016 /* Special case */
3017 if( len <= start_bits )
3018 {
3019 for( ; len != 0; len-- )
3020 mask[first_byte_idx] |= 1 << ( start_bits - len );
3021
3022 /* Avoid potential issues with offset or len becoming invalid */
3023 return;
3024 }
3025
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003026 offset += start_bits; /* Now offset % 8 == 0 */
3027 len -= start_bits;
3028
3029 for( ; start_bits != 0; start_bits-- )
3030 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3031 }
3032
3033 end_bits = len % 8;
3034 if( end_bits != 0 )
3035 {
3036 size_t last_byte_idx = ( offset + len ) / 8;
3037
3038 len -= end_bits; /* Now len % 8 == 0 */
3039
3040 for( ; end_bits != 0; end_bits-- )
3041 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3042 }
3043
3044 memset( mask + offset / 8, 0xFF, len / 8 );
3045}
3046
3047/*
3048 * Check that bitmask is full
3049 */
3050static int ssl_bitmask_check( unsigned char *mask, size_t len )
3051{
3052 size_t i;
3053
3054 for( i = 0; i < len / 8; i++ )
3055 if( mask[i] != 0xFF )
3056 return( -1 );
3057
3058 for( i = 0; i < len % 8; i++ )
3059 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3060 return( -1 );
3061
3062 return( 0 );
3063}
3064
3065/*
3066 * Reassemble fragmented DTLS handshake messages.
3067 *
3068 * Use a temporary buffer for reassembly, divided in two parts:
3069 * - the first holds the reassembled message (including handshake header),
3070 * - the second holds a bitmask indicating which parts of the message
3071 * (excluding headers) have been received so far.
3072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003074{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003075 unsigned char *msg, *bitmask;
3076 size_t frag_len, frag_off;
3077 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3078
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003079 if( ssl->handshake == NULL )
3080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3082 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003083 }
3084
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003085 /*
3086 * For first fragment, check size and allocate buffer
3087 */
3088 if( ssl->handshake->hs_msg == NULL )
3089 {
3090 size_t alloc_len;
3091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003093 msg_len ) );
3094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3098 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003099 }
3100
3101 /* The bitmask needs one bit per byte of message excluding header */
3102 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3103
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003104 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003105 if( ssl->handshake->hs_msg == NULL )
3106 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003108 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003109 }
3110
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003111 /* Prepare final header: copy msg_type, length and message_seq,
3112 * then add standardised fragment_offset and fragment_length */
3113 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3114 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3115 memcpy( ssl->handshake->hs_msg + 9,
3116 ssl->handshake->hs_msg + 1, 3 );
3117 }
3118 else
3119 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003120 /* Make sure msg_type and length are consistent */
3121 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3124 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003125 }
3126 }
3127
3128 msg = ssl->handshake->hs_msg + 12;
3129 bitmask = msg + msg_len;
3130
3131 /*
3132 * Check and copy current fragment
3133 */
3134 frag_off = ( ssl->in_msg[6] << 16 ) |
3135 ( ssl->in_msg[7] << 8 ) |
3136 ssl->in_msg[8];
3137 frag_len = ( ssl->in_msg[9] << 16 ) |
3138 ( ssl->in_msg[10] << 8 ) |
3139 ssl->in_msg[11];
3140
3141 if( frag_off + frag_len > msg_len )
3142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003144 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003146 }
3147
3148 if( frag_len + 12 > ssl->in_msglen )
3149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003151 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003153 }
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003156 frag_off, frag_len ) );
3157
3158 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3159 ssl_bitmask_set( bitmask, frag_off, frag_len );
3160
3161 /*
3162 * Do we have the complete message by now?
3163 * If yes, finalize it, else ask to read the next record.
3164 */
3165 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003168 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003169 }
3170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003172
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003173 if( frag_len + 12 < ssl->in_msglen )
3174 {
3175 /*
3176 * We'got more handshake messages in the same record.
3177 * This case is not handled now because no know implementation does
3178 * that and it's hard to test, so we prefer to fail cleanly for now.
3179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3181 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003182 }
3183
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003184 if( ssl->in_left > ssl->next_record_offset )
3185 {
3186 /*
3187 * We've got more data in the buffer after the current record,
3188 * that we don't want to overwrite. Move it before writing the
3189 * reassembled message, and adjust in_left and next_record_offset.
3190 */
3191 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3192 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3193 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3194
3195 /* First compute and check new lengths */
3196 ssl->next_record_offset = new_remain - ssl->in_hdr;
3197 ssl->in_left = ssl->next_record_offset + remain_len;
3198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003200 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3203 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003204 }
3205
3206 memmove( new_remain, cur_remain, remain_len );
3207 }
3208
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003209 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003212 ssl->handshake->hs_msg = NULL;
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003215 ssl->in_msg, ssl->in_hslen );
3216
3217 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221static int ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003226 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003228 }
3229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003231 ( ssl->in_msg[1] << 16 ) |
3232 ( ssl->in_msg[2] << 8 ) |
3233 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003236 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003237 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003240 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003241 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003242 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003243 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003244
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003245 /* ssl->handshake is NULL when receiving ClientHello for renego */
3246 if( ssl->handshake != NULL &&
3247 recv_msg_seq != ssl->handshake->in_msg_seq )
3248 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003249 /* Retransmit only on last message from previous flight, to avoid
3250 * too many retransmissions.
3251 * Besides, No sane server ever retransmits HelloVerifyRequest */
3252 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003256 "message_seq = %d, start_of_flight = %d",
3257 recv_msg_seq,
3258 ssl->handshake->in_flight_start_seq ) );
3259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003263 return( ret );
3264 }
3265 }
3266 else
3267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003269 "message_seq = %d, expected = %d",
3270 recv_msg_seq,
3271 ssl->handshake->in_msg_seq ) );
3272 }
3273
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003274 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003275 }
3276 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003277
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003278 /* Reassemble if current message is fragmented or reassembly is
3279 * already in progress */
3280 if( ssl->in_msglen < ssl->in_hslen ||
3281 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3282 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3283 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003286
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003287 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003290 return( ret );
3291 }
3292 }
3293 }
3294 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003296 /* With TLS we don't handle fragmentation (for now) */
3297 if( ssl->in_msglen < ssl->in_hslen )
3298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3300 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003301 }
3302
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003303 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3304 ssl->handshake != NULL )
3305 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003306 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003307 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003308
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003309 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003312 ssl->handshake != NULL )
3313 {
3314 ssl->handshake->in_msg_seq++;
3315 }
3316#endif
3317
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003318 return( 0 );
3319}
3320
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003321/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003322 * DTLS anti-replay: RFC 6347 4.1.2.6
3323 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003324 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3325 * Bit n is set iff record number in_window_top - n has been seen.
3326 *
3327 * Usually, in_window_top is the last record number seen and the lsb of
3328 * in_window is set. The only exception is the initial state (record number 0
3329 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3332static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003333{
3334 ssl->in_window_top = 0;
3335 ssl->in_window = 0;
3336}
3337
3338static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3339{
3340 return( ( (uint64_t) buf[0] << 40 ) |
3341 ( (uint64_t) buf[1] << 32 ) |
3342 ( (uint64_t) buf[2] << 24 ) |
3343 ( (uint64_t) buf[3] << 16 ) |
3344 ( (uint64_t) buf[4] << 8 ) |
3345 ( (uint64_t) buf[5] ) );
3346}
3347
3348/*
3349 * Return 0 if sequence number is acceptable, -1 otherwise
3350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003352{
3353 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3354 uint64_t bit;
3355
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003356 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003357 return( 0 );
3358
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003359 if( rec_seqnum > ssl->in_window_top )
3360 return( 0 );
3361
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003362 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003363
3364 if( bit >= 64 )
3365 return( -1 );
3366
3367 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3368 return( -1 );
3369
3370 return( 0 );
3371}
3372
3373/*
3374 * Update replay window on new validated record
3375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003377{
3378 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3379
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003380 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003381 return;
3382
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003383 if( rec_seqnum > ssl->in_window_top )
3384 {
3385 /* Update window_top and the contents of the window */
3386 uint64_t shift = rec_seqnum - ssl->in_window_top;
3387
3388 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003389 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003390 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003391 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003392 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003393 ssl->in_window |= 1;
3394 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003395
3396 ssl->in_window_top = rec_seqnum;
3397 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003398 else
3399 {
3400 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003401 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003402
3403 if( bit < 64 ) /* Always true, but be extra sure */
3404 ssl->in_window |= (uint64_t) 1 << bit;
3405 }
3406}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003408
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003409#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003410/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003411static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3412
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003413/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003414 * Without any SSL context, check if a datagram looks like a ClientHello with
3415 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003416 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003417 *
3418 * - if cookie is valid, return 0
3419 * - if ClientHello looks superficially valid but cookie is not,
3420 * fill obuf and set olen, then
3421 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3422 * - otherwise return a specific error code
3423 */
3424static int ssl_check_dtls_clihlo_cookie(
3425 mbedtls_ssl_cookie_write_t *f_cookie_write,
3426 mbedtls_ssl_cookie_check_t *f_cookie_check,
3427 void *p_cookie,
3428 const unsigned char *cli_id, size_t cli_id_len,
3429 const unsigned char *in, size_t in_len,
3430 unsigned char *obuf, size_t buf_len, size_t *olen )
3431{
3432 size_t sid_len, cookie_len;
3433 unsigned char *p;
3434
3435 if( f_cookie_write == NULL || f_cookie_check == NULL )
3436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3437
3438 /*
3439 * Structure of ClientHello with record and handshake headers,
3440 * and expected values. We don't need to check a lot, more checks will be
3441 * done when actually parsing the ClientHello - skipping those checks
3442 * avoids code duplication and does not make cookie forging any easier.
3443 *
3444 * 0-0 ContentType type; copied, must be handshake
3445 * 1-2 ProtocolVersion version; copied
3446 * 3-4 uint16 epoch; copied, must be 0
3447 * 5-10 uint48 sequence_number; copied
3448 * 11-12 uint16 length; (ignored)
3449 *
3450 * 13-13 HandshakeType msg_type; (ignored)
3451 * 14-16 uint24 length; (ignored)
3452 * 17-18 uint16 message_seq; copied
3453 * 19-21 uint24 fragment_offset; copied, must be 0
3454 * 22-24 uint24 fragment_length; (ignored)
3455 *
3456 * 25-26 ProtocolVersion client_version; (ignored)
3457 * 27-58 Random random; (ignored)
3458 * 59-xx SessionID session_id; 1 byte len + sid_len content
3459 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3460 * ...
3461 *
3462 * Minimum length is 61 bytes.
3463 */
3464 if( in_len < 61 ||
3465 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3466 in[3] != 0 || in[4] != 0 ||
3467 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3468 {
3469 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3470 }
3471
3472 sid_len = in[59];
3473 if( sid_len > in_len - 61 )
3474 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3475
3476 cookie_len = in[60 + sid_len];
3477 if( cookie_len > in_len - 60 )
3478 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3479
3480 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3481 cli_id, cli_id_len ) == 0 )
3482 {
3483 /* Valid cookie */
3484 return( 0 );
3485 }
3486
3487 /*
3488 * If we get here, we've got an invalid cookie, let's prepare HVR.
3489 *
3490 * 0-0 ContentType type; copied
3491 * 1-2 ProtocolVersion version; copied
3492 * 3-4 uint16 epoch; copied
3493 * 5-10 uint48 sequence_number; copied
3494 * 11-12 uint16 length; olen - 13
3495 *
3496 * 13-13 HandshakeType msg_type; hello_verify_request
3497 * 14-16 uint24 length; olen - 25
3498 * 17-18 uint16 message_seq; copied
3499 * 19-21 uint24 fragment_offset; copied
3500 * 22-24 uint24 fragment_length; olen - 25
3501 *
3502 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3503 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3504 *
3505 * Minimum length is 28.
3506 */
3507 if( buf_len < 28 )
3508 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3509
3510 /* Copy most fields and adapt others */
3511 memcpy( obuf, in, 25 );
3512 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3513 obuf[25] = 0xfe;
3514 obuf[26] = 0xff;
3515
3516 /* Generate and write actual cookie */
3517 p = obuf + 28;
3518 if( f_cookie_write( p_cookie,
3519 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3520 {
3521 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3522 }
3523
3524 *olen = p - obuf;
3525
3526 /* Go back and fill length fields */
3527 obuf[27] = (unsigned char)( *olen - 28 );
3528
3529 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3530 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3531 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3532
3533 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3534 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3535
3536 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3537}
3538
3539/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003540 * Handle possible client reconnect with the same UDP quadruplet
3541 * (RFC 6347 Section 4.2.8).
3542 *
3543 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3544 * that looks like a ClientHello.
3545 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003546 * - if the input looks like a ClientHello without cookies,
3547 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003548 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003549 * - if the input looks like a ClientHello with a valid cookie,
3550 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003551 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003552 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003553 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003554 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003555 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3556 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003557 */
3558static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3559{
3560 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003561 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003562
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003563 ret = ssl_check_dtls_clihlo_cookie(
3564 ssl->conf->f_cookie_write,
3565 ssl->conf->f_cookie_check,
3566 ssl->conf->p_cookie,
3567 ssl->cli_id, ssl->cli_id_len,
3568 ssl->in_buf, ssl->in_left,
3569 ssl->out_buf, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003570
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003571 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3572
3573 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003574 {
Brian J Murraye7f8dc32016-11-06 04:45:15 -08003575 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003576 * If the error is permanent we'll catch it later,
3577 * if it's not, then hopefully it'll work next time. */
3578 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3579
3580 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003581 }
3582
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003583 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003584 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003585 /* Got a valid cookie, partially reset context */
3586 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3587 {
3588 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3589 return( ret );
3590 }
3591
3592 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003593 }
3594
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003595 return( ret );
3596}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003597#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003598
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003599/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003600 * ContentType type;
3601 * ProtocolVersion version;
3602 * uint16 epoch; // DTLS only
3603 * uint48 sequence_number; // DTLS only
3604 * uint16 length;
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003605 *
3606 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butchere103aa82015-12-16 01:51:01 +00003607 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003608 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3609 *
3610 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butchere103aa82015-12-16 01:51:01 +00003611 * 1. proceed with the record if this function returns 0
3612 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3613 * 3. return CLIENT_RECONNECT if this function returns that value
3614 * 4. drop the whole datagram if this function returns anything else.
3615 * Point 2 is needed when the peer is resending, and we have already received
3616 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003619{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003620 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 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 +02003623
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003625 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003626 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003630 ssl->in_msgtype,
3631 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003633 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3635 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3636 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3637 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003640
Andres Amaya Garcia1042d862017-07-05 13:26:34 +01003641#if defined(MBEDTLS_SSL_PROTO_DTLS)
3642 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3643 * Section 4.1.2.7 */
3644 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3645#endif /* MBEDTLS_SSL_PROTO_DTLS */
3646 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3647 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003650 }
3651
3652 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003653 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 }
3658
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003659 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3662 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003663 }
3664
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003665 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003667 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3670 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003671 }
3672
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003673 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003674 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003675 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003676 if( ssl->in_msglen < 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 }
3682 }
3683 else
3684 {
Paul Bakker48916f92012-09-16 19:57:18 +00003685 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3688 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003689 }
3690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691#if defined(MBEDTLS_SSL_PROTO_SSL3)
3692 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3693 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3696 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3700 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003701 /*
3702 * TLS encrypted messages can have up to 256 bytes of padding
3703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003705 ssl->in_msglen > ssl->transform_in->minlen +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3709 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003711#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 }
3713
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003714 /*
3715 * DTLS-related tests done last, because most of them may result in
3716 * silently dropping the record (but not the whole datagram), and we only
3717 * want to consider that after ensuring that the "basic" fields (type,
3718 * version, length) are sane.
3719 */
3720#if defined(MBEDTLS_SSL_PROTO_DTLS)
3721 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3722 {
3723 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3724
3725 /* Drop unexpected ChangeCipherSpec messages */
3726 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3727 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3728 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3729 {
3730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3731 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3732 }
3733
3734 /* Drop unexpected ApplicationData records,
3735 * except at the beginning of renegotiations */
3736 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3737 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3738#if defined(MBEDTLS_SSL_RENEGOTIATION)
3739 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3740 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3741#endif
3742 )
3743 {
3744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3745 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3746 }
3747
3748 /* Check epoch (and sequence number) with DTLS */
3749 if( rec_epoch != ssl->in_epoch )
3750 {
3751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3752 "expected %d, received %d",
3753 ssl->in_epoch, rec_epoch ) );
3754
3755#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3756 /*
3757 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3758 * access the first byte of record content (handshake type), as we
3759 * have an active transform (possibly iv_len != 0), so use the
3760 * fact that the record header len is 13 instead.
3761 */
3762 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3763 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3764 rec_epoch == 0 &&
3765 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3766 ssl->in_left > 13 &&
3767 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3768 {
3769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3770 "from the same port" ) );
3771 return( ssl_handle_possible_reconnect( ssl ) );
3772 }
3773 else
3774#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3775 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3776 }
3777
3778#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3779 /* Replay detection only works for the current epoch */
3780 if( rec_epoch == ssl->in_epoch &&
3781 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3782 {
3783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3784 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3785 }
3786#endif
3787 }
3788#endif /* MBEDTLS_SSL_PROTO_DTLS */
3789
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003790 return( 0 );
3791}
Paul Bakker5121ce52009-01-03 21:22:43 +00003792
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003793/*
3794 * If applicable, decrypt (and decompress) record content
3795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003797{
3798 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3801 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3804 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808 ret = mbedtls_ssl_hw_record_read( ssl );
3809 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3812 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003813 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003814
3815 if( ret == 0 )
3816 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003817 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003819 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003820 {
3821 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003824 return( ret );
3825 }
3826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003828 ssl->in_msg, ssl->in_msglen );
3829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3833 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 }
3835 }
3836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003838 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003840 {
3841 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003844 return( ret );
3845 }
3846
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003847 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3848 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003849 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003853 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003856 }
3857#endif
3858
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003859 return( 0 );
3860}
3861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003863
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003864/*
3865 * Read a record.
3866 *
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02003867 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3868 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3869 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003872{
3873 int ret;
3874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003876
Hanno Becker704f4932017-06-08 13:08:45 +01003877 if( ssl->keep_current_message == 1 )
3878 {
3879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
3880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3881 ssl->keep_current_message = 0;
3882
3883 return( 0 );
3884 }
3885
Hanno Becker6a582e82017-06-08 13:38:05 +01003886 /*
3887 * Step A
3888 *
3889 * Consume last content-layer message and potentially
3890 * update in_msglen which keeps track of the contents'
3891 * consumption state.
3892 *
3893 * (1) Handshake messages:
3894 * Remove last handshake message, move content
3895 * and adapt in_msglen.
3896 *
3897 * (2) Alert messages:
3898 * Consume whole record content, in_msglen = 0.
3899 *
3900 * NOTE: This needs to be fixed, since like for
3901 * handshake messages it is allowed to have
3902 * multiple alerts witin a single record.
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003903 * Internal reference IOTSSL-1321.
Hanno Becker6a582e82017-06-08 13:38:05 +01003904 *
3905 * (3) Change cipher spec:
3906 * Consume whole record content, in_msglen = 0.
3907 *
3908 * (4) Application data:
3909 * Don't do anything - the record layer provides
3910 * the application data as a stream transport
3911 * and consumes through mbedtls_ssl_read only.
3912 *
3913 */
3914
3915 /* Case (1): Handshake messages */
3916
3917 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003918 {
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003919 if( ssl->in_offt != NULL )
3920 {
3921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3923 }
3924
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003925 /*
3926 * Get next Handshake message in the current record
3927 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003928
Hanno Becker6a582e82017-06-08 13:38:05 +01003929 /* Notes:
3930 * (1) in_hslen is *NOT* necessarily the size of the
3931 * current handshake content: If DTLS handshake
3932 * fragmentation is used, that's the fragment
3933 * size instead. Using the total handshake message
3934 * size here is FAULTY and should be changed at
3935 * some point. Internal reference IOTSSL-1414.
3936 * (2) While it doesn't seem to cause problems, one
3937 * has to be very careful not to assume that in_hslen
3938 * is always <= in_msglen in a sensible communication.
3939 * Again, it's wrong for DTLS handshake fragmentation.
3940 * The following check is therefore mandatory, and
3941 * should not be treated as a silently corrected assertion.
3942 */
3943 if( ssl->in_hslen < ssl->in_msglen )
3944 {
3945 ssl->in_msglen -= ssl->in_hslen;
3946 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3947 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003948
Hanno Becker6a582e82017-06-08 13:38:05 +01003949 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
3950 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003951
Hanno Becker6a582e82017-06-08 13:38:05 +01003952 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3953 return( ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003954
Hanno Becker6a582e82017-06-08 13:38:05 +01003955 return( 0 );
3956 }
3957 else
3958 {
3959 ssl->in_msglen = 0;
3960 }
3961
3962 ssl->in_hslen = 0;
3963 }
3964 /* Case (4): Application data */
3965 else if( ssl->in_offt != NULL )
3966 {
3967 return( 0 );
3968 }
3969 /* Everything else (CCS & Alerts) */
3970 else
3971 {
3972 ssl->in_msglen = 0;
3973 }
3974
3975 /*
3976 * Step B
3977 *
3978 * Fetch and decode new record if current one is fully consumed.
3979 *
3980 */
3981
3982 if( ssl->in_msglen > 0 )
3983 {
3984 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003985 return( 0 );
3986 }
3987
Hanno Becker6a582e82017-06-08 13:38:05 +01003988 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003989
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003990read_record_header:
Hanno Becker6a582e82017-06-08 13:38:05 +01003991
3992 /* Current record either fully processed or to be discarded. */
3993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003997 return( ret );
3998 }
3999
4000 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004003 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4004 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004005 {
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01004006 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4007 {
4008 /* Skip unexpected record (but not whole datagram) */
4009 ssl->next_record_offset = ssl->in_msglen
4010 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004011
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01004012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4013 "(header)" ) );
4014 }
4015 else
4016 {
4017 /* Skip invalid record and the rest of the datagram */
4018 ssl->next_record_offset = 0;
4019 ssl->in_left = 0;
4020
4021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4022 "(header)" ) );
4023 }
4024
4025 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004026 goto read_record_header;
4027 }
4028#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004029 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004030 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004031
4032 /*
4033 * Read and optionally decrypt the message contents
4034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4036 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004039 return( ret );
4040 }
4041
4042 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004044 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004046 else
4047#endif
4048 ssl->in_left = 0;
4049
4050 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004053 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004054 {
4055 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4057 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004058 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004059 /* Except when waiting for Finished as a bad mac here
4060 * probably means something went wrong in the handshake
4061 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4062 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4063 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4064 {
4065#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4066 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4067 {
4068 mbedtls_ssl_send_alert_message( ssl,
4069 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4070 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4071 }
4072#endif
4073 return( ret );
4074 }
4075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004077 if( ssl->conf->badmac_limit != 0 &&
4078 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4081 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004082 }
4083#endif
4084
Hanno Becker6a582e82017-06-08 13:38:05 +01004085 /* As above, invalid records cause
4086 * dismissal of the whole datagram. */
4087
4088 ssl->next_record_offset = 0;
4089 ssl->in_left = 0;
4090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004092 goto read_record_header;
4093 }
4094
4095 return( ret );
4096 }
4097 else
4098#endif
4099 {
4100 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4102 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 mbedtls_ssl_send_alert_message( ssl,
4105 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4106 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004107 }
4108#endif
4109 return( ret );
4110 }
4111 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004112
4113 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004114 * When we sent the last flight of the handshake, we MUST respond to a
4115 * retransmit of the peer's previous flight with a retransmit. (In
4116 * practice, only the Finished message will make it, other messages
4117 * including CCS use the old transform so they're dropped as invalid.)
4118 *
4119 * If the record we received is not a handshake message, however, it
4120 * means the peer received our last flight so we can clean up
4121 * handshake info.
4122 *
4123 * This check needs to be done before prepare_handshake() due to an edge
4124 * case: if the client immediately requests renegotiation, this
4125 * finishes the current handshake first, avoiding the new ClientHello
4126 * being mistaken for an ancient message in the current handshake.
4127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004129 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004130 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4134 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004141 return( ret );
4142 }
4143
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004144 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004145 }
4146 else
4147 {
4148 ssl_handshake_wrapup_free_hs_transform( ssl );
4149 }
4150 }
4151#endif
4152
4153 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004154 * Handle particular types of records
4155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004157 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004158 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
4159 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 }
4161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004163 {
Angus Grattonfd1c5e82018-06-20 15:43:50 +10004164 if( ssl->in_msglen != 2 )
4165 {
4166 /* Note: Standard allows for more than one 2 byte alert
4167 to be packed in a single message, but Mbed TLS doesn't
4168 currently support this. */
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4170 ssl->in_msglen ) );
4171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4172 }
4173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004175 ssl->in_msg[0], ssl->in_msg[1] ) );
4176
4177 /*
Simon Butcher94c5e3c2015-10-27 16:09:03 +00004178 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004183 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004185 }
4186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4188 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4191 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004192 }
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02004193
4194#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4195 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4196 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4197 {
4198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4199 /* Will be handled when trying to parse ServerHello */
4200 return( 0 );
4201 }
4202#endif
4203
4204#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4205 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4206 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4207 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4208 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4209 {
4210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4211 /* Will be handled in mbedtls_ssl_parse_certificate() */
4212 return( 0 );
4213 }
4214#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4215
4216 /* Silently ignore: fetch new message */
4217 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00004218 }
4219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004221
4222 return( 0 );
4223}
4224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004226{
4227 int ret;
4228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4230 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4231 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004232 {
4233 return( ret );
4234 }
4235
4236 return( 0 );
4237}
4238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004240 unsigned char level,
4241 unsigned char message )
4242{
4243 int ret;
4244
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004245 if( ssl == NULL || ssl->conf == NULL )
4246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004251 ssl->out_msglen = 2;
4252 ssl->out_msg[0] = level;
4253 ssl->out_msg[1] = message;
4254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004258 return( ret );
4259 }
4260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004262
4263 return( 0 );
4264}
4265
Paul Bakker5121ce52009-01-03 21:22:43 +00004266/*
4267 * Handshake functions
4268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004269#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4270 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4271 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4272 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4273 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4274 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4275 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4276int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004277{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4283 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4284 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004287 ssl->state++;
4288 return( 0 );
4289 }
4290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004293}
4294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4302 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4303 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004306 ssl->state++;
4307 return( 0 );
4308 }
4309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004312}
4313#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004317 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318 const mbedtls_x509_crt *crt;
4319 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4324 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4325 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004328 ssl->state++;
4329 return( 0 );
4330 }
4331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004333 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 {
4335 if( ssl->client_auth == 0 )
4336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 ssl->state++;
4339 return( 0 );
4340 }
4341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 /*
4344 * If using SSLv3 and got no cert, send an Alert message
4345 * (otherwise an empty Certificate message will be sent).
4346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4348 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 {
4350 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4352 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4353 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004356 goto write_msg;
4357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360#endif /* MBEDTLS_SSL_CLI_C */
4361#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004362 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4367 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 }
4369 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004370#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004373
4374 /*
4375 * 0 . 0 handshake type
4376 * 1 . 3 handshake length
4377 * 4 . 6 length of all certs
4378 * 7 . 9 length of cert. 1
4379 * 10 . n-1 peer certificate
4380 * n . n+2 length of cert. 2
4381 * n+3 . ... upper level cert, etc.
4382 */
4383 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004385
Paul Bakker29087132010-03-21 21:03:34 +00004386 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 {
4388 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4392 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4393 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 }
4395
4396 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4397 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4398 ssl->out_msg[i + 2] = (unsigned char)( n );
4399
4400 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4401 i += n; crt = crt->next;
4402 }
4403
4404 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4405 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4406 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4407
4408 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4410 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004411
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004412#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004413write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004414#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004415
4416 ssl->state++;
4417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004421 return( ret );
4422 }
4423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004425
Paul Bakkered27a042013-04-18 22:46:23 +02004426 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004427}
4428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004430{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004432 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004433 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004434 int authmode = ssl->conf->authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4439 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4440 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004443 ssl->state++;
4444 return( 0 );
4445 }
4446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004448 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004449 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4450 {
4451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4452 ssl->state++;
4453 return( 0 );
4454 }
4455
4456#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4457 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4458 authmode = ssl->handshake->sni_authmode;
4459#endif
4460
4461 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4462 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004463 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004464 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004466 ssl->state++;
4467 return( 0 );
4468 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004469#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004474 return( ret );
4475 }
4476
4477 ssl->state++;
4478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479#if defined(MBEDTLS_SSL_SRV_C)
4480#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004481 /*
4482 * Check if the client sent an empty certificate
4483 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004484 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004486 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004487 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4489 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4490 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004493
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004494 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004495 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004496 return( 0 );
4497 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 }
4500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4504 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004505 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4509 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4510 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4511 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004514
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004515 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004516 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004518 else
4519 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004520 }
4521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004522#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4523 MBEDTLS_SSL_PROTO_TLS1_2 */
4524#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4529 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004530 }
4531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4533 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4536 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004537 }
4538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004540
Paul Bakker5121ce52009-01-03 21:22:43 +00004541 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004543 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004544 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004545
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004546 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4550 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004551 }
4552
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004553 /* In case we tried to reuse a session but it failed */
4554 if( ssl->session_negotiate->peer_cert != NULL )
4555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4557 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004558 }
4559
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004560 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004562 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 sizeof( mbedtls_x509_crt ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004565 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004566 }
4567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004569
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004570 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004571
4572 while( i < ssl->in_hslen )
4573 {
Philippe Antoinebbc79182018-05-30 09:13:21 +02004574 if ( i + 3 > ssl->in_hslen ) {
4575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4576 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4577 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4578 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4579 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004580 if( ssl->in_msg[i] != 0 )
4581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4583 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004584 }
4585
4586 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4587 | (unsigned int) ssl->in_msg[i + 2];
4588 i += 3;
4589
4590 if( n < 128 || i + n > ssl->in_hslen )
4591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4593 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004594 }
4595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004597 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004598 if( ret != 0 )
4599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 return( ret );
4602 }
4603
4604 i += n;
4605 }
4606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004608
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004609 /*
4610 * On client, make sure the server cert doesn't change during renego to
4611 * avoid "triple handshake" attack: https://secure-resumption.com/
4612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004614 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004616 {
4617 if( ssl->session->peer_cert == NULL )
4618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
4620 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004621 }
4622
4623 if( ssl->session->peer_cert->raw.len !=
4624 ssl->session_negotiate->peer_cert->raw.len ||
4625 memcmp( ssl->session->peer_cert->raw.p,
4626 ssl->session_negotiate->peer_cert->raw.p,
4627 ssl->session->peer_cert->raw.len ) != 0 )
4628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
4630 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004631 }
4632 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004634
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004635 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004637 mbedtls_x509_crt *ca_chain;
4638 mbedtls_x509_crl *ca_crl;
4639
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004640#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004641 if( ssl->handshake->sni_ca_chain != NULL )
4642 {
4643 ca_chain = ssl->handshake->sni_ca_chain;
4644 ca_crl = ssl->handshake->sni_ca_crl;
4645 }
4646 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004647#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004648 {
4649 ca_chain = ssl->conf->ca_chain;
4650 ca_crl = ssl->conf->ca_crl;
4651 }
4652
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004653 /*
4654 * Main check: verify certificate
4655 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004656 ret = mbedtls_x509_crt_verify_with_profile(
4657 ssl->session_negotiate->peer_cert,
4658 ca_chain, ca_crl,
4659 ssl->conf->cert_profile,
4660 ssl->hostname,
4661 &ssl->session_negotiate->verify_result,
4662 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004663
4664 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004667 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004668
4669 /*
4670 * Secondary checks: always done, but change 'ret' only if it was 0
4671 */
4672
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004673#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004676
4677 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004678 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004679 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004680 {
Hanno Beckera3929ba2017-05-08 16:31:14 +01004681 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004684 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004686 }
4687 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004688#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Beckera3929ba2017-05-08 16:31:14 +01004691 ciphersuite_info,
4692 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004693 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004696 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004698 }
4699
Hanno Beckera3929ba2017-05-08 16:31:14 +01004700 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4701 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4702 * with details encoded in the verification flags. All other kinds
4703 * of error codes, including those from the user provided f_vrfy
4704 * functions, are treated as fatal and lead to a failure of
4705 * ssl_parse_certificate even if verification was optional. */
4706 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4707 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4708 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4709 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004710 ret = 0;
Hanno Beckera3929ba2017-05-08 16:31:14 +01004711 }
4712
4713 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4714 {
4715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4716 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4717 }
4718
Hanno Becker61c0c702017-05-15 16:05:15 +01004719#if defined(MBEDTLS_DEBUG_C)
4720 if( ssl->session_negotiate->verify_result != 0 )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4723 ssl->session_negotiate->verify_result ) );
4724 }
4725 else
4726 {
4727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4728 }
4729#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004730 }
4731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004733
4734 return( ret );
4735}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
4737 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
4738 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
4739 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4740 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4741 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4742 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004745{
4746 int ret;
4747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004751 ssl->out_msglen = 1;
4752 ssl->out_msg[0] = 1;
4753
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 ssl->state++;
4755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004759 return( ret );
4760 }
4761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004763
4764 return( 0 );
4765}
4766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004768{
4769 int ret;
4770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004776 return( ret );
4777 }
4778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4782 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004783 }
4784
4785 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4788 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004789 }
4790
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004791 /*
4792 * Switch to our negotiated transform and session parameters for inbound
4793 * data.
4794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004796 ssl->transform_in = ssl->transform_negotiate;
4797 ssl->session_in = ssl->session_negotiate;
4798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004800 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004803 ssl_dtls_replay_reset( ssl );
4804#endif
4805
4806 /* Increment epoch */
4807 if( ++ssl->in_epoch == 0 )
4808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4810 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004811 }
4812 }
4813 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004815 memset( ssl->in_ctr, 0, 8 );
4816
4817 /*
4818 * Set the in_msg pointer to the correct location based on IV length
4819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004821 {
4822 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4823 ssl->transform_negotiate->fixed_ivlen;
4824 }
4825 else
4826 ssl->in_msg = ssl->in_iv;
4827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4829 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4834 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004835 }
4836 }
4837#endif
4838
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 ssl->state++;
4840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004842
4843 return( 0 );
4844}
4845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
4847 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004848{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004849 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4852 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4853 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004854 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004855 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004856#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4858#if defined(MBEDTLS_SHA512_C)
4859 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004860 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4861 else
4862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863#if defined(MBEDTLS_SHA256_C)
4864 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004865 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004866 else
4867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004871 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004872 }
Paul Bakker380da532012-04-18 16:10:25 +00004873}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4878 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4879 mbedtls_md5_starts( &ssl->handshake->fin_md5 );
4880 mbedtls_sha1_starts( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4883#if defined(MBEDTLS_SHA256_C)
4884 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886#if defined(MBEDTLS_SHA512_C)
4887 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004890}
4891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004893 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4896 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4897 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4898 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4901#if defined(MBEDTLS_SHA256_C)
4902 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004903#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904#if defined(MBEDTLS_SHA512_C)
4905 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004906#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004908}
4909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4911 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4912static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004913 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004914{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4916 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004917}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004918#endif
Paul Bakker380da532012-04-18 16:10:25 +00004919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4921#if defined(MBEDTLS_SHA256_C)
4922static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004923 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004926}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004927#endif
Paul Bakker380da532012-04-18 16:10:25 +00004928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004929#if defined(MBEDTLS_SHA512_C)
4930static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004931 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004934}
Paul Bakker769075d2012-11-24 11:26:46 +01004935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004939static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004941{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004942 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004943 mbedtls_md5_context md5;
4944 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004945
Paul Bakker5121ce52009-01-03 21:22:43 +00004946 unsigned char padbuf[48];
4947 unsigned char md5sum[16];
4948 unsigned char sha1sum[20];
4949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004951 if( !session )
4952 session = ssl->session;
4953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004955
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004956 mbedtls_md5_init( &md5 );
4957 mbedtls_sha1_init( &sha1 );
4958
4959 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
4960 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004961
4962 /*
4963 * SSLv3:
4964 * hash =
4965 * MD5( master + pad2 +
4966 * MD5( handshake + sender + master + pad1 ) )
4967 * + SHA1( master + pad2 +
4968 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004969 */
4970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004972 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4973 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004974#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004977 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4978 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004979#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02004982 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004983
Paul Bakker1ef83d62012-04-11 12:09:53 +00004984 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004985
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004986 mbedtls_md5_update( &md5, (const unsigned char *) sender, 4 );
4987 mbedtls_md5_update( &md5, session->master, 48 );
4988 mbedtls_md5_update( &md5, padbuf, 48 );
4989 mbedtls_md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004990
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004991 mbedtls_sha1_update( &sha1, (const unsigned char *) sender, 4 );
4992 mbedtls_sha1_update( &sha1, session->master, 48 );
4993 mbedtls_sha1_update( &sha1, padbuf, 40 );
4994 mbedtls_sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004995
Paul Bakker1ef83d62012-04-11 12:09:53 +00004996 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004997
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004998 mbedtls_md5_starts( &md5 );
4999 mbedtls_md5_update( &md5, session->master, 48 );
5000 mbedtls_md5_update( &md5, padbuf, 48 );
5001 mbedtls_md5_update( &md5, md5sum, 16 );
5002 mbedtls_md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005003
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005004 mbedtls_sha1_starts( &sha1 );
5005 mbedtls_sha1_update( &sha1, session->master, 48 );
5006 mbedtls_sha1_update( &sha1, padbuf , 40 );
5007 mbedtls_sha1_update( &sha1, sha1sum, 20 );
5008 mbedtls_sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005011
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005012 mbedtls_md5_free( &md5 );
5013 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5016 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5017 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005020}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005023#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005024static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005025 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005026{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005027 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005028 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005029 mbedtls_md5_context md5;
5030 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005031 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005034 if( !session )
5035 session = ssl->session;
5036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005038
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005039 mbedtls_md5_init( &md5 );
5040 mbedtls_sha1_init( &sha1 );
5041
5042 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5043 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005044
Paul Bakker1ef83d62012-04-11 12:09:53 +00005045 /*
5046 * TLSv1:
5047 * hash = PRF( master, finished_label,
5048 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5049 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005052 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5053 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005054#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005057 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5058 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005059#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005062 ? "client finished"
5063 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005064
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005065 mbedtls_md5_finish( &md5, padbuf );
5066 mbedtls_sha1_finish( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005067
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005068 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005069 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005072
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005073 mbedtls_md5_free( &md5 );
5074 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5083#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005084static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005086{
5087 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005088 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005089 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005090 unsigned char padbuf[32];
5091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005093 if( !session )
5094 session = ssl->session;
5095
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005096 mbedtls_sha256_init( &sha256 );
5097
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005099
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005100 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005101
5102 /*
5103 * TLSv1.2:
5104 * hash = PRF( master, finished_label,
5105 * Hash( handshake ) )[0.11]
5106 */
5107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108#if !defined(MBEDTLS_SHA256_ALT)
5109 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005110 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005111#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005114 ? "client finished"
5115 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005116
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005117 mbedtls_sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005118
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005119 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005120 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005123
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005124 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005126 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005133static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005135{
5136 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005137 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005138 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005139 unsigned char padbuf[48];
5140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005141 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005142 if( !session )
5143 session = ssl->session;
5144
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005145 mbedtls_sha512_init( &sha512 );
5146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005148
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005149 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005150
5151 /*
5152 * TLSv1.2:
5153 * hash = PRF( master, finished_label,
5154 * Hash( handshake ) )[0.11]
5155 */
5156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005158 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5159 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005160#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005163 ? "client finished"
5164 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005165
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005166 mbedtls_sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005167
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005168 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005169 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005172
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005173 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179#endif /* MBEDTLS_SHA512_C */
5180#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005185
5186 /*
5187 * Free our handshake params
5188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189 mbedtls_ssl_handshake_free( ssl->handshake );
5190 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005191 ssl->handshake = NULL;
5192
5193 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005194 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005195 */
5196 if( ssl->transform )
5197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 mbedtls_ssl_transform_free( ssl->transform );
5199 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005200 }
5201 ssl->transform = ssl->transform_negotiate;
5202 ssl->transform_negotiate = NULL;
5203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005205}
5206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005207void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005208{
5209 int resume = ssl->handshake->resume;
5210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213#if defined(MBEDTLS_SSL_RENEGOTIATION)
5214 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005217 ssl->renego_records_seen = 0;
5218 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005219#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005220
5221 /*
5222 * Free the previous session and switch in the current one
5223 */
Paul Bakker0a597072012-09-25 21:55:46 +00005224 if( ssl->session )
5225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005227 /* RFC 7366 3.1: keep the EtM state */
5228 ssl->session_negotiate->encrypt_then_mac =
5229 ssl->session->encrypt_then_mac;
5230#endif
5231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232 mbedtls_ssl_session_free( ssl->session );
5233 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005234 }
5235 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005236 ssl->session_negotiate = NULL;
5237
Paul Bakker0a597072012-09-25 21:55:46 +00005238 /*
5239 * Add cache entry
5240 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005241 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005242 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005243 resume == 0 )
5244 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005245 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005247 }
Paul Bakker0a597072012-09-25 21:55:46 +00005248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005251 ssl->handshake->flight != NULL )
5252 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005253 /* Cancel handshake timer */
5254 ssl_set_timer( ssl, 0 );
5255
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005256 /* Keep last flight around in case we need to resend it:
5257 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005259 }
5260 else
5261#endif
5262 ssl_handshake_wrapup_free_hs_transform( ssl );
5263
Paul Bakker48916f92012-09-16 19:57:18 +00005264 ssl->state++;
5265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005267}
5268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005270{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005271 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005274
Paul Bakker92be97b2013-01-02 17:30:03 +01005275 /*
5276 * Set the out_msg pointer to the correct location based on IV length
5277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005279 {
5280 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5281 ssl->transform_negotiate->fixed_ivlen;
5282 }
5283 else
5284 ssl->out_msg = ssl->out_iv;
5285
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005286 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005291 ssl->verify_data_len = hash_len;
5292 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005293#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005294
Paul Bakker5121ce52009-01-03 21:22:43 +00005295 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005296 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5297 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005298
5299 /*
5300 * In case of session resuming, invert the client and server
5301 * ChangeCipherSpec messages order.
5302 */
Paul Bakker0a597072012-09-25 21:55:46 +00005303 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005306 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005310 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 }
5314 else
5315 ssl->state++;
5316
Paul Bakker48916f92012-09-16 19:57:18 +00005317 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005318 * Switch to our negotiated transform and session parameters for outbound
5319 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005324 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005325 {
5326 unsigned char i;
5327
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005328 /* Remember current epoch settings for resending */
5329 ssl->handshake->alt_transform_out = ssl->transform_out;
5330 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5331
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005332 /* Set sequence_number to zero */
5333 memset( ssl->out_ctr + 2, 0, 6 );
5334
5335 /* Increment epoch */
5336 for( i = 2; i > 0; i-- )
5337 if( ++ssl->out_ctr[i - 1] != 0 )
5338 break;
5339
5340 /* The loop goes to its end iff the counter is wrapping */
5341 if( i == 0 )
5342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5344 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005345 }
5346 }
5347 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005349 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005350
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005351 ssl->transform_out = ssl->transform_negotiate;
5352 ssl->session_out = ssl->session_negotiate;
5353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5355 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5360 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005361 }
5362 }
5363#endif
5364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005366 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005368#endif
5369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005373 return( ret );
5374 }
5375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005377
5378 return( 0 );
5379}
5380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005382#define SSL_MAX_HASH_LEN 36
5383#else
5384#define SSL_MAX_HASH_LEN 12
5385#endif
5386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005388{
Paul Bakker23986e52011-04-24 08:57:21 +00005389 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005390 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005391 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005394
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005395 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005400 return( ret );
5401 }
5402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5406 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005407 }
5408
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005409 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410#if defined(MBEDTLS_SSL_PROTO_SSL3)
5411 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005412 hash_len = 36;
5413 else
5414#endif
5415 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005417 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5418 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5421 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005422 }
5423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005425 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5428 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005429 }
5430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005432 ssl->verify_data_len = hash_len;
5433 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005434#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005435
Paul Bakker0a597072012-09-25 21:55:46 +00005436 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005439 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005441#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005443 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005446 }
5447 else
5448 ssl->state++;
5449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005451 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005453#endif
5454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005456
5457 return( 0 );
5458}
5459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5465 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5466 mbedtls_md5_init( &handshake->fin_md5 );
5467 mbedtls_sha1_init( &handshake->fin_sha1 );
5468 mbedtls_md5_starts( &handshake->fin_md5 );
5469 mbedtls_sha1_starts( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5472#if defined(MBEDTLS_SHA256_C)
5473 mbedtls_sha256_init( &handshake->fin_sha256 );
5474 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476#if defined(MBEDTLS_SHA512_C)
5477 mbedtls_sha512_init( &handshake->fin_sha512 );
5478 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005481
5482 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01005483
5484#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5485 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5486 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5487#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#if defined(MBEDTLS_DHM_C)
5490 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005491#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492#if defined(MBEDTLS_ECDH_C)
5493 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005494#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005495
5496#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5497 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5498#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005499}
5500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5506 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 mbedtls_md_init( &transform->md_ctx_enc );
5509 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005510}
5511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005515}
5516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005518{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005519 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005520 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005522 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005524 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005525 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005526
5527 /*
5528 * Either the pointers are now NULL or cleared properly and can be freed.
5529 * Now allocate missing structures.
5530 */
5531 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005532 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005533 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005534 }
Paul Bakker48916f92012-09-16 19:57:18 +00005535
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005536 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005537 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005538 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005539 }
Paul Bakker48916f92012-09-16 19:57:18 +00005540
Paul Bakker82788fb2014-10-20 13:59:19 +02005541 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005542 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005543 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005544 }
Paul Bakker48916f92012-09-16 19:57:18 +00005545
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005546 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005547 if( ssl->handshake == NULL ||
5548 ssl->transform_negotiate == NULL ||
5549 ssl->session_negotiate == NULL )
5550 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553 mbedtls_free( ssl->handshake );
5554 mbedtls_free( ssl->transform_negotiate );
5555 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005556
5557 ssl->handshake = NULL;
5558 ssl->transform_negotiate = NULL;
5559 ssl->session_negotiate = NULL;
5560
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005561 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005562 }
5563
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005564 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005566 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005567 ssl_handshake_params_init( ssl->handshake );
5568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005570 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5571 {
5572 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005573
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005574 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5575 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5576 else
5577 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005578
5579 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005580 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005581#endif
5582
Paul Bakker48916f92012-09-16 19:57:18 +00005583 return( 0 );
5584}
5585
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005586#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005587/* Dummy cookie callbacks for defaults */
5588static int ssl_cookie_write_dummy( void *ctx,
5589 unsigned char **p, unsigned char *end,
5590 const unsigned char *cli_id, size_t cli_id_len )
5591{
5592 ((void) ctx);
5593 ((void) p);
5594 ((void) end);
5595 ((void) cli_id);
5596 ((void) cli_id_len);
5597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005599}
5600
5601static int ssl_cookie_check_dummy( void *ctx,
5602 const unsigned char *cookie, size_t cookie_len,
5603 const unsigned char *cli_id, size_t cli_id_len )
5604{
5605 ((void) ctx);
5606 ((void) cookie);
5607 ((void) cookie_len);
5608 ((void) cli_id);
5609 ((void) cli_id_len);
5610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005612}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005613#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005614
Paul Bakker5121ce52009-01-03 21:22:43 +00005615/*
5616 * Initialize an SSL context
5617 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005618void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5619{
5620 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5621}
5622
5623/*
5624 * Setup an SSL context
5625 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005626int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005627 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005628{
Paul Bakker48916f92012-09-16 19:57:18 +00005629 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005630 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005631
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005632 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005633
5634 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005635 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005636 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005637 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5638 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005641 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005642 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005643 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 }
5645
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005646#if defined(MBEDTLS_SSL_PROTO_DTLS)
5647 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5648 {
5649 ssl->out_hdr = ssl->out_buf;
5650 ssl->out_ctr = ssl->out_buf + 3;
5651 ssl->out_len = ssl->out_buf + 11;
5652 ssl->out_iv = ssl->out_buf + 13;
5653 ssl->out_msg = ssl->out_buf + 13;
5654
5655 ssl->in_hdr = ssl->in_buf;
5656 ssl->in_ctr = ssl->in_buf + 3;
5657 ssl->in_len = ssl->in_buf + 11;
5658 ssl->in_iv = ssl->in_buf + 13;
5659 ssl->in_msg = ssl->in_buf + 13;
5660 }
5661 else
5662#endif
5663 {
5664 ssl->out_ctr = ssl->out_buf;
5665 ssl->out_hdr = ssl->out_buf + 8;
5666 ssl->out_len = ssl->out_buf + 11;
5667 ssl->out_iv = ssl->out_buf + 13;
5668 ssl->out_msg = ssl->out_buf + 13;
5669
5670 ssl->in_ctr = ssl->in_buf;
5671 ssl->in_hdr = ssl->in_buf + 8;
5672 ssl->in_len = ssl->in_buf + 11;
5673 ssl->in_iv = ssl->in_buf + 13;
5674 ssl->in_msg = ssl->in_buf + 13;
5675 }
5676
Paul Bakker48916f92012-09-16 19:57:18 +00005677 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5678 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005679
5680 return( 0 );
5681}
5682
5683/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005684 * Reset an initialized and used SSL context for re-use while retaining
5685 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005686 *
5687 * If partial is non-zero, keep data in the input buffer and client ID.
5688 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00005689 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005690static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005691{
Paul Bakker48916f92012-09-16 19:57:18 +00005692 int ret;
5693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005695
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005696 /* Cancel any possibly running timer */
5697 ssl_set_timer( ssl, 0 );
5698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699#if defined(MBEDTLS_SSL_RENEGOTIATION)
5700 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005701 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005702
5703 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
5705 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005706#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005708
Paul Bakker7eb013f2011-10-06 12:37:39 +00005709 ssl->in_offt = NULL;
5710
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005711 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005712 ssl->in_msgtype = 0;
5713 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005714 if( partial == 0 )
5715 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005717 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005718 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005719#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005721 ssl_dtls_replay_reset( ssl );
5722#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005723
5724 ssl->in_hslen = 0;
5725 ssl->nb_zero = 0;
Hanno Becker704f4932017-06-08 13:08:45 +01005726 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005727
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005728 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005729 ssl->out_msgtype = 0;
5730 ssl->out_msglen = 0;
5731 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
5733 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005734 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005735#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005736
Paul Bakker48916f92012-09-16 19:57:18 +00005737 ssl->transform_in = NULL;
5738 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005739
Hanno Becker3328d8c2018-08-13 16:35:15 +01005740 ssl->session_in = NULL;
5741 ssl->session_out = NULL;
5742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005744 if( partial == 0 )
5745 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5748 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
5751 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
5754 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005755 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005756 }
5757#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005758
Paul Bakker48916f92012-09-16 19:57:18 +00005759 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761 mbedtls_ssl_transform_free( ssl->transform );
5762 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005763 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005764 }
Paul Bakker48916f92012-09-16 19:57:18 +00005765
Paul Bakkerc0463502013-02-14 11:19:38 +01005766 if( ssl->session )
5767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 mbedtls_ssl_session_free( ssl->session );
5769 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005770 ssl->session = NULL;
5771 }
5772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005774 ssl->alpn_chosen = NULL;
5775#endif
5776
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005777#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005778 if( partial == 0 )
5779 {
5780 mbedtls_free( ssl->cli_id );
5781 ssl->cli_id = NULL;
5782 ssl->cli_id_len = 0;
5783 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005784#endif
5785
Paul Bakker48916f92012-09-16 19:57:18 +00005786 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5787 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005788
5789 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005790}
5791
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005792/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005793 * Reset an initialized and used SSL context for re-use while retaining
5794 * all application-set variables, function pointers and data.
5795 */
5796int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
5797{
5798 return( ssl_session_reset_int( ssl, 0 ) );
5799}
5800
5801/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 * SSL set accessors
5803 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005804void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00005805{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005806 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00005807}
5808
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02005809void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005810{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005811 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005812}
5813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005814#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005815void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005816{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005817 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005818}
5819#endif
5820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005822void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005823{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005824 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005825}
5826#endif
5827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005828#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005829void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005830{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005831 conf->hs_timeout_min = min;
5832 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005833}
5834#endif
5835
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005836void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00005837{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005838 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00005839}
5840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005842void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005843 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005844 void *p_vrfy )
5845{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005846 conf->f_vrfy = f_vrfy;
5847 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005850
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005851void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005852 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005853 void *p_rng )
5854{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01005855 conf->f_rng = f_rng;
5856 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00005857}
5858
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005859void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02005860 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 void *p_dbg )
5862{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005863 conf->f_dbg = f_dbg;
5864 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00005865}
5866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005867void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005868 void *p_bio,
5869 int (*f_send)(void *, const unsigned char *, size_t),
5870 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005871 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005872{
5873 ssl->p_bio = p_bio;
5874 ssl->f_send = f_send;
5875 ssl->f_recv = f_recv;
5876 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005877}
5878
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005879void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005880{
5881 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005882}
5883
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005884void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
5885 void *p_timer,
5886 void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms),
5887 int (*f_get_timer)(void *) )
5888{
5889 ssl->p_timer = p_timer;
5890 ssl->f_set_timer = f_set_timer;
5891 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005892
5893 /* Make sure we start with no timer running */
5894 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005895}
5896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005898void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005899 void *p_cache,
5900 int (*f_get_cache)(void *, mbedtls_ssl_session *),
5901 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005902{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005903 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005904 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005905 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005907#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005909#if defined(MBEDTLS_SSL_CLI_C)
5910int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005911{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005912 int ret;
5913
5914 if( ssl == NULL ||
5915 session == NULL ||
5916 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005917 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005920 }
5921
5922 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5923 return( ret );
5924
Paul Bakker0a597072012-09-25 21:55:46 +00005925 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005926
5927 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005930
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005931void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005932 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005933{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005934 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
5935 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
5936 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
5937 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005938}
5939
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005940void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005941 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005942 int major, int minor )
5943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005945 return;
5946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005947 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005948 return;
5949
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005950 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005951}
5952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005954void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01005955 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005956{
5957 conf->cert_profile = profile;
5958}
5959
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005960/* Append a new keycert entry to a (possibly empty) list */
5961static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
5962 mbedtls_x509_crt *cert,
5963 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005964{
niisato8ba6ff52018-06-25 19:05:48 +09005965 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005966
niisato8ba6ff52018-06-25 19:05:48 +09005967 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
5968 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005969 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005970
niisato8ba6ff52018-06-25 19:05:48 +09005971 new_cert->cert = cert;
5972 new_cert->key = key;
5973 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005974
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005975 /* Update head is the list was null, else add to the end */
5976 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005977 {
niisato8ba6ff52018-06-25 19:05:48 +09005978 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005979 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005980 else
5981 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005982 mbedtls_ssl_key_cert *cur = *head;
5983 while( cur->next != NULL )
5984 cur = cur->next;
niisato8ba6ff52018-06-25 19:05:48 +09005985 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005986 }
5987
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005988 return( 0 );
5989}
5990
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005991int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005992 mbedtls_x509_crt *own_cert,
5993 mbedtls_pk_context *pk_key )
5994{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02005995 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005996}
5997
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005998void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01005999 mbedtls_x509_crt *ca_chain,
6000 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006001{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006002 conf->ca_chain = ca_chain;
6003 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006006
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006007#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6008int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6009 mbedtls_x509_crt *own_cert,
6010 mbedtls_pk_context *pk_key )
6011{
6012 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6013 own_cert, pk_key ) );
6014}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006015
6016void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6017 mbedtls_x509_crt *ca_chain,
6018 mbedtls_x509_crl *ca_crl )
6019{
6020 ssl->handshake->sni_ca_chain = ca_chain;
6021 ssl->handshake->sni_ca_crl = ca_crl;
6022}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006023
6024void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6025 int authmode )
6026{
6027 ssl->handshake->sni_authmode = authmode;
6028}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006029#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006032int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006033 const unsigned char *psk, size_t psk_len,
6034 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006035{
Paul Bakker6db455e2013-09-18 17:29:31 +02006036 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006041
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006042 /* Identity len will be encoded on two bytes */
6043 if( ( psk_identity_len >> 16 ) != 0 ||
6044 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6045 {
6046 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6047 }
6048
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006049 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006050 {
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006051 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006052
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006053 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnardffb81802015-10-20 19:56:45 +02006054 conf->psk = NULL;
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006055 conf->psk_len = 0;
6056 }
6057 if( conf->psk_identity != NULL )
6058 {
6059 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnardffb81802015-10-20 19:56:45 +02006060 conf->psk_identity = NULL;
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006061 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006062 }
6063
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006064 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6065 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006066 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006067 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006068 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006069 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006070 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006071 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006072 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006073
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006074 conf->psk_len = psk_len;
6075 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006076
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006077 memcpy( conf->psk, psk, conf->psk_len );
6078 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006079
6080 return( 0 );
6081}
6082
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006083int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6084 const unsigned char *psk, size_t psk_len )
6085{
6086 if( psk == NULL || ssl->handshake == NULL )
6087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6088
6089 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6091
6092 if( ssl->handshake->psk != NULL )
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006093 {
6094 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006095 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006096 ssl->handshake->psk_len = 0;
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006097 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006098
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006099 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006100 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006101
6102 ssl->handshake->psk_len = psk_len;
6103 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6104
6105 return( 0 );
6106}
6107
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006108void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006110 size_t),
6111 void *p_psk )
6112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006113 conf->f_psk = f_psk;
6114 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006117
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006118#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006119int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006120{
6121 int ret;
6122
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006123 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6124 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6125 {
6126 mbedtls_mpi_free( &conf->dhm_P );
6127 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006128 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006130
6131 return( 0 );
6132}
6133
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006134int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006135{
6136 int ret;
6137
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006138 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6139 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6140 {
6141 mbedtls_mpi_free( &conf->dhm_P );
6142 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006143 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006144 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006145
6146 return( 0 );
6147}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006148#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006149
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006150#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6151/*
6152 * Set the minimum length for Diffie-Hellman parameters
6153 */
6154void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6155 unsigned int bitlen )
6156{
6157 conf->dhm_min_bitlen = bitlen;
6158}
6159#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6160
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02006161#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006162/*
6163 * Set allowed/preferred hashes for handshake signatures
6164 */
6165void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6166 const int *hashes )
6167{
6168 conf->sig_hashes = hashes;
6169}
Hanno Becker593b0d32017-04-07 13:25:49 +01006170#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006171
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006172#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006173/*
6174 * Set the allowed elliptic curves
6175 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006176void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006177 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006178{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006179 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006180}
Hanno Becker593b0d32017-04-07 13:25:49 +01006181#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006182
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006183#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006185{
Hanno Becker593b0d32017-04-07 13:25:49 +01006186 /* Initialize to suppress unnecessary compiler warning */
6187 size_t hostname_len = 0;
6188
6189 /* Check if new hostname is valid before
6190 * making any change to current one */
6191
6192 if( hostname != NULL )
6193 {
6194 hostname_len = strlen( hostname );
6195
6196 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6197 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6198 }
6199
6200 /* Now it's clear that we will overwrite the old hostname,
6201 * so we can free it safely */
6202
6203 if( ssl->hostname != NULL )
6204 {
6205 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6206 mbedtls_free( ssl->hostname );
6207 }
6208
6209 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006210
Paul Bakker5121ce52009-01-03 21:22:43 +00006211 if( hostname == NULL )
Hanno Becker593b0d32017-04-07 13:25:49 +01006212 {
6213 ssl->hostname = NULL;
6214 }
6215 else
6216 {
6217 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006218
Hanno Becker593b0d32017-04-07 13:25:49 +01006219 if( ssl->hostname == NULL )
6220 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006221
Hanno Becker593b0d32017-04-07 13:25:49 +01006222 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006223
Hanno Becker593b0d32017-04-07 13:25:49 +01006224 ssl->hostname[hostname_len] = '\0';
6225 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006226
6227 return( 0 );
6228}
Hanno Beckerc7845e52017-04-07 13:02:16 +01006229#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006230
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006231#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006232void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006234 const unsigned char *, size_t),
6235 void *p_sni )
6236{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006237 conf->f_sni = f_sni;
6238 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006243int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006244{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006245 size_t cur_len, tot_len;
6246 const char **p;
6247
6248 /*
Brian J Murraye7f8dc32016-11-06 04:45:15 -08006249 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6250 * MUST NOT be truncated."
6251 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006252 */
6253 tot_len = 0;
6254 for( p = protos; *p != NULL; p++ )
6255 {
6256 cur_len = strlen( *p );
6257 tot_len += cur_len;
6258
6259 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006261 }
6262
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006263 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006264
6265 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006266}
6267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006269{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006270 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006271}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006273
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006274void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006275{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006276 conf->max_major_ver = major;
6277 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006278}
6279
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006280void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006281{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006282 conf->min_major_ver = major;
6283 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006284}
6285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006287void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006288{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006289 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006290}
6291#endif
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006294void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006295{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006296 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006297}
6298#endif
6299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006301void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006302{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006303 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006304}
6305#endif
6306
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006307#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006308void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006309{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006310 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006311}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006312#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006315int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6318 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006321 }
6322
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006323 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006324
6325 return( 0 );
6326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006330void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006331{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006332 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006337void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006338{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006339 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006340}
6341#endif
6342
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006343void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006344{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006345 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006346}
6347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006349void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006350{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006351 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006352}
6353
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006354void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006355{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006356 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006357}
6358
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006359void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006360 const unsigned char period[8] )
6361{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006362 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006367#if defined(MBEDTLS_SSL_CLI_C)
6368void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006369{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006370 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006371}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006372#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006373
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006374#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006375void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6376 mbedtls_ssl_ticket_write_t *f_ticket_write,
6377 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6378 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006379{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006380 conf->f_ticket_write = f_ticket_write;
6381 conf->f_ticket_parse = f_ticket_parse;
6382 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006383}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006386
Paul Bakker5121ce52009-01-03 21:22:43 +00006387/*
6388 * SSL get accessors
6389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006391{
6392 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6393}
6394
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006395uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006396{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006397 if( ssl->session != NULL )
6398 return( ssl->session->verify_result );
6399
6400 if( ssl->session_negotiate != NULL )
6401 return( ssl->session_negotiate->verify_result );
6402
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006403 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006404}
6405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006407{
Paul Bakker926c8e42013-03-06 10:23:34 +01006408 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006409 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006412}
6413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006417 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006418 {
6419 switch( ssl->minor_ver )
6420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006422 return( "DTLSv1.0" );
6423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006425 return( "DTLSv1.2" );
6426
6427 default:
6428 return( "unknown (DTLS)" );
6429 }
6430 }
6431#endif
6432
Paul Bakker43ca69c2011-01-15 17:35:19 +00006433 switch( ssl->minor_ver )
6434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006436 return( "SSLv3.0" );
6437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006439 return( "TLSv1.0" );
6440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006442 return( "TLSv1.1" );
6443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006445 return( "TLSv1.2" );
6446
Paul Bakker43ca69c2011-01-15 17:35:19 +00006447 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006448 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006449 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006450}
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006453{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006454 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006456
Hanno Becker3328d8c2018-08-13 16:35:15 +01006457 if( transform == NULL )
6458 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460#if defined(MBEDTLS_ZLIB_SUPPORT)
6461 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6462 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006463#endif
6464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006465 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 case MBEDTLS_MODE_GCM:
6468 case MBEDTLS_MODE_CCM:
6469 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006470 transform_expansion = transform->minlen;
6471 break;
6472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 case MBEDTLS_MODE_CBC:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006474 transform_expansion = transform->maclen
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 + mbedtls_cipher_get_block_size( &transform->cipher_ctx_enc );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006476 break;
6477
6478 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006481 }
6482
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006483 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006484}
6485
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6487size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6488{
6489 size_t max_len;
6490
6491 /*
6492 * Assume mfl_code is correct since it was checked when set
6493 */
6494 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6495
6496 /*
6497 * Check if a smaller max length was negotiated
6498 */
6499 if( ssl->session_out != NULL &&
6500 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6501 {
6502 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6503 }
6504
6505 return max_len;
6506}
6507#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509#if defined(MBEDTLS_X509_CRT_PARSE_C)
6510const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006511{
6512 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006513 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006514
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006515 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006519#if defined(MBEDTLS_SSL_CLI_C)
6520int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006521{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006522 if( ssl == NULL ||
6523 dst == NULL ||
6524 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006525 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006528 }
6529
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006530 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006533
Paul Bakker5121ce52009-01-03 21:22:43 +00006534/*
Paul Bakker1961b702013-01-25 14:49:24 +01006535 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006540
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006541 if( ssl == NULL || ssl->conf == NULL )
6542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006545 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006549 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551#endif
6552
Paul Bakker1961b702013-01-25 14:49:24 +01006553 return( ret );
6554}
6555
6556/*
6557 * Perform the SSL handshake
6558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006560{
6561 int ret = 0;
6562
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006563 if( ssl == NULL || ssl->conf == NULL )
6564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006571
6572 if( ret != 0 )
6573 break;
6574 }
6575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006577
6578 return( ret );
6579}
6580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581#if defined(MBEDTLS_SSL_RENEGOTIATION)
6582#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006583/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006584 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006587{
6588 int ret;
6589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006591
6592 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6594 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006599 return( ret );
6600 }
6601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006603
6604 return( 0 );
6605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006607
6608/*
6609 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 * - any side: calling mbedtls_ssl_renegotiate(),
6611 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
6612 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02006613 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006614 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006618{
6619 int ret;
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006622
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006623 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6624 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006625
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006626 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
6627 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006629 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006631 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006632 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006633 ssl->handshake->out_msg_seq = 1;
6634 else
6635 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006636 }
6637#endif
6638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
6640 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00006641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006645 return( ret );
6646 }
6647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006649
6650 return( 0 );
6651}
6652
6653/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006654 * Renegotiate current connection on client,
6655 * or request renegotiation on server
6656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006660
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006661 if( ssl == NULL || ssl->conf == NULL )
6662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006665 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006666 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006672
6673 /* Did we already try/start sending HelloRequest? */
6674 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006676
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006677 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006678 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006682 /*
6683 * On client, either start the renegotiation process or,
6684 * if already in progress, continue the handshake
6685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006690
6691 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006694 return( ret );
6695 }
6696 }
6697 else
6698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006702 return( ret );
6703 }
6704 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006706
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006707 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006708}
6709
6710/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006711 * Check record counters and renegotiate if they're above the limit.
6712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006714{
Andres AG7fa66d42016-12-15 17:01:16 +00006715 size_t ep_len = ssl_ep_len( ssl );
6716 int in_ctr_cmp;
6717 int out_ctr_cmp;
6718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
6720 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006721 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006722 {
6723 return( 0 );
6724 }
6725
Andres AG7fa66d42016-12-15 17:01:16 +00006726 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
6727 ssl->conf->renego_period + ep_len, 8 - ep_len );
6728 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
6729 ssl->conf->renego_period + ep_len, 8 - ep_len );
6730
6731 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006732 {
6733 return( 0 );
6734 }
6735
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006738}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006740
6741/*
6742 * Receive application data decrypted from the SSL layer
6743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006745{
Hanno Becker6a582e82017-06-08 13:38:05 +01006746 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006747 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006748
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006749 if( ssl == NULL || ssl->conf == NULL )
6750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006755 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006758 return( ret );
6759
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006760 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006764 return( ret );
6765 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006766 }
6767#endif
6768
Hanno Becker6a582e82017-06-08 13:38:05 +01006769 /*
6770 * Check if renegotiation is necessary and/or handshake is
6771 * in process. If yes, perform/continue, and fall through
6772 * if an unexpected packet is received while the client
6773 * is waiting for the ServerHello.
6774 *
6775 * (There is no equivalent to the last condition on
6776 * the server-side as it is not treated as within
6777 * a handshake while waiting for the ClientHello
6778 * after a renegotiation request.)
6779 */
6780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker6a582e82017-06-08 13:38:05 +01006782 ret = ssl_check_ctr_renegotiate( ssl );
6783 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6784 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006787 return( ret );
6788 }
6789#endif
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker6a582e82017-06-08 13:38:05 +01006794 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6795 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 return( ret );
6799 }
6800 }
6801
6802 if( ssl->in_offt == NULL )
6803 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006804 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006805 if( ssl->f_get_timer != NULL &&
6806 ssl->f_get_timer( ssl->p_timer ) == -1 )
6807 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006808 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006809 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006810
Hanno Becker6a582e82017-06-08 13:38:05 +01006811 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006812 {
Hanno Becker6a582e82017-06-08 13:38:05 +01006813 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
6814 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006815
Hanno Becker6a582e82017-06-08 13:38:05 +01006816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6817 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818 }
6819
6820 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 {
6823 /*
6824 * OpenSSL sends empty messages to randomize the IV
6825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00006829 return( 0 );
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832 return( ret );
6833 }
6834 }
6835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00006837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006841 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
6843 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006846
6847 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006849 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006850 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006853 }
Hanno Becker6a582e82017-06-08 13:38:05 +01006854#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006855
Hanno Becker6a582e82017-06-08 13:38:05 +01006856#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006857 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006861
6862 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006864 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006865 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00006868 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006869#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006870
Hanno Becker3cd07be2017-10-24 11:49:19 +01006871#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckere454d732017-10-24 11:47:37 +01006872 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
6873 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
6874 ssl->conf->allow_legacy_renegotiation ==
6875 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
6876 {
6877 /* DTLS clients need to know renego is server-initiated */
6878#if defined(MBEDTLS_SSL_PROTO_DTLS)
6879 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6880 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6881 {
6882 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
6883 }
6884#endif
6885 ret = ssl_start_renegotiation( ssl );
6886 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6887 ret != 0 )
6888 {
6889 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6890 return( ret );
6891 }
6892 }
6893 else
Hanno Becker3cd07be2017-10-24 11:49:19 +01006894#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00006895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#if defined(MBEDTLS_SSL_PROTO_SSL3)
6899 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006900 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006901 /*
6902 * SSLv3 does not have a "no_renegotiation" alert
6903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006905 return( ret );
6906 }
6907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6909#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6910 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6911 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6914 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
6915 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006916 {
6917 return( ret );
6918 }
Paul Bakker48916f92012-09-16 19:57:18 +00006919 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006920 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
6922 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006926 }
Paul Bakker48916f92012-09-16 19:57:18 +00006927 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006928
Hanno Becker6a582e82017-06-08 13:38:05 +01006929 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006930 }
Hanno Becker3cd07be2017-10-24 11:49:19 +01006931#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006933 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006934 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006935 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006936 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006939 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006941 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006942 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006943 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
6947 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006950 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006951 }
6952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
6956 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006957 }
6958
6959 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006960
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006961 /* We're going to return something now, cancel timer,
6962 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006964 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006965
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006966#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006967 /* If we requested renego but received AppData, resend HelloRequest.
6968 * Do it now, after setting in_offt, to avoid taking this branch
6969 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006970#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006971 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006973 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006974 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006977 return( ret );
6978 }
6979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006981#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006982 }
6983
6984 n = ( len < ssl->in_msglen )
6985 ? len : ssl->in_msglen;
6986
6987 memcpy( buf, ssl->in_offt, n );
6988 ssl->in_msglen -= n;
6989
6990 if( ssl->in_msglen == 0 )
Hanno Beckercc019082017-06-09 10:51:37 +01006991 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006992 /* all bytes consumed */
6993 ssl->in_offt = NULL;
Hanno Beckercc019082017-06-09 10:51:37 +01006994 ssl->keep_current_message = 0;
6995 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006996 else
6997 /* more data available */
6998 ssl->in_offt += n;
6999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007001
Paul Bakker23986e52011-04-24 08:57:21 +00007002 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003}
7004
7005/*
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007006 * Send application data to be encrypted by the SSL layer, taking care of max
7007 * fragment length and buffer size.
7008 *
7009 * According to RFC 5246 Section 6.2.1:
7010 *
7011 * Zero-length fragments of Application data MAY be sent as they are
7012 * potentially useful as a traffic analysis countermeasure.
7013 *
7014 * Therefore, it is possible that the input message length is 0 and the
7015 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007016 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007017static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007018 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007019{
Paul Bakker23986e52011-04-24 08:57:21 +00007020 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007022 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florina3604112017-07-22 09:01:44 +02007023#else
7024 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7025#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007026 if( len > max_len )
7027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007029 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007032 "maximum fragment length: %d > %d",
7033 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007035 }
7036 else
7037#endif
7038 len = max_len;
7039 }
Paul Bakker887bd502011-06-08 13:10:54 +00007040
Paul Bakker5121ce52009-01-03 21:22:43 +00007041 if( ssl->out_left != 0 )
7042 {
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007043 /*
7044 * The user has previously tried to send the data and
7045 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7046 * written. In this case, we expect the high-level write function
7047 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007052 return( ret );
7053 }
7054 }
Paul Bakker887bd502011-06-08 13:10:54 +00007055 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007056 {
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007057 /*
7058 * The user is trying to send a message the first time, so we need to
7059 * copy the data into the internal buffers and setup the data structure
7060 * to keep track of partial writes
7061 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007062 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007064 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007069 return( ret );
7070 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007071 }
7072
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007073 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007074}
7075
7076/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007077 * Write application data, doing 1/n-1 splitting if necessary.
7078 *
7079 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007080 * then the caller will call us again with the same arguments, so
Hanno Beckere298c8b2017-09-18 14:58:11 +01007081 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007084static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007085 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007086{
7087 int ret;
7088
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007089 if( ssl->conf->cbc_record_splitting ==
7090 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007091 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7093 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7094 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007095 {
7096 return( ssl_write_real( ssl, buf, len ) );
7097 }
7098
7099 if( ssl->split_done == 0 )
7100 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007101 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007102 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007103 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007104 }
7105
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007106 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7107 return( ret );
7108 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007109
7110 return( ret + 1 );
7111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007113
7114/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007115 * Write application data (public-facing wrapper)
7116 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007117int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007118{
7119 int ret;
7120
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007122
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007123 if( ssl == NULL || ssl->conf == NULL )
7124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7125
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007126#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007127 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7128 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007129 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007130 return( ret );
7131 }
7132#endif
7133
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007134 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007135 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007136 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007137 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007139 return( ret );
7140 }
7141 }
7142
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007143#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007144 ret = ssl_write_split( ssl, buf, len );
7145#else
7146 ret = ssl_write_real( ssl, buf, len );
7147#endif
7148
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007150
7151 return( ret );
7152}
7153
7154/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007155 * Notify the peer that the connection is being closed
7156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007158{
7159 int ret;
7160
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007161 if( ssl == NULL || ssl->conf == NULL )
7162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007165
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007166 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7172 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7173 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007176 return( ret );
7177 }
7178 }
7179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007181
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007182 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007183}
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007186{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007187 if( transform == NULL )
7188 return;
7189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007191 deflateEnd( &transform->ctx_deflate );
7192 inflateEnd( &transform->ctx_inflate );
7193#endif
7194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7196 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198 mbedtls_md_free( &transform->md_ctx_enc );
7199 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007202}
7203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_X509_CRT_PARSE_C)
7205static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007206{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007208
7209 while( cur != NULL )
7210 {
7211 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007213 cur = next;
7214 }
7215}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007219{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007220 if( handshake == NULL )
7221 return;
7222
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007223#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7224 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7225 mbedtls_md5_free( &handshake->fin_md5 );
7226 mbedtls_sha1_free( &handshake->fin_sha1 );
7227#endif
7228#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7229#if defined(MBEDTLS_SHA256_C)
7230 mbedtls_sha256_free( &handshake->fin_sha256 );
7231#endif
7232#if defined(MBEDTLS_SHA512_C)
7233 mbedtls_sha512_free( &handshake->fin_sha512 );
7234#endif
7235#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237#if defined(MBEDTLS_DHM_C)
7238 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240#if defined(MBEDTLS_ECDH_C)
7241 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007242#endif
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02007245 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007247#endif
7248
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007249#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7250 if( handshake->psk != NULL )
7251 {
7252 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7253 mbedtls_free( handshake->psk );
7254 }
7255#endif
7256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7258 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007259 /*
7260 * Free only the linked list wrapper, not the keys themselves
7261 * since the belong to the SNI callback
7262 */
7263 if( handshake->sni_key_cert != NULL )
7264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007266
7267 while( cur != NULL )
7268 {
7269 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007271 cur = next;
7272 }
7273 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#if defined(MBEDTLS_SSL_PROTO_DTLS)
7277 mbedtls_free( handshake->verify_cookie );
7278 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007279 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007280#endif
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007283}
7284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007286{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007287 if( session == NULL )
7288 return;
7289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007291 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 mbedtls_x509_crt_free( session->peer_cert );
7294 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007295 }
Paul Bakkered27a042013-04-18 22:46:23 +02007296#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007297
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007298#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007300#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007303}
7304
Paul Bakker5121ce52009-01-03 21:22:43 +00007305/*
7306 * Free an SSL context
7307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007309{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007310 if( ssl == NULL )
7311 return;
7312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007314
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007315 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7318 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007319 }
7320
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007321 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7324 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007325 }
7326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007328 if( ssl->compress_buf != NULL )
7329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7331 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007332 }
7333#endif
7334
Paul Bakker48916f92012-09-16 19:57:18 +00007335 if( ssl->transform )
7336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 mbedtls_ssl_transform_free( ssl->transform );
7338 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007339 }
7340
7341 if( ssl->handshake )
7342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343 mbedtls_ssl_handshake_free( ssl->handshake );
7344 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7345 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 mbedtls_free( ssl->handshake );
7348 mbedtls_free( ssl->transform_negotiate );
7349 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007350 }
7351
Paul Bakkerc0463502013-02-14 11:19:38 +01007352 if( ssl->session )
7353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 mbedtls_ssl_session_free( ssl->session );
7355 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007356 }
7357
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007358#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007359 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007360 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007361 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007363 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007364#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7367 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7370 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007371 }
7372#endif
7373
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007374#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007376#endif
7377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007379
Paul Bakker86f04f42013-02-14 11:20:09 +01007380 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007382}
7383
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007384/*
7385 * Initialze mbedtls_ssl_config
7386 */
7387void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7388{
7389 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7390}
7391
Simon Butcherc941b6c2015-12-28 01:29:10 +00007392#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007393static int ssl_preset_default_hashes[] = {
7394#if defined(MBEDTLS_SHA512_C)
7395 MBEDTLS_MD_SHA512,
7396 MBEDTLS_MD_SHA384,
7397#endif
7398#if defined(MBEDTLS_SHA256_C)
7399 MBEDTLS_MD_SHA256,
7400 MBEDTLS_MD_SHA224,
7401#endif
Gilles Peskine7344e1b2017-05-12 13:16:40 +02007402#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007403 MBEDTLS_MD_SHA1,
7404#endif
7405 MBEDTLS_MD_NONE
7406};
Simon Butcherc941b6c2015-12-28 01:29:10 +00007407#endif
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007408
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007409static int ssl_preset_suiteb_ciphersuites[] = {
7410 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7411 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7412 0
7413};
7414
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007415#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007416static int ssl_preset_suiteb_hashes[] = {
7417 MBEDTLS_MD_SHA256,
7418 MBEDTLS_MD_SHA384,
7419 MBEDTLS_MD_NONE
7420};
7421#endif
7422
7423#if defined(MBEDTLS_ECP_C)
7424static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
7425 MBEDTLS_ECP_DP_SECP256R1,
7426 MBEDTLS_ECP_DP_SECP384R1,
7427 MBEDTLS_ECP_DP_NONE
7428};
7429#endif
7430
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007431/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007432 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007433 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007434int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007435 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007436{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007437#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007438 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007439#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007440
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007441 /* Use the functions here so that they are covered in tests,
7442 * but otherwise access member directly for efficiency */
7443 mbedtls_ssl_conf_endpoint( conf, endpoint );
7444 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007445
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007446 /*
7447 * Things that are common to all presets
7448 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007449#if defined(MBEDTLS_SSL_CLI_C)
7450 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7451 {
7452 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7453#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7454 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7455#endif
7456 }
7457#endif
7458
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007459#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007460 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007461#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007462
7463#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7464 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7465#endif
7466
7467#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7468 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7469#endif
7470
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007471#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7472 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7473#endif
7474
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007475#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007476 conf->f_cookie_write = ssl_cookie_write_dummy;
7477 conf->f_cookie_check = ssl_cookie_check_dummy;
7478#endif
7479
7480#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7481 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7482#endif
7483
7484#if defined(MBEDTLS_SSL_PROTO_DTLS)
7485 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7486 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7487#endif
7488
7489#if defined(MBEDTLS_SSL_RENEGOTIATION)
7490 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG7fa66d42016-12-15 17:01:16 +00007491 memset( conf->renego_period, 0x00, 2 );
7492 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007493#endif
7494
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007495#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7496 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7497 {
7498 if( ( ret = mbedtls_ssl_conf_dh_param( conf,
Hanno Becker80e0d462017-10-13 16:51:54 +01007499 MBEDTLS_DHM_RFC3526_MODP_2048_P,
7500 MBEDTLS_DHM_RFC3526_MODP_2048_G ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007501 {
7502 return( ret );
7503 }
7504 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007505#endif
7506
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007507 /*
7508 * Preset-specific defaults
7509 */
7510 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007511 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007512 /*
7513 * NSA Suite B
7514 */
7515 case MBEDTLS_SSL_PRESET_SUITEB:
7516 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7517 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7518 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7519 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7520
7521 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7522 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7523 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7524 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7525 ssl_preset_suiteb_ciphersuites;
7526
7527#if defined(MBEDTLS_X509_CRT_PARSE_C)
7528 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007529#endif
7530
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007531#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007532 conf->sig_hashes = ssl_preset_suiteb_hashes;
7533#endif
7534
7535#if defined(MBEDTLS_ECP_C)
7536 conf->curve_list = ssl_preset_suiteb_curves;
7537#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007538 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007539
7540 /*
7541 * Default
7542 */
7543 default:
Ron Eldor1ac9aa72017-05-28 10:46:38 +03007544 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7545 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7546 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7547 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7548 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7549 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
7550 MBEDTLS_SSL_MIN_MINOR_VERSION :
7551 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007552 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7553 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7554
7555#if defined(MBEDTLS_SSL_PROTO_DTLS)
7556 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7557 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
7558#endif
7559
7560 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7561 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7562 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7563 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7564 mbedtls_ssl_list_ciphersuites();
7565
7566#if defined(MBEDTLS_X509_CRT_PARSE_C)
7567 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7568#endif
7569
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007570#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007571 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007572#endif
7573
7574#if defined(MBEDTLS_ECP_C)
7575 conf->curve_list = mbedtls_ecp_grp_id_list();
7576#endif
7577
7578#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7579 conf->dhm_min_bitlen = 1024;
7580#endif
7581 }
7582
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007583 return( 0 );
7584}
7585
7586/*
7587 * Free mbedtls_ssl_config
7588 */
7589void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
7590{
7591#if defined(MBEDTLS_DHM_C)
7592 mbedtls_mpi_free( &conf->dhm_P );
7593 mbedtls_mpi_free( &conf->dhm_G );
7594#endif
7595
7596#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7597 if( conf->psk != NULL )
7598 {
7599 mbedtls_zeroize( conf->psk, conf->psk_len );
7600 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
7601 mbedtls_free( conf->psk );
7602 mbedtls_free( conf->psk_identity );
7603 conf->psk_len = 0;
7604 conf->psk_identity_len = 0;
7605 }
7606#endif
7607
7608#if defined(MBEDTLS_X509_CRT_PARSE_C)
7609 ssl_key_cert_free( conf->key_cert );
7610#endif
7611
7612 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
7613}
7614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007616/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621#if defined(MBEDTLS_RSA_C)
7622 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
7623 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007624#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625#if defined(MBEDTLS_ECDSA_C)
7626 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
7627 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007628#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007630}
7631
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007632unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
7633{
7634 switch( type ) {
7635 case MBEDTLS_PK_RSA:
7636 return( MBEDTLS_SSL_SIG_RSA );
7637 case MBEDTLS_PK_ECDSA:
7638 case MBEDTLS_PK_ECKEY:
7639 return( MBEDTLS_SSL_SIG_ECDSA );
7640 default:
7641 return( MBEDTLS_SSL_SIG_ANON );
7642 }
7643}
7644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007646{
7647 switch( sig )
7648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649#if defined(MBEDTLS_RSA_C)
7650 case MBEDTLS_SSL_SIG_RSA:
7651 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007652#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#if defined(MBEDTLS_ECDSA_C)
7654 case MBEDTLS_SSL_SIG_ECDSA:
7655 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007656#endif
7657 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007659 }
7660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661#endif /* MBEDTLS_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007662
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007663#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7664 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7665
7666/* Find an entry in a signature-hash set matching a given hash algorithm. */
7667mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7668 mbedtls_pk_type_t sig_alg )
7669{
7670 switch( sig_alg )
7671 {
7672 case MBEDTLS_PK_RSA:
7673 return( set->rsa );
7674 case MBEDTLS_PK_ECDSA:
7675 return( set->ecdsa );
7676 default:
7677 return( MBEDTLS_MD_NONE );
7678 }
7679}
7680
7681/* Add a signature-hash-pair to a signature-hash set */
7682void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7683 mbedtls_pk_type_t sig_alg,
7684 mbedtls_md_type_t md_alg )
7685{
7686 switch( sig_alg )
7687 {
7688 case MBEDTLS_PK_RSA:
7689 if( set->rsa == MBEDTLS_MD_NONE )
7690 set->rsa = md_alg;
7691 break;
7692
7693 case MBEDTLS_PK_ECDSA:
7694 if( set->ecdsa == MBEDTLS_MD_NONE )
7695 set->ecdsa = md_alg;
7696 break;
7697
7698 default:
7699 break;
7700 }
7701}
7702
7703/* Allow exactly one hash algorithm for each signature. */
7704void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7705 mbedtls_md_type_t md_alg )
7706{
7707 set->rsa = md_alg;
7708 set->ecdsa = md_alg;
7709}
7710
7711#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
7712 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
7713
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007714/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007715 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007718{
7719 switch( hash )
7720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721#if defined(MBEDTLS_MD5_C)
7722 case MBEDTLS_SSL_HASH_MD5:
7723 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007724#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SHA1_C)
7726 case MBEDTLS_SSL_HASH_SHA1:
7727 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729#if defined(MBEDTLS_SHA256_C)
7730 case MBEDTLS_SSL_HASH_SHA224:
7731 return( MBEDTLS_MD_SHA224 );
7732 case MBEDTLS_SSL_HASH_SHA256:
7733 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007734#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if defined(MBEDTLS_SHA512_C)
7736 case MBEDTLS_SSL_HASH_SHA384:
7737 return( MBEDTLS_MD_SHA384 );
7738 case MBEDTLS_SSL_HASH_SHA512:
7739 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007740#endif
7741 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007743 }
7744}
7745
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007746/*
7747 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7748 */
7749unsigned char mbedtls_ssl_hash_from_md_alg( int md )
7750{
7751 switch( md )
7752 {
7753#if defined(MBEDTLS_MD5_C)
7754 case MBEDTLS_MD_MD5:
7755 return( MBEDTLS_SSL_HASH_MD5 );
7756#endif
7757#if defined(MBEDTLS_SHA1_C)
7758 case MBEDTLS_MD_SHA1:
7759 return( MBEDTLS_SSL_HASH_SHA1 );
7760#endif
7761#if defined(MBEDTLS_SHA256_C)
7762 case MBEDTLS_MD_SHA224:
7763 return( MBEDTLS_SSL_HASH_SHA224 );
7764 case MBEDTLS_MD_SHA256:
7765 return( MBEDTLS_SSL_HASH_SHA256 );
7766#endif
7767#if defined(MBEDTLS_SHA512_C)
7768 case MBEDTLS_MD_SHA384:
7769 return( MBEDTLS_SSL_HASH_SHA384 );
7770 case MBEDTLS_MD_SHA512:
7771 return( MBEDTLS_SSL_HASH_SHA512 );
7772#endif
7773 default:
7774 return( MBEDTLS_SSL_HASH_NONE );
7775 }
7776}
7777
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007778#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007779/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007780 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007781 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007782 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007783int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007784{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007786
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007787 if( ssl->conf->curve_list == NULL )
7788 return( -1 );
7789
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007790 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007791 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007792 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007793
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007794 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007795}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007796#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007797
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007798#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007799/*
7800 * Check if a hash proposed by the peer is in our list.
7801 * Return 0 if we're willing to use it, -1 otherwise.
7802 */
7803int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
7804 mbedtls_md_type_t md )
7805{
7806 const int *cur;
7807
7808 if( ssl->conf->sig_hashes == NULL )
7809 return( -1 );
7810
7811 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
7812 if( *cur == (int) md )
7813 return( 0 );
7814
7815 return( -1 );
7816}
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007817#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819#if defined(MBEDTLS_X509_CRT_PARSE_C)
7820int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
7821 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007822 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007823 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007824{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007825 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007827 int usage = 0;
7828#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007830 const char *ext_oid;
7831 size_t ext_len;
7832#endif
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7835 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007836 ((void) cert);
7837 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007838 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007839#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
7842 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007843 {
7844 /* Server part of the key exchange */
7845 switch( ciphersuite->key_exchange )
7846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 case MBEDTLS_KEY_EXCHANGE_RSA:
7848 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007849 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007850 break;
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7853 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7854 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7855 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007856 break;
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7859 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007860 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007861 break;
7862
7863 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 case MBEDTLS_KEY_EXCHANGE_NONE:
7865 case MBEDTLS_KEY_EXCHANGE_PSK:
7866 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7867 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007868 usage = 0;
7869 }
7870 }
7871 else
7872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7874 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007875 }
7876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007878 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007879 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007880 ret = -1;
7881 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007882#else
7883 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
7887 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 ext_oid = MBEDTLS_OID_SERVER_AUTH;
7890 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007891 }
7892 else
7893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
7895 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007896 }
7897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007899 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007900 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007901 ret = -1;
7902 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007904
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007905 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007908
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007909/*
7910 * Convert version numbers to/from wire format
7911 * and, for DTLS, to/from TLS equivalent.
7912 *
7913 * For TLS this is the identity.
Brian J Murraye7f8dc32016-11-06 04:45:15 -08007914 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007915 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
7916 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
7917 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007919 unsigned char ver[2] )
7920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921#if defined(MBEDTLS_SSL_PROTO_DTLS)
7922 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007925 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7926
7927 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
7928 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
7929 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007930 else
7931#else
7932 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007933#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007934 {
7935 ver[0] = (unsigned char) major;
7936 ver[1] = (unsigned char) minor;
7937 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007938}
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007941 const unsigned char ver[2] )
7942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_SSL_PROTO_DTLS)
7944 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007945 {
7946 *major = 255 - ver[0] + 2;
7947 *minor = 255 - ver[1] + 1;
7948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007950 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7951 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007952 else
7953#else
7954 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007955#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007956 {
7957 *major = ver[0];
7958 *minor = ver[1];
7959 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007960}
7961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962#endif /* MBEDTLS_SSL_TLS_C */