blob: 00ae9fcc364c2daa74b983b0afaacfaae55c1a8a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/debug.h"
39#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020040#include "mbedtls/ssl_internal.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
45 defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020055#endif
56
Paul Bakker34617722014-06-13 17:20:13 +020057/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020059 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
60}
61
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010062/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020066 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010068#else
69 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070#endif
71 return( 0 );
72}
73
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074/*
75 * Start a timer.
76 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020079{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020080 if( ssl->f_set_timer == NULL )
81 return;
82
83 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
84 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020085}
86
87/*
88 * Return -1 is timer is expired, 0 if it isn't.
89 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020091{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020092 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020093 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020094
95 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020096 {
97 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200100
101 return( 0 );
102}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200105/*
106 * Double the retransmit timeout value, within the allowed range,
107 * returning -1 if the maximum value has already been reached.
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200110{
111 uint32_t new_timeout;
112
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200113 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200114 return( -1 );
115
116 new_timeout = 2 * ssl->handshake->retransmit_timeout;
117
118 /* Avoid arithmetic overflow and range overflow */
119 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200120 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200121 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200122 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200123 }
124
125 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200127 ssl->handshake->retransmit_timeout ) );
128
129 return( 0 );
130}
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200133{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200134 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200136 ssl->handshake->retransmit_timeout ) );
137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200141/*
142 * Convert max_fragment_length codes to length.
143 * RFC 6066 says:
144 * enum{
145 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
146 * } MaxFragmentLength;
147 * and we add 0 -> extension unused
148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149static unsigned int mfl_code_to_length[MBEDTLS_SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 MBEDTLS_SSL_MAX_CONTENT_LEN, /* MBEDTLS_SSL_MAX_FRAG_LEN_NONE */
152 512, /* MBEDTLS_SSL_MAX_FRAG_LEN_512 */
153 1024, /* MBEDTLS_SSL_MAX_FRAG_LEN_1024 */
154 2048, /* MBEDTLS_SSL_MAX_FRAG_LEN_2048 */
155 4096, /* MBEDTLS_SSL_MAX_FRAG_LEN_4096 */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200156};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200158
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200159#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_ssl_session_free( dst );
163 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200166 if( src->peer_cert != NULL )
167 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200168 int ret;
169
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200170 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200171 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200172 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200177 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180 dst->peer_cert = NULL;
181 return( ret );
182 }
183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200186#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187 if( src->ticket != NULL )
188 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200189 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200190 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200191 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200192
193 memcpy( dst->ticket, src->ticket, src->ticket_len );
194 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200195#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200196
197 return( 0 );
198}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200199#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
202int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200203 const unsigned char *key_enc, const unsigned char *key_dec,
204 size_t keylen,
205 const unsigned char *iv_enc, const unsigned char *iv_dec,
206 size_t ivlen,
207 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200208 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
210int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
211int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
212int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
213int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
214#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000215
Paul Bakker5121ce52009-01-03 21:22:43 +0000216/*
217 * Key material generation
218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200220static int ssl3_prf( const unsigned char *secret, size_t slen,
221 const char *label,
222 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000223 unsigned char *dstbuf, size_t dlen )
224{
225 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200226 mbedtls_md5_context md5;
227 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000228 unsigned char padding[16];
229 unsigned char sha1sum[20];
230 ((void)label);
231
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200232 mbedtls_md5_init( &md5 );
233 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200234
Paul Bakker5f70b252012-09-13 14:23:06 +0000235 /*
236 * SSLv3:
237 * block =
238 * MD5( secret + SHA1( 'A' + secret + random ) ) +
239 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
240 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
241 * ...
242 */
243 for( i = 0; i < dlen / 16; i++ )
244 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200245 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000246
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200247 mbedtls_sha1_starts( &sha1 );
248 mbedtls_sha1_update( &sha1, padding, 1 + i );
249 mbedtls_sha1_update( &sha1, secret, slen );
250 mbedtls_sha1_update( &sha1, random, rlen );
251 mbedtls_sha1_finish( &sha1, sha1sum );
Paul Bakker5f70b252012-09-13 14:23:06 +0000252
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200253 mbedtls_md5_starts( &md5 );
254 mbedtls_md5_update( &md5, secret, slen );
255 mbedtls_md5_update( &md5, sha1sum, 20 );
256 mbedtls_md5_finish( &md5, dstbuf + i * 16 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000257 }
258
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200259 mbedtls_md5_free( &md5 );
260 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_zeroize( padding, sizeof( padding ) );
263 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000264
265 return( 0 );
266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200270static int tls1_prf( const unsigned char *secret, size_t slen,
271 const char *label,
272 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000273 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000274{
Paul Bakker23986e52011-04-24 08:57:21 +0000275 size_t nb, hs;
276 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200277 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 unsigned char tmp[128];
279 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 const mbedtls_md_info_t *md_info;
281 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100282 int ret;
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
289 hs = ( slen + 1 ) / 2;
290 S1 = secret;
291 S2 = secret + slen - hs;
292
293 nb = strlen( label );
294 memcpy( tmp + 20, label, nb );
295 memcpy( tmp + 20 + nb, random, rlen );
296 nb += rlen;
297
298 /*
299 * First compute P_md5(secret,label+random)[0..dlen]
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100305 return( ret );
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
308 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
309 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 for( i = 0; i < dlen; i += 16 )
312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_md_hmac_reset ( &md_ctx );
314 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
315 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_md_hmac_reset ( &md_ctx );
318 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
319 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
322
323 for( j = 0; j < k; j++ )
324 dstbuf[i + j] = h_i[j];
325 }
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100328
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 /*
330 * XOR out with P_sha1(secret,label+random)[0..dlen]
331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100336 return( ret );
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
339 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
340 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342 for( i = 0; i < dlen; i += 20 )
343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_md_hmac_reset ( &md_ctx );
345 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
346 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_md_hmac_reset ( &md_ctx );
349 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
350 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
353
354 for( j = 0; j < k; j++ )
355 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
356 }
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_zeroize( tmp, sizeof( tmp ) );
361 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
363 return( 0 );
364}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
368static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100369 const unsigned char *secret, size_t slen,
370 const char *label,
371 const unsigned char *random, size_t rlen,
372 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373{
374 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100375 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000376 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
378 const mbedtls_md_info_t *md_info;
379 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100380 int ret;
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
385 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100388
389 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000391
392 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100393 memcpy( tmp + md_len, label, nb );
394 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000395 nb += rlen;
396
397 /*
398 * Compute P_<hash>(secret, label + random)[0..dlen]
399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100401 return( ret );
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
404 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
405 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100406
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100407 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_md_hmac_reset ( &md_ctx );
410 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
411 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_md_hmac_reset ( &md_ctx );
414 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
415 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000416
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100417 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000418
419 for( j = 0; j < k; j++ )
420 dstbuf[i + j] = h_i[j];
421 }
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_zeroize( tmp, sizeof( tmp ) );
426 mbedtls_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000427
428 return( 0 );
429}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100432static int tls_prf_sha256( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
435 unsigned char *dstbuf, size_t dlen )
436{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100438 label, random, rlen, dstbuf, dlen ) );
439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200443static int tls_prf_sha384( const unsigned char *secret, size_t slen,
444 const char *label,
445 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 unsigned char *dstbuf, size_t dlen )
447{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100449 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#endif /* MBEDTLS_SHA512_C */
452#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
457 defined(MBEDTLS_SSL_PROTO_TLS1_1)
458static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200459#endif
Paul Bakker380da532012-04-18 16:10:25 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_SSL_PROTO_SSL3)
462static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
463static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200464#endif
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
467static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
468static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200469#endif
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
472#if defined(MBEDTLS_SHA256_C)
473static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
474static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
475static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200476#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#if defined(MBEDTLS_SHA512_C)
479static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
480static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
481static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000486{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200487 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 unsigned char keyblk[256];
490 unsigned char *key1;
491 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100492 unsigned char *mac_enc;
493 unsigned char *mac_dec;
Hanno Becker64f0aed2017-11-09 18:39:33 +0000494 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200495 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 const mbedtls_cipher_info_t *cipher_info;
497 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_ssl_session *session = ssl->session_negotiate;
500 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
501 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100506 if( cipher_info == NULL )
507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100509 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100511 }
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100514 if( md_info == NULL )
515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100517 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100519 }
520
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000522 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_SSL_PROTO_SSL3)
525 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000526 {
Paul Bakker48916f92012-09-16 19:57:18 +0000527 handshake->tls_prf = ssl3_prf;
528 handshake->calc_verify = ssl_calc_verify_ssl;
529 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000530 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200531 else
532#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
534 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000535 {
Paul Bakker48916f92012-09-16 19:57:18 +0000536 handshake->tls_prf = tls1_prf;
537 handshake->calc_verify = ssl_calc_verify_tls;
538 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000539 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200540 else
541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
543#if defined(MBEDTLS_SHA512_C)
544 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
545 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000546 {
Paul Bakker48916f92012-09-16 19:57:18 +0000547 handshake->tls_prf = tls_prf_sha384;
548 handshake->calc_verify = ssl_calc_verify_tls_sha384;
549 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000550 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000551 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_SHA256_C)
554 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000555 {
Paul Bakker48916f92012-09-16 19:57:18 +0000556 handshake->tls_prf = tls_prf_sha256;
557 handshake->calc_verify = ssl_calc_verify_tls_sha256;
558 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000559 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200560 else
561#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200566 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000567
568 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 * SSLv3:
570 * master =
571 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
572 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
573 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200574 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200575 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 * master = PRF( premaster, "master secret", randbytes )[0..47]
577 */
Paul Bakker0a597072012-09-25 21:55:46 +0000578 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000581 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
584 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200585 {
586 unsigned char session_hash[48];
587 size_t hash_len;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200590
591 ssl->handshake->calc_verify( ssl, session_hash );
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
594 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200597 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200599 {
600 hash_len = 48;
601 }
602 else
603#endif
604 hash_len = 32;
605 }
606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200608 hash_len = 36;
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200611
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100612 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
613 "extended master secret",
614 session_hash, hash_len,
615 session->master, 48 );
616 if( ret != 0 )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100619 return( ret );
620 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200621
622 }
623 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200624#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100625 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
626 "master secret",
627 handshake->randbytes, 64,
628 session->master, 48 );
629 if( ret != 0 )
630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100632 return( ret );
633 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 }
637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 /*
641 * Swap the client and server random values.
642 */
Paul Bakker48916f92012-09-16 19:57:18 +0000643 memcpy( tmp, handshake->randbytes, 64 );
644 memcpy( handshake->randbytes, tmp + 32, 32 );
645 memcpy( handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647
648 /*
649 * SSLv3:
650 * key block =
651 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
652 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
653 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
654 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
655 * ...
656 *
657 * TLSv1:
658 * key block = PRF( master, "key expansion", randbytes )
659 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100660 ret = handshake->tls_prf( session->master, 48, "key expansion",
661 handshake->randbytes, 64, keyblk, 256 );
662 if( ret != 0 )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100665 return( ret );
666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
669 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
670 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
671 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
672 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 /*
677 * Determine the appropriate key, IV and MAC length.
678 */
Paul Bakker68884e32013-01-07 18:20:04 +0100679
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200680 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
683 cipher_info->mode == MBEDTLS_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200685 transform->maclen = 0;
Hanno Becker64f0aed2017-11-09 18:39:33 +0000686 mac_key_len = 0;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200687
Paul Bakker68884e32013-01-07 18:20:04 +0100688 transform->ivlen = 12;
689 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200690
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200691 /* Minimum length is expicit IV + tag */
692 transform->minlen = transform->ivlen - transform->fixed_ivlen
693 + ( transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100695 }
696 else
697 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200698 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
700 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200703 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100704 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200706 /* Get MAC length */
Hanno Becker64f0aed2017-11-09 18:39:33 +0000707 mac_key_len = mbedtls_md_get_size( md_info );
708 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200711 /*
712 * If HMAC is to be truncated, we shall keep the leftmost bytes,
713 * (rfc 6066 page 13 or rfc 2104 section 4),
714 * so we only need to adjust the length here.
715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Becker053b3452017-11-20 16:36:41 +0000717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Becker053b3452017-11-20 16:36:41 +0000719
720#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
721 /* Fall back to old, non-compliant version of the truncated
Hanno Beckeradb30b92017-11-21 17:20:17 +0000722 * HMAC implementation which also truncates the key (pre 2.1.10) */
Hanno Becker053b3452017-11-20 16:36:41 +0000723 mac_key_len = transform->maclen;
724#endif
725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200727
728 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100729 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200731 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200733 transform->minlen = transform->maclen;
734 else
Paul Bakker68884e32013-01-07 18:20:04 +0100735 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200736 /*
737 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100738 * 1. if EtM is in use: one block plus MAC
739 * otherwise: * first multiple of blocklen greater than maclen
740 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
743 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100744 {
745 transform->minlen = transform->maclen
746 + cipher_info->block_size;
747 }
748 else
749#endif
750 {
751 transform->minlen = transform->maclen
752 + cipher_info->block_size
753 - transform->maclen % cipher_info->block_size;
754 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
757 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
758 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200759 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100760 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
763 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
764 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200765 {
766 transform->minlen += transform->ivlen;
767 }
768 else
769#endif
770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
772 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200773 }
Paul Bakker68884e32013-01-07 18:20:04 +0100774 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 }
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000778 transform->keylen, transform->minlen, transform->ivlen,
779 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
781 /*
782 * Finally setup the cipher contexts, IVs and MAC secrets.
783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200785 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 {
Hanno Becker64f0aed2017-11-09 18:39:33 +0000787 key1 = keyblk + mac_key_len * 2;
788 key2 = keyblk + mac_key_len * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000789
Paul Bakker68884e32013-01-07 18:20:04 +0100790 mac_enc = keyblk;
Hanno Becker64f0aed2017-11-09 18:39:33 +0000791 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000793 /*
794 * This is not used in TLS v1.1.
795 */
Paul Bakker48916f92012-09-16 19:57:18 +0000796 iv_copy_len = ( transform->fixed_ivlen ) ?
797 transform->fixed_ivlen : transform->ivlen;
798 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
799 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000800 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 }
802 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_SSL_CLI_C */
804#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200805 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 {
Hanno Becker64f0aed2017-11-09 18:39:33 +0000807 key1 = keyblk + mac_key_len * 2 + transform->keylen;
808 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
Hanno Becker64f0aed2017-11-09 18:39:33 +0000810 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100811 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000812
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000813 /*
814 * This is not used in TLS v1.1.
815 */
Paul Bakker48916f92012-09-16 19:57:18 +0000816 iv_copy_len = ( transform->fixed_ivlen ) ?
817 transform->fixed_ivlen : transform->ivlen;
818 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
819 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000820 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100827 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_SSL_PROTO_SSL3)
830 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100831 {
Hanno Becker64f0aed2017-11-09 18:39:33 +0000832 if( mac_key_len > sizeof transform->mac_enc )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100836 }
837
Hanno Becker64f0aed2017-11-09 18:39:33 +0000838 memcpy( transform->mac_enc, mac_enc, mac_key_len );
839 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +0100840 }
841 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#endif /* MBEDTLS_SSL_PROTO_SSL3 */
843#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
844 defined(MBEDTLS_SSL_PROTO_TLS1_2)
845 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100846 {
Gilles Peskine823734b2018-03-19 19:06:08 +0100847 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
848 For AEAD-based ciphersuites, there is nothing to do here. */
849 if( mac_key_len != 0 )
850 {
851 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
852 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
853 }
Paul Bakker68884e32013-01-07 18:20:04 +0100854 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855 else
856#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200860 }
Paul Bakker68884e32013-01-07 18:20:04 +0100861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
863 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000864 {
865 int ret = 0;
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100870 transform->iv_enc, transform->iv_dec,
871 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100872 mac_enc, mac_dec,
Hanno Becker64f0aed2017-11-09 18:39:33 +0000873 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
876 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000877 }
878 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000880
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200881 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200882 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200885 return( ret );
886 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200887
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200888 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200889 cipher_info ) ) != 0 )
890 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200892 return( ret );
893 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200896 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200900 return( ret );
901 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200904 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200908 return( ret );
909 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_CIPHER_MODE_CBC)
912 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
915 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200918 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200919 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
922 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200925 return( ret );
926 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +0000933 // Initialize compression
934 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000936 {
Paul Bakker16770332013-10-11 09:59:44 +0200937 if( ssl->compress_buf == NULL )
938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200940 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +0200941 if( ssl->compress_buf == NULL )
942 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_SSL_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200945 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +0200946 }
947 }
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000950
Paul Bakker48916f92012-09-16 19:57:18 +0000951 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
952 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000953
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200954 if( deflateInit( &transform->ctx_deflate,
955 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000956 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
959 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000960 }
961 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +0000963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
966 return( 0 );
967}
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_SSL_PROTO_SSL3)
970void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000971{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200972 mbedtls_md5_context md5;
973 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 unsigned char pad_1[48];
975 unsigned char pad_2[48];
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000978
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +0200979 mbedtls_md5_init( &md5 );
980 mbedtls_sha1_init( &sha1 );
981
982 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
983 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Paul Bakker380da532012-04-18 16:10:25 +0000985 memset( pad_1, 0x36, 48 );
986 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200988 mbedtls_md5_update( &md5, ssl->session_negotiate->master, 48 );
989 mbedtls_md5_update( &md5, pad_1, 48 );
990 mbedtls_md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200992 mbedtls_md5_starts( &md5 );
993 mbedtls_md5_update( &md5, ssl->session_negotiate->master, 48 );
994 mbedtls_md5_update( &md5, pad_2, 48 );
995 mbedtls_md5_update( &md5, hash, 16 );
996 mbedtls_md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200998 mbedtls_sha1_update( &sha1, ssl->session_negotiate->master, 48 );
999 mbedtls_sha1_update( &sha1, pad_1, 40 );
1000 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001001
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001002 mbedtls_sha1_starts( &sha1 );
1003 mbedtls_sha1_update( &sha1, ssl->session_negotiate->master, 48 );
1004 mbedtls_sha1_update( &sha1, pad_2, 40 );
1005 mbedtls_sha1_update( &sha1, hash + 16, 20 );
1006 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001010
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001011 mbedtls_md5_free( &md5 );
1012 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001013
Paul Bakker380da532012-04-18 16:10:25 +00001014 return;
1015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1019void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001020{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001021 mbedtls_md5_context md5;
1022 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001025
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001026 mbedtls_md5_init( &md5 );
1027 mbedtls_sha1_init( &sha1 );
1028
1029 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1030 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001031
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001032 mbedtls_md5_finish( &md5, hash );
1033 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001037
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001038 mbedtls_md5_free( &md5 );
1039 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001040
Paul Bakker380da532012-04-18 16:10:25 +00001041 return;
1042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1046#if defined(MBEDTLS_SHA256_C)
1047void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001048{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001049 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001050
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001051 mbedtls_sha256_init( &sha256 );
1052
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001054
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001055 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001056 mbedtls_sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001060
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001061 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001062
Paul Bakker380da532012-04-18 16:10:25 +00001063 return;
1064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#if defined(MBEDTLS_SHA512_C)
1068void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001069{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001070 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001071
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001072 mbedtls_sha512_init( &sha512 );
1073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001075
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001076 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1077 mbedtls_sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001082 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001083
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 return;
1085}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_SHA512_C */
1087#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1090int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001091{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001092 unsigned char *p = ssl->handshake->premaster;
1093 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001094 const unsigned char *psk = ssl->conf->psk;
1095 size_t psk_len = ssl->conf->psk_len;
1096
1097 /* If the psk callback was called, use its result */
1098 if( ssl->handshake->psk != NULL )
1099 {
1100 psk = ssl->handshake->psk;
1101 psk_len = ssl->handshake->psk_len;
1102 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001103
1104 /*
1105 * PMS = struct {
1106 * opaque other_secret<0..2^16-1>;
1107 * opaque psk<0..2^16-1>;
1108 * };
1109 * with "other_secret" depending on the particular key exchange
1110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1112 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001113 {
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001114 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001116
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001117 *(p++) = (unsigned char)( psk_len >> 8 );
1118 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001119
1120 if( end < p || (size_t)( end - p ) < psk_len )
1121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1122
1123 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001124 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001125 }
1126 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1128#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1129 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001130 {
1131 /*
1132 * other_secret already set by the ClientKeyExchange message,
1133 * and is 48 bytes long
1134 */
Philippe Antoinebbc79182018-05-30 09:13:21 +02001135 if( end - p < 2 )
1136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1137
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001138 *p++ = 0;
1139 *p++ = 48;
1140 p += 48;
1141 }
1142 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1144#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1145 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001146 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001147 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001148 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001149
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001150 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001152 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001153 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001156 return( ret );
1157 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001158 *(p++) = (unsigned char)( len >> 8 );
1159 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001160 p += len;
1161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001163 }
1164 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1166#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1167 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001168 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001169 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001170 size_t zlen;
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001173 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001174 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001177 return( ret );
1178 }
1179
1180 *(p++) = (unsigned char)( zlen >> 8 );
1181 *(p++) = (unsigned char)( zlen );
1182 p += zlen;
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001185 }
1186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1190 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001191 }
1192
1193 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001194 if( end - p < 2 )
1195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001196
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001197 *(p++) = (unsigned char)( psk_len >> 8 );
1198 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001199
1200 if( end < p || (size_t)( end - p ) < psk_len )
1201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1202
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001203 memcpy( p, psk, psk_len );
1204 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001205
1206 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1207
1208 return( 0 );
1209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001213/*
1214 * SSLv3.0 MAC functions
1215 */
Manuel Pégourié-Gonnard4b133e62017-12-19 10:03:46 +01001216#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001217static void ssl_mac( mbedtls_md_context_t *md_ctx,
1218 const unsigned char *secret,
1219 const unsigned char *buf, size_t len,
1220 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnard4b133e62017-12-19 10:03:46 +01001221 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001222{
1223 unsigned char header[11];
1224 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001225 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1227 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001228
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001229 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001231 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001232 else
Paul Bakker68884e32013-01-07 18:20:04 +01001233 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
1235 memcpy( header, ctr, 8 );
1236 header[ 8] = (unsigned char) type;
1237 header[ 9] = (unsigned char)( len >> 8 );
1238 header[10] = (unsigned char)( len );
1239
Paul Bakker68884e32013-01-07 18:20:04 +01001240 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_md_starts( md_ctx );
1242 mbedtls_md_update( md_ctx, secret, md_size );
1243 mbedtls_md_update( md_ctx, padding, padlen );
1244 mbedtls_md_update( md_ctx, header, 11 );
1245 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001246 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Paul Bakker68884e32013-01-07 18:20:04 +01001248 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_md_starts( md_ctx );
1250 mbedtls_md_update( md_ctx, secret, md_size );
1251 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001252 mbedtls_md_update( md_ctx, out, md_size );
1253 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1258 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1259 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001260#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001261#endif
1262
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02001263/* The function below is only used in the Lucky 13 counter-measure in
1264 * ssl_decrypt_buf(). These are the defines that guard the call site. */
1265#if defined(SSL_SOME_MODES_USE_MAC) && \
1266 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1267 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1268 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1269/* This function makes sure every byte in the memory region is accessed
1270 * (in ascending addresses order) */
1271static void ssl_read_memory( unsigned char *p, size_t len )
1272{
1273 unsigned char acc = 0;
1274 volatile unsigned char force;
1275
1276 for( ; len != 0; p++, len-- )
1277 acc ^= *p;
1278
1279 force = acc;
1280 (void) force;
1281}
1282#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1283
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001284/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001290 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001294 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001298 }
1299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001303 ssl->out_msg, ssl->out_msglen );
1304
Hanno Beckeraede1832017-09-18 10:55:31 +01001305 if( ssl->out_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
1306 {
Hanno Becker6e052b02017-10-04 13:47:33 +01001307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1308 (unsigned) ssl->out_msglen,
Hanno Beckeraede1832017-09-18 10:55:31 +01001309 MBEDTLS_SSL_MAX_CONTENT_LEN ) );
1310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1311 }
1312
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001314 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001316#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 if( mode == MBEDTLS_MODE_STREAM ||
1318 ( mode == MBEDTLS_MODE_CBC
1319#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1320 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001321#endif
1322 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_SSL_PROTO_SSL3)
1325 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001326 {
Manuel Pégourié-Gonnard4b133e62017-12-19 10:03:46 +01001327 unsigned char mac[SSL_MAC_MAX_BYTES];
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001328
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001329 ssl_mac( &ssl->transform_out->md_ctx_enc,
1330 ssl->transform_out->mac_enc,
1331 ssl->out_msg, ssl->out_msglen,
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001332 ssl->out_ctr, ssl->out_msgtype,
1333 mac );
1334
1335 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001336 }
1337 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1340 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1341 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001342 {
Hanno Beckerce516ff2017-11-09 18:57:39 +00001343 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1346 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1347 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1348 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001349 ssl->out_msg, ssl->out_msglen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001350 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001352
1353 memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001354 }
1355 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001356#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001360 }
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001363 ssl->out_msg + ssl->out_msglen,
1364 ssl->transform_out->maclen );
1365
1366 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001367 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001368 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001369#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001371 /*
1372 * Encrypt
1373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1375 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001377 int ret;
1378 size_t olen = 0;
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 "including %d bytes of padding",
1382 ssl->out_msglen, 0 ) );
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001385 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001386 ssl->transform_out->ivlen,
1387 ssl->out_msg, ssl->out_msglen,
1388 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001391 return( ret );
1392 }
1393
1394 if( ssl->out_msglen != olen )
1395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1397 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
Paul Bakker68884e32013-01-07 18:20:04 +01001400 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1402#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1403 if( mode == MBEDTLS_MODE_GCM ||
1404 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001405 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001406 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001407 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001408 unsigned char *enc_msg;
1409 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001410 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001412
Paul Bakkerca4ab492012-04-18 14:23:57 +00001413 memcpy( add_data, ssl->out_ctr, 8 );
1414 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001416 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001417 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1418 add_data[12] = ssl->out_msglen & 0xFF;
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakkerca4ab492012-04-18 14:23:57 +00001421 add_data, 13 );
1422
Paul Bakker68884e32013-01-07 18:20:04 +01001423 /*
1424 * Generate IV
1425 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001426 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1427 {
1428 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1430 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001431 }
1432
1433 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1434 ssl->out_ctr, 8 );
1435 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001438 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001439
Paul Bakker68884e32013-01-07 18:20:04 +01001440 /*
1441 * Fix pointer positions and message length with added IV
1442 */
1443 enc_msg = ssl->out_msg;
1444 enc_msglen = ssl->out_msglen;
1445 ssl->out_msglen += ssl->transform_out->ivlen -
1446 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker68884e32013-01-07 18:20:04 +01001449 "including %d bytes of padding",
1450 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001451
Paul Bakker68884e32013-01-07 18:20:04 +01001452 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001453 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001456 ssl->transform_out->iv_enc,
1457 ssl->transform_out->ivlen,
1458 add_data, 13,
1459 enc_msg, enc_msglen,
1460 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001461 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001464 return( ret );
1465 }
1466
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001467 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001471 }
1472
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001473 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001474 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1480#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1481 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1482 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001484 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001485 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001486 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001487
Paul Bakker48916f92012-09-16 19:57:18 +00001488 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1489 ssl->transform_out->ivlen;
1490 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 padlen = 0;
1492
1493 for( i = 0; i <= padlen; i++ )
1494 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1495
1496 ssl->out_msglen += padlen + 1;
1497
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001498 enc_msglen = ssl->out_msglen;
1499 enc_msg = ssl->out_msg;
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001502 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001503 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1504 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001507 {
1508 /*
1509 * Generate IV
1510 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001511 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001512 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001513 if( ret != 0 )
1514 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001515
Paul Bakker92be97b2013-01-02 17:30:03 +01001516 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001517 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001518
1519 /*
1520 * Fix pointer positions and message length with added IV
1521 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001522 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001523 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001524 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001529 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001530 ssl->out_msglen, ssl->transform_out->ivlen,
1531 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001534 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001535 ssl->transform_out->ivlen,
1536 enc_msg, enc_msglen,
1537 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001540 return( ret );
1541 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001542
Paul Bakkercca5b812013-08-31 17:40:26 +02001543 if( enc_msglen != olen )
1544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001547 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1550 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001551 {
1552 /*
1553 * Save IV in SSL3 and TLS1
1554 */
1555 memcpy( ssl->transform_out->iv_enc,
1556 ssl->transform_out->cipher_ctx_enc.iv,
1557 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001559#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001562 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001563 {
Hanno Becker394767c2018-01-05 16:24:22 +00001564 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1565
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001566 /*
1567 * MAC(MAC_write_key, seq_num +
1568 * TLSCipherText.type +
1569 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001570 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001571 * IV + // except for TLS 1.0
1572 * ENC(content + padding + padding_length));
1573 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001574 unsigned char pseudo_hdr[13];
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001577
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001578 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1579 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001580 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1581 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1586 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001587 ssl->out_iv, ssl->out_msglen );
Hanno Becker394767c2018-01-05 16:24:22 +00001588 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001590
Hanno Becker394767c2018-01-05 16:24:22 +00001591 memcpy( ssl->out_iv + ssl->out_msglen, mac,
1592 ssl->transform_out->maclen );
1593
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001594 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001595 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001596 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001599 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1601 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001607 /* Make extra sure authentication was performed, exactly once */
1608 if( auth_done != 1 )
1609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001612 }
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
1616 return( 0 );
1617}
1618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001620{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001621 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001623 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001624#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001625 size_t padlen = 0, correct = 1;
1626#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001629
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001630 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001634 }
1635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001637
Paul Bakker48916f92012-09-16 19:57:18 +00001638 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001641 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 }
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1646 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001647 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001648 int ret;
1649 size_t olen = 0;
1650
Paul Bakker68884e32013-01-07 18:20:04 +01001651 padlen = 0;
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001654 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001655 ssl->transform_in->ivlen,
1656 ssl->in_msg, ssl->in_msglen,
1657 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001660 return( ret );
1661 }
1662
1663 if( ssl->in_msglen != olen )
1664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1666 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 }
Paul Bakker68884e32013-01-07 18:20:04 +01001669 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1671#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1672 if( mode == MBEDTLS_MODE_GCM ||
1673 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001674 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001675 int ret;
1676 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001677 unsigned char *dec_msg;
1678 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001679 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001680 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001682 size_t explicit_iv_len = ssl->transform_in->ivlen -
1683 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001684
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001685 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001688 "+ taglen (%d)", ssl->in_msglen,
1689 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001691 }
1692 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1693
Paul Bakker68884e32013-01-07 18:20:04 +01001694 dec_msg = ssl->in_msg;
1695 dec_msg_result = ssl->in_msg;
1696 ssl->in_msglen = dec_msglen;
1697
1698 memcpy( add_data, ssl->in_ctr, 8 );
1699 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001701 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001702 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1703 add_data[12] = ssl->in_msglen & 0xFF;
1704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001706 add_data, 13 );
1707
1708 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1709 ssl->in_iv,
1710 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001713 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001715
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001716 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001717 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001720 ssl->transform_in->iv_dec,
1721 ssl->transform_in->ivlen,
1722 add_data, 13,
1723 dec_msg, dec_msglen,
1724 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001725 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1730 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001731
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001732 return( ret );
1733 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001734 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001735
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001736 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1739 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001740 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001742 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1744#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1745 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1746 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 {
Paul Bakker45829992013-01-03 14:52:21 +01001748 /*
1749 * Decrypt and check the padding
1750 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001751 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001752 unsigned char *dec_msg;
1753 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001754 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001755 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001756 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001757
Paul Bakker5121ce52009-01-03 21:22:43 +00001758 /*
Paul Bakker45829992013-01-03 14:52:21 +01001759 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1762 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001763 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001764#endif
Paul Bakker45829992013-01-03 14:52:21 +01001765
1766 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1767 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001770 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1771 ssl->transform_in->ivlen,
1772 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001774 }
1775
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001776 dec_msglen = ssl->in_msglen;
1777 dec_msg = ssl->in_msg;
1778 dec_msg_result = ssl->in_msg;
1779
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001780 /*
1781 * Authenticate before decrypt if enabled
1782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1784 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001785 {
Hanno Beckerce516ff2017-11-09 18:57:39 +00001786 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001787 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001790
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001791 dec_msglen -= ssl->transform_in->maclen;
1792 ssl->in_msglen -= ssl->transform_in->maclen;
1793
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001794 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1795 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1796 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1797 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1802 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001803 ssl->in_iv, ssl->in_msglen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001804 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001808 ssl->transform_in->maclen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00001809 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001810 ssl->transform_in->maclen );
1811
Hanno Beckerce516ff2017-11-09 18:57:39 +00001812 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect,
1813 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001818 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001819 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001822
1823 /*
1824 * Check length sanity
1825 */
1826 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001829 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001831 }
1832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001834 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001835 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001838 {
Paul Bakker48916f92012-09-16 19:57:18 +00001839 dec_msglen -= ssl->transform_in->ivlen;
1840 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001841
Paul Bakker48916f92012-09-16 19:57:18 +00001842 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001843 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001844 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001848 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001849 ssl->transform_in->ivlen,
1850 dec_msg, dec_msglen,
1851 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001854 return( ret );
1855 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001856
Paul Bakkercca5b812013-08-31 17:40:26 +02001857 if( dec_msglen != olen )
1858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1860 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001861 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1864 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001865 {
1866 /*
1867 * Save IV in SSL3 and TLS1
1868 */
1869 memcpy( ssl->transform_in->iv_dec,
1870 ssl->transform_in->cipher_ctx_dec.iv,
1871 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001873#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
1875 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001876
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001877 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001878 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880#if defined(MBEDTLS_SSL_DEBUG_ALL)
1881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01001882 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001883#endif
Paul Bakker45829992013-01-03 14:52:21 +01001884 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001885 correct = 0;
1886 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#if defined(MBEDTLS_SSL_PROTO_SSL3)
1889 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 {
Paul Bakker48916f92012-09-16 19:57:18 +00001891 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893#if defined(MBEDTLS_SSL_DEBUG_ALL)
1894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001896 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001897#endif
Paul Bakker45829992013-01-03 14:52:21 +01001898 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 }
1900 }
1901 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1903#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1904 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1905 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 {
1907 /*
Paul Bakker45829992013-01-03 14:52:21 +01001908 * TLSv1+: always check the padding up to the first failure
1909 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001911 size_t pad_count = 0, real_count = 1;
Angus Gratton1226dd72018-06-19 15:57:50 +10001912 size_t padding_idx = ssl->in_msglen - padlen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001913
Paul Bakker956c9e02013-12-19 14:42:28 +01001914 /*
1915 * Padding is guaranteed to be incorrect if:
Angus Gratton1226dd72018-06-19 15:57:50 +10001916 * 1. padlen > ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001917 *
Angus Gratton1226dd72018-06-19 15:57:50 +10001918 * 2. padding_idx > MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001919 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001920 *
1921 * In both cases we reset padding_idx to a safe value (0) to
1922 * prevent out-of-buffer reads.
1923 */
Angus Gratton1226dd72018-06-19 15:57:50 +10001924 correct &= ( padlen <= ssl->in_msglen );
1925 correct &= ( padding_idx <= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001926 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001927
1928 padding_idx *= correct;
1929
Angus Gratton1226dd72018-06-19 15:57:50 +10001930 for( i = 0; i < 256; i++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001931 {
Angus Gratton1226dd72018-06-19 15:57:50 +10001932 real_count &= ( i < padlen );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001933 pad_count += real_count *
1934 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1935 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001936
1937 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001940 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001942#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001943 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001945 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1947 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001951 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001952
1953 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001955 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001956#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1957 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Manuel Pégourié-Gonnard671f9322018-07-10 11:15:36 +02001963#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard671f9322018-07-10 11:15:36 +02001966#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001967
1968 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001969 * Authenticate if not done yet.
1970 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001972#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001973 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001974 {
Hanno Beckerce516ff2017-11-09 18:57:39 +00001975 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001976
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001977 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001979 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1980 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_SSL_PROTO_SSL3)
1983 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001984 {
1985 ssl_mac( &ssl->transform_in->md_ctx_dec,
1986 ssl->transform_in->mac_dec,
1987 ssl->in_msg, ssl->in_msglen,
Manuel Pégourié-Gonnardb67a5c12017-12-18 18:04:59 +01001988 ssl->in_ctr, ssl->in_msgtype,
1989 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001990 }
1991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1993#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1994 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1995 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001996 {
1997 /*
1998 * Process MAC and always update for padlen afterwards to make
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02001999 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002000 *
2001 * Known timing attacks:
2002 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2003 *
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02002004 * To compensate for different timings for the MAC calculation
2005 * depending on how much padding was removed (which is determined
2006 * by padlen), process extra_run more blocks through the hash
2007 * function.
2008 *
2009 * The formula in the paper is
2010 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2011 * where L1 is the size of the header plus the decrypted message
2012 * plus CBC padding and L2 is the size of the header plus the
2013 * decrypted message. This is for an underlying hash function
2014 * with 64-byte blocks.
2015 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2016 * correctly. We round down instead of up, so -56 is the correct
2017 * value for our calculations instead of -55.
2018 *
2019 * Repeat the formula rather than defining a block_size variable.
2020 * This avoids requiring division by a variable at runtime
2021 * (which would be marginally less efficient and would require
2022 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002023 */
2024 size_t j, extra_run = 0;
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002025
2026 /*
2027 * The next two sizes are the minimum and maximum values of
2028 * in_msglen over all padlen values.
2029 *
2030 * They're independent of padlen, since we previously did
2031 * in_msglen -= padlen.
2032 *
2033 * Note that max_len + maclen is never more than the buffer
2034 * length, as we previously did in_msglen -= maclen too.
2035 */
2036 const size_t max_len = ssl->in_msglen + padlen;
2037 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2038
Gilles Peskinee8dd77b2018-06-06 17:24:50 +02002039 switch( ssl->transform_in->ciphersuite_info->mac )
2040 {
2041#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2042 defined(MBEDTLS_SHA256_C)
2043 case MBEDTLS_MD_MD5:
2044 case MBEDTLS_MD_SHA1:
2045 case MBEDTLS_MD_SHA256:
2046 /* 8 bytes of message size, 64-byte compression blocks */
2047 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
2048 ( 13 + ssl->in_msglen + 8 ) / 64;
2049 break;
2050#endif
2051#if defined(MBEDTLS_SHA512_C)
2052 case MBEDTLS_MD_SHA384:
2053 /* 16 bytes of message size, 128-byte compression blocks */
2054 extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 -
2055 ( 13 + ssl->in_msglen + 16 ) / 128;
2056 break;
2057#endif
2058 default:
2059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2061 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002062
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002063 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
2066 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
2067 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
2068 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002069 ssl->in_msglen );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002070 /* Make sure we access everything even when padlen > 0. This
2071 * makes the synchronisation requirements for just-in-time
2072 * Prime+Probe attacks much tighter and hopefully impractical. */
2073 ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen );
Hanno Beckerce516ff2017-11-09 18:57:39 +00002074 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002075
2076 /* Call mbedtls_md_process at least once due to cache attacks
2077 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002078 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002082
2083 /* Make sure we access all the memory that could contain the MAC,
2084 * before we check it in the next code block. This makes the
2085 * synchronisation requirements for just-in-time Prime+Probe
2086 * attacks much tighter and hopefully impractical. */
2087 ssl_read_memory( ssl->in_msg + min_len,
2088 max_len - min_len + ssl->transform_in->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002089 }
2090 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2092 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002097
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002098#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Beckerce516ff2017-11-09 18:57:39 +00002099 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen );
2100 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen,
2101 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard99b6a712018-06-28 10:38:35 +02002102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002103
Hanno Beckerce516ff2017-11-09 18:57:39 +00002104 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect,
2105 ssl->transform_in->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107#if defined(MBEDTLS_SSL_DEBUG_ALL)
2108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002109#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002110 correct = 0;
2111 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002112 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002114 /*
2115 * Finally check the correct flag
2116 */
2117 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002119 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002120#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121
2122 /* Make extra sure authentication was performed, exactly once */
2123 if( auth_done != 1 )
2124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2126 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002127 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
2129 if( ssl->in_msglen == 0 )
2130 {
Angus Gratton485b3932018-06-19 15:58:22 +10002131#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
2132 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
2133 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
2134 {
2135 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
2136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
2137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2138 }
2139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
2140
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 ssl->nb_zero++;
2142
2143 /*
2144 * Three or more empty messages may be a DoS attack
2145 * (excessive CPU consumption).
2146 */
2147 if( ssl->nb_zero > 3 )
2148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152 }
2153 }
2154 else
2155 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002159 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002160 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002161 }
2162 else
2163#endif
2164 {
2165 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2166 if( ++ssl->in_ctr[i - 1] != 0 )
2167 break;
2168
2169 /* The loop goes to its end iff the counter is wrapping */
2170 if( i == ssl_ep_len( ssl ) )
2171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2173 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002174 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002175 }
2176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002178
2179 return( 0 );
2180}
2181
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002182#undef MAC_NONE
2183#undef MAC_PLAINTEXT
2184#undef MAC_CIPHERTEXT
2185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002187/*
2188 * Compression/decompression functions
2189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002191{
2192 int ret;
2193 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002194 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002195 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002196 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002199
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002200 if( len_pre == 0 )
2201 return( 0 );
2202
Paul Bakker2770fbd2012-07-03 13:30:23 +00002203 memcpy( msg_pre, ssl->out_msg, len_pre );
2204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002206 ssl->out_msglen ) );
2207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002209 ssl->out_msg, ssl->out_msglen );
2210
Paul Bakker48916f92012-09-16 19:57:18 +00002211 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2212 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2213 ssl->transform_out->ctx_deflate.next_out = msg_post;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002214 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002215
Paul Bakker48916f92012-09-16 19:57:18 +00002216 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002217 if( ret != Z_OK )
2218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2220 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002221 }
2222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurekbb666142018-04-23 08:29:36 -04002224 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002227 ssl->out_msglen ) );
2228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002230 ssl->out_msg, ssl->out_msglen );
2231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002233
2234 return( 0 );
2235}
2236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002238{
2239 int ret;
2240 unsigned char *msg_post = ssl->in_msg;
Andrzej Kurek078014a2018-04-24 06:32:44 -04002241 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002242 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002243 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002246
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002247 if( len_pre == 0 )
2248 return( 0 );
2249
Paul Bakker2770fbd2012-07-03 13:30:23 +00002250 memcpy( msg_pre, ssl->in_msg, len_pre );
2251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002253 ssl->in_msglen ) );
2254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002256 ssl->in_msg, ssl->in_msglen );
2257
Paul Bakker48916f92012-09-16 19:57:18 +00002258 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2259 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2260 ssl->transform_in->ctx_inflate.next_out = msg_post;
Andrzej Kurekbb666142018-04-23 08:29:36 -04002261 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek078014a2018-04-24 06:32:44 -04002262 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002263
Paul Bakker48916f92012-09-16 19:57:18 +00002264 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002265 if( ret != Z_OK )
2266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2268 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002269 }
2270
Andrzej Kurekbb666142018-04-23 08:29:36 -04002271 ssl->in_msglen = MBEDTLS_SSL_BUFFER_LEN -
Andrzej Kurek078014a2018-04-24 06:32:44 -04002272 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002275 ssl->in_msglen ) );
2276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002278 ssl->in_msg, ssl->in_msglen );
2279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002281
2282 return( 0 );
2283}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2287static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#if defined(MBEDTLS_SSL_PROTO_DTLS)
2290static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002291{
2292 /* If renegotiation is not enforced, retransmit until we would reach max
2293 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002294 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002295 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002296 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002297 unsigned char doublings = 1;
2298
2299 while( ratio != 0 )
2300 {
2301 ++doublings;
2302 ratio >>= 1;
2303 }
2304
2305 if( ++ssl->renego_records_seen > doublings )
2306 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002308 return( 0 );
2309 }
2310 }
2311
2312 return( ssl_write_hello_request( ssl ) );
2313}
2314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002316
Paul Bakker5121ce52009-01-03 21:22:43 +00002317/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002318 * Fill the input message buffer by appending data to it.
2319 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002320 *
2321 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2322 * available (from this read and/or a previous one). Otherwise, an error code
2323 * is returned (possibly EOF or WANT_READ).
2324 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002325 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2326 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2327 * since we always read a whole datagram at once.
2328 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002329 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002330 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002333{
Paul Bakker23986e52011-04-24 08:57:21 +00002334 int ret;
2335 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002339 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002342 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002344 }
2345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002350 }
2351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002355 uint32_t timeout;
2356
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002357 /* Just to be sure */
2358 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2359 {
2360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2361 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2362 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2363 }
2364
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002365 /*
2366 * The point is, we need to always read a full datagram at once, so we
2367 * sometimes read more then requested, and handle the additional data.
2368 * It could be the rest of the current record (while fetching the
2369 * header) and/or some other records in the same datagram.
2370 */
2371
2372 /*
2373 * Move to the next record in the already read datagram if applicable
2374 */
2375 if( ssl->next_record_offset != 0 )
2376 {
2377 if( ssl->in_left < ssl->next_record_offset )
2378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2380 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002381 }
2382
2383 ssl->in_left -= ssl->next_record_offset;
2384
2385 if( ssl->in_left != 0 )
2386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002388 ssl->next_record_offset ) );
2389 memmove( ssl->in_hdr,
2390 ssl->in_hdr + ssl->next_record_offset,
2391 ssl->in_left );
2392 }
2393
2394 ssl->next_record_offset = 0;
2395 }
2396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002399
2400 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002401 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002402 */
2403 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002406 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002407 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002408
2409 /*
2410 * A record can't be split accross datagrams. If we need to read but
2411 * are not at the beginning of a new record, the caller did something
2412 * wrong.
2413 */
2414 if( ssl->in_left != 0 )
2415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002418 }
2419
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002420 /*
2421 * Don't even try to read if time's out already.
2422 * This avoids by-passing the timer when repeatedly receiving messages
2423 * that will end up being dropped.
2424 */
2425 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002426 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002427 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002429 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002432 timeout = ssl->handshake->retransmit_timeout;
2433 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002434 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002437
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002438 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002439 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2440 timeout );
2441 else
2442 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002445
2446 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002448 }
2449
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002450 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002453 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002456 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002457 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002460 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002461 }
2462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002466 return( ret );
2467 }
2468
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002469 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002472 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002474 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002475 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002477 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002478 return( ret );
2479 }
2480
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002481 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002482 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002484 }
2485
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 if( ret < 0 )
2487 return( ret );
2488
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002489 ssl->in_left = ret;
2490 }
2491 else
2492#endif
2493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002495 ssl->in_left, nb_want ) );
2496
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002497 while( ssl->in_left < nb_want )
2498 {
2499 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002500
2501 if( ssl_check_timer( ssl ) != 0 )
2502 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2503 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002504 {
2505 if( ssl->f_recv_timeout != NULL )
2506 {
2507 ret = ssl->f_recv_timeout( ssl->p_bio,
2508 ssl->in_hdr + ssl->in_left, len,
2509 ssl->conf->read_timeout );
2510 }
2511 else
2512 {
2513 ret = ssl->f_recv( ssl->p_bio,
2514 ssl->in_hdr + ssl->in_left, len );
2515 }
2516 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002519 ssl->in_left, nb_want ) );
2520 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002521
2522 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002524
2525 if( ret < 0 )
2526 return( ret );
2527
mohammad1603f72e51f2018-03-28 23:44:39 -07002528 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad160389c12ec2018-03-19 07:15:50 -07002529 {
Jaeden Ameroee6c8222018-04-03 12:07:19 +01002530 MBEDTLS_SSL_DEBUG_MSG( 1,
2531 ( "f_recv returned %d bytes but only %lu were requested",
mohammad1603ad2908c2018-04-02 07:30:32 -07002532 ret, (unsigned long)len ) );
mohammad160389c12ec2018-03-19 07:15:50 -07002533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2534 }
2535
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002536 ssl->in_left += ret;
2537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 }
2539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
2542 return( 0 );
2543}
2544
2545/*
2546 * Flush any data not yet written
2547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002549{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002550 int ret;
2551 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002555 if( ssl->f_send == NULL )
2556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002558 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002560 }
2561
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002562 /* Avoid incrementing counter if data is flushed */
2563 if( ssl->out_left == 0 )
2564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002566 return( 0 );
2567 }
2568
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 while( ssl->out_left > 0 )
2570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2572 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002575 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002576 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002578 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
2580 if( ret <= 0 )
2581 return( ret );
2582
mohammad1603f72e51f2018-03-28 23:44:39 -07002583 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad1603f65add42018-02-22 04:29:04 -08002584 {
Jaeden Ameroee6c8222018-04-03 12:07:19 +01002585 MBEDTLS_SSL_DEBUG_MSG( 1,
2586 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad1603ad2908c2018-04-02 07:30:32 -07002587 ret, (unsigned long)ssl->out_left ) );
mohammad1603f65add42018-02-22 04:29:04 -08002588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2589 }
2590
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 ssl->out_left -= ret;
2592 }
2593
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002594 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002595 if( ++ssl->out_ctr[i - 1] != 0 )
2596 break;
2597
2598 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002599 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2602 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002603 }
2604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002606
2607 return( 0 );
2608}
2609
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002610/*
2611 * Functions to handle the DTLS retransmission state machine
2612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002614/*
2615 * Append current handshake message to current outgoing flight
2616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002620
2621 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002622 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002623 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002626 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002627 }
2628
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002629 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002630 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002633 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002634 }
2635
2636 /* Copy current handshake message with headers */
2637 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2638 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002639 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002640 msg->next = NULL;
2641
2642 /* Append to the current flight */
2643 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002644 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002645 else
2646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002648 while( cur->next != NULL )
2649 cur = cur->next;
2650 cur->next = msg;
2651 }
2652
2653 return( 0 );
2654}
2655
2656/*
2657 * Free the current flight of handshake messages
2658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002660{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_ssl_flight_item *cur = flight;
2662 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002663
2664 while( cur != NULL )
2665 {
2666 next = cur->next;
2667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 mbedtls_free( cur->p );
2669 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002670
2671 cur = next;
2672 }
2673}
2674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2676static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002677#endif
2678
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002679/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002680 * Swap transform_out and out_ctr with the alternative ones
2681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002685 unsigned char tmp_out_ctr[8];
2686
2687 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002690 return;
2691 }
2692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002694
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002695 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002696 tmp_transform = ssl->transform_out;
2697 ssl->transform_out = ssl->handshake->alt_transform_out;
2698 ssl->handshake->alt_transform_out = tmp_transform;
2699
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002700 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002701 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2702 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2703 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002704
2705 /* Adjust to the newly activated transform */
2706 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002708 {
2709 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2710 ssl->transform_out->fixed_ivlen;
2711 }
2712 else
2713 ssl->out_msg = ssl->out_iv;
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2716 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2721 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002722 }
2723 }
2724#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002725}
2726
2727/*
2728 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002729 *
2730 * Need to remember the current message in case flush_output returns
2731 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002732 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002741
2742 ssl->handshake->cur_msg = ssl->handshake->flight;
2743 ssl_swap_epochs( ssl );
2744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002746 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002747
2748 while( ssl->handshake->cur_msg != NULL )
2749 {
2750 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002752
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002753 /* Swap epochs before sending Finished: we can't do it after
2754 * sending ChangeCipherSpec, in case write returns WANT_READ.
2755 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2757 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002758 {
2759 ssl_swap_epochs( ssl );
2760 }
2761
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002762 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002763 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002764 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002765
2766 ssl->handshake->cur_msg = cur->next;
2767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002773 return( ret );
2774 }
2775 }
2776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2778 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002779 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002782 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2783 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002786
2787 return( 0 );
2788}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002789
2790/*
2791 * To be called when the last message of an incoming flight is received.
2792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002794{
2795 /* We won't need to resend that one any more */
2796 ssl_flight_free( ssl->handshake->flight );
2797 ssl->handshake->flight = NULL;
2798 ssl->handshake->cur_msg = NULL;
2799
2800 /* The next incoming flight will start with this msg_seq */
2801 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2802
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002803 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002804 ssl_set_timer( ssl, 0 );
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2807 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002810 }
2811 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002813}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002814
2815/*
2816 * To be called when the last message of an outgoing flight is send.
2817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002819{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002820 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002821 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2824 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002827 }
2828 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002830}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002832
Paul Bakker5121ce52009-01-03 21:22:43 +00002833/*
2834 * Record layer functions
2835 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002836
2837/*
2838 * Write current record.
2839 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002842{
Paul Bakker05ef8352012-05-08 09:17:57 +00002843 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002844 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002849 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002850 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002852 {
2853 ; /* Skip special handshake treatment when resending */
2854 }
2855 else
2856#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 {
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002859 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST &&
2860 ssl->handshake == NULL )
2861 {
2862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2863 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2864 }
2865
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2867 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2868 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2869
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002870 /*
2871 * DTLS has additional fields in the Handshake layer,
2872 * between the length field and the actual payload:
2873 * uint16 message_seq;
2874 * uint24 fragment_offset;
2875 * uint24 fragment_length;
2876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002878 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002879 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002880 /* Make room for the additional DTLS fields */
Hanno Becker0983dc42017-09-18 10:55:54 +01002881 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
2882 {
2883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2884 "size %u, maximum %u",
2885 (unsigned) ( ssl->in_hslen - 4 ),
2886 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
2887 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2888 }
2889
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002890 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002891 ssl->out_msglen += 8;
2892 len += 8;
2893
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002894 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002896 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002897 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2898 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2899 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002900 }
2901 else
2902 {
2903 ssl->out_msg[4] = 0;
2904 ssl->out_msg[5] = 0;
2905 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002906
2907 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2908 memset( ssl->out_msg + 6, 0x00, 3 );
2909 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002914 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 }
2916
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002917 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002919 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002920 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
2922 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
2923 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002924 {
2925 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002928 return( ret );
2929 }
2930 }
2931#endif
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002934 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936 {
2937 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940 return( ret );
2941 }
2942
2943 len = ssl->out_msglen;
2944 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2948 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 ret = mbedtls_ssl_hw_record_write( ssl );
2953 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2956 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002957 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002958
2959 if( ret == 0 )
2960 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002961 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002963 if( !done )
2964 {
2965 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002967 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002968
2969 ssl->out_len[0] = (unsigned char)( len >> 8 );
2970 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002971
Paul Bakker48916f92012-09-16 19:57:18 +00002972 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002973 {
2974 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002977 return( ret );
2978 }
2979
2980 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002981 ssl->out_len[0] = (unsigned char)( len >> 8 );
2982 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002983 }
2984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00002988 "version = [%d:%d], msglen = %d",
2989 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002990 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
2993 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 }
2995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999 return( ret );
3000 }
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
3004 return( 0 );
3005}
3006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003008/*
3009 * Mark bits in bitmask (used for DTLS HS reassembly)
3010 */
3011static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3012{
3013 unsigned int start_bits, end_bits;
3014
3015 start_bits = 8 - ( offset % 8 );
3016 if( start_bits != 8 )
3017 {
3018 size_t first_byte_idx = offset / 8;
3019
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003020 /* Special case */
3021 if( len <= start_bits )
3022 {
3023 for( ; len != 0; len-- )
3024 mask[first_byte_idx] |= 1 << ( start_bits - len );
3025
3026 /* Avoid potential issues with offset or len becoming invalid */
3027 return;
3028 }
3029
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003030 offset += start_bits; /* Now offset % 8 == 0 */
3031 len -= start_bits;
3032
3033 for( ; start_bits != 0; start_bits-- )
3034 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3035 }
3036
3037 end_bits = len % 8;
3038 if( end_bits != 0 )
3039 {
3040 size_t last_byte_idx = ( offset + len ) / 8;
3041
3042 len -= end_bits; /* Now len % 8 == 0 */
3043
3044 for( ; end_bits != 0; end_bits-- )
3045 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3046 }
3047
3048 memset( mask + offset / 8, 0xFF, len / 8 );
3049}
3050
3051/*
3052 * Check that bitmask is full
3053 */
3054static int ssl_bitmask_check( unsigned char *mask, size_t len )
3055{
3056 size_t i;
3057
3058 for( i = 0; i < len / 8; i++ )
3059 if( mask[i] != 0xFF )
3060 return( -1 );
3061
3062 for( i = 0; i < len % 8; i++ )
3063 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3064 return( -1 );
3065
3066 return( 0 );
3067}
3068
3069/*
3070 * Reassemble fragmented DTLS handshake messages.
3071 *
3072 * Use a temporary buffer for reassembly, divided in two parts:
3073 * - the first holds the reassembled message (including handshake header),
3074 * - the second holds a bitmask indicating which parts of the message
3075 * (excluding headers) have been received so far.
3076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003078{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003079 unsigned char *msg, *bitmask;
3080 size_t frag_len, frag_off;
3081 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
3082
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003083 if( ssl->handshake == NULL )
3084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
3086 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003087 }
3088
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003089 /*
3090 * For first fragment, check size and allocate buffer
3091 */
3092 if( ssl->handshake->hs_msg == NULL )
3093 {
3094 size_t alloc_len;
3095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003097 msg_len ) );
3098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
3102 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003103 }
3104
3105 /* The bitmask needs one bit per byte of message excluding header */
3106 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
3107
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003108 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003109 if( ssl->handshake->hs_msg == NULL )
3110 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003113 }
3114
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003115 /* Prepare final header: copy msg_type, length and message_seq,
3116 * then add standardised fragment_offset and fragment_length */
3117 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
3118 memset( ssl->handshake->hs_msg + 6, 0, 3 );
3119 memcpy( ssl->handshake->hs_msg + 9,
3120 ssl->handshake->hs_msg + 1, 3 );
3121 }
3122 else
3123 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003124 /* Make sure msg_type and length are consistent */
3125 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
3128 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003129 }
3130 }
3131
3132 msg = ssl->handshake->hs_msg + 12;
3133 bitmask = msg + msg_len;
3134
3135 /*
3136 * Check and copy current fragment
3137 */
3138 frag_off = ( ssl->in_msg[6] << 16 ) |
3139 ( ssl->in_msg[7] << 8 ) |
3140 ssl->in_msg[8];
3141 frag_len = ( ssl->in_msg[9] << 16 ) |
3142 ( ssl->in_msg[10] << 8 ) |
3143 ssl->in_msg[11];
3144
3145 if( frag_off + frag_len > msg_len )
3146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003148 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003150 }
3151
3152 if( frag_len + 12 > ssl->in_msglen )
3153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003155 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003157 }
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003160 frag_off, frag_len ) );
3161
3162 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3163 ssl_bitmask_set( bitmask, frag_off, frag_len );
3164
3165 /*
3166 * Do we have the complete message by now?
3167 * If yes, finalize it, else ask to read the next record.
3168 */
3169 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003172 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003173 }
3174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003176
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003177 if( frag_len + 12 < ssl->in_msglen )
3178 {
3179 /*
3180 * We'got more handshake messages in the same record.
3181 * This case is not handled now because no know implementation does
3182 * that and it's hard to test, so we prefer to fail cleanly for now.
3183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3185 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003186 }
3187
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003188 if( ssl->in_left > ssl->next_record_offset )
3189 {
3190 /*
3191 * We've got more data in the buffer after the current record,
3192 * that we don't want to overwrite. Move it before writing the
3193 * reassembled message, and adjust in_left and next_record_offset.
3194 */
3195 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3196 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3197 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3198
3199 /* First compute and check new lengths */
3200 ssl->next_record_offset = new_remain - ssl->in_hdr;
3201 ssl->in_left = ssl->next_record_offset + remain_len;
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003204 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3207 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003208 }
3209
3210 memmove( new_remain, cur_remain, remain_len );
3211 }
3212
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003213 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3214
Hanno Becker728d6cd2018-10-15 13:22:22 +01003215 mbedtls_zeroize( ssl->handshake->hs_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003217 ssl->handshake->hs_msg = NULL;
3218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003220 ssl->in_msg, ssl->in_hslen );
3221
3222 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226static int ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003231 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003233 }
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003236 ( ssl->in_msg[1] << 16 ) |
3237 ( ssl->in_msg[2] << 8 ) |
3238 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003241 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003242 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003246 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003247 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003248 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003249
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003250 /* ssl->handshake is NULL when receiving ClientHello for renego */
3251 if( ssl->handshake != NULL &&
3252 recv_msg_seq != ssl->handshake->in_msg_seq )
3253 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003254 /* Retransmit only on last message from previous flight, to avoid
3255 * too many retransmissions.
3256 * Besides, No sane server ever retransmits HelloVerifyRequest */
3257 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003261 "message_seq = %d, start_of_flight = %d",
3262 recv_msg_seq,
3263 ssl->handshake->in_flight_start_seq ) );
3264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003268 return( ret );
3269 }
3270 }
3271 else
3272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003274 "message_seq = %d, expected = %d",
3275 recv_msg_seq,
3276 ssl->handshake->in_msg_seq ) );
3277 }
3278
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003279 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003280 }
3281 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003282
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003283 /* Reassemble if current message is fragmented or reassembly is
3284 * already in progress */
3285 if( ssl->in_msglen < ssl->in_hslen ||
3286 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3287 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3288 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003291
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003292 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003295 return( ret );
3296 }
3297 }
3298 }
3299 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003301 /* With TLS we don't handle fragmentation (for now) */
3302 if( ssl->in_msglen < ssl->in_hslen )
3303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3305 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003306 }
3307
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003308 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3309 ssl->handshake != NULL )
3310 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003311 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003312 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003313
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003314 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003316 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003317 ssl->handshake != NULL )
3318 {
3319 ssl->handshake->in_msg_seq++;
3320 }
3321#endif
3322
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003323 return( 0 );
3324}
3325
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003326/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003327 * DTLS anti-replay: RFC 6347 4.1.2.6
3328 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003329 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3330 * Bit n is set iff record number in_window_top - n has been seen.
3331 *
3332 * Usually, in_window_top is the last record number seen and the lsb of
3333 * in_window is set. The only exception is the initial state (record number 0
3334 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3337static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003338{
3339 ssl->in_window_top = 0;
3340 ssl->in_window = 0;
3341}
3342
3343static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3344{
3345 return( ( (uint64_t) buf[0] << 40 ) |
3346 ( (uint64_t) buf[1] << 32 ) |
3347 ( (uint64_t) buf[2] << 24 ) |
3348 ( (uint64_t) buf[3] << 16 ) |
3349 ( (uint64_t) buf[4] << 8 ) |
3350 ( (uint64_t) buf[5] ) );
3351}
3352
3353/*
3354 * Return 0 if sequence number is acceptable, -1 otherwise
3355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003357{
3358 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3359 uint64_t bit;
3360
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003361 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003362 return( 0 );
3363
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003364 if( rec_seqnum > ssl->in_window_top )
3365 return( 0 );
3366
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003367 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003368
3369 if( bit >= 64 )
3370 return( -1 );
3371
3372 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3373 return( -1 );
3374
3375 return( 0 );
3376}
3377
3378/*
3379 * Update replay window on new validated record
3380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003382{
3383 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3384
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003385 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003386 return;
3387
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003388 if( rec_seqnum > ssl->in_window_top )
3389 {
3390 /* Update window_top and the contents of the window */
3391 uint64_t shift = rec_seqnum - ssl->in_window_top;
3392
3393 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003394 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003395 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003396 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003397 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003398 ssl->in_window |= 1;
3399 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003400
3401 ssl->in_window_top = rec_seqnum;
3402 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003403 else
3404 {
3405 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003406 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003407
3408 if( bit < 64 ) /* Always true, but be extra sure */
3409 ssl->in_window |= (uint64_t) 1 << bit;
3410 }
3411}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003413
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003414#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003415/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003416static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3417
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003418/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003419 * Without any SSL context, check if a datagram looks like a ClientHello with
3420 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003421 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003422 *
3423 * - if cookie is valid, return 0
3424 * - if ClientHello looks superficially valid but cookie is not,
3425 * fill obuf and set olen, then
3426 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3427 * - otherwise return a specific error code
3428 */
3429static int ssl_check_dtls_clihlo_cookie(
3430 mbedtls_ssl_cookie_write_t *f_cookie_write,
3431 mbedtls_ssl_cookie_check_t *f_cookie_check,
3432 void *p_cookie,
3433 const unsigned char *cli_id, size_t cli_id_len,
3434 const unsigned char *in, size_t in_len,
3435 unsigned char *obuf, size_t buf_len, size_t *olen )
3436{
3437 size_t sid_len, cookie_len;
3438 unsigned char *p;
3439
3440 if( f_cookie_write == NULL || f_cookie_check == NULL )
3441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3442
3443 /*
3444 * Structure of ClientHello with record and handshake headers,
3445 * and expected values. We don't need to check a lot, more checks will be
3446 * done when actually parsing the ClientHello - skipping those checks
3447 * avoids code duplication and does not make cookie forging any easier.
3448 *
3449 * 0-0 ContentType type; copied, must be handshake
3450 * 1-2 ProtocolVersion version; copied
3451 * 3-4 uint16 epoch; copied, must be 0
3452 * 5-10 uint48 sequence_number; copied
3453 * 11-12 uint16 length; (ignored)
3454 *
3455 * 13-13 HandshakeType msg_type; (ignored)
3456 * 14-16 uint24 length; (ignored)
3457 * 17-18 uint16 message_seq; copied
3458 * 19-21 uint24 fragment_offset; copied, must be 0
3459 * 22-24 uint24 fragment_length; (ignored)
3460 *
3461 * 25-26 ProtocolVersion client_version; (ignored)
3462 * 27-58 Random random; (ignored)
3463 * 59-xx SessionID session_id; 1 byte len + sid_len content
3464 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3465 * ...
3466 *
3467 * Minimum length is 61 bytes.
3468 */
3469 if( in_len < 61 ||
3470 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3471 in[3] != 0 || in[4] != 0 ||
3472 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3473 {
3474 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3475 }
3476
3477 sid_len = in[59];
3478 if( sid_len > in_len - 61 )
3479 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3480
3481 cookie_len = in[60 + sid_len];
3482 if( cookie_len > in_len - 60 )
3483 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3484
3485 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3486 cli_id, cli_id_len ) == 0 )
3487 {
3488 /* Valid cookie */
3489 return( 0 );
3490 }
3491
3492 /*
3493 * If we get here, we've got an invalid cookie, let's prepare HVR.
3494 *
3495 * 0-0 ContentType type; copied
3496 * 1-2 ProtocolVersion version; copied
3497 * 3-4 uint16 epoch; copied
3498 * 5-10 uint48 sequence_number; copied
3499 * 11-12 uint16 length; olen - 13
3500 *
3501 * 13-13 HandshakeType msg_type; hello_verify_request
3502 * 14-16 uint24 length; olen - 25
3503 * 17-18 uint16 message_seq; copied
3504 * 19-21 uint24 fragment_offset; copied
3505 * 22-24 uint24 fragment_length; olen - 25
3506 *
3507 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3508 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3509 *
3510 * Minimum length is 28.
3511 */
3512 if( buf_len < 28 )
3513 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3514
3515 /* Copy most fields and adapt others */
3516 memcpy( obuf, in, 25 );
3517 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3518 obuf[25] = 0xfe;
3519 obuf[26] = 0xff;
3520
3521 /* Generate and write actual cookie */
3522 p = obuf + 28;
3523 if( f_cookie_write( p_cookie,
3524 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3525 {
3526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3527 }
3528
3529 *olen = p - obuf;
3530
3531 /* Go back and fill length fields */
3532 obuf[27] = (unsigned char)( *olen - 28 );
3533
3534 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3535 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3536 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3537
3538 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3539 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3540
3541 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3542}
3543
3544/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003545 * Handle possible client reconnect with the same UDP quadruplet
3546 * (RFC 6347 Section 4.2.8).
3547 *
3548 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3549 * that looks like a ClientHello.
3550 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003551 * - if the input looks like a ClientHello without cookies,
3552 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003553 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003554 * - if the input looks like a ClientHello with a valid cookie,
3555 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003556 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003557 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003558 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003559 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003560 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3561 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003562 */
3563static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3564{
3565 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003566 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003567
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003568 ret = ssl_check_dtls_clihlo_cookie(
3569 ssl->conf->f_cookie_write,
3570 ssl->conf->f_cookie_check,
3571 ssl->conf->p_cookie,
3572 ssl->cli_id, ssl->cli_id_len,
3573 ssl->in_buf, ssl->in_left,
3574 ssl->out_buf, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003575
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003576 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3577
3578 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003579 {
Brian J Murraye7f8dc32016-11-06 04:45:15 -08003580 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003581 * If the error is permanent we'll catch it later,
3582 * if it's not, then hopefully it'll work next time. */
3583 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3584
3585 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003586 }
3587
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003588 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003589 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003590 /* Got a valid cookie, partially reset context */
3591 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3592 {
3593 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3594 return( ret );
3595 }
3596
3597 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003598 }
3599
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003600 return( ret );
3601}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003602#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003603
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003604/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003605 * ContentType type;
3606 * ProtocolVersion version;
3607 * uint16 epoch; // DTLS only
3608 * uint48 sequence_number; // DTLS only
3609 * uint16 length;
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003610 *
3611 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butchere103aa82015-12-16 01:51:01 +00003612 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003613 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3614 *
3615 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butchere103aa82015-12-16 01:51:01 +00003616 * 1. proceed with the record if this function returns 0
3617 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3618 * 3. return CLIENT_RECONNECT if this function returns that value
3619 * 4. drop the whole datagram if this function returns anything else.
3620 * Point 2 is needed when the peer is resending, and we have already received
3621 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003624{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003625 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 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 +02003628
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003630 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003631 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003635 ssl->in_msgtype,
3636 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003638 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3640 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3641 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3642 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003645
Andres Amaya Garcia1042d862017-07-05 13:26:34 +01003646#if defined(MBEDTLS_SSL_PROTO_DTLS)
3647 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3648 * Section 4.1.2.7 */
3649 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3650#endif /* MBEDTLS_SSL_PROTO_DTLS */
3651 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3652 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003655 }
3656
3657 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003658 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3661 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 }
3663
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003664 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3667 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 }
3669
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003670 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003672 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3675 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003676 }
3677
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003678 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003679 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003681 if( ssl->in_msglen < 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3685 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 }
3687 }
3688 else
3689 {
Paul Bakker48916f92012-09-16 19:57:18 +00003690 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3693 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003694 }
3695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696#if defined(MBEDTLS_SSL_PROTO_SSL3)
3697 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3698 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3701 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003702 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3705 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 /*
3707 * TLS encrypted messages can have up to 256 bytes of padding
3708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003709 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003710 ssl->in_msglen > ssl->transform_in->minlen +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3714 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003716#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 }
3718
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003719 /*
3720 * DTLS-related tests done last, because most of them may result in
3721 * silently dropping the record (but not the whole datagram), and we only
3722 * want to consider that after ensuring that the "basic" fields (type,
3723 * version, length) are sane.
3724 */
3725#if defined(MBEDTLS_SSL_PROTO_DTLS)
3726 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3727 {
3728 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3729
3730 /* Drop unexpected ChangeCipherSpec messages */
3731 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3732 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3733 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3734 {
3735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3736 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3737 }
3738
3739 /* Drop unexpected ApplicationData records,
3740 * except at the beginning of renegotiations */
3741 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3742 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3743#if defined(MBEDTLS_SSL_RENEGOTIATION)
3744 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3745 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3746#endif
3747 )
3748 {
3749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3750 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3751 }
3752
3753 /* Check epoch (and sequence number) with DTLS */
3754 if( rec_epoch != ssl->in_epoch )
3755 {
3756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3757 "expected %d, received %d",
3758 ssl->in_epoch, rec_epoch ) );
3759
3760#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3761 /*
3762 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3763 * access the first byte of record content (handshake type), as we
3764 * have an active transform (possibly iv_len != 0), so use the
3765 * fact that the record header len is 13 instead.
3766 */
3767 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3768 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3769 rec_epoch == 0 &&
3770 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3771 ssl->in_left > 13 &&
3772 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3773 {
3774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3775 "from the same port" ) );
3776 return( ssl_handle_possible_reconnect( ssl ) );
3777 }
3778 else
3779#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3780 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3781 }
3782
3783#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3784 /* Replay detection only works for the current epoch */
3785 if( rec_epoch == ssl->in_epoch &&
3786 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3787 {
3788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3789 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3790 }
3791#endif
3792 }
3793#endif /* MBEDTLS_SSL_PROTO_DTLS */
3794
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003795 return( 0 );
3796}
Paul Bakker5121ce52009-01-03 21:22:43 +00003797
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003798/*
3799 * If applicable, decrypt (and decompress) record content
3800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003802{
3803 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3806 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003808#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3809 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 ret = mbedtls_ssl_hw_record_read( ssl );
3814 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3817 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003818 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003819
3820 if( ret == 0 )
3821 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003822 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003824 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003825 {
3826 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003829 return( ret );
3830 }
3831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003833 ssl->in_msg, ssl->in_msglen );
3834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3838 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003839 }
3840 }
3841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003843 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003845 {
3846 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003849 return( ret );
3850 }
3851
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003852 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3853 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003858 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003861 }
3862#endif
3863
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003864 return( 0 );
3865}
3866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003868
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003869/*
3870 * Read a record.
3871 *
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02003872 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3873 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3874 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003875 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003877{
3878 int ret;
3879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003881
Hanno Becker704f4932017-06-08 13:08:45 +01003882 if( ssl->keep_current_message == 1 )
3883 {
3884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
3885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3886 ssl->keep_current_message = 0;
3887
3888 return( 0 );
3889 }
3890
Hanno Becker6a582e82017-06-08 13:38:05 +01003891 /*
3892 * Step A
3893 *
3894 * Consume last content-layer message and potentially
3895 * update in_msglen which keeps track of the contents'
3896 * consumption state.
3897 *
3898 * (1) Handshake messages:
3899 * Remove last handshake message, move content
3900 * and adapt in_msglen.
3901 *
3902 * (2) Alert messages:
3903 * Consume whole record content, in_msglen = 0.
3904 *
3905 * NOTE: This needs to be fixed, since like for
3906 * handshake messages it is allowed to have
3907 * multiple alerts witin a single record.
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003908 * Internal reference IOTSSL-1321.
Hanno Becker6a582e82017-06-08 13:38:05 +01003909 *
3910 * (3) Change cipher spec:
3911 * Consume whole record content, in_msglen = 0.
3912 *
3913 * (4) Application data:
3914 * Don't do anything - the record layer provides
3915 * the application data as a stream transport
3916 * and consumes through mbedtls_ssl_read only.
3917 *
3918 */
3919
3920 /* Case (1): Handshake messages */
3921
3922 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003923 {
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003924 if( ssl->in_offt != NULL )
3925 {
3926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3928 }
3929
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003930 /*
3931 * Get next Handshake message in the current record
3932 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003933
Hanno Becker6a582e82017-06-08 13:38:05 +01003934 /* Notes:
3935 * (1) in_hslen is *NOT* necessarily the size of the
3936 * current handshake content: If DTLS handshake
3937 * fragmentation is used, that's the fragment
3938 * size instead. Using the total handshake message
3939 * size here is FAULTY and should be changed at
3940 * some point. Internal reference IOTSSL-1414.
3941 * (2) While it doesn't seem to cause problems, one
3942 * has to be very careful not to assume that in_hslen
3943 * is always <= in_msglen in a sensible communication.
3944 * Again, it's wrong for DTLS handshake fragmentation.
3945 * The following check is therefore mandatory, and
3946 * should not be treated as a silently corrected assertion.
3947 */
3948 if( ssl->in_hslen < ssl->in_msglen )
3949 {
3950 ssl->in_msglen -= ssl->in_hslen;
3951 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3952 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003953
Hanno Becker6a582e82017-06-08 13:38:05 +01003954 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
3955 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003956
Hanno Becker6a582e82017-06-08 13:38:05 +01003957 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3958 return( ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003959
Hanno Becker6a582e82017-06-08 13:38:05 +01003960 return( 0 );
3961 }
3962 else
3963 {
3964 ssl->in_msglen = 0;
3965 }
3966
3967 ssl->in_hslen = 0;
3968 }
3969 /* Case (4): Application data */
3970 else if( ssl->in_offt != NULL )
3971 {
3972 return( 0 );
3973 }
3974 /* Everything else (CCS & Alerts) */
3975 else
3976 {
3977 ssl->in_msglen = 0;
3978 }
3979
3980 /*
3981 * Step B
3982 *
3983 * Fetch and decode new record if current one is fully consumed.
3984 *
3985 */
3986
3987 if( ssl->in_msglen > 0 )
3988 {
3989 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003990 return( 0 );
3991 }
3992
Hanno Becker6a582e82017-06-08 13:38:05 +01003993 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003994
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003995read_record_header:
Hanno Becker6a582e82017-06-08 13:38:05 +01003996
3997 /* Current record either fully processed or to be discarded. */
3998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004002 return( ret );
4003 }
4004
4005 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004008 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4009 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004010 {
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01004011 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4012 {
4013 /* Skip unexpected record (but not whole datagram) */
4014 ssl->next_record_offset = ssl->in_msglen
4015 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004016
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01004017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4018 "(header)" ) );
4019 }
4020 else
4021 {
4022 /* Skip invalid record and the rest of the datagram */
4023 ssl->next_record_offset = 0;
4024 ssl->in_left = 0;
4025
4026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4027 "(header)" ) );
4028 }
4029
4030 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004031 goto read_record_header;
4032 }
4033#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004034 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004035 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004036
4037 /*
4038 * Read and optionally decrypt the message contents
4039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 if( ( ret = mbedtls_ssl_fetch_input( ssl,
4041 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004044 return( ret );
4045 }
4046
4047 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004049 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004051 else
4052#endif
4053 ssl->in_left = 0;
4054
4055 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004058 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004059 {
4060 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
4062 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004063 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004064 /* Except when waiting for Finished as a bad mac here
4065 * probably means something went wrong in the handshake
4066 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4067 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4068 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4069 {
4070#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4071 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4072 {
4073 mbedtls_ssl_send_alert_message( ssl,
4074 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4075 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4076 }
4077#endif
4078 return( ret );
4079 }
4080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004082 if( ssl->conf->badmac_limit != 0 &&
4083 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4086 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004087 }
4088#endif
4089
Hanno Becker6a582e82017-06-08 13:38:05 +01004090 /* As above, invalid records cause
4091 * dismissal of the whole datagram. */
4092
4093 ssl->next_record_offset = 0;
4094 ssl->in_left = 0;
4095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004097 goto read_record_header;
4098 }
4099
4100 return( ret );
4101 }
4102 else
4103#endif
4104 {
4105 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4107 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 mbedtls_ssl_send_alert_message( ssl,
4110 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4111 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004112 }
4113#endif
4114 return( ret );
4115 }
4116 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004117
4118 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004119 * When we sent the last flight of the handshake, we MUST respond to a
4120 * retransmit of the peer's previous flight with a retransmit. (In
4121 * practice, only the Finished message will make it, other messages
4122 * including CCS use the old transform so they're dropped as invalid.)
4123 *
4124 * If the record we received is not a handshake message, however, it
4125 * means the peer received our last flight so we can clean up
4126 * handshake info.
4127 *
4128 * This check needs to be done before prepare_handshake() due to an edge
4129 * case: if the client immediately requests renegotiation, this
4130 * finishes the current handshake first, avoiding the new ClientHello
4131 * being mistaken for an ancient message in the current handshake.
4132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004134 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004135 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4139 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004146 return( ret );
4147 }
4148
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004149 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004150 }
4151 else
4152 {
4153 ssl_handshake_wrapup_free_hs_transform( ssl );
4154 }
4155 }
4156#endif
4157
4158 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004159 * Handle particular types of records
4160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004162 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004163 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
4164 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004165 }
4166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004168 {
Angus Grattonfd1c5e82018-06-20 15:43:50 +10004169 if( ssl->in_msglen != 2 )
4170 {
4171 /* Note: Standard allows for more than one 2 byte alert
4172 to be packed in a single message, but Mbed TLS doesn't
4173 currently support this. */
4174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
4175 ssl->in_msglen ) );
4176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4177 }
4178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004180 ssl->in_msg[0], ssl->in_msg[1] ) );
4181
4182 /*
Simon Butcher94c5e3c2015-10-27 16:09:03 +00004183 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004188 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004190 }
4191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4193 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4196 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 }
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02004198
4199#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4200 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4201 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4202 {
4203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4204 /* Will be handled when trying to parse ServerHello */
4205 return( 0 );
4206 }
4207#endif
4208
4209#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4210 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4211 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4212 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4213 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4214 {
4215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4216 /* Will be handled in mbedtls_ssl_parse_certificate() */
4217 return( 0 );
4218 }
4219#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4220
4221 /* Silently ignore: fetch new message */
4222 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00004223 }
4224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004226
4227 return( 0 );
4228}
4229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004230int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004231{
4232 int ret;
4233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4235 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4236 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004237 {
4238 return( ret );
4239 }
4240
4241 return( 0 );
4242}
4243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004245 unsigned char level,
4246 unsigned char message )
4247{
4248 int ret;
4249
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004250 if( ssl == NULL || ssl->conf == NULL )
4251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004256 ssl->out_msglen = 2;
4257 ssl->out_msg[0] = level;
4258 ssl->out_msg[1] = message;
4259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004262 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004263 return( ret );
4264 }
4265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004267
4268 return( 0 );
4269}
4270
Paul Bakker5121ce52009-01-03 21:22:43 +00004271/*
4272 * Handshake functions
4273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4275 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4276 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4277 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4278 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4279 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4280 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4281int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004287 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4289 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004292 ssl->state++;
4293 return( 0 );
4294 }
4295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004298}
4299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4307 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4308 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004311 ssl->state++;
4312 return( 0 );
4313 }
4314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004317}
4318#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004320{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004322 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 const mbedtls_x509_crt *crt;
4324 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4329 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4330 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004333 ssl->state++;
4334 return( 0 );
4335 }
4336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004338 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 {
4340 if( ssl->client_auth == 0 )
4341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 ssl->state++;
4344 return( 0 );
4345 }
4346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 /*
4349 * If using SSLv3 and got no cert, send an Alert message
4350 * (otherwise an empty Certificate message will be sent).
4351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4353 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004354 {
4355 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4357 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4358 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 goto write_msg;
4362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365#endif /* MBEDTLS_SSL_CLI_C */
4366#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004367 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4372 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 }
4374 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004375#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004378
4379 /*
4380 * 0 . 0 handshake type
4381 * 1 . 3 handshake length
4382 * 4 . 6 length of all certs
4383 * 7 . 9 length of cert. 1
4384 * 10 . n-1 peer certificate
4385 * n . n+2 length of cert. 2
4386 * n+3 . ... upper level cert, etc.
4387 */
4388 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004390
Paul Bakker29087132010-03-21 21:03:34 +00004391 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 {
4393 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4397 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4398 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004399 }
4400
4401 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4402 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4403 ssl->out_msg[i + 2] = (unsigned char)( n );
4404
4405 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4406 i += n; crt = crt->next;
4407 }
4408
4409 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4410 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4411 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4412
4413 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4415 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004416
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004417#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004418write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004419#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004420
4421 ssl->state++;
4422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004426 return( ret );
4427 }
4428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004430
Paul Bakkered27a042013-04-18 22:46:23 +02004431 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004432}
4433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004437 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004439 int authmode = ssl->conf->authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4444 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4445 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004448 ssl->state++;
4449 return( 0 );
4450 }
4451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004453 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004454 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4455 {
4456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4457 ssl->state++;
4458 return( 0 );
4459 }
4460
4461#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4462 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4463 authmode = ssl->handshake->sni_authmode;
4464#endif
4465
4466 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4467 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004468 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004469 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004471 ssl->state++;
4472 return( 0 );
4473 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004476 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004479 return( ret );
4480 }
4481
4482 ssl->state++;
4483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#if defined(MBEDTLS_SSL_SRV_C)
4485#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004486 /*
4487 * Check if the client sent an empty certificate
4488 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004489 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004492 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4494 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4495 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004498
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004499 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004500 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004501 return( 0 );
4502 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004503 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 }
4505 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4509 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004510 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4514 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4515 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4516 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004519
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004520 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004521 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004522 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004523 else
4524 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004525 }
4526 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4528 MBEDTLS_SSL_PROTO_TLS1_2 */
4529#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004531 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4534 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004535 }
4536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4538 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4541 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004542 }
4543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004545
Paul Bakker5121ce52009-01-03 21:22:43 +00004546 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004548 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004549 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004550
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004551 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4555 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 }
4557
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004558 /* In case we tried to reuse a session but it failed */
4559 if( ssl->session_negotiate->peer_cert != NULL )
4560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4562 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004563 }
4564
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004565 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 sizeof( mbedtls_x509_crt ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004570 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004571 }
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004574
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004575 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004576
4577 while( i < ssl->in_hslen )
4578 {
Philippe Antoinebbc79182018-05-30 09:13:21 +02004579 if ( i + 3 > ssl->in_hslen ) {
4580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4581 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4582 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
4583 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
4584 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 if( ssl->in_msg[i] != 0 )
4586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4588 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004589 }
4590
4591 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4592 | (unsigned int) ssl->in_msg[i + 2];
4593 i += 3;
4594
4595 if( n < 128 || i + n > ssl->in_hslen )
4596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4598 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 }
4600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004602 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004603 if( ret != 0 )
4604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 return( ret );
4607 }
4608
4609 i += n;
4610 }
4611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004613
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004614 /*
4615 * On client, make sure the server cert doesn't change during renego to
4616 * avoid "triple handshake" attack: https://secure-resumption.com/
4617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004619 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004621 {
4622 if( ssl->session->peer_cert == NULL )
4623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
4625 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004626 }
4627
4628 if( ssl->session->peer_cert->raw.len !=
4629 ssl->session_negotiate->peer_cert->raw.len ||
4630 memcmp( ssl->session->peer_cert->raw.p,
4631 ssl->session_negotiate->peer_cert->raw.p,
4632 ssl->session->peer_cert->raw.len ) != 0 )
4633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
4635 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004636 }
4637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004639
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004640 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004642 mbedtls_x509_crt *ca_chain;
4643 mbedtls_x509_crl *ca_crl;
4644
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004645#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004646 if( ssl->handshake->sni_ca_chain != NULL )
4647 {
4648 ca_chain = ssl->handshake->sni_ca_chain;
4649 ca_crl = ssl->handshake->sni_ca_crl;
4650 }
4651 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004652#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004653 {
4654 ca_chain = ssl->conf->ca_chain;
4655 ca_crl = ssl->conf->ca_crl;
4656 }
4657
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004658 /*
4659 * Main check: verify certificate
4660 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004661 ret = mbedtls_x509_crt_verify_with_profile(
4662 ssl->session_negotiate->peer_cert,
4663 ca_chain, ca_crl,
4664 ssl->conf->cert_profile,
4665 ssl->hostname,
4666 &ssl->session_negotiate->verify_result,
4667 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668
4669 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004672 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004673
4674 /*
4675 * Secondary checks: always done, but change 'ret' only if it was 0
4676 */
4677
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004678#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004681
4682 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004684 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004685 {
Hanno Beckera3929ba2017-05-08 16:31:14 +01004686 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004689 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004690 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004691 }
4692 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004693#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004695 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Beckera3929ba2017-05-08 16:31:14 +01004696 ciphersuite_info,
4697 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004698 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004701 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004702 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004703 }
4704
Hanno Beckera3929ba2017-05-08 16:31:14 +01004705 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4706 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4707 * with details encoded in the verification flags. All other kinds
4708 * of error codes, including those from the user provided f_vrfy
4709 * functions, are treated as fatal and lead to a failure of
4710 * ssl_parse_certificate even if verification was optional. */
4711 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4712 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4713 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4714 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004715 ret = 0;
Hanno Beckera3929ba2017-05-08 16:31:14 +01004716 }
4717
4718 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4719 {
4720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4721 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4722 }
4723
Hanno Becker61c0c702017-05-15 16:05:15 +01004724#if defined(MBEDTLS_DEBUG_C)
4725 if( ssl->session_negotiate->verify_result != 0 )
4726 {
4727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4728 ssl->session_negotiate->verify_result ) );
4729 }
4730 else
4731 {
4732 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4733 }
4734#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 }
4736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004738
4739 return( ret );
4740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
4742 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
4743 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
4744 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4745 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4746 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4747 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004750{
4751 int ret;
4752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004756 ssl->out_msglen = 1;
4757 ssl->out_msg[0] = 1;
4758
Paul Bakker5121ce52009-01-03 21:22:43 +00004759 ssl->state++;
4760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004764 return( ret );
4765 }
4766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004768
4769 return( 0 );
4770}
4771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004773{
4774 int ret;
4775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004781 return( ret );
4782 }
4783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4787 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004788 }
4789
4790 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4793 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004794 }
4795
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004796 /*
4797 * Switch to our negotiated transform and session parameters for inbound
4798 * data.
4799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004801 ssl->transform_in = ssl->transform_negotiate;
4802 ssl->session_in = ssl->session_negotiate;
4803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004805 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004808 ssl_dtls_replay_reset( ssl );
4809#endif
4810
4811 /* Increment epoch */
4812 if( ++ssl->in_epoch == 0 )
4813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4815 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004816 }
4817 }
4818 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004820 memset( ssl->in_ctr, 0, 8 );
4821
4822 /*
4823 * Set the in_msg pointer to the correct location based on IV length
4824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004826 {
4827 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4828 ssl->transform_negotiate->fixed_ivlen;
4829 }
4830 else
4831 ssl->in_msg = ssl->in_iv;
4832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4834 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4839 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004840 }
4841 }
4842#endif
4843
Paul Bakker5121ce52009-01-03 21:22:43 +00004844 ssl->state++;
4845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004847
4848 return( 0 );
4849}
4850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
4852 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004853{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004854 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4857 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4858 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004859 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004860 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004861#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4863#if defined(MBEDTLS_SHA512_C)
4864 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004865 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4866 else
4867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868#if defined(MBEDTLS_SHA256_C)
4869 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004870 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004871 else
4872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004876 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004877 }
Paul Bakker380da532012-04-18 16:10:25 +00004878}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004880void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004881{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4883 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4884 mbedtls_md5_starts( &ssl->handshake->fin_md5 );
4885 mbedtls_sha1_starts( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4888#if defined(MBEDTLS_SHA256_C)
4889 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004890#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891#if defined(MBEDTLS_SHA512_C)
4892 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004893#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004895}
4896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004898 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4901 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4902 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4903 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4906#if defined(MBEDTLS_SHA256_C)
4907 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909#if defined(MBEDTLS_SHA512_C)
4910 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004913}
4914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004915#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4916 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4917static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004918 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004919{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4921 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004922}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004923#endif
Paul Bakker380da532012-04-18 16:10:25 +00004924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4926#if defined(MBEDTLS_SHA256_C)
4927static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004928 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004931}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004932#endif
Paul Bakker380da532012-04-18 16:10:25 +00004933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#if defined(MBEDTLS_SHA512_C)
4935static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004936 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004939}
Paul Bakker769075d2012-11-24 11:26:46 +01004940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004943#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004944static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004945 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004946{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004947 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004948 mbedtls_md5_context md5;
4949 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004950
Paul Bakker5121ce52009-01-03 21:22:43 +00004951 unsigned char padbuf[48];
4952 unsigned char md5sum[16];
4953 unsigned char sha1sum[20];
4954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004956 if( !session )
4957 session = ssl->session;
4958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004960
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004961 mbedtls_md5_init( &md5 );
4962 mbedtls_sha1_init( &sha1 );
4963
4964 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
4965 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004966
4967 /*
4968 * SSLv3:
4969 * hash =
4970 * MD5( master + pad2 +
4971 * MD5( handshake + sender + master + pad1 ) )
4972 * + SHA1( master + pad2 +
4973 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004974 */
4975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004977 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4978 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004982 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4983 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004984#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02004987 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004988
Paul Bakker1ef83d62012-04-11 12:09:53 +00004989 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004990
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004991 mbedtls_md5_update( &md5, (const unsigned char *) sender, 4 );
4992 mbedtls_md5_update( &md5, session->master, 48 );
4993 mbedtls_md5_update( &md5, padbuf, 48 );
4994 mbedtls_md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004995
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004996 mbedtls_sha1_update( &sha1, (const unsigned char *) sender, 4 );
4997 mbedtls_sha1_update( &sha1, session->master, 48 );
4998 mbedtls_sha1_update( &sha1, padbuf, 40 );
4999 mbedtls_sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00005000
Paul Bakker1ef83d62012-04-11 12:09:53 +00005001 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005002
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005003 mbedtls_md5_starts( &md5 );
5004 mbedtls_md5_update( &md5, session->master, 48 );
5005 mbedtls_md5_update( &md5, padbuf, 48 );
5006 mbedtls_md5_update( &md5, md5sum, 16 );
5007 mbedtls_md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005008
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005009 mbedtls_sha1_starts( &sha1 );
5010 mbedtls_sha1_update( &sha1, session->master, 48 );
5011 mbedtls_sha1_update( &sha1, padbuf , 40 );
5012 mbedtls_sha1_update( &sha1, sha1sum, 20 );
5013 mbedtls_sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005016
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005017 mbedtls_md5_free( &md5 );
5018 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005020 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
5021 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
5022 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00005029static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00005031{
Paul Bakker1ef83d62012-04-11 12:09:53 +00005032 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005033 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005034 mbedtls_md5_context md5;
5035 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005036 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00005037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005039 if( !session )
5040 session = ssl->session;
5041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005043
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005044 mbedtls_md5_init( &md5 );
5045 mbedtls_sha1_init( &sha1 );
5046
5047 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
5048 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005049
Paul Bakker1ef83d62012-04-11 12:09:53 +00005050 /*
5051 * TLSv1:
5052 * hash = PRF( master, finished_label,
5053 * MD5( handshake ) + SHA1( handshake ) )[0..11]
5054 */
Paul Bakker5121ce52009-01-03 21:22:43 +00005055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005057 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
5058 md5.state, sizeof( md5.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#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005062 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
5063 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005064#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005067 ? "client finished"
5068 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005069
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005070 mbedtls_md5_finish( &md5, padbuf );
5071 mbedtls_sha1_finish( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005072
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005073 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005074 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005077
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005078 mbedtls_md5_free( &md5 );
5079 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005084}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5088#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005089static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005091{
5092 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005093 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005094 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005095 unsigned char padbuf[32];
5096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005098 if( !session )
5099 session = ssl->session;
5100
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005101 mbedtls_sha256_init( &sha256 );
5102
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005104
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005105 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005106
5107 /*
5108 * TLSv1.2:
5109 * hash = PRF( master, finished_label,
5110 * Hash( handshake ) )[0.11]
5111 */
5112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113#if !defined(MBEDTLS_SHA256_ALT)
5114 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005115 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005116#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00005117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005119 ? "client finished"
5120 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005121
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005122 mbedtls_sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005123
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005124 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005125 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005128
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005129 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00005136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00005138static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00005140{
5141 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02005142 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005143 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00005144 unsigned char padbuf[48];
5145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005146 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005147 if( !session )
5148 session = ssl->session;
5149
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005150 mbedtls_sha512_init( &sha512 );
5151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005153
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02005154 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005155
5156 /*
5157 * TLSv1.2:
5158 * hash = PRF( master, finished_label,
5159 * Hash( handshake ) )[0.11]
5160 */
5161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005163 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5164 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005165#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005168 ? "client finished"
5169 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005170
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005171 mbedtls_sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005172
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005173 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005174 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005177
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005178 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184#endif /* MBEDTLS_SHA512_C */
5185#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005190
5191 /*
5192 * Free our handshake params
5193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005194 mbedtls_ssl_handshake_free( ssl->handshake );
5195 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005196 ssl->handshake = NULL;
5197
5198 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005199 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005200 */
5201 if( ssl->transform )
5202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203 mbedtls_ssl_transform_free( ssl->transform );
5204 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005205 }
5206 ssl->transform = ssl->transform_negotiate;
5207 ssl->transform_negotiate = NULL;
5208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005210}
5211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005213{
5214 int resume = ssl->handshake->resume;
5215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218#if defined(MBEDTLS_SSL_RENEGOTIATION)
5219 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005222 ssl->renego_records_seen = 0;
5223 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005224#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005225
5226 /*
5227 * Free the previous session and switch in the current one
5228 */
Paul Bakker0a597072012-09-25 21:55:46 +00005229 if( ssl->session )
5230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005232 /* RFC 7366 3.1: keep the EtM state */
5233 ssl->session_negotiate->encrypt_then_mac =
5234 ssl->session->encrypt_then_mac;
5235#endif
5236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237 mbedtls_ssl_session_free( ssl->session );
5238 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005239 }
5240 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005241 ssl->session_negotiate = NULL;
5242
Paul Bakker0a597072012-09-25 21:55:46 +00005243 /*
5244 * Add cache entry
5245 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005246 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005247 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005248 resume == 0 )
5249 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005250 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005252 }
Paul Bakker0a597072012-09-25 21:55:46 +00005253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005256 ssl->handshake->flight != NULL )
5257 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005258 /* Cancel handshake timer */
5259 ssl_set_timer( ssl, 0 );
5260
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005261 /* Keep last flight around in case we need to resend it:
5262 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005264 }
5265 else
5266#endif
5267 ssl_handshake_wrapup_free_hs_transform( ssl );
5268
Paul Bakker48916f92012-09-16 19:57:18 +00005269 ssl->state++;
5270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005272}
5273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005275{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005276 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005279
Paul Bakker92be97b2013-01-02 17:30:03 +01005280 /*
5281 * Set the out_msg pointer to the correct location based on IV length
5282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005284 {
5285 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5286 ssl->transform_negotiate->fixed_ivlen;
5287 }
5288 else
5289 ssl->out_msg = ssl->out_iv;
5290
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005291 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005296 ssl->verify_data_len = hash_len;
5297 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005298#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005299
Paul Bakker5121ce52009-01-03 21:22:43 +00005300 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5302 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005303
5304 /*
5305 * In case of session resuming, invert the client and server
5306 * ChangeCipherSpec messages order.
5307 */
Paul Bakker0a597072012-09-25 21:55:46 +00005308 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005311 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005315 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005318 }
5319 else
5320 ssl->state++;
5321
Paul Bakker48916f92012-09-16 19:57:18 +00005322 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005323 * Switch to our negotiated transform and session parameters for outbound
5324 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005329 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005330 {
5331 unsigned char i;
5332
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005333 /* Remember current epoch settings for resending */
5334 ssl->handshake->alt_transform_out = ssl->transform_out;
5335 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5336
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005337 /* Set sequence_number to zero */
5338 memset( ssl->out_ctr + 2, 0, 6 );
5339
5340 /* Increment epoch */
5341 for( i = 2; i > 0; i-- )
5342 if( ++ssl->out_ctr[i - 1] != 0 )
5343 break;
5344
5345 /* The loop goes to its end iff the counter is wrapping */
5346 if( i == 0 )
5347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5349 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005350 }
5351 }
5352 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005354 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005355
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005356 ssl->transform_out = ssl->transform_negotiate;
5357 ssl->session_out = ssl->session_negotiate;
5358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5360 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5365 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005366 }
5367 }
5368#endif
5369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005371 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005373#endif
5374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005378 return( ret );
5379 }
5380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005382
5383 return( 0 );
5384}
5385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005387#define SSL_MAX_HASH_LEN 36
5388#else
5389#define SSL_MAX_HASH_LEN 12
5390#endif
5391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005393{
Paul Bakker23986e52011-04-24 08:57:21 +00005394 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005395 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005396 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005399
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005400 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005405 return( ret );
5406 }
5407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5411 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005412 }
5413
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005414 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#if defined(MBEDTLS_SSL_PROTO_SSL3)
5416 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005417 hash_len = 36;
5418 else
5419#endif
5420 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005422 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5423 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5426 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005427 }
5428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005430 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5433 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005434 }
5435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005437 ssl->verify_data_len = hash_len;
5438 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005439#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005440
Paul Bakker0a597072012-09-25 21:55:46 +00005441 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005444 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005448 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005450#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005451 }
5452 else
5453 ssl->state++;
5454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005456 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005458#endif
5459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005461
5462 return( 0 );
5463}
5464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005466{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5470 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5471 mbedtls_md5_init( &handshake->fin_md5 );
5472 mbedtls_sha1_init( &handshake->fin_sha1 );
5473 mbedtls_md5_starts( &handshake->fin_md5 );
5474 mbedtls_sha1_starts( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005475#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5477#if defined(MBEDTLS_SHA256_C)
5478 mbedtls_sha256_init( &handshake->fin_sha256 );
5479 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481#if defined(MBEDTLS_SHA512_C)
5482 mbedtls_sha512_init( &handshake->fin_sha512 );
5483 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005484#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005486
5487 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01005488
5489#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5490 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5491 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5492#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494#if defined(MBEDTLS_DHM_C)
5495 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497#if defined(MBEDTLS_ECDH_C)
5498 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005499#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005500
5501#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5502 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5503#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005504}
5505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5511 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 mbedtls_md_init( &transform->md_ctx_enc );
5514 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005515}
5516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005517void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005520}
5521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005522static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005523{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005524 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005525 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005527 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005529 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005531
5532 /*
5533 * Either the pointers are now NULL or cleared properly and can be freed.
5534 * Now allocate missing structures.
5535 */
5536 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005537 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005538 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005539 }
Paul Bakker48916f92012-09-16 19:57:18 +00005540
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005541 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005542 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005543 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005544 }
Paul Bakker48916f92012-09-16 19:57:18 +00005545
Paul Bakker82788fb2014-10-20 13:59:19 +02005546 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005547 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005548 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005549 }
Paul Bakker48916f92012-09-16 19:57:18 +00005550
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005551 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005552 if( ssl->handshake == NULL ||
5553 ssl->transform_negotiate == NULL ||
5554 ssl->session_negotiate == NULL )
5555 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 mbedtls_free( ssl->handshake );
5559 mbedtls_free( ssl->transform_negotiate );
5560 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005561
5562 ssl->handshake = NULL;
5563 ssl->transform_negotiate = NULL;
5564 ssl->session_negotiate = NULL;
5565
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005566 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005567 }
5568
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005569 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005571 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005572 ssl_handshake_params_init( ssl->handshake );
5573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5576 {
5577 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005578
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005579 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5580 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5581 else
5582 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005583
5584 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005585 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005586#endif
5587
Paul Bakker48916f92012-09-16 19:57:18 +00005588 return( 0 );
5589}
5590
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005591#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005592/* Dummy cookie callbacks for defaults */
5593static int ssl_cookie_write_dummy( void *ctx,
5594 unsigned char **p, unsigned char *end,
5595 const unsigned char *cli_id, size_t cli_id_len )
5596{
5597 ((void) ctx);
5598 ((void) p);
5599 ((void) end);
5600 ((void) cli_id);
5601 ((void) cli_id_len);
5602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005604}
5605
5606static int ssl_cookie_check_dummy( void *ctx,
5607 const unsigned char *cookie, size_t cookie_len,
5608 const unsigned char *cli_id, size_t cli_id_len )
5609{
5610 ((void) ctx);
5611 ((void) cookie);
5612 ((void) cookie_len);
5613 ((void) cli_id);
5614 ((void) cli_id_len);
5615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005617}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005618#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005619
Paul Bakker5121ce52009-01-03 21:22:43 +00005620/*
5621 * Initialize an SSL context
5622 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005623void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5624{
5625 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5626}
5627
5628/*
5629 * Setup an SSL context
5630 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005631int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005632 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005633{
Paul Bakker48916f92012-09-16 19:57:18 +00005634 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005635 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005636
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005637 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005638
5639 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005640 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005641 */
k-stachowiak4772a1f2018-07-09 10:43:37 +02005642 ssl->in_buf = NULL;
5643 ssl->out_buf = NULL;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005644 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5645 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005646 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
k-stachowiak83f9fba2018-07-31 17:13:26 +02005648 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiak4772a1f2018-07-09 10:43:37 +02005649 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005650 }
5651
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005652#if defined(MBEDTLS_SSL_PROTO_DTLS)
5653 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5654 {
5655 ssl->out_hdr = ssl->out_buf;
5656 ssl->out_ctr = ssl->out_buf + 3;
5657 ssl->out_len = ssl->out_buf + 11;
5658 ssl->out_iv = ssl->out_buf + 13;
5659 ssl->out_msg = ssl->out_buf + 13;
5660
5661 ssl->in_hdr = ssl->in_buf;
5662 ssl->in_ctr = ssl->in_buf + 3;
5663 ssl->in_len = ssl->in_buf + 11;
5664 ssl->in_iv = ssl->in_buf + 13;
5665 ssl->in_msg = ssl->in_buf + 13;
5666 }
5667 else
5668#endif
5669 {
5670 ssl->out_ctr = ssl->out_buf;
5671 ssl->out_hdr = ssl->out_buf + 8;
5672 ssl->out_len = ssl->out_buf + 11;
5673 ssl->out_iv = ssl->out_buf + 13;
5674 ssl->out_msg = ssl->out_buf + 13;
5675
5676 ssl->in_ctr = ssl->in_buf;
5677 ssl->in_hdr = ssl->in_buf + 8;
5678 ssl->in_len = ssl->in_buf + 11;
5679 ssl->in_iv = ssl->in_buf + 13;
5680 ssl->in_msg = ssl->in_buf + 13;
5681 }
5682
Paul Bakker48916f92012-09-16 19:57:18 +00005683 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiak4772a1f2018-07-09 10:43:37 +02005684 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00005685
5686 return( 0 );
k-stachowiak4772a1f2018-07-09 10:43:37 +02005687
5688error:
5689 mbedtls_free( ssl->in_buf );
5690 mbedtls_free( ssl->out_buf );
5691
5692 ssl->conf = NULL;
5693
5694 ssl->in_buf = NULL;
5695 ssl->out_buf = NULL;
5696
5697 ssl->in_hdr = NULL;
5698 ssl->in_ctr = NULL;
5699 ssl->in_len = NULL;
5700 ssl->in_iv = NULL;
5701 ssl->in_msg = NULL;
5702
5703 ssl->out_hdr = NULL;
5704 ssl->out_ctr = NULL;
5705 ssl->out_len = NULL;
5706 ssl->out_iv = NULL;
5707 ssl->out_msg = NULL;
5708
k-stachowiak83f9fba2018-07-31 17:13:26 +02005709 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005710}
5711
5712/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005713 * Reset an initialized and used SSL context for re-use while retaining
5714 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005715 *
5716 * If partial is non-zero, keep data in the input buffer and client ID.
5717 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00005718 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005719static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005720{
Paul Bakker48916f92012-09-16 19:57:18 +00005721 int ret;
5722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005724
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005725 /* Cancel any possibly running timer */
5726 ssl_set_timer( ssl, 0 );
5727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#if defined(MBEDTLS_SSL_RENEGOTIATION)
5729 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005730 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005731
5732 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
5734 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005737
Paul Bakker7eb013f2011-10-06 12:37:39 +00005738 ssl->in_offt = NULL;
5739
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005740 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005741 ssl->in_msgtype = 0;
5742 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005743 if( partial == 0 )
5744 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005746 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005747 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005750 ssl_dtls_replay_reset( ssl );
5751#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005752
5753 ssl->in_hslen = 0;
5754 ssl->nb_zero = 0;
Hanno Becker704f4932017-06-08 13:08:45 +01005755 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005756
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005757 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005758 ssl->out_msgtype = 0;
5759 ssl->out_msglen = 0;
5760 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005761#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
5762 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005763 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005764#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005765
Paul Bakker48916f92012-09-16 19:57:18 +00005766 ssl->transform_in = NULL;
5767 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005768
Hanno Becker3328d8c2018-08-13 16:35:15 +01005769 ssl->session_in = NULL;
5770 ssl->session_out = NULL;
5771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005773 if( partial == 0 )
5774 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5777 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
5780 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
5783 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005784 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005785 }
5786#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005787
Paul Bakker48916f92012-09-16 19:57:18 +00005788 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005790 mbedtls_ssl_transform_free( ssl->transform );
5791 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005792 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005793 }
Paul Bakker48916f92012-09-16 19:57:18 +00005794
Paul Bakkerc0463502013-02-14 11:19:38 +01005795 if( ssl->session )
5796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005797 mbedtls_ssl_session_free( ssl->session );
5798 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005799 ssl->session = NULL;
5800 }
5801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005803 ssl->alpn_chosen = NULL;
5804#endif
5805
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005806#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005807 if( partial == 0 )
5808 {
5809 mbedtls_free( ssl->cli_id );
5810 ssl->cli_id = NULL;
5811 ssl->cli_id_len = 0;
5812 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005813#endif
5814
Paul Bakker48916f92012-09-16 19:57:18 +00005815 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5816 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005817
5818 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005819}
5820
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005821/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005822 * Reset an initialized and used SSL context for re-use while retaining
5823 * all application-set variables, function pointers and data.
5824 */
5825int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
5826{
5827 return( ssl_session_reset_int( ssl, 0 ) );
5828}
5829
5830/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 * SSL set accessors
5832 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005833void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00005834{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005835 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00005836}
5837
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02005838void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005839{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005840 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005841}
5842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005844void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005845{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005846 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005847}
5848#endif
5849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005850#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005851void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005852{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005853 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005854}
5855#endif
5856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005858void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005859{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005860 conf->hs_timeout_min = min;
5861 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005862}
5863#endif
5864
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005865void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00005866{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005867 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00005868}
5869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005871void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005872 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005873 void *p_vrfy )
5874{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005875 conf->f_vrfy = f_vrfy;
5876 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005879
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005880void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005881 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 void *p_rng )
5883{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01005884 conf->f_rng = f_rng;
5885 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00005886}
5887
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005888void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02005889 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 void *p_dbg )
5891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005892 conf->f_dbg = f_dbg;
5893 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00005894}
5895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005897 void *p_bio,
5898 int (*f_send)(void *, const unsigned char *, size_t),
5899 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005900 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005901{
5902 ssl->p_bio = p_bio;
5903 ssl->f_send = f_send;
5904 ssl->f_recv = f_recv;
5905 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005906}
5907
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005908void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005909{
5910 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005911}
5912
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005913void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
5914 void *p_timer,
5915 void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms),
5916 int (*f_get_timer)(void *) )
5917{
5918 ssl->p_timer = p_timer;
5919 ssl->f_set_timer = f_set_timer;
5920 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005921
5922 /* Make sure we start with no timer running */
5923 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005924}
5925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005927void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005928 void *p_cache,
5929 int (*f_get_cache)(void *, mbedtls_ssl_session *),
5930 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005931{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005932 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005933 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005934 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938#if defined(MBEDTLS_SSL_CLI_C)
5939int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005940{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005941 int ret;
5942
5943 if( ssl == NULL ||
5944 session == NULL ||
5945 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005946 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005949 }
5950
5951 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5952 return( ret );
5953
Paul Bakker0a597072012-09-25 21:55:46 +00005954 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005955
5956 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005957}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005959
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005960void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005961 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005962{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005963 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
5964 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
5965 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
5966 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005967}
5968
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005969void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005970 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005971 int major, int minor )
5972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005974 return;
5975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005977 return;
5978
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005979 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005980}
5981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005983void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01005984 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005985{
5986 conf->cert_profile = profile;
5987}
5988
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005989/* Append a new keycert entry to a (possibly empty) list */
5990static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
5991 mbedtls_x509_crt *cert,
5992 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005993{
niisato8ba6ff52018-06-25 19:05:48 +09005994 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005995
niisato8ba6ff52018-06-25 19:05:48 +09005996 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
5997 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005998 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005999
niisato8ba6ff52018-06-25 19:05:48 +09006000 new_cert->cert = cert;
6001 new_cert->key = key;
6002 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006003
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006004 /* Update head is the list was null, else add to the end */
6005 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01006006 {
niisato8ba6ff52018-06-25 19:05:48 +09006007 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01006008 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006009 else
6010 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006011 mbedtls_ssl_key_cert *cur = *head;
6012 while( cur->next != NULL )
6013 cur = cur->next;
niisato8ba6ff52018-06-25 19:05:48 +09006014 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006015 }
6016
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006017 return( 0 );
6018}
6019
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006020int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02006021 mbedtls_x509_crt *own_cert,
6022 mbedtls_pk_context *pk_key )
6023{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02006024 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006025}
6026
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006027void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006028 mbedtls_x509_crt *ca_chain,
6029 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006030{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006031 conf->ca_chain = ca_chain;
6032 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00006033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00006035
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006036#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6037int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
6038 mbedtls_x509_crt *own_cert,
6039 mbedtls_pk_context *pk_key )
6040{
6041 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
6042 own_cert, pk_key ) );
6043}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006044
6045void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
6046 mbedtls_x509_crt *ca_chain,
6047 mbedtls_x509_crl *ca_crl )
6048{
6049 ssl->handshake->sni_ca_chain = ca_chain;
6050 ssl->handshake->sni_ca_crl = ca_crl;
6051}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006052
6053void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
6054 int authmode )
6055{
6056 ssl->handshake->sni_authmode = authmode;
6057}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02006058#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
6059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006061int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006062 const unsigned char *psk, size_t psk_len,
6063 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006064{
Paul Bakker6db455e2013-09-18 17:29:31 +02006065 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02006067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01006070
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02006071 /* Identity len will be encoded on two bytes */
6072 if( ( psk_identity_len >> 16 ) != 0 ||
6073 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
6074 {
6075 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6076 }
6077
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006078 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02006079 {
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006080 mbedtls_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006081
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006082 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnardffb81802015-10-20 19:56:45 +02006083 conf->psk = NULL;
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006084 conf->psk_len = 0;
6085 }
6086 if( conf->psk_identity != NULL )
6087 {
6088 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnardffb81802015-10-20 19:56:45 +02006089 conf->psk_identity = NULL;
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006090 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02006091 }
6092
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006093 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
6094 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05006095 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006096 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006097 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006098 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02006099 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006100 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05006101 }
Paul Bakker6db455e2013-09-18 17:29:31 +02006102
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006103 conf->psk_len = psk_len;
6104 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02006105
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01006106 memcpy( conf->psk, psk, conf->psk_len );
6107 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006108
6109 return( 0 );
6110}
6111
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006112int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
6113 const unsigned char *psk, size_t psk_len )
6114{
6115 if( psk == NULL || ssl->handshake == NULL )
6116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6117
6118 if( psk_len > MBEDTLS_PSK_MAX_LEN )
6119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6120
6121 if( ssl->handshake->psk != NULL )
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006122 {
6123 mbedtls_zeroize( ssl->handshake->psk, ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01006124 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garcia3d231462017-07-05 14:25:21 +01006125 ssl->handshake->psk_len = 0;
Andres Amaya Garcia1b7d6f82017-06-26 11:35:17 +01006126 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006127
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006128 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006129 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006130
6131 ssl->handshake->psk_len = psk_len;
6132 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
6133
6134 return( 0 );
6135}
6136
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006137void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02006139 size_t),
6140 void *p_psk )
6141{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006142 conf->f_psk = f_psk;
6143 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00006146
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006147#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006148int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00006149{
6150 int ret;
6151
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006152 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
6153 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
6154 {
6155 mbedtls_mpi_free( &conf->dhm_P );
6156 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006157 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006158 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006159
6160 return( 0 );
6161}
6162
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006163int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00006164{
6165 int ret;
6166
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006167 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
6168 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
6169 {
6170 mbedtls_mpi_free( &conf->dhm_P );
6171 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00006172 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01006173 }
Paul Bakker1b57b062011-01-06 15:48:19 +00006174
6175 return( 0 );
6176}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006177#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00006178
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006179#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6180/*
6181 * Set the minimum length for Diffie-Hellman parameters
6182 */
6183void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
6184 unsigned int bitlen )
6185{
6186 conf->dhm_min_bitlen = bitlen;
6187}
6188#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
6189
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02006190#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006191/*
6192 * Set allowed/preferred hashes for handshake signatures
6193 */
6194void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
6195 const int *hashes )
6196{
6197 conf->sig_hashes = hashes;
6198}
Hanno Becker593b0d32017-04-07 13:25:49 +01006199#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02006200
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006201#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006202/*
6203 * Set the allowed elliptic curves
6204 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006205void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006206 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006207{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006208 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006209}
Hanno Becker593b0d32017-04-07 13:25:49 +01006210#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006211
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006212#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006214{
Hanno Becker593b0d32017-04-07 13:25:49 +01006215 /* Initialize to suppress unnecessary compiler warning */
6216 size_t hostname_len = 0;
6217
6218 /* Check if new hostname is valid before
6219 * making any change to current one */
6220
6221 if( hostname != NULL )
6222 {
6223 hostname_len = strlen( hostname );
6224
6225 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6227 }
6228
6229 /* Now it's clear that we will overwrite the old hostname,
6230 * so we can free it safely */
6231
6232 if( ssl->hostname != NULL )
6233 {
6234 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6235 mbedtls_free( ssl->hostname );
6236 }
6237
6238 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006239
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 if( hostname == NULL )
Hanno Becker593b0d32017-04-07 13:25:49 +01006241 {
6242 ssl->hostname = NULL;
6243 }
6244 else
6245 {
6246 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006247
Hanno Becker593b0d32017-04-07 13:25:49 +01006248 if( ssl->hostname == NULL )
6249 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006250
Hanno Becker593b0d32017-04-07 13:25:49 +01006251 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006252
Hanno Becker593b0d32017-04-07 13:25:49 +01006253 ssl->hostname[hostname_len] = '\0';
6254 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006255
6256 return( 0 );
6257}
Hanno Beckerc7845e52017-04-07 13:02:16 +01006258#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006259
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006260#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006261void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006263 const unsigned char *, size_t),
6264 void *p_sni )
6265{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006266 conf->f_sni = f_sni;
6267 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006272int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006273{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006274 size_t cur_len, tot_len;
6275 const char **p;
6276
6277 /*
Brian J Murraye7f8dc32016-11-06 04:45:15 -08006278 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6279 * MUST NOT be truncated."
6280 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006281 */
6282 tot_len = 0;
6283 for( p = protos; *p != NULL; p++ )
6284 {
6285 cur_len = strlen( *p );
6286 tot_len += cur_len;
6287
6288 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006290 }
6291
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006292 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006293
6294 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006295}
6296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006298{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006299 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006301#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006302
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006303void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006304{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006305 conf->max_major_ver = major;
6306 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006307}
6308
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006309void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006310{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006311 conf->min_major_ver = major;
6312 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006313}
6314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006316void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006317{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006318 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006319}
6320#endif
6321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006323void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006324{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006325 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006326}
6327#endif
6328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006330void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006331{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006332 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006333}
6334#endif
6335
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006336#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006337void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006338{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006339 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006340}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006341#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006344int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6347 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006350 }
6351
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006352 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006353
6354 return( 0 );
6355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006359void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006360{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006361 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006366void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006367{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006368 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006369}
6370#endif
6371
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006372void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006373{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006374 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006375}
6376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006378void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006379{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006380 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006381}
6382
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006383void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006384{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006385 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006386}
6387
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006388void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006389 const unsigned char period[8] )
6390{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006391 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006392}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006396#if defined(MBEDTLS_SSL_CLI_C)
6397void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006398{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006399 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006400}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006401#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006402
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006403#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006404void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6405 mbedtls_ssl_ticket_write_t *f_ticket_write,
6406 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6407 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006408{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006409 conf->f_ticket_write = f_ticket_write;
6410 conf->f_ticket_parse = f_ticket_parse;
6411 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006412}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006415
Paul Bakker5121ce52009-01-03 21:22:43 +00006416/*
6417 * SSL get accessors
6418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006420{
6421 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6422}
6423
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006424uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006425{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006426 if( ssl->session != NULL )
6427 return( ssl->session->verify_result );
6428
6429 if( ssl->session_negotiate != NULL )
6430 return( ssl->session_negotiate->verify_result );
6431
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006432 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006433}
6434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006436{
Paul Bakker926c8e42013-03-06 10:23:34 +01006437 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006438 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006441}
6442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006444{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006446 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006447 {
6448 switch( ssl->minor_ver )
6449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006451 return( "DTLSv1.0" );
6452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006453 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006454 return( "DTLSv1.2" );
6455
6456 default:
6457 return( "unknown (DTLS)" );
6458 }
6459 }
6460#endif
6461
Paul Bakker43ca69c2011-01-15 17:35:19 +00006462 switch( ssl->minor_ver )
6463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006464 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006465 return( "SSLv3.0" );
6466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006468 return( "TLSv1.0" );
6469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006471 return( "TLSv1.1" );
6472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006473 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006474 return( "TLSv1.2" );
6475
Paul Bakker43ca69c2011-01-15 17:35:19 +00006476 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006477 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006478 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006479}
6480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006482{
Hanno Becker42d267b2018-08-17 15:28:19 +01006483 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker07eb7ca2018-08-03 09:40:07 +01006485 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006486
Hanno Becker3328d8c2018-08-13 16:35:15 +01006487 if( transform == NULL )
6488 return( (int) mbedtls_ssl_hdr_len( ssl ) );
6489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490#if defined(MBEDTLS_ZLIB_SUPPORT)
6491 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6492 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006493#endif
6494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 case MBEDTLS_MODE_GCM:
6498 case MBEDTLS_MODE_CCM:
6499 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006500 transform_expansion = transform->minlen;
6501 break;
6502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 case MBEDTLS_MODE_CBC:
Hanno Becker07eb7ca2018-08-03 09:40:07 +01006504
6505 block_size = mbedtls_cipher_get_block_size(
6506 &transform->cipher_ctx_enc );
6507
Hanno Becker42d267b2018-08-17 15:28:19 +01006508 /* Expansion due to the addition of the MAC. */
6509 transform_expansion += transform->maclen;
6510
6511 /* Expansion due to the addition of CBC padding;
6512 * Theoretically up to 256 bytes, but we never use
6513 * more than the block size of the underlying cipher. */
6514 transform_expansion += block_size;
6515
6516 /* For TLS 1.1 or higher, an explicit IV is added
6517 * after the record header. */
Hanno Becker07eb7ca2018-08-03 09:40:07 +01006518#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
6519 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker42d267b2018-08-17 15:28:19 +01006520 transform_expansion += block_size;
Hanno Becker07eb7ca2018-08-03 09:40:07 +01006521#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker42d267b2018-08-17 15:28:19 +01006522
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006523 break;
6524
6525 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006528 }
6529
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006530 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006531}
6532
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006533#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6534size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6535{
6536 size_t max_len;
6537
6538 /*
6539 * Assume mfl_code is correct since it was checked when set
6540 */
6541 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6542
6543 /*
6544 * Check if a smaller max length was negotiated
6545 */
6546 if( ssl->session_out != NULL &&
6547 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6548 {
6549 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6550 }
6551
6552 return max_len;
6553}
6554#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556#if defined(MBEDTLS_X509_CRT_PARSE_C)
6557const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006558{
6559 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006560 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006561
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006562 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006563}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566#if defined(MBEDTLS_SSL_CLI_C)
6567int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006568{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006569 if( ssl == NULL ||
6570 dst == NULL ||
6571 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006572 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006575 }
6576
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006577 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006580
Paul Bakker5121ce52009-01-03 21:22:43 +00006581/*
Paul Bakker1961b702013-01-25 14:49:24 +01006582 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006587
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006588 if( ssl == NULL || ssl->conf == NULL )
6589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006592 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006596 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006597 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006598#endif
6599
Paul Bakker1961b702013-01-25 14:49:24 +01006600 return( ret );
6601}
6602
6603/*
6604 * Perform the SSL handshake
6605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006607{
6608 int ret = 0;
6609
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006610 if( ssl == NULL || ssl->conf == NULL )
6611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006617 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006618
6619 if( ret != 0 )
6620 break;
6621 }
6622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006624
6625 return( ret );
6626}
6627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628#if defined(MBEDTLS_SSL_RENEGOTIATION)
6629#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006630/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006631 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006634{
6635 int ret;
6636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006638
6639 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6641 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006646 return( ret );
6647 }
6648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006650
6651 return( 0 );
6652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006654
6655/*
6656 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 * - any side: calling mbedtls_ssl_renegotiate(),
6658 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
6659 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02006660 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006661 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006665{
6666 int ret;
6667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006669
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006670 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6671 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006672
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006673 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
6674 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006676 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006678 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006679 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006680 ssl->handshake->out_msg_seq = 1;
6681 else
6682 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006683 }
6684#endif
6685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
6687 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00006688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006692 return( ret );
6693 }
6694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006696
6697 return( 0 );
6698}
6699
6700/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006701 * Renegotiate current connection on client,
6702 * or request renegotiation on server
6703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006707
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006708 if( ssl == NULL || ssl->conf == NULL )
6709 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006712 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006713 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6716 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006719
6720 /* Did we already try/start sending HelloRequest? */
6721 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006723
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006724 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006729 /*
6730 * On client, either start the renegotiation process or,
6731 * if already in progress, continue the handshake
6732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006737
6738 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006741 return( ret );
6742 }
6743 }
6744 else
6745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006749 return( ret );
6750 }
6751 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006753
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006754 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006755}
6756
6757/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006758 * Check record counters and renegotiate if they're above the limit.
6759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006761{
Andres AG7fa66d42016-12-15 17:01:16 +00006762 size_t ep_len = ssl_ep_len( ssl );
6763 int in_ctr_cmp;
6764 int out_ctr_cmp;
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
6767 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006768 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006769 {
6770 return( 0 );
6771 }
6772
Andres AG7fa66d42016-12-15 17:01:16 +00006773 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
6774 ssl->conf->renego_period + ep_len, 8 - ep_len );
6775 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
6776 ssl->conf->renego_period + ep_len, 8 - ep_len );
6777
6778 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006779 {
6780 return( 0 );
6781 }
6782
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006787
6788/*
6789 * Receive application data decrypted from the SSL layer
6790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006792{
Hanno Becker6a582e82017-06-08 13:38:05 +01006793 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006794 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006795
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006796 if( ssl == NULL || ssl->conf == NULL )
6797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006802 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006805 return( ret );
6806
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006807 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006811 return( ret );
6812 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006813 }
6814#endif
6815
Hanno Becker6a582e82017-06-08 13:38:05 +01006816 /*
6817 * Check if renegotiation is necessary and/or handshake is
6818 * in process. If yes, perform/continue, and fall through
6819 * if an unexpected packet is received while the client
6820 * is waiting for the ServerHello.
6821 *
6822 * (There is no equivalent to the last condition on
6823 * the server-side as it is not treated as within
6824 * a handshake while waiting for the ClientHello
6825 * after a renegotiation request.)
6826 */
6827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker6a582e82017-06-08 13:38:05 +01006829 ret = ssl_check_ctr_renegotiate( ssl );
6830 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6831 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006834 return( ret );
6835 }
6836#endif
6837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker6a582e82017-06-08 13:38:05 +01006841 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6842 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 return( ret );
6846 }
6847 }
6848
6849 if( ssl->in_offt == NULL )
6850 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006851 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006852 if( ssl->f_get_timer != NULL &&
6853 ssl->f_get_timer( ssl->p_timer ) == -1 )
6854 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006855 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006856 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006857
Hanno Becker6a582e82017-06-08 13:38:05 +01006858 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006859 {
Hanno Becker6a582e82017-06-08 13:38:05 +01006860 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
6861 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006862
Hanno Becker6a582e82017-06-08 13:38:05 +01006863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6864 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006865 }
6866
6867 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006869 {
6870 /*
6871 * OpenSSL sends empty messages to randomize the IV
6872 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006873 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00006876 return( 0 );
6877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006879 return( ret );
6880 }
6881 }
6882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00006884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006888 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
6890 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006893
6894 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006896 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006897 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006898#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006900 }
Hanno Becker6a582e82017-06-08 13:38:05 +01006901#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006902
Hanno Becker6a582e82017-06-08 13:38:05 +01006903#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006904 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006908
6909 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006912 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006913#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00006915 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006916#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006917
Hanno Becker3cd07be2017-10-24 11:49:19 +01006918#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckere454d732017-10-24 11:47:37 +01006919 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
6920 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
6921 ssl->conf->allow_legacy_renegotiation ==
6922 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
6923 {
6924 /* DTLS clients need to know renego is server-initiated */
6925#if defined(MBEDTLS_SSL_PROTO_DTLS)
6926 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6927 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6928 {
6929 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
6930 }
6931#endif
6932 ret = ssl_start_renegotiation( ssl );
6933 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6934 ret != 0 )
6935 {
6936 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6937 return( ret );
6938 }
6939 }
6940 else
Hanno Becker3cd07be2017-10-24 11:49:19 +01006941#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00006942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945#if defined(MBEDTLS_SSL_PROTO_SSL3)
6946 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006947 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006948 /*
6949 * SSLv3 does not have a "no_renegotiation" alert
6950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006952 return( ret );
6953 }
6954 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6956#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6957 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6958 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6961 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
6962 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006963 {
6964 return( ret );
6965 }
Paul Bakker48916f92012-09-16 19:57:18 +00006966 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
6969 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006973 }
Paul Bakker48916f92012-09-16 19:57:18 +00006974 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006975
Hanno Becker6a582e82017-06-08 13:38:05 +01006976 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006977 }
Hanno Becker3cd07be2017-10-24 11:49:19 +01006978#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006980 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006981 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006982 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006983 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006986 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006988 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006989 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006990 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
6994 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006997 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006998 }
6999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00007001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
7003 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007004 }
7005
7006 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007007
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007008 /* We're going to return something now, cancel timer,
7009 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02007011 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007012
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007014 /* If we requested renego but received AppData, resend HelloRequest.
7015 * Do it now, after setting in_offt, to avoid taking this branch
7016 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007018 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007020 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02007021 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02007024 return( ret );
7025 }
7026 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02007028#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007029 }
7030
7031 n = ( len < ssl->in_msglen )
7032 ? len : ssl->in_msglen;
7033
7034 memcpy( buf, ssl->in_offt, n );
7035 ssl->in_msglen -= n;
7036
7037 if( ssl->in_msglen == 0 )
Hanno Beckercc019082017-06-09 10:51:37 +01007038 {
Paul Bakker5121ce52009-01-03 21:22:43 +00007039 /* all bytes consumed */
7040 ssl->in_offt = NULL;
Hanno Beckercc019082017-06-09 10:51:37 +01007041 ssl->keep_current_message = 0;
7042 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007043 else
7044 /* more data available */
7045 ssl->in_offt += n;
7046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007048
Paul Bakker23986e52011-04-24 08:57:21 +00007049 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007050}
7051
7052/*
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007053 * Send application data to be encrypted by the SSL layer, taking care of max
7054 * fragment length and buffer size.
7055 *
7056 * According to RFC 5246 Section 6.2.1:
7057 *
7058 * Zero-length fragments of Application data MAY be sent as they are
7059 * potentially useful as a traffic analysis countermeasure.
7060 *
7061 * Therefore, it is possible that the input message length is 0 and the
7062 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00007063 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007064static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007065 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007066{
Paul Bakker23986e52011-04-24 08:57:21 +00007067 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02007069 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florina3604112017-07-22 09:01:44 +02007070#else
7071 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
7072#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007073 if( len > max_len )
7074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007079 "maximum fragment length: %d > %d",
7080 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007082 }
7083 else
7084#endif
7085 len = max_len;
7086 }
Paul Bakker887bd502011-06-08 13:10:54 +00007087
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 if( ssl->out_left != 0 )
7089 {
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007090 /*
7091 * The user has previously tried to send the data and
7092 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
7093 * written. In this case, we expect the high-level write function
7094 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
7095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007099 return( ret );
7100 }
7101 }
Paul Bakker887bd502011-06-08 13:10:54 +00007102 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00007103 {
Andres Amaya Garciab999a732017-09-28 14:41:17 +01007104 /*
7105 * The user is trying to send a message the first time, so we need to
7106 * copy the data into the internal buffers and setup the data structure
7107 * to keep track of partial writes
7108 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007109 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007111 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00007112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00007114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00007116 return( ret );
7117 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007118 }
7119
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02007120 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007121}
7122
7123/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007124 * Write application data, doing 1/n-1 splitting if necessary.
7125 *
7126 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007127 * then the caller will call us again with the same arguments, so
Hanno Beckere298c8b2017-09-18 14:58:11 +01007128 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007131static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007132 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007133{
7134 int ret;
7135
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007136 if( ssl->conf->cbc_record_splitting ==
7137 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007138 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
7140 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
7141 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007142 {
7143 return( ssl_write_real( ssl, buf, len ) );
7144 }
7145
7146 if( ssl->split_done == 0 )
7147 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007148 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007149 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007150 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007151 }
7152
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01007153 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
7154 return( ret );
7155 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007156
7157 return( ret + 1 );
7158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007160
7161/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007162 * Write application data (public-facing wrapper)
7163 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007164int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007165{
7166 int ret;
7167
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007169
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007170 if( ssl == NULL || ssl->conf == NULL )
7171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7172
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007173#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007174 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
7175 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007176 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007177 return( ret );
7178 }
7179#endif
7180
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007181 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007182 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007183 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007184 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02007185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007186 return( ret );
7187 }
7188 }
7189
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007190#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007191 ret = ssl_write_split( ssl, buf, len );
7192#else
7193 ret = ssl_write_real( ssl, buf, len );
7194#endif
7195
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02007196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02007197
7198 return( ret );
7199}
7200
7201/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007202 * Notify the peer that the connection is being closed
7203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007205{
7206 int ret;
7207
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02007208 if( ssl == NULL || ssl->conf == NULL )
7209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007212
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007213 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
7219 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
7220 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007223 return( ret );
7224 }
7225 }
7226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007228
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02007229 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007230}
7231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00007233{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007234 if( transform == NULL )
7235 return;
7236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00007238 deflateEnd( &transform->ctx_deflate );
7239 inflateEnd( &transform->ctx_inflate );
7240#endif
7241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 mbedtls_cipher_free( &transform->cipher_ctx_enc );
7243 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02007244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 mbedtls_md_free( &transform->md_ctx_enc );
7246 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02007247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007249}
7250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_X509_CRT_PARSE_C)
7252static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007255
7256 while( cur != NULL )
7257 {
7258 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007260 cur = next;
7261 }
7262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007266{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007267 if( handshake == NULL )
7268 return;
7269
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007270#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7271 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7272 mbedtls_md5_free( &handshake->fin_md5 );
7273 mbedtls_sha1_free( &handshake->fin_sha1 );
7274#endif
7275#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7276#if defined(MBEDTLS_SHA256_C)
7277 mbedtls_sha256_free( &handshake->fin_sha256 );
7278#endif
7279#if defined(MBEDTLS_SHA512_C)
7280 mbedtls_sha512_free( &handshake->fin_sha512 );
7281#endif
7282#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284#if defined(MBEDTLS_DHM_C)
7285 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#if defined(MBEDTLS_ECDH_C)
7288 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007289#endif
7290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02007292 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007294#endif
7295
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007296#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7297 if( handshake->psk != NULL )
7298 {
7299 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7300 mbedtls_free( handshake->psk );
7301 }
7302#endif
7303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7305 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007306 /*
7307 * Free only the linked list wrapper, not the keys themselves
7308 * since the belong to the SNI callback
7309 */
7310 if( handshake->sni_key_cert != NULL )
7311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007313
7314 while( cur != NULL )
7315 {
7316 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007318 cur = next;
7319 }
7320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#if defined(MBEDTLS_SSL_PROTO_DTLS)
7324 mbedtls_free( handshake->verify_cookie );
7325 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007326 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007327#endif
7328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007330}
7331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007333{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007334 if( session == NULL )
7335 return;
7336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007338 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 mbedtls_x509_crt_free( session->peer_cert );
7341 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007342 }
Paul Bakkered27a042013-04-18 22:46:23 +02007343#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007344
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007345#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007347#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007350}
7351
Paul Bakker5121ce52009-01-03 21:22:43 +00007352/*
7353 * Free an SSL context
7354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007356{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007357 if( ssl == NULL )
7358 return;
7359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007362 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7365 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007366 }
7367
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007368 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7371 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007372 }
7373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007375 if( ssl->compress_buf != NULL )
7376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7378 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007379 }
7380#endif
7381
Paul Bakker48916f92012-09-16 19:57:18 +00007382 if( ssl->transform )
7383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 mbedtls_ssl_transform_free( ssl->transform );
7385 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007386 }
7387
7388 if( ssl->handshake )
7389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 mbedtls_ssl_handshake_free( ssl->handshake );
7391 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7392 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007394 mbedtls_free( ssl->handshake );
7395 mbedtls_free( ssl->transform_negotiate );
7396 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007397 }
7398
Paul Bakkerc0463502013-02-14 11:19:38 +01007399 if( ssl->session )
7400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 mbedtls_ssl_session_free( ssl->session );
7402 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007403 }
7404
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007405#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007406 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007407 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007408 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007410 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007411#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7414 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7417 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007418 }
7419#endif
7420
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007421#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007423#endif
7424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007426
Paul Bakker86f04f42013-02-14 11:20:09 +01007427 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007429}
7430
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007431/*
7432 * Initialze mbedtls_ssl_config
7433 */
7434void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7435{
7436 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7437}
7438
Simon Butcherc941b6c2015-12-28 01:29:10 +00007439#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007440static int ssl_preset_default_hashes[] = {
7441#if defined(MBEDTLS_SHA512_C)
7442 MBEDTLS_MD_SHA512,
7443 MBEDTLS_MD_SHA384,
7444#endif
7445#if defined(MBEDTLS_SHA256_C)
7446 MBEDTLS_MD_SHA256,
7447 MBEDTLS_MD_SHA224,
7448#endif
Gilles Peskine7344e1b2017-05-12 13:16:40 +02007449#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007450 MBEDTLS_MD_SHA1,
7451#endif
7452 MBEDTLS_MD_NONE
7453};
Simon Butcherc941b6c2015-12-28 01:29:10 +00007454#endif
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007455
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007456static int ssl_preset_suiteb_ciphersuites[] = {
7457 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7458 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7459 0
7460};
7461
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007462#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007463static int ssl_preset_suiteb_hashes[] = {
7464 MBEDTLS_MD_SHA256,
7465 MBEDTLS_MD_SHA384,
7466 MBEDTLS_MD_NONE
7467};
7468#endif
7469
7470#if defined(MBEDTLS_ECP_C)
7471static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
7472 MBEDTLS_ECP_DP_SECP256R1,
7473 MBEDTLS_ECP_DP_SECP384R1,
7474 MBEDTLS_ECP_DP_NONE
7475};
7476#endif
7477
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007478/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007479 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007480 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007481int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007482 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007483{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007484#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007485 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007486#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007487
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007488 /* Use the functions here so that they are covered in tests,
7489 * but otherwise access member directly for efficiency */
7490 mbedtls_ssl_conf_endpoint( conf, endpoint );
7491 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007492
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007493 /*
7494 * Things that are common to all presets
7495 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007496#if defined(MBEDTLS_SSL_CLI_C)
7497 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7498 {
7499 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7500#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7501 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7502#endif
7503 }
7504#endif
7505
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007506#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007507 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007508#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007509
7510#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7511 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7512#endif
7513
7514#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7515 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7516#endif
7517
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007518#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7519 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7520#endif
7521
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007522#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007523 conf->f_cookie_write = ssl_cookie_write_dummy;
7524 conf->f_cookie_check = ssl_cookie_check_dummy;
7525#endif
7526
7527#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7528 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7529#endif
7530
7531#if defined(MBEDTLS_SSL_PROTO_DTLS)
7532 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7533 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7534#endif
7535
7536#if defined(MBEDTLS_SSL_RENEGOTIATION)
7537 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG7fa66d42016-12-15 17:01:16 +00007538 memset( conf->renego_period, 0x00, 2 );
7539 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007540#endif
7541
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007542#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7543 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7544 {
7545 if( ( ret = mbedtls_ssl_conf_dh_param( conf,
Hanno Becker80e0d462017-10-13 16:51:54 +01007546 MBEDTLS_DHM_RFC3526_MODP_2048_P,
7547 MBEDTLS_DHM_RFC3526_MODP_2048_G ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007548 {
7549 return( ret );
7550 }
7551 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007552#endif
7553
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007554 /*
7555 * Preset-specific defaults
7556 */
7557 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007558 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007559 /*
7560 * NSA Suite B
7561 */
7562 case MBEDTLS_SSL_PRESET_SUITEB:
7563 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7564 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7565 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7566 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7567
7568 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7569 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7570 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7571 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7572 ssl_preset_suiteb_ciphersuites;
7573
7574#if defined(MBEDTLS_X509_CRT_PARSE_C)
7575 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007576#endif
7577
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007578#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007579 conf->sig_hashes = ssl_preset_suiteb_hashes;
7580#endif
7581
7582#if defined(MBEDTLS_ECP_C)
7583 conf->curve_list = ssl_preset_suiteb_curves;
7584#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007585 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007586
7587 /*
7588 * Default
7589 */
7590 default:
Ron Eldor1ac9aa72017-05-28 10:46:38 +03007591 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
7592 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
7593 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7594 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7595 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
7596 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
7597 MBEDTLS_SSL_MIN_MINOR_VERSION :
7598 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007599 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7600 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7601
7602#if defined(MBEDTLS_SSL_PROTO_DTLS)
7603 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7604 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
7605#endif
7606
7607 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7608 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7609 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7610 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7611 mbedtls_ssl_list_ciphersuites();
7612
7613#if defined(MBEDTLS_X509_CRT_PARSE_C)
7614 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7615#endif
7616
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007617#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007618 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007619#endif
7620
7621#if defined(MBEDTLS_ECP_C)
7622 conf->curve_list = mbedtls_ecp_grp_id_list();
7623#endif
7624
7625#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7626 conf->dhm_min_bitlen = 1024;
7627#endif
7628 }
7629
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007630 return( 0 );
7631}
7632
7633/*
7634 * Free mbedtls_ssl_config
7635 */
7636void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
7637{
7638#if defined(MBEDTLS_DHM_C)
7639 mbedtls_mpi_free( &conf->dhm_P );
7640 mbedtls_mpi_free( &conf->dhm_G );
7641#endif
7642
7643#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7644 if( conf->psk != NULL )
7645 {
7646 mbedtls_zeroize( conf->psk, conf->psk_len );
7647 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
7648 mbedtls_free( conf->psk );
7649 mbedtls_free( conf->psk_identity );
7650 conf->psk_len = 0;
7651 conf->psk_identity_len = 0;
7652 }
7653#endif
7654
7655#if defined(MBEDTLS_X509_CRT_PARSE_C)
7656 ssl_key_cert_free( conf->key_cert );
7657#endif
7658
7659 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
7660}
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007663/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#if defined(MBEDTLS_RSA_C)
7669 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
7670 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007671#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if defined(MBEDTLS_ECDSA_C)
7673 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
7674 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007675#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007677}
7678
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007679unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
7680{
7681 switch( type ) {
7682 case MBEDTLS_PK_RSA:
7683 return( MBEDTLS_SSL_SIG_RSA );
7684 case MBEDTLS_PK_ECDSA:
7685 case MBEDTLS_PK_ECKEY:
7686 return( MBEDTLS_SSL_SIG_ECDSA );
7687 default:
7688 return( MBEDTLS_SSL_SIG_ANON );
7689 }
7690}
7691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007693{
7694 switch( sig )
7695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696#if defined(MBEDTLS_RSA_C)
7697 case MBEDTLS_SSL_SIG_RSA:
7698 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007699#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700#if defined(MBEDTLS_ECDSA_C)
7701 case MBEDTLS_SSL_SIG_ECDSA:
7702 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007703#endif
7704 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007706 }
7707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708#endif /* MBEDTLS_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007709
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007710#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7711 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7712
7713/* Find an entry in a signature-hash set matching a given hash algorithm. */
7714mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7715 mbedtls_pk_type_t sig_alg )
7716{
7717 switch( sig_alg )
7718 {
7719 case MBEDTLS_PK_RSA:
7720 return( set->rsa );
7721 case MBEDTLS_PK_ECDSA:
7722 return( set->ecdsa );
7723 default:
7724 return( MBEDTLS_MD_NONE );
7725 }
7726}
7727
7728/* Add a signature-hash-pair to a signature-hash set */
7729void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7730 mbedtls_pk_type_t sig_alg,
7731 mbedtls_md_type_t md_alg )
7732{
7733 switch( sig_alg )
7734 {
7735 case MBEDTLS_PK_RSA:
7736 if( set->rsa == MBEDTLS_MD_NONE )
7737 set->rsa = md_alg;
7738 break;
7739
7740 case MBEDTLS_PK_ECDSA:
7741 if( set->ecdsa == MBEDTLS_MD_NONE )
7742 set->ecdsa = md_alg;
7743 break;
7744
7745 default:
7746 break;
7747 }
7748}
7749
7750/* Allow exactly one hash algorithm for each signature. */
7751void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7752 mbedtls_md_type_t md_alg )
7753{
7754 set->rsa = md_alg;
7755 set->ecdsa = md_alg;
7756}
7757
7758#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
7759 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
7760
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007761/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007762 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007765{
7766 switch( hash )
7767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768#if defined(MBEDTLS_MD5_C)
7769 case MBEDTLS_SSL_HASH_MD5:
7770 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772#if defined(MBEDTLS_SHA1_C)
7773 case MBEDTLS_SSL_HASH_SHA1:
7774 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776#if defined(MBEDTLS_SHA256_C)
7777 case MBEDTLS_SSL_HASH_SHA224:
7778 return( MBEDTLS_MD_SHA224 );
7779 case MBEDTLS_SSL_HASH_SHA256:
7780 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782#if defined(MBEDTLS_SHA512_C)
7783 case MBEDTLS_SSL_HASH_SHA384:
7784 return( MBEDTLS_MD_SHA384 );
7785 case MBEDTLS_SSL_HASH_SHA512:
7786 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007787#endif
7788 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007790 }
7791}
7792
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007793/*
7794 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7795 */
7796unsigned char mbedtls_ssl_hash_from_md_alg( int md )
7797{
7798 switch( md )
7799 {
7800#if defined(MBEDTLS_MD5_C)
7801 case MBEDTLS_MD_MD5:
7802 return( MBEDTLS_SSL_HASH_MD5 );
7803#endif
7804#if defined(MBEDTLS_SHA1_C)
7805 case MBEDTLS_MD_SHA1:
7806 return( MBEDTLS_SSL_HASH_SHA1 );
7807#endif
7808#if defined(MBEDTLS_SHA256_C)
7809 case MBEDTLS_MD_SHA224:
7810 return( MBEDTLS_SSL_HASH_SHA224 );
7811 case MBEDTLS_MD_SHA256:
7812 return( MBEDTLS_SSL_HASH_SHA256 );
7813#endif
7814#if defined(MBEDTLS_SHA512_C)
7815 case MBEDTLS_MD_SHA384:
7816 return( MBEDTLS_SSL_HASH_SHA384 );
7817 case MBEDTLS_MD_SHA512:
7818 return( MBEDTLS_SSL_HASH_SHA512 );
7819#endif
7820 default:
7821 return( MBEDTLS_SSL_HASH_NONE );
7822 }
7823}
7824
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007825#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007826/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007827 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007828 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007829 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007830int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007833
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007834 if( ssl->conf->curve_list == NULL )
7835 return( -1 );
7836
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007837 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007838 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007839 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007840
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007841 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007842}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007843#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007844
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007845#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007846/*
7847 * Check if a hash proposed by the peer is in our list.
7848 * Return 0 if we're willing to use it, -1 otherwise.
7849 */
7850int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
7851 mbedtls_md_type_t md )
7852{
7853 const int *cur;
7854
7855 if( ssl->conf->sig_hashes == NULL )
7856 return( -1 );
7857
7858 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
7859 if( *cur == (int) md )
7860 return( 0 );
7861
7862 return( -1 );
7863}
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007864#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866#if defined(MBEDTLS_X509_CRT_PARSE_C)
7867int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
7868 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007869 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007870 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007871{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007872 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007874 int usage = 0;
7875#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007877 const char *ext_oid;
7878 size_t ext_len;
7879#endif
7880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7882 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007883 ((void) cert);
7884 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007885 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007886#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
7889 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007890 {
7891 /* Server part of the key exchange */
7892 switch( ciphersuite->key_exchange )
7893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 case MBEDTLS_KEY_EXCHANGE_RSA:
7895 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007896 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007897 break;
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7900 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7901 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7902 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007903 break;
7904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7906 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007907 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007908 break;
7909
7910 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 case MBEDTLS_KEY_EXCHANGE_NONE:
7912 case MBEDTLS_KEY_EXCHANGE_PSK:
7913 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7914 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007915 usage = 0;
7916 }
7917 }
7918 else
7919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7921 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007922 }
7923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007925 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007926 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007927 ret = -1;
7928 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007929#else
7930 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
7934 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 ext_oid = MBEDTLS_OID_SERVER_AUTH;
7937 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007938 }
7939 else
7940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
7942 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007943 }
7944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007946 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007947 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007948 ret = -1;
7949 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007951
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007952 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007953}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007955
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007956/*
7957 * Convert version numbers to/from wire format
7958 * and, for DTLS, to/from TLS equivalent.
7959 *
7960 * For TLS this is the identity.
Brian J Murraye7f8dc32016-11-06 04:45:15 -08007961 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007962 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
7963 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
7964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007966 unsigned char ver[2] )
7967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_PROTO_DTLS)
7969 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007972 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7973
7974 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
7975 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
7976 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007977 else
7978#else
7979 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007980#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007981 {
7982 ver[0] = (unsigned char) major;
7983 ver[1] = (unsigned char) minor;
7984 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007985}
7986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007988 const unsigned char ver[2] )
7989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990#if defined(MBEDTLS_SSL_PROTO_DTLS)
7991 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007992 {
7993 *major = 255 - ver[0] + 2;
7994 *minor = 255 - ver[1] + 1;
7995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007997 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7998 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007999 else
8000#else
8001 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008002#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01008003 {
8004 *major = ver[0];
8005 *minor = ver[1];
8006 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01008007}
8008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009#endif /* MBEDTLS_SSL_TLS_C */