blob: d95180d1a17fb43f340d99ed483afa45449da052 [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;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200494 size_t iv_copy_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 const mbedtls_cipher_info_t *cipher_info;
496 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_ssl_session *session = ssl->session_negotiate;
499 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
500 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100505 if( cipher_info == NULL )
506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100508 transform->ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100510 }
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100513 if( md_info == NULL )
514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100516 transform->ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100518 }
519
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000521 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_SSL_PROTO_SSL3)
524 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000525 {
Paul Bakker48916f92012-09-16 19:57:18 +0000526 handshake->tls_prf = ssl3_prf;
527 handshake->calc_verify = ssl_calc_verify_ssl;
528 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000529 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200530 else
531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
533 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000534 {
Paul Bakker48916f92012-09-16 19:57:18 +0000535 handshake->tls_prf = tls1_prf;
536 handshake->calc_verify = ssl_calc_verify_tls;
537 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000538 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200539 else
540#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
542#if defined(MBEDTLS_SHA512_C)
543 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
544 transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000545 {
Paul Bakker48916f92012-09-16 19:57:18 +0000546 handshake->tls_prf = tls_prf_sha384;
547 handshake->calc_verify = ssl_calc_verify_tls_sha384;
548 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000549 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000550 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200551#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_SHA256_C)
553 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000554 {
Paul Bakker48916f92012-09-16 19:57:18 +0000555 handshake->tls_prf = tls_prf_sha256;
556 handshake->calc_verify = ssl_calc_verify_tls_sha256;
557 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000558 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200559 else
560#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200565 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000566
567 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 * SSLv3:
569 * master =
570 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
571 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
572 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200573 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200574 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 * master = PRF( premaster, "master secret", randbytes )[0..47]
576 */
Paul Bakker0a597072012-09-25 21:55:46 +0000577 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
Paul Bakker48916f92012-09-16 19:57:18 +0000580 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
583 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200584 {
585 unsigned char session_hash[48];
586 size_t hash_len;
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200589
590 ssl->handshake->calc_verify( ssl, session_hash );
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
593 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200596 if( ssl->transform_negotiate->ciphersuite_info->mac ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200598 {
599 hash_len = 48;
600 }
601 else
602#endif
603 hash_len = 32;
604 }
605 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200607 hash_len = 36;
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200610
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100611 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
612 "extended master secret",
613 session_hash, hash_len,
614 session->master, 48 );
615 if( ret != 0 )
616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100618 return( ret );
619 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200620
621 }
622 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200623#endif
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100624 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
625 "master secret",
626 handshake->randbytes, 64,
627 session->master, 48 );
628 if( ret != 0 )
629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100631 return( ret );
632 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 }
636 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
639 /*
640 * Swap the client and server random values.
641 */
Paul Bakker48916f92012-09-16 19:57:18 +0000642 memcpy( tmp, handshake->randbytes, 64 );
643 memcpy( handshake->randbytes, tmp + 32, 32 );
644 memcpy( handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 /*
648 * SSLv3:
649 * key block =
650 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
651 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
652 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
653 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
654 * ...
655 *
656 * TLSv1:
657 * key block = PRF( master, "key expansion", randbytes )
658 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100659 ret = handshake->tls_prf( session->master, 48, "key expansion",
660 handshake->randbytes, 64, keyblk, 256 );
661 if( ret != 0 )
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100664 return( ret );
665 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
668 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
669 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
670 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
671 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 /*
676 * Determine the appropriate key, IV and MAC length.
677 */
Paul Bakker68884e32013-01-07 18:20:04 +0100678
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200679 transform->keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
682 cipher_info->mode == MBEDTLS_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200684 transform->maclen = 0;
685
Paul Bakker68884e32013-01-07 18:20:04 +0100686 transform->ivlen = 12;
687 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200688
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200689 /* Minimum length is expicit IV + tag */
690 transform->minlen = transform->ivlen - transform->fixed_ivlen
691 + ( transform->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100693 }
694 else
695 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200696 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
698 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200701 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100702 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200704 /* Get MAC length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 transform->maclen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200708 /*
709 * If HMAC is to be truncated, we shall keep the leftmost bytes,
710 * (rfc 6066 page 13 or rfc 2104 section 4),
711 * so we only need to adjust the length here.
712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
714 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
715#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200716
717 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100718 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200720 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200722 transform->minlen = transform->maclen;
723 else
Paul Bakker68884e32013-01-07 18:20:04 +0100724 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200725 /*
726 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100727 * 1. if EtM is in use: one block plus MAC
728 * otherwise: * first multiple of blocklen greater than maclen
729 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
732 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100733 {
734 transform->minlen = transform->maclen
735 + cipher_info->block_size;
736 }
737 else
738#endif
739 {
740 transform->minlen = transform->maclen
741 + cipher_info->block_size
742 - transform->maclen % cipher_info->block_size;
743 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
746 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
747 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200748 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100749 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200750#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
752 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
753 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200754 {
755 transform->minlen += transform->ivlen;
756 }
757 else
758#endif
759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200762 }
Paul Bakker68884e32013-01-07 18:20:04 +0100763 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 }
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000767 transform->keylen, transform->minlen, transform->ivlen,
768 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770 /*
771 * Finally setup the cipher contexts, IVs and MAC secrets.
772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200774 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 {
Paul Bakker48916f92012-09-16 19:57:18 +0000776 key1 = keyblk + transform->maclen * 2;
777 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Paul Bakker68884e32013-01-07 18:20:04 +0100779 mac_enc = keyblk;
780 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000782 /*
783 * This is not used in TLS v1.1.
784 */
Paul Bakker48916f92012-09-16 19:57:18 +0000785 iv_copy_len = ( transform->fixed_ivlen ) ?
786 transform->fixed_ivlen : transform->ivlen;
787 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
788 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790 }
791 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#endif /* MBEDTLS_SSL_CLI_C */
793#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200794 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000795 {
Paul Bakker48916f92012-09-16 19:57:18 +0000796 key1 = keyblk + transform->maclen * 2 + transform->keylen;
797 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000798
Paul Bakker68884e32013-01-07 18:20:04 +0100799 mac_enc = keyblk + transform->maclen;
800 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000802 /*
803 * This is not used in TLS v1.1.
804 */
Paul Bakker48916f92012-09-16 19:57:18 +0000805 iv_copy_len = ( transform->fixed_ivlen ) ?
806 transform->fixed_ivlen : transform->ivlen;
807 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
808 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000809 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100811 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
815 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100816 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_SSL_PROTO_SSL3)
819 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100820 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100821 if( transform->maclen > sizeof transform->mac_enc )
822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100825 }
826
Paul Bakker68884e32013-01-07 18:20:04 +0100827 memcpy( transform->mac_enc, mac_enc, transform->maclen );
828 memcpy( transform->mac_dec, mac_dec, transform->maclen );
829 }
830 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#endif /* MBEDTLS_SSL_PROTO_SSL3 */
832#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
833 defined(MBEDTLS_SSL_PROTO_TLS1_2)
834 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
837 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
Paul Bakker68884e32013-01-07 18:20:04 +0100838 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200839 else
840#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200844 }
Paul Bakker68884e32013-01-07 18:20:04 +0100845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
847 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000848 {
849 int ret = 0;
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +0000852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100854 transform->iv_enc, transform->iv_dec,
855 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100856 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100857 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
860 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000861 }
862 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000864
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200865 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200866 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200869 return( ret );
870 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200871
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200872 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200873 cipher_info ) ) != 0 )
874 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200876 return( ret );
877 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200880 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200884 return( ret );
885 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200888 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", 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 defined(MBEDTLS_CIPHER_MODE_CBC)
896 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
899 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200902 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200903 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
906 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200909 return( ret );
910 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +0000917 // Initialize compression
918 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000920 {
Paul Bakker16770332013-10-11 09:59:44 +0200921 if( ssl->compress_buf == NULL )
922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200924 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +0200925 if( ssl->compress_buf == NULL )
926 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 MBEDTLS_SSL_BUFFER_LEN ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200929 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker16770332013-10-11 09:59:44 +0200930 }
931 }
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000934
Paul Bakker48916f92012-09-16 19:57:18 +0000935 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
936 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000937
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200938 if( deflateInit( &transform->ctx_deflate,
939 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000940 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
943 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000944 }
945 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +0000947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
950 return( 0 );
951}
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#if defined(MBEDTLS_SSL_PROTO_SSL3)
954void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000955{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200956 mbedtls_md5_context md5;
957 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000958 unsigned char pad_1[48];
959 unsigned char pad_2[48];
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +0200963 mbedtls_md5_init( &md5 );
964 mbedtls_sha1_init( &sha1 );
965
966 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
967 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Paul Bakker380da532012-04-18 16:10:25 +0000969 memset( pad_1, 0x36, 48 );
970 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000971
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200972 mbedtls_md5_update( &md5, ssl->session_negotiate->master, 48 );
973 mbedtls_md5_update( &md5, pad_1, 48 );
974 mbedtls_md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000975
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200976 mbedtls_md5_starts( &md5 );
977 mbedtls_md5_update( &md5, ssl->session_negotiate->master, 48 );
978 mbedtls_md5_update( &md5, pad_2, 48 );
979 mbedtls_md5_update( &md5, hash, 16 );
980 mbedtls_md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200982 mbedtls_sha1_update( &sha1, ssl->session_negotiate->master, 48 );
983 mbedtls_sha1_update( &sha1, pad_1, 40 );
984 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +0000985
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200986 mbedtls_sha1_starts( &sha1 );
987 mbedtls_sha1_update( &sha1, ssl->session_negotiate->master, 48 );
988 mbedtls_sha1_update( &sha1, pad_2, 40 );
989 mbedtls_sha1_update( &sha1, hash + 16, 20 );
990 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +0000991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +0000994
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200995 mbedtls_md5_free( &md5 );
996 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200997
Paul Bakker380da532012-04-18 16:10:25 +0000998 return;
999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1003void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001004{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001005 mbedtls_md5_context md5;
1006 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001009
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001010 mbedtls_md5_init( &md5 );
1011 mbedtls_sha1_init( &sha1 );
1012
1013 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1014 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001015
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001016 mbedtls_md5_finish( &md5, hash );
1017 mbedtls_sha1_finish( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001021
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001022 mbedtls_md5_free( &md5 );
1023 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001024
Paul Bakker380da532012-04-18 16:10:25 +00001025 return;
1026}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1030#if defined(MBEDTLS_SHA256_C)
1031void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001032{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001033 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001034
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001035 mbedtls_sha256_init( &sha256 );
1036
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001038
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001039 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001040 mbedtls_sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001044
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001045 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001046
Paul Bakker380da532012-04-18 16:10:25 +00001047 return;
1048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_SHA512_C)
1052void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001053{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001054 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001055
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001056 mbedtls_sha512_init( &sha512 );
1057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001059
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001060 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1061 mbedtls_sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001066 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001067
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 return;
1069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070#endif /* MBEDTLS_SHA512_C */
1071#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1074int 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 +02001075{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001076 unsigned char *p = ssl->handshake->premaster;
1077 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001078 const unsigned char *psk = ssl->conf->psk;
1079 size_t psk_len = ssl->conf->psk_len;
1080
1081 /* If the psk callback was called, use its result */
1082 if( ssl->handshake->psk != NULL )
1083 {
1084 psk = ssl->handshake->psk;
1085 psk_len = ssl->handshake->psk_len;
1086 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001087
1088 /*
1089 * PMS = struct {
1090 * opaque other_secret<0..2^16-1>;
1091 * opaque psk<0..2^16-1>;
1092 * };
1093 * with "other_secret" depending on the particular key exchange
1094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1096 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001097 {
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001098 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001100
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001101 *(p++) = (unsigned char)( psk_len >> 8 );
1102 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001103
1104 if( end < p || (size_t)( end - p ) < psk_len )
1105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1106
1107 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001108 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001109 }
1110 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1112#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1113 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001114 {
1115 /*
1116 * other_secret already set by the ClientKeyExchange message,
1117 * and is 48 bytes long
1118 */
1119 *p++ = 0;
1120 *p++ = 48;
1121 p += 48;
1122 }
1123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1125#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1126 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001127 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001128 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001129 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001130
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001131 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001133 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001134 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001137 return( ret );
1138 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001139 *(p++) = (unsigned char)( len >> 8 );
1140 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001141 p += len;
1142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001144 }
1145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1147#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1148 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001149 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001150 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001151 size_t zlen;
1152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001154 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001155 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001158 return( ret );
1159 }
1160
1161 *(p++) = (unsigned char)( zlen >> 8 );
1162 *(p++) = (unsigned char)( zlen );
1163 p += zlen;
1164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001166 }
1167 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001172 }
1173
1174 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001175 if( end - p < 2 )
1176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001177
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001178 *(p++) = (unsigned char)( psk_len >> 8 );
1179 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardc2824052015-10-21 12:35:29 +02001180
1181 if( end < p || (size_t)( end - p ) < psk_len )
1182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1183
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001184 memcpy( p, psk, psk_len );
1185 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001186
1187 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1188
1189 return( 0 );
1190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001194/*
1195 * SSLv3.0 MAC functions
1196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197static void ssl_mac( mbedtls_md_context_t *md_ctx, unsigned char *secret,
Paul Bakker68884e32013-01-07 18:20:04 +01001198 unsigned char *buf, size_t len,
1199 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001200{
1201 unsigned char header[11];
1202 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001203 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1205 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001206
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001207 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001209 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001210 else
Paul Bakker68884e32013-01-07 18:20:04 +01001211 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
1213 memcpy( header, ctr, 8 );
1214 header[ 8] = (unsigned char) type;
1215 header[ 9] = (unsigned char)( len >> 8 );
1216 header[10] = (unsigned char)( len );
1217
Paul Bakker68884e32013-01-07 18:20:04 +01001218 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_md_starts( md_ctx );
1220 mbedtls_md_update( md_ctx, secret, md_size );
1221 mbedtls_md_update( md_ctx, padding, padlen );
1222 mbedtls_md_update( md_ctx, header, 11 );
1223 mbedtls_md_update( md_ctx, buf, len );
1224 mbedtls_md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Paul Bakker68884e32013-01-07 18:20:04 +01001226 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_md_starts( md_ctx );
1228 mbedtls_md_update( md_ctx, secret, md_size );
1229 mbedtls_md_update( md_ctx, padding, padlen );
1230 mbedtls_md_update( md_ctx, buf + len, md_size );
1231 mbedtls_md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
1236 ( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1237 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001238#define SSL_SOME_MODES_USE_MAC
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001239#endif
1240
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001241/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244static int ssl_encrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001247 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001251 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1254 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001255 }
1256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001260 ssl->out_msg, ssl->out_msglen );
1261
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001263 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001264 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001265#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 if( mode == MBEDTLS_MODE_STREAM ||
1267 ( mode == MBEDTLS_MODE_CBC
1268#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1269 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001270#endif
1271 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_SSL_PROTO_SSL3)
1274 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001275 {
1276 ssl_mac( &ssl->transform_out->md_ctx_enc,
1277 ssl->transform_out->mac_enc,
1278 ssl->out_msg, ssl->out_msglen,
1279 ssl->out_ctr, ssl->out_msgtype );
1280 }
1281 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001282#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1284 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1285 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1288 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1289 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1290 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001291 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001293 ssl->out_msg + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001295 }
1296 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001297#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001301 }
1302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001304 ssl->out_msg + ssl->out_msglen,
1305 ssl->transform_out->maclen );
1306
1307 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001308 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001309 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001310#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001312 /*
1313 * Encrypt
1314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1316 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001318 int ret;
1319 size_t olen = 0;
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 "including %d bytes of padding",
1323 ssl->out_msglen, 0 ) );
1324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001326 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001327 ssl->transform_out->ivlen,
1328 ssl->out_msg, ssl->out_msglen,
1329 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001332 return( ret );
1333 }
1334
1335 if( ssl->out_msglen != olen )
1336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
Paul Bakker68884e32013-01-07 18:20:04 +01001341 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1343#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1344 if( mode == MBEDTLS_MODE_GCM ||
1345 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001346 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001347 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001348 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001349 unsigned char *enc_msg;
1350 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001351 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353
Paul Bakkerca4ab492012-04-18 14:23:57 +00001354 memcpy( add_data, ssl->out_ctr, 8 );
1355 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001357 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001358 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1359 add_data[12] = ssl->out_msglen & 0xFF;
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakkerca4ab492012-04-18 14:23:57 +00001362 add_data, 13 );
1363
Paul Bakker68884e32013-01-07 18:20:04 +01001364 /*
1365 * Generate IV
1366 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001367 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1368 {
1369 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001372 }
1373
1374 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1375 ssl->out_ctr, 8 );
1376 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001379 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001380
Paul Bakker68884e32013-01-07 18:20:04 +01001381 /*
1382 * Fix pointer positions and message length with added IV
1383 */
1384 enc_msg = ssl->out_msg;
1385 enc_msglen = ssl->out_msglen;
1386 ssl->out_msglen += ssl->transform_out->ivlen -
1387 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker68884e32013-01-07 18:20:04 +01001390 "including %d bytes of padding",
1391 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392
Paul Bakker68884e32013-01-07 18:20:04 +01001393 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001394 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001397 ssl->transform_out->iv_enc,
1398 ssl->transform_out->ivlen,
1399 add_data, 13,
1400 enc_msg, enc_msglen,
1401 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001402 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001405 return( ret );
1406 }
1407
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001408 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001412 }
1413
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001414 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001415 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1421#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1422 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1423 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001425 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001426 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001427 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001428
Paul Bakker48916f92012-09-16 19:57:18 +00001429 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1430 ssl->transform_out->ivlen;
1431 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 padlen = 0;
1433
1434 for( i = 0; i <= padlen; i++ )
1435 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1436
1437 ssl->out_msglen += padlen + 1;
1438
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001439 enc_msglen = ssl->out_msglen;
1440 enc_msg = ssl->out_msg;
1441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001443 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001444 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1445 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001448 {
1449 /*
1450 * Generate IV
1451 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001452 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001453 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001454 if( ret != 0 )
1455 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001456
Paul Bakker92be97b2013-01-02 17:30:03 +01001457 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001458 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001459
1460 /*
1461 * Fix pointer positions and message length with added IV
1462 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001463 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001465 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001470 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001471 ssl->out_msglen, ssl->transform_out->ivlen,
1472 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001475 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001476 ssl->transform_out->ivlen,
1477 enc_msg, enc_msglen,
1478 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001481 return( ret );
1482 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001483
Paul Bakkercca5b812013-08-31 17:40:26 +02001484 if( enc_msglen != olen )
1485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001488 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1491 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001492 {
1493 /*
1494 * Save IV in SSL3 and TLS1
1495 */
1496 memcpy( ssl->transform_out->iv_enc,
1497 ssl->transform_out->cipher_ctx_enc.iv,
1498 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001500#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001503 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001504 {
1505 /*
1506 * MAC(MAC_write_key, seq_num +
1507 * TLSCipherText.type +
1508 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001509 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001510 * IV + // except for TLS 1.0
1511 * ENC(content + padding + padding_length));
1512 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001513 unsigned char pseudo_hdr[13];
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001516
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001517 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1518 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001519 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1520 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1525 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001526 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001528 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001530
1531 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001532 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001536 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1538 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001544 /* Make extra sure authentication was performed, exactly once */
1545 if( auth_done != 1 )
1546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001549 }
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001552
1553 return( 0 );
1554}
1555
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001556#define SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001559{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001560 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001562 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001563#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001564 size_t padlen = 0, correct = 1;
1565#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001569 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001573 }
1574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001576
Paul Bakker48916f92012-09-16 19:57:18 +00001577 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001580 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 }
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1585 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001586 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001587 int ret;
1588 size_t olen = 0;
1589
Paul Bakker68884e32013-01-07 18:20:04 +01001590 padlen = 0;
1591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001593 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001594 ssl->transform_in->ivlen,
1595 ssl->in_msg, ssl->in_msglen,
1596 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001599 return( ret );
1600 }
1601
1602 if( ssl->in_msglen != olen )
1603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001606 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 }
Paul Bakker68884e32013-01-07 18:20:04 +01001608 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1610#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1611 if( mode == MBEDTLS_MODE_GCM ||
1612 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001613 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001614 int ret;
1615 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001616 unsigned char *dec_msg;
1617 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001618 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001619 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001621 size_t explicit_iv_len = ssl->transform_in->ivlen -
1622 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001623
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001624 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001627 "+ taglen (%d)", ssl->in_msglen,
1628 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001630 }
1631 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1632
Paul Bakker68884e32013-01-07 18:20:04 +01001633 dec_msg = ssl->in_msg;
1634 dec_msg_result = ssl->in_msg;
1635 ssl->in_msglen = dec_msglen;
1636
1637 memcpy( add_data, ssl->in_ctr, 8 );
1638 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001640 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001641 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1642 add_data[12] = ssl->in_msglen & 0xFF;
1643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001645 add_data, 13 );
1646
1647 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1648 ssl->in_iv,
1649 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001652 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001654
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001655 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001656 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001659 ssl->transform_in->iv_dec,
1660 ssl->transform_in->ivlen,
1661 add_data, 13,
1662 dec_msg, dec_msglen,
1663 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001664 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1669 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001670
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001671 return( ret );
1672 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001673 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001674
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001675 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001679 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001681 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1683#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1684 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1685 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001686 {
Paul Bakker45829992013-01-03 14:52:21 +01001687 /*
1688 * Decrypt and check the padding
1689 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001690 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001691 unsigned char *dec_msg;
1692 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001693 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001694 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001695 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001696
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 /*
Paul Bakker45829992013-01-03 14:52:21 +01001698 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1701 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001702 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001703#endif
Paul Bakker45829992013-01-03 14:52:21 +01001704
1705 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1706 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001709 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1710 ssl->transform_in->ivlen,
1711 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001713 }
1714
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715 dec_msglen = ssl->in_msglen;
1716 dec_msg = ssl->in_msg;
1717 dec_msg_result = ssl->in_msg;
1718
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001719 /*
1720 * Authenticate before decrypt if enabled
1721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1723 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001724 {
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001725 unsigned char computed_mac[SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001726 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001729
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001730 dec_msglen -= ssl->transform_in->maclen;
1731 ssl->in_msglen -= ssl->transform_in->maclen;
1732
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001733 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1734 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1735 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1736 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1741 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001742 ssl->in_iv, ssl->in_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1744 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001747 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001749 ssl->transform_in->maclen );
1750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001752 ssl->transform_in->maclen ) != 0 )
1753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001757 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001758 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001759 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001761
1762 /*
1763 * Check length sanity
1764 */
1765 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001768 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001770 }
1771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001773 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001774 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001777 {
Paul Bakker48916f92012-09-16 19:57:18 +00001778 dec_msglen -= ssl->transform_in->ivlen;
1779 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001780
Paul Bakker48916f92012-09-16 19:57:18 +00001781 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001782 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001783 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001787 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001788 ssl->transform_in->ivlen,
1789 dec_msg, dec_msglen,
1790 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001793 return( ret );
1794 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001795
Paul Bakkercca5b812013-08-31 17:40:26 +02001796 if( dec_msglen != olen )
1797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1799 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001800 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1803 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001804 {
1805 /*
1806 * Save IV in SSL3 and TLS1
1807 */
1808 memcpy( ssl->transform_in->iv_dec,
1809 ssl->transform_in->cipher_ctx_dec.iv,
1810 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001811 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001812#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
1814 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001815
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001816 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001817 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819#if defined(MBEDTLS_SSL_DEBUG_ALL)
1820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01001821 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001822#endif
Paul Bakker45829992013-01-03 14:52:21 +01001823 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001824 correct = 0;
1825 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#if defined(MBEDTLS_SSL_PROTO_SSL3)
1828 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 {
Paul Bakker48916f92012-09-16 19:57:18 +00001830 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#if defined(MBEDTLS_SSL_DEBUG_ALL)
1833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001835 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001836#endif
Paul Bakker45829992013-01-03 14:52:21 +01001837 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 }
1839 }
1840 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1842#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1843 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1844 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 {
1846 /*
Paul Bakker45829992013-01-03 14:52:21 +01001847 * TLSv1+: always check the padding up to the first failure
1848 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001850 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001851 size_t padding_idx = ssl->in_msglen - padlen - 1;
1852
Paul Bakker956c9e02013-12-19 14:42:28 +01001853 /*
1854 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001855 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001856 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 * 2. padding_idx >= MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001858 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001859 *
1860 * In both cases we reset padding_idx to a safe value (0) to
1861 * prevent out-of-buffer reads.
1862 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001863 correct &= ( ssl->in_msglen >= padlen + 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 correct &= ( padding_idx < MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001865 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001866
1867 padding_idx *= correct;
1868
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001869 for( i = 1; i <= 256; i++ )
1870 {
1871 real_count &= ( i <= padlen );
1872 pad_count += real_count *
1873 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1874 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001875
1876 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001879 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001881#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001882 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001884 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1886 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1889 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001890 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001891
1892 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001893 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001894 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1896 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1899 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001900 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00001903 ssl->in_msg, ssl->in_msglen );
1904
1905 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001906 * Authenticate if not done yet.
1907 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001908 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001909#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001910 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001911 {
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001912 unsigned char tmp[SSL_MAX_MAC_SIZE];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001913
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001914 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001916 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1917 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001919 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#if defined(MBEDTLS_SSL_PROTO_SSL3)
1922 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001923 {
1924 ssl_mac( &ssl->transform_in->md_ctx_dec,
1925 ssl->transform_in->mac_dec,
1926 ssl->in_msg, ssl->in_msglen,
1927 ssl->in_ctr, ssl->in_msgtype );
1928 }
1929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1931#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1932 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1933 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001934 {
1935 /*
1936 * Process MAC and always update for padlen afterwards to make
1937 * total time independent of padlen
1938 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001939 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001940 *
1941 * Known timing attacks:
1942 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1943 *
1944 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1945 * correctly. (We round down instead of up, so -56 is the correct
1946 * value for our calculations instead of -55)
1947 */
1948 size_t j, extra_run = 0;
1949 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1950 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001951
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001952 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1955 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1956 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
1957 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001958 ssl->in_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001960 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001961 /* Call mbedtls_md_process at least once due to cache attacks */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001962 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001966 }
1967 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1969 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1976 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001977 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 if( mbedtls_ssl_safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001980 ssl->transform_in->maclen ) != 0 )
1981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982#if defined(MBEDTLS_SSL_DEBUG_ALL)
1983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001984#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001985 correct = 0;
1986 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001987 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001988
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001989 /*
1990 * Finally check the correct flag
1991 */
1992 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001994 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001995#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001996
1997 /* Make extra sure authentication was performed, exactly once */
1998 if( auth_done != 1 )
1999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002002 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002003
2004 if( ssl->in_msglen == 0 )
2005 {
2006 ssl->nb_zero++;
2007
2008 /*
2009 * Three or more empty messages may be a DoS attack
2010 * (excessive CPU consumption).
2011 */
2012 if( ssl->nb_zero > 3 )
2013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 }
2018 }
2019 else
2020 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002023 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002024 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002025 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002026 }
2027 else
2028#endif
2029 {
2030 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2031 if( ++ssl->in_ctr[i - 1] != 0 )
2032 break;
2033
2034 /* The loop goes to its end iff the counter is wrapping */
2035 if( i == ssl_ep_len( ssl ) )
2036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2038 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002039 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002040 }
2041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
2044 return( 0 );
2045}
2046
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002047#undef MAC_NONE
2048#undef MAC_PLAINTEXT
2049#undef MAC_CIPHERTEXT
2050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002052/*
2053 * Compression/decompression functions
2054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002056{
2057 int ret;
2058 unsigned char *msg_post = ssl->out_msg;
2059 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002060 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002063
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002064 if( len_pre == 0 )
2065 return( 0 );
2066
Paul Bakker2770fbd2012-07-03 13:30:23 +00002067 memcpy( msg_pre, ssl->out_msg, len_pre );
2068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002070 ssl->out_msglen ) );
2071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002073 ssl->out_msg, ssl->out_msglen );
2074
Paul Bakker48916f92012-09-16 19:57:18 +00002075 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2076 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2077 ssl->transform_out->ctx_deflate.next_out = msg_post;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002079
Paul Bakker48916f92012-09-16 19:57:18 +00002080 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002081 if( ret != Z_OK )
2082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2084 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002085 }
2086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002088 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002091 ssl->out_msglen ) );
2092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002094 ssl->out_msg, ssl->out_msglen );
2095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002097
2098 return( 0 );
2099}
2100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002102{
2103 int ret;
2104 unsigned char *msg_post = ssl->in_msg;
2105 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002106 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002109
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002110 if( len_pre == 0 )
2111 return( 0 );
2112
Paul Bakker2770fbd2012-07-03 13:30:23 +00002113 memcpy( msg_pre, ssl->in_msg, len_pre );
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002116 ssl->in_msglen ) );
2117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002119 ssl->in_msg, ssl->in_msglen );
2120
Paul Bakker48916f92012-09-16 19:57:18 +00002121 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2122 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2123 ssl->transform_in->ctx_inflate.next_out = msg_post;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002125
Paul Bakker48916f92012-09-16 19:57:18 +00002126 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002127 if( ret != Z_OK )
2128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2130 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002131 }
2132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 ssl->in_msglen = MBEDTLS_SSL_MAX_CONTENT_LEN -
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002134 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002137 ssl->in_msglen ) );
2138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002140 ssl->in_msg, ssl->in_msglen );
2141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002143
2144 return( 0 );
2145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2149static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#if defined(MBEDTLS_SSL_PROTO_DTLS)
2152static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002153{
2154 /* If renegotiation is not enforced, retransmit until we would reach max
2155 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002156 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002157 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002158 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002159 unsigned char doublings = 1;
2160
2161 while( ratio != 0 )
2162 {
2163 ++doublings;
2164 ratio >>= 1;
2165 }
2166
2167 if( ++ssl->renego_records_seen > doublings )
2168 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002170 return( 0 );
2171 }
2172 }
2173
2174 return( ssl_write_hello_request( ssl ) );
2175}
2176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002178
Paul Bakker5121ce52009-01-03 21:22:43 +00002179/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002180 * Fill the input message buffer by appending data to it.
2181 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002182 *
2183 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2184 * available (from this read and/or a previous one). Otherwise, an error code
2185 * is returned (possibly EOF or WANT_READ).
2186 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002187 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2188 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2189 * since we always read a whole datagram at once.
2190 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002191 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002192 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002195{
Paul Bakker23986e52011-04-24 08:57:21 +00002196 int ret;
2197 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002200
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002201 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002204 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002206 }
2207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002212 }
2213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002215 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002217 uint32_t timeout;
2218
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002219 /* Just to be sure */
2220 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2221 {
2222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2223 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2225 }
2226
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002227 /*
2228 * The point is, we need to always read a full datagram at once, so we
2229 * sometimes read more then requested, and handle the additional data.
2230 * It could be the rest of the current record (while fetching the
2231 * header) and/or some other records in the same datagram.
2232 */
2233
2234 /*
2235 * Move to the next record in the already read datagram if applicable
2236 */
2237 if( ssl->next_record_offset != 0 )
2238 {
2239 if( ssl->in_left < ssl->next_record_offset )
2240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002243 }
2244
2245 ssl->in_left -= ssl->next_record_offset;
2246
2247 if( ssl->in_left != 0 )
2248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002250 ssl->next_record_offset ) );
2251 memmove( ssl->in_hdr,
2252 ssl->in_hdr + ssl->next_record_offset,
2253 ssl->in_left );
2254 }
2255
2256 ssl->next_record_offset = 0;
2257 }
2258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002261
2262 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002263 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002264 */
2265 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002268 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002269 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002270
2271 /*
2272 * A record can't be split accross datagrams. If we need to read but
2273 * are not at the beginning of a new record, the caller did something
2274 * wrong.
2275 */
2276 if( ssl->in_left != 0 )
2277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002280 }
2281
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002282 /*
2283 * Don't even try to read if time's out already.
2284 * This avoids by-passing the timer when repeatedly receiving messages
2285 * that will end up being dropped.
2286 */
2287 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002288 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002289 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002294 timeout = ssl->handshake->retransmit_timeout;
2295 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002296 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002299
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002300 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002301 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2302 timeout );
2303 else
2304 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002307
2308 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002310 }
2311
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002312 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002315 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002318 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002319 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002322 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002323 }
2324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002328 return( ret );
2329 }
2330
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002331 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002334 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002336 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002337 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002340 return( ret );
2341 }
2342
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002343 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002344 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002346 }
2347
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 if( ret < 0 )
2349 return( ret );
2350
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002351 ssl->in_left = ret;
2352 }
2353 else
2354#endif
2355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002357 ssl->in_left, nb_want ) );
2358
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002359 while( ssl->in_left < nb_want )
2360 {
2361 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002362
2363 if( ssl_check_timer( ssl ) != 0 )
2364 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2365 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002366 {
2367 if( ssl->f_recv_timeout != NULL )
2368 {
2369 ret = ssl->f_recv_timeout( ssl->p_bio,
2370 ssl->in_hdr + ssl->in_left, len,
2371 ssl->conf->read_timeout );
2372 }
2373 else
2374 {
2375 ret = ssl->f_recv( ssl->p_bio,
2376 ssl->in_hdr + ssl->in_left, len );
2377 }
2378 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002381 ssl->in_left, nb_want ) );
2382 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002383
2384 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002386
2387 if( ret < 0 )
2388 return( ret );
2389
2390 ssl->in_left += ret;
2391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
2393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
2396 return( 0 );
2397}
2398
2399/*
2400 * Flush any data not yet written
2401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002404 int ret;
2405 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002409 if( ssl->f_send == NULL )
2410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002412 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002414 }
2415
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002416 /* Avoid incrementing counter if data is flushed */
2417 if( ssl->out_left == 0 )
2418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002420 return( 0 );
2421 }
2422
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 while( ssl->out_left > 0 )
2424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2426 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002429 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002430 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
2434 if( ret <= 0 )
2435 return( ret );
2436
2437 ssl->out_left -= ret;
2438 }
2439
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002440 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002441 if( ++ssl->out_ctr[i - 1] != 0 )
2442 break;
2443
2444 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002445 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2448 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002449 }
2450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
2453 return( 0 );
2454}
2455
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002456/*
2457 * Functions to handle the DTLS retransmission state machine
2458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002460/*
2461 * Append current handshake message to current outgoing flight
2462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002466
2467 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002468 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002469 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002472 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002473 }
2474
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002475 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002476 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002479 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002480 }
2481
2482 /* Copy current handshake message with headers */
2483 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2484 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002485 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002486 msg->next = NULL;
2487
2488 /* Append to the current flight */
2489 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002490 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002491 else
2492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002494 while( cur->next != NULL )
2495 cur = cur->next;
2496 cur->next = msg;
2497 }
2498
2499 return( 0 );
2500}
2501
2502/*
2503 * Free the current flight of handshake messages
2504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507 mbedtls_ssl_flight_item *cur = flight;
2508 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002509
2510 while( cur != NULL )
2511 {
2512 next = cur->next;
2513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 mbedtls_free( cur->p );
2515 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002516
2517 cur = next;
2518 }
2519}
2520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2522static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002523#endif
2524
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002525/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002526 * Swap transform_out and out_ctr with the alternative ones
2527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002529{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002531 unsigned char tmp_out_ctr[8];
2532
2533 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002536 return;
2537 }
2538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002540
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002541 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002542 tmp_transform = ssl->transform_out;
2543 ssl->transform_out = ssl->handshake->alt_transform_out;
2544 ssl->handshake->alt_transform_out = tmp_transform;
2545
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002546 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002547 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2548 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2549 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002550
2551 /* Adjust to the newly activated transform */
2552 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002553 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002554 {
2555 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2556 ssl->transform_out->fixed_ivlen;
2557 }
2558 else
2559 ssl->out_msg = ssl->out_iv;
2560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2562 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2567 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002568 }
2569 }
2570#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002571}
2572
2573/*
2574 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002575 *
2576 * Need to remember the current message in case flush_output returns
2577 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002578 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002587
2588 ssl->handshake->cur_msg = ssl->handshake->flight;
2589 ssl_swap_epochs( ssl );
2590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002592 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002593
2594 while( ssl->handshake->cur_msg != NULL )
2595 {
2596 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002598
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002599 /* Swap epochs before sending Finished: we can't do it after
2600 * sending ChangeCipherSpec, in case write returns WANT_READ.
2601 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002602 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2603 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002604 {
2605 ssl_swap_epochs( ssl );
2606 }
2607
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002608 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002609 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002610 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002611
2612 ssl->handshake->cur_msg = cur->next;
2613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002619 return( ret );
2620 }
2621 }
2622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2624 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002625 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002628 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2629 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002632
2633 return( 0 );
2634}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002635
2636/*
2637 * To be called when the last message of an incoming flight is received.
2638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002640{
2641 /* We won't need to resend that one any more */
2642 ssl_flight_free( ssl->handshake->flight );
2643 ssl->handshake->flight = NULL;
2644 ssl->handshake->cur_msg = NULL;
2645
2646 /* The next incoming flight will start with this msg_seq */
2647 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2648
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002649 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002650 ssl_set_timer( ssl, 0 );
2651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2653 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002656 }
2657 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002659}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002660
2661/*
2662 * To be called when the last message of an outgoing flight is send.
2663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002665{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002666 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002667 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2670 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002673 }
2674 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002678
Paul Bakker5121ce52009-01-03 21:22:43 +00002679/*
2680 * Record layer functions
2681 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002682
2683/*
2684 * Write current record.
2685 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688{
Paul Bakker05ef8352012-05-08 09:17:57 +00002689 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002690 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002696 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002698 {
2699 ; /* Skip special handshake treatment when resending */
2700 }
2701 else
2702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002705 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST &&
2706 ssl->handshake == NULL )
2707 {
2708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2710 }
2711
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2713 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2714 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2715
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002716 /*
2717 * DTLS has additional fields in the Handshake layer,
2718 * between the length field and the actual payload:
2719 * uint16 message_seq;
2720 * uint24 fragment_offset;
2721 * uint24 fragment_length;
2722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002724 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002725 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002726 /* Make room for the additional DTLS fields */
2727 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002728 ssl->out_msglen += 8;
2729 len += 8;
2730
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002731 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002733 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002734 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2735 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2736 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002737 }
2738 else
2739 {
2740 ssl->out_msg[4] = 0;
2741 ssl->out_msg[5] = 0;
2742 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002743
2744 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2745 memset( ssl->out_msg + 6, 0x00, 3 );
2746 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002747 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002751 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 }
2753
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002754 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002756 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002757 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
2759 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
2760 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002761 {
2762 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002765 return( ret );
2766 }
2767 }
2768#endif
2769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002771 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002773 {
2774 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002777 return( ret );
2778 }
2779
2780 len = ssl->out_msglen;
2781 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2785 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 ret = mbedtls_ssl_hw_record_write( ssl );
2790 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2793 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002794 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002795
2796 if( ret == 0 )
2797 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002798 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002800 if( !done )
2801 {
2802 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002804 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002805
2806 ssl->out_len[0] = (unsigned char)( len >> 8 );
2807 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002808
Paul Bakker48916f92012-09-16 19:57:18 +00002809 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002810 {
2811 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002814 return( ret );
2815 }
2816
2817 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002818 ssl->out_len[0] = (unsigned char)( len >> 8 );
2819 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002820 }
2821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00002825 "version = [%d:%d], msglen = %d",
2826 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002827 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
2830 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831 }
2832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 return( ret );
2837 }
2838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
2841 return( 0 );
2842}
2843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002845/*
2846 * Mark bits in bitmask (used for DTLS HS reassembly)
2847 */
2848static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2849{
2850 unsigned int start_bits, end_bits;
2851
2852 start_bits = 8 - ( offset % 8 );
2853 if( start_bits != 8 )
2854 {
2855 size_t first_byte_idx = offset / 8;
2856
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002857 /* Special case */
2858 if( len <= start_bits )
2859 {
2860 for( ; len != 0; len-- )
2861 mask[first_byte_idx] |= 1 << ( start_bits - len );
2862
2863 /* Avoid potential issues with offset or len becoming invalid */
2864 return;
2865 }
2866
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002867 offset += start_bits; /* Now offset % 8 == 0 */
2868 len -= start_bits;
2869
2870 for( ; start_bits != 0; start_bits-- )
2871 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2872 }
2873
2874 end_bits = len % 8;
2875 if( end_bits != 0 )
2876 {
2877 size_t last_byte_idx = ( offset + len ) / 8;
2878
2879 len -= end_bits; /* Now len % 8 == 0 */
2880
2881 for( ; end_bits != 0; end_bits-- )
2882 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2883 }
2884
2885 memset( mask + offset / 8, 0xFF, len / 8 );
2886}
2887
2888/*
2889 * Check that bitmask is full
2890 */
2891static int ssl_bitmask_check( unsigned char *mask, size_t len )
2892{
2893 size_t i;
2894
2895 for( i = 0; i < len / 8; i++ )
2896 if( mask[i] != 0xFF )
2897 return( -1 );
2898
2899 for( i = 0; i < len % 8; i++ )
2900 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2901 return( -1 );
2902
2903 return( 0 );
2904}
2905
2906/*
2907 * Reassemble fragmented DTLS handshake messages.
2908 *
2909 * Use a temporary buffer for reassembly, divided in two parts:
2910 * - the first holds the reassembled message (including handshake header),
2911 * - the second holds a bitmask indicating which parts of the message
2912 * (excluding headers) have been received so far.
2913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002915{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002916 unsigned char *msg, *bitmask;
2917 size_t frag_len, frag_off;
2918 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2919
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002920 if( ssl->handshake == NULL )
2921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2923 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002924 }
2925
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002926 /*
2927 * For first fragment, check size and allocate buffer
2928 */
2929 if( ssl->handshake->hs_msg == NULL )
2930 {
2931 size_t alloc_len;
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002934 msg_len ) );
2935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2939 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002940 }
2941
2942 /* The bitmask needs one bit per byte of message excluding header */
2943 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2944
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002945 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002946 if( ssl->handshake->hs_msg == NULL )
2947 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002949 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002950 }
2951
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002952 /* Prepare final header: copy msg_type, length and message_seq,
2953 * then add standardised fragment_offset and fragment_length */
2954 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2955 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2956 memcpy( ssl->handshake->hs_msg + 9,
2957 ssl->handshake->hs_msg + 1, 3 );
2958 }
2959 else
2960 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002961 /* Make sure msg_type and length are consistent */
2962 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2965 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002966 }
2967 }
2968
2969 msg = ssl->handshake->hs_msg + 12;
2970 bitmask = msg + msg_len;
2971
2972 /*
2973 * Check and copy current fragment
2974 */
2975 frag_off = ( ssl->in_msg[6] << 16 ) |
2976 ( ssl->in_msg[7] << 8 ) |
2977 ssl->in_msg[8];
2978 frag_len = ( ssl->in_msg[9] << 16 ) |
2979 ( ssl->in_msg[10] << 8 ) |
2980 ssl->in_msg[11];
2981
2982 if( frag_off + frag_len > msg_len )
2983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002985 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002987 }
2988
2989 if( frag_len + 12 > ssl->in_msglen )
2990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002992 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002994 }
2995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002997 frag_off, frag_len ) );
2998
2999 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3000 ssl_bitmask_set( bitmask, frag_off, frag_len );
3001
3002 /*
3003 * Do we have the complete message by now?
3004 * If yes, finalize it, else ask to read the next record.
3005 */
3006 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003009 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003010 }
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003013
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003014 if( frag_len + 12 < ssl->in_msglen )
3015 {
3016 /*
3017 * We'got more handshake messages in the same record.
3018 * This case is not handled now because no know implementation does
3019 * that and it's hard to test, so we prefer to fail cleanly for now.
3020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3022 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003023 }
3024
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003025 if( ssl->in_left > ssl->next_record_offset )
3026 {
3027 /*
3028 * We've got more data in the buffer after the current record,
3029 * that we don't want to overwrite. Move it before writing the
3030 * reassembled message, and adjust in_left and next_record_offset.
3031 */
3032 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3033 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3034 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3035
3036 /* First compute and check new lengths */
3037 ssl->next_record_offset = new_remain - ssl->in_hdr;
3038 ssl->in_left = ssl->next_record_offset + remain_len;
3039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003041 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3044 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003045 }
3046
3047 memmove( new_remain, cur_remain, remain_len );
3048 }
3049
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003050 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003053 ssl->handshake->hs_msg = NULL;
3054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003056 ssl->in_msg, ssl->in_hslen );
3057
3058 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062static int ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003067 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003069 }
3070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003072 ( ssl->in_msg[1] << 16 ) |
3073 ( ssl->in_msg[2] << 8 ) |
3074 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003077 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003078 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003081 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003082 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003083 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003084 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003085
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003086 /* ssl->handshake is NULL when receiving ClientHello for renego */
3087 if( ssl->handshake != NULL &&
3088 recv_msg_seq != ssl->handshake->in_msg_seq )
3089 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003090 /* Retransmit only on last message from previous flight, to avoid
3091 * too many retransmissions.
3092 * Besides, No sane server ever retransmits HelloVerifyRequest */
3093 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003097 "message_seq = %d, start_of_flight = %d",
3098 recv_msg_seq,
3099 ssl->handshake->in_flight_start_seq ) );
3100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003104 return( ret );
3105 }
3106 }
3107 else
3108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003110 "message_seq = %d, expected = %d",
3111 recv_msg_seq,
3112 ssl->handshake->in_msg_seq ) );
3113 }
3114
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003115 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003116 }
3117 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003118
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003119 /* Reassemble if current message is fragmented or reassembly is
3120 * already in progress */
3121 if( ssl->in_msglen < ssl->in_hslen ||
3122 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3123 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3124 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003127
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003128 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003131 return( ret );
3132 }
3133 }
3134 }
3135 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003137 /* With TLS we don't handle fragmentation (for now) */
3138 if( ssl->in_msglen < ssl->in_hslen )
3139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3141 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003142 }
3143
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003144 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3145 ssl->handshake != NULL )
3146 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003147 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003148 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003149
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003150 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003152 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003153 ssl->handshake != NULL )
3154 {
3155 ssl->handshake->in_msg_seq++;
3156 }
3157#endif
3158
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003159 return( 0 );
3160}
3161
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003162/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003163 * DTLS anti-replay: RFC 6347 4.1.2.6
3164 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003165 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3166 * Bit n is set iff record number in_window_top - n has been seen.
3167 *
3168 * Usually, in_window_top is the last record number seen and the lsb of
3169 * in_window is set. The only exception is the initial state (record number 0
3170 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3173static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003174{
3175 ssl->in_window_top = 0;
3176 ssl->in_window = 0;
3177}
3178
3179static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3180{
3181 return( ( (uint64_t) buf[0] << 40 ) |
3182 ( (uint64_t) buf[1] << 32 ) |
3183 ( (uint64_t) buf[2] << 24 ) |
3184 ( (uint64_t) buf[3] << 16 ) |
3185 ( (uint64_t) buf[4] << 8 ) |
3186 ( (uint64_t) buf[5] ) );
3187}
3188
3189/*
3190 * Return 0 if sequence number is acceptable, -1 otherwise
3191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003193{
3194 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3195 uint64_t bit;
3196
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003197 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003198 return( 0 );
3199
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003200 if( rec_seqnum > ssl->in_window_top )
3201 return( 0 );
3202
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003203 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003204
3205 if( bit >= 64 )
3206 return( -1 );
3207
3208 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3209 return( -1 );
3210
3211 return( 0 );
3212}
3213
3214/*
3215 * Update replay window on new validated record
3216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003218{
3219 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3220
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003221 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003222 return;
3223
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003224 if( rec_seqnum > ssl->in_window_top )
3225 {
3226 /* Update window_top and the contents of the window */
3227 uint64_t shift = rec_seqnum - ssl->in_window_top;
3228
3229 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003230 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003231 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003232 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003233 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003234 ssl->in_window |= 1;
3235 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003236
3237 ssl->in_window_top = rec_seqnum;
3238 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003239 else
3240 {
3241 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003242 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003243
3244 if( bit < 64 ) /* Always true, but be extra sure */
3245 ssl->in_window |= (uint64_t) 1 << bit;
3246 }
3247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003249
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003250#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003251/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003252static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3253
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003254/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003255 * Without any SSL context, check if a datagram looks like a ClientHello with
3256 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003257 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003258 *
3259 * - if cookie is valid, return 0
3260 * - if ClientHello looks superficially valid but cookie is not,
3261 * fill obuf and set olen, then
3262 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3263 * - otherwise return a specific error code
3264 */
3265static int ssl_check_dtls_clihlo_cookie(
3266 mbedtls_ssl_cookie_write_t *f_cookie_write,
3267 mbedtls_ssl_cookie_check_t *f_cookie_check,
3268 void *p_cookie,
3269 const unsigned char *cli_id, size_t cli_id_len,
3270 const unsigned char *in, size_t in_len,
3271 unsigned char *obuf, size_t buf_len, size_t *olen )
3272{
3273 size_t sid_len, cookie_len;
3274 unsigned char *p;
3275
3276 if( f_cookie_write == NULL || f_cookie_check == NULL )
3277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3278
3279 /*
3280 * Structure of ClientHello with record and handshake headers,
3281 * and expected values. We don't need to check a lot, more checks will be
3282 * done when actually parsing the ClientHello - skipping those checks
3283 * avoids code duplication and does not make cookie forging any easier.
3284 *
3285 * 0-0 ContentType type; copied, must be handshake
3286 * 1-2 ProtocolVersion version; copied
3287 * 3-4 uint16 epoch; copied, must be 0
3288 * 5-10 uint48 sequence_number; copied
3289 * 11-12 uint16 length; (ignored)
3290 *
3291 * 13-13 HandshakeType msg_type; (ignored)
3292 * 14-16 uint24 length; (ignored)
3293 * 17-18 uint16 message_seq; copied
3294 * 19-21 uint24 fragment_offset; copied, must be 0
3295 * 22-24 uint24 fragment_length; (ignored)
3296 *
3297 * 25-26 ProtocolVersion client_version; (ignored)
3298 * 27-58 Random random; (ignored)
3299 * 59-xx SessionID session_id; 1 byte len + sid_len content
3300 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3301 * ...
3302 *
3303 * Minimum length is 61 bytes.
3304 */
3305 if( in_len < 61 ||
3306 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3307 in[3] != 0 || in[4] != 0 ||
3308 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3309 {
3310 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3311 }
3312
3313 sid_len = in[59];
3314 if( sid_len > in_len - 61 )
3315 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3316
3317 cookie_len = in[60 + sid_len];
3318 if( cookie_len > in_len - 60 )
3319 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3320
3321 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3322 cli_id, cli_id_len ) == 0 )
3323 {
3324 /* Valid cookie */
3325 return( 0 );
3326 }
3327
3328 /*
3329 * If we get here, we've got an invalid cookie, let's prepare HVR.
3330 *
3331 * 0-0 ContentType type; copied
3332 * 1-2 ProtocolVersion version; copied
3333 * 3-4 uint16 epoch; copied
3334 * 5-10 uint48 sequence_number; copied
3335 * 11-12 uint16 length; olen - 13
3336 *
3337 * 13-13 HandshakeType msg_type; hello_verify_request
3338 * 14-16 uint24 length; olen - 25
3339 * 17-18 uint16 message_seq; copied
3340 * 19-21 uint24 fragment_offset; copied
3341 * 22-24 uint24 fragment_length; olen - 25
3342 *
3343 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3344 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3345 *
3346 * Minimum length is 28.
3347 */
3348 if( buf_len < 28 )
3349 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3350
3351 /* Copy most fields and adapt others */
3352 memcpy( obuf, in, 25 );
3353 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3354 obuf[25] = 0xfe;
3355 obuf[26] = 0xff;
3356
3357 /* Generate and write actual cookie */
3358 p = obuf + 28;
3359 if( f_cookie_write( p_cookie,
3360 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3361 {
3362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3363 }
3364
3365 *olen = p - obuf;
3366
3367 /* Go back and fill length fields */
3368 obuf[27] = (unsigned char)( *olen - 28 );
3369
3370 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3371 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3372 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3373
3374 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3375 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3376
3377 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3378}
3379
3380/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003381 * Handle possible client reconnect with the same UDP quadruplet
3382 * (RFC 6347 Section 4.2.8).
3383 *
3384 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3385 * that looks like a ClientHello.
3386 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003387 * - if the input looks like a ClientHello without cookies,
3388 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003389 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003390 * - if the input looks like a ClientHello with a valid cookie,
3391 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003392 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003393 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003394 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003395 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003396 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3397 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003398 */
3399static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3400{
3401 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003402 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003403
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003404 ret = ssl_check_dtls_clihlo_cookie(
3405 ssl->conf->f_cookie_write,
3406 ssl->conf->f_cookie_check,
3407 ssl->conf->p_cookie,
3408 ssl->cli_id, ssl->cli_id_len,
3409 ssl->in_buf, ssl->in_left,
3410 ssl->out_buf, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003411
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003412 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3413
3414 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003415 {
Brian J Murraye7f8dc32016-11-06 04:45:15 -08003416 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003417 * If the error is permanent we'll catch it later,
3418 * if it's not, then hopefully it'll work next time. */
3419 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3420
3421 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003422 }
3423
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003424 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003425 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003426 /* Got a valid cookie, partially reset context */
3427 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3428 {
3429 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3430 return( ret );
3431 }
3432
3433 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003434 }
3435
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003436 return( ret );
3437}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003438#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003439
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003440/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003441 * ContentType type;
3442 * ProtocolVersion version;
3443 * uint16 epoch; // DTLS only
3444 * uint48 sequence_number; // DTLS only
3445 * uint16 length;
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003446 *
3447 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butchere103aa82015-12-16 01:51:01 +00003448 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003449 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3450 *
3451 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butchere103aa82015-12-16 01:51:01 +00003452 * 1. proceed with the record if this function returns 0
3453 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3454 * 3. return CLIENT_RECONNECT if this function returns that value
3455 * 4. drop the whole datagram if this function returns anything else.
3456 * Point 2 is needed when the peer is resending, and we have already received
3457 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003460{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003461 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003462 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 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 +02003465
Paul Bakker5121ce52009-01-03 21:22:43 +00003466 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003467 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003468 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003472 ssl->in_msgtype,
3473 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003474
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003475 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3477 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3478 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3479 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
3484 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3485 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003486 {
3487 return( ret );
3488 }
3489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003491 }
3492
3493 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003494 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3497 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 }
3499
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003500 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3503 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 }
3505
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003506 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003508 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3511 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003512 }
3513
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003514 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003515 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003516 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003517 if( ssl->in_msglen < 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3521 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003522 }
3523 }
3524 else
3525 {
Paul Bakker48916f92012-09-16 19:57:18 +00003526 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3529 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 }
3531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532#if defined(MBEDTLS_SSL_PROTO_SSL3)
3533 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3534 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3537 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003538 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3541 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 /*
3543 * TLS encrypted messages can have up to 256 bytes of padding
3544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003546 ssl->in_msglen > ssl->transform_in->minlen +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003547 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3550 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003552#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 }
3554
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003555 /*
3556 * DTLS-related tests done last, because most of them may result in
3557 * silently dropping the record (but not the whole datagram), and we only
3558 * want to consider that after ensuring that the "basic" fields (type,
3559 * version, length) are sane.
3560 */
3561#if defined(MBEDTLS_SSL_PROTO_DTLS)
3562 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3563 {
3564 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3565
3566 /* Drop unexpected ChangeCipherSpec messages */
3567 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3568 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3569 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3570 {
3571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3572 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3573 }
3574
3575 /* Drop unexpected ApplicationData records,
3576 * except at the beginning of renegotiations */
3577 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3578 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3579#if defined(MBEDTLS_SSL_RENEGOTIATION)
3580 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3581 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3582#endif
3583 )
3584 {
3585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3586 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3587 }
3588
3589 /* Check epoch (and sequence number) with DTLS */
3590 if( rec_epoch != ssl->in_epoch )
3591 {
3592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3593 "expected %d, received %d",
3594 ssl->in_epoch, rec_epoch ) );
3595
3596#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3597 /*
3598 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3599 * access the first byte of record content (handshake type), as we
3600 * have an active transform (possibly iv_len != 0), so use the
3601 * fact that the record header len is 13 instead.
3602 */
3603 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3604 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3605 rec_epoch == 0 &&
3606 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3607 ssl->in_left > 13 &&
3608 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3609 {
3610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3611 "from the same port" ) );
3612 return( ssl_handle_possible_reconnect( ssl ) );
3613 }
3614 else
3615#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3616 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3617 }
3618
3619#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3620 /* Replay detection only works for the current epoch */
3621 if( rec_epoch == ssl->in_epoch &&
3622 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3623 {
3624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3625 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3626 }
3627#endif
3628 }
3629#endif /* MBEDTLS_SSL_PROTO_DTLS */
3630
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003631 return( 0 );
3632}
Paul Bakker5121ce52009-01-03 21:22:43 +00003633
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003634/*
3635 * If applicable, decrypt (and decompress) record content
3636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003638{
3639 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3642 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3645 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 ret = mbedtls_ssl_hw_record_read( ssl );
3650 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3653 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003654 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003655
3656 if( ret == 0 )
3657 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003658 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003660 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 {
3662 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 return( ret );
3666 }
3667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003669 ssl->in_msg, ssl->in_msglen );
3670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3674 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003675 }
3676 }
3677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003679 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003681 {
3682 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003685 return( ret );
3686 }
3687
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003688 // TODO: what's the purpose of these lines? is in_len used?
3689 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3690 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003695 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003698 }
3699#endif
3700
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003701 return( 0 );
3702}
3703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003705
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003706/*
3707 * Read a record.
3708 *
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02003709 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3710 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3711 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003714{
3715 int ret;
3716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003718
Hanno Becker704f4932017-06-08 13:08:45 +01003719 if( ssl->keep_current_message == 1 )
3720 {
3721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
3722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3723 ssl->keep_current_message = 0;
3724
3725 return( 0 );
3726 }
3727
Hanno Becker6a582e82017-06-08 13:38:05 +01003728 /*
3729 * Step A
3730 *
3731 * Consume last content-layer message and potentially
3732 * update in_msglen which keeps track of the contents'
3733 * consumption state.
3734 *
3735 * (1) Handshake messages:
3736 * Remove last handshake message, move content
3737 * and adapt in_msglen.
3738 *
3739 * (2) Alert messages:
3740 * Consume whole record content, in_msglen = 0.
3741 *
3742 * NOTE: This needs to be fixed, since like for
3743 * handshake messages it is allowed to have
3744 * multiple alerts witin a single record.
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003745 * Internal reference IOTSSL-1321.
Hanno Becker6a582e82017-06-08 13:38:05 +01003746 *
3747 * (3) Change cipher spec:
3748 * Consume whole record content, in_msglen = 0.
3749 *
3750 * (4) Application data:
3751 * Don't do anything - the record layer provides
3752 * the application data as a stream transport
3753 * and consumes through mbedtls_ssl_read only.
3754 *
3755 */
3756
3757 /* Case (1): Handshake messages */
3758
3759 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003760 {
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003761 if( ssl->in_offt != NULL )
3762 {
3763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3764 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3765 }
3766
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003767 /*
3768 * Get next Handshake message in the current record
3769 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003770
Hanno Becker6a582e82017-06-08 13:38:05 +01003771 /* Notes:
3772 * (1) in_hslen is *NOT* necessarily the size of the
3773 * current handshake content: If DTLS handshake
3774 * fragmentation is used, that's the fragment
3775 * size instead. Using the total handshake message
3776 * size here is FAULTY and should be changed at
3777 * some point. Internal reference IOTSSL-1414.
3778 * (2) While it doesn't seem to cause problems, one
3779 * has to be very careful not to assume that in_hslen
3780 * is always <= in_msglen in a sensible communication.
3781 * Again, it's wrong for DTLS handshake fragmentation.
3782 * The following check is therefore mandatory, and
3783 * should not be treated as a silently corrected assertion.
3784 */
3785 if( ssl->in_hslen < ssl->in_msglen )
3786 {
3787 ssl->in_msglen -= ssl->in_hslen;
3788 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3789 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003790
Hanno Becker6a582e82017-06-08 13:38:05 +01003791 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
3792 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003793
Hanno Becker6a582e82017-06-08 13:38:05 +01003794 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3795 return( ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003796
Hanno Becker6a582e82017-06-08 13:38:05 +01003797 return( 0 );
3798 }
3799 else
3800 {
3801 ssl->in_msglen = 0;
3802 }
3803
3804 ssl->in_hslen = 0;
3805 }
3806 /* Case (4): Application data */
3807 else if( ssl->in_offt != NULL )
3808 {
3809 return( 0 );
3810 }
3811 /* Everything else (CCS & Alerts) */
3812 else
3813 {
3814 ssl->in_msglen = 0;
3815 }
3816
3817 /*
3818 * Step B
3819 *
3820 * Fetch and decode new record if current one is fully consumed.
3821 *
3822 */
3823
3824 if( ssl->in_msglen > 0 )
3825 {
3826 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003827 return( 0 );
3828 }
3829
Hanno Becker6a582e82017-06-08 13:38:05 +01003830 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003831
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003832read_record_header:
Hanno Becker6a582e82017-06-08 13:38:05 +01003833
3834 /* Current record either fully processed or to be discarded. */
3835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003839 return( ret );
3840 }
3841
3842 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3846 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003847 {
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003848 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
3849 {
3850 /* Skip unexpected record (but not whole datagram) */
3851 ssl->next_record_offset = ssl->in_msglen
3852 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003853
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
3855 "(header)" ) );
3856 }
3857 else
3858 {
3859 /* Skip invalid record and the rest of the datagram */
3860 ssl->next_record_offset = 0;
3861 ssl->in_left = 0;
3862
3863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
3864 "(header)" ) );
3865 }
3866
3867 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003868 goto read_record_header;
3869 }
3870#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003871 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003872 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003873
3874 /*
3875 * Read and optionally decrypt the message contents
3876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 if( ( ret = mbedtls_ssl_fetch_input( ssl,
3878 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003881 return( ret );
3882 }
3883
3884 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003886 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003888 else
3889#endif
3890 ssl->in_left = 0;
3891
3892 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003895 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003896 {
3897 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
3899 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003900 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02003901 /* Except when waiting for Finished as a bad mac here
3902 * probably means something went wrong in the handshake
3903 * (eg wrong psk used, mitm downgrade attempt, etc.) */
3904 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
3905 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
3906 {
3907#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
3908 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
3909 {
3910 mbedtls_ssl_send_alert_message( ssl,
3911 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3912 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
3913 }
3914#endif
3915 return( ret );
3916 }
3917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003919 if( ssl->conf->badmac_limit != 0 &&
3920 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3923 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003924 }
3925#endif
3926
Hanno Becker6a582e82017-06-08 13:38:05 +01003927 /* As above, invalid records cause
3928 * dismissal of the whole datagram. */
3929
3930 ssl->next_record_offset = 0;
3931 ssl->in_left = 0;
3932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003934 goto read_record_header;
3935 }
3936
3937 return( ret );
3938 }
3939 else
3940#endif
3941 {
3942 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
3944 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 mbedtls_ssl_send_alert_message( ssl,
3947 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3948 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003949 }
3950#endif
3951 return( ret );
3952 }
3953 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003954
3955 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003956 * When we sent the last flight of the handshake, we MUST respond to a
3957 * retransmit of the peer's previous flight with a retransmit. (In
3958 * practice, only the Finished message will make it, other messages
3959 * including CCS use the old transform so they're dropped as invalid.)
3960 *
3961 * If the record we received is not a handshake message, however, it
3962 * means the peer received our last flight so we can clean up
3963 * handshake info.
3964 *
3965 * This check needs to be done before prepare_handshake() due to an edge
3966 * case: if the client immediately requests renegotiation, this
3967 * finishes the current handshake first, avoiding the new ClientHello
3968 * being mistaken for an ancient message in the current handshake.
3969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003972 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3976 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003983 return( ret );
3984 }
3985
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003986 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003987 }
3988 else
3989 {
3990 ssl_handshake_wrapup_free_hs_transform( ssl );
3991 }
3992 }
3993#endif
3994
3995 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003996 * Handle particular types of records
3997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003999 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004000 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
4001 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004002 }
4003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 ssl->in_msg[0], ssl->in_msg[1] ) );
4008
4009 /*
Simon Butcher94c5e3c2015-10-27 16:09:03 +00004010 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004015 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 }
4018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4020 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4023 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004024 }
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02004025
4026#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4027 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4028 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4029 {
4030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4031 /* Will be handled when trying to parse ServerHello */
4032 return( 0 );
4033 }
4034#endif
4035
4036#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4037 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4038 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4039 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4040 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4041 {
4042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4043 /* Will be handled in mbedtls_ssl_parse_certificate() */
4044 return( 0 );
4045 }
4046#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4047
4048 /* Silently ignore: fetch new message */
4049 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 }
4051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004053
4054 return( 0 );
4055}
4056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004058{
4059 int ret;
4060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4062 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4063 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004064 {
4065 return( ret );
4066 }
4067
4068 return( 0 );
4069}
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004072 unsigned char level,
4073 unsigned char message )
4074{
4075 int ret;
4076
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004077 if( ssl == NULL || ssl->conf == NULL )
4078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004083 ssl->out_msglen = 2;
4084 ssl->out_msg[0] = level;
4085 ssl->out_msg[1] = message;
4086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004090 return( ret );
4091 }
4092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004094
4095 return( 0 );
4096}
4097
Paul Bakker5121ce52009-01-03 21:22:43 +00004098/*
4099 * Handshake functions
4100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4102 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4103 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4104 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4105 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4106 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4107 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4108int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4115 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4116 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004119 ssl->state++;
4120 return( 0 );
4121 }
4122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004125}
4126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4134 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4135 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004138 ssl->state++;
4139 return( 0 );
4140 }
4141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004144}
4145#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004149 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 const mbedtls_x509_crt *crt;
4151 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4156 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4157 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004160 ssl->state++;
4161 return( 0 );
4162 }
4163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004165 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004166 {
4167 if( ssl->client_auth == 0 )
4168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004170 ssl->state++;
4171 return( 0 );
4172 }
4173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004175 /*
4176 * If using SSLv3 and got no cert, send an Alert message
4177 * (otherwise an empty Certificate message will be sent).
4178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4180 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004181 {
4182 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4184 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4185 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004188 goto write_msg;
4189 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192#endif /* MBEDTLS_SSL_CLI_C */
4193#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004194 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004196 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4199 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004200 }
4201 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004202#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004205
4206 /*
4207 * 0 . 0 handshake type
4208 * 1 . 3 handshake length
4209 * 4 . 6 length of all certs
4210 * 7 . 9 length of cert. 1
4211 * 10 . n-1 peer certificate
4212 * n . n+2 length of cert. 2
4213 * n+3 . ... upper level cert, etc.
4214 */
4215 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004217
Paul Bakker29087132010-03-21 21:03:34 +00004218 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004219 {
4220 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4224 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4225 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004226 }
4227
4228 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4229 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4230 ssl->out_msg[i + 2] = (unsigned char)( n );
4231
4232 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4233 i += n; crt = crt->next;
4234 }
4235
4236 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4237 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4238 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4239
4240 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4242 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004243
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004244#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004245write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004246#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004247
4248 ssl->state++;
4249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004250 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004253 return( ret );
4254 }
4255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004257
Paul Bakkered27a042013-04-18 22:46:23 +02004258 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004259}
4260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004263 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004264 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004266 int authmode = ssl->conf->authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4271 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4272 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004275 ssl->state++;
4276 return( 0 );
4277 }
4278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004280 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004281 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4282 {
4283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4284 ssl->state++;
4285 return( 0 );
4286 }
4287
4288#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4289 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4290 authmode = ssl->handshake->sni_authmode;
4291#endif
4292
4293 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4294 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004295 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004296 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004298 ssl->state++;
4299 return( 0 );
4300 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004301#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004306 return( ret );
4307 }
4308
4309 ssl->state++;
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311#if defined(MBEDTLS_SSL_SRV_C)
4312#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004313 /*
4314 * Check if the client sent an empty certificate
4315 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004316 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004317 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004318 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004319 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4321 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4322 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004326 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004327 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004328 return( 0 );
4329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 }
4332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4336 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004337 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4341 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4342 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4343 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004346
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004347 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004348 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004349 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004350 else
4351 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 }
4353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4355 MBEDTLS_SSL_PROTO_TLS1_2 */
4356#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4361 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 }
4363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4365 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4368 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004369 }
4370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004372
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004376 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004377
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004378 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4382 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004383 }
4384
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004385 /* In case we tried to reuse a session but it failed */
4386 if( ssl->session_negotiate->peer_cert != NULL )
4387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004388 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4389 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004390 }
4391
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004392 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 sizeof( mbedtls_x509_crt ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004397 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004398 }
4399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004401
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004402 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004403
4404 while( i < ssl->in_hslen )
4405 {
4406 if( ssl->in_msg[i] != 0 )
4407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4409 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004410 }
4411
4412 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4413 | (unsigned int) ssl->in_msg[i + 2];
4414 i += 3;
4415
4416 if( n < 128 || i + n > ssl->in_hslen )
4417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4419 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004420 }
4421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004423 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 if( ret != 0 )
4425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004427 return( ret );
4428 }
4429
4430 i += n;
4431 }
4432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004433 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004435 /*
4436 * On client, make sure the server cert doesn't change during renego to
4437 * avoid "triple handshake" attack: https://secure-resumption.com/
4438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004440 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004442 {
4443 if( ssl->session->peer_cert == NULL )
4444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
4446 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004447 }
4448
4449 if( ssl->session->peer_cert->raw.len !=
4450 ssl->session_negotiate->peer_cert->raw.len ||
4451 memcmp( ssl->session->peer_cert->raw.p,
4452 ssl->session_negotiate->peer_cert->raw.p,
4453 ssl->session->peer_cert->raw.len ) != 0 )
4454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
4456 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004457 }
4458 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004460
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004461 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004462 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004463 mbedtls_x509_crt *ca_chain;
4464 mbedtls_x509_crl *ca_crl;
4465
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004466#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004467 if( ssl->handshake->sni_ca_chain != NULL )
4468 {
4469 ca_chain = ssl->handshake->sni_ca_chain;
4470 ca_crl = ssl->handshake->sni_ca_crl;
4471 }
4472 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004473#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004474 {
4475 ca_chain = ssl->conf->ca_chain;
4476 ca_crl = ssl->conf->ca_crl;
4477 }
4478
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004479 /*
4480 * Main check: verify certificate
4481 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004482 ret = mbedtls_x509_crt_verify_with_profile(
4483 ssl->session_negotiate->peer_cert,
4484 ca_chain, ca_crl,
4485 ssl->conf->cert_profile,
4486 ssl->hostname,
4487 &ssl->session_negotiate->verify_result,
4488 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004489
4490 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004493 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004494
4495 /*
4496 * Secondary checks: always done, but change 'ret' only if it was 0
4497 */
4498
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004499#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004502
4503 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004505 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004506 {
Hanno Beckera3929ba2017-05-08 16:31:14 +01004507 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004510 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004512 }
4513 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004514#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Beckera3929ba2017-05-08 16:31:14 +01004517 ciphersuite_info,
4518 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004519 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004522 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004524 }
4525
Hanno Beckera3929ba2017-05-08 16:31:14 +01004526 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4527 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4528 * with details encoded in the verification flags. All other kinds
4529 * of error codes, including those from the user provided f_vrfy
4530 * functions, are treated as fatal and lead to a failure of
4531 * ssl_parse_certificate even if verification was optional. */
4532 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4533 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4534 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4535 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004536 ret = 0;
Hanno Beckera3929ba2017-05-08 16:31:14 +01004537 }
4538
4539 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4540 {
4541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4542 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4543 }
4544
Hanno Becker61c0c702017-05-15 16:05:15 +01004545#if defined(MBEDTLS_DEBUG_C)
4546 if( ssl->session_negotiate->verify_result != 0 )
4547 {
4548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4549 ssl->session_negotiate->verify_result ) );
4550 }
4551 else
4552 {
4553 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4554 }
4555#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 }
4557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004559
4560 return( ret );
4561}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004562#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
4563 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
4564 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
4565 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4566 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4567 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4568 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004571{
4572 int ret;
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004577 ssl->out_msglen = 1;
4578 ssl->out_msg[0] = 1;
4579
Paul Bakker5121ce52009-01-03 21:22:43 +00004580 ssl->state++;
4581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 return( ret );
4586 }
4587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004589
4590 return( 0 );
4591}
4592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594{
4595 int ret;
4596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004602 return( ret );
4603 }
4604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4608 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004609 }
4610
4611 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4614 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 }
4616
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004617 /*
4618 * Switch to our negotiated transform and session parameters for inbound
4619 * data.
4620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004622 ssl->transform_in = ssl->transform_negotiate;
4623 ssl->session_in = ssl->session_negotiate;
4624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004625#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004626 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004629 ssl_dtls_replay_reset( ssl );
4630#endif
4631
4632 /* Increment epoch */
4633 if( ++ssl->in_epoch == 0 )
4634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4636 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004637 }
4638 }
4639 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004641 memset( ssl->in_ctr, 0, 8 );
4642
4643 /*
4644 * Set the in_msg pointer to the correct location based on IV length
4645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004647 {
4648 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4649 ssl->transform_negotiate->fixed_ivlen;
4650 }
4651 else
4652 ssl->in_msg = ssl->in_iv;
4653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4655 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4660 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004661 }
4662 }
4663#endif
4664
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 ssl->state++;
4666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668
4669 return( 0 );
4670}
4671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
4673 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004674{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004675 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4678 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4679 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004680 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004681 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004682#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4684#if defined(MBEDTLS_SHA512_C)
4685 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004686 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4687 else
4688#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689#if defined(MBEDTLS_SHA256_C)
4690 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004691 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004692 else
4693#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004697 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004698 }
Paul Bakker380da532012-04-18 16:10:25 +00004699}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004702{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4704 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4705 mbedtls_md5_starts( &ssl->handshake->fin_md5 );
4706 mbedtls_sha1_starts( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004707#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4709#if defined(MBEDTLS_SHA256_C)
4710 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712#if defined(MBEDTLS_SHA512_C)
4713 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004716}
4717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004719 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004720{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4722 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4723 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4724 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4727#if defined(MBEDTLS_SHA256_C)
4728 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730#if defined(MBEDTLS_SHA512_C)
4731 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004732#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004734}
4735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4737 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4738static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004739 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004740{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4742 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004743}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004744#endif
Paul Bakker380da532012-04-18 16:10:25 +00004745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4747#if defined(MBEDTLS_SHA256_C)
4748static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004749 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004752}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004753#endif
Paul Bakker380da532012-04-18 16:10:25 +00004754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755#if defined(MBEDTLS_SHA512_C)
4756static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004757 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004760}
Paul Bakker769075d2012-11-24 11:26:46 +01004761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004765static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004766 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004767{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004768 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004769 mbedtls_md5_context md5;
4770 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004771
Paul Bakker5121ce52009-01-03 21:22:43 +00004772 unsigned char padbuf[48];
4773 unsigned char md5sum[16];
4774 unsigned char sha1sum[20];
4775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004777 if( !session )
4778 session = ssl->session;
4779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004781
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004782 mbedtls_md5_init( &md5 );
4783 mbedtls_sha1_init( &sha1 );
4784
4785 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
4786 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004787
4788 /*
4789 * SSLv3:
4790 * hash =
4791 * MD5( master + pad2 +
4792 * MD5( handshake + sender + master + pad1 ) )
4793 * + SHA1( master + pad2 +
4794 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 */
4796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004798 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4799 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004800#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004803 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4804 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004805#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02004808 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004809
Paul Bakker1ef83d62012-04-11 12:09:53 +00004810 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004811
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004812 mbedtls_md5_update( &md5, (const unsigned char *) sender, 4 );
4813 mbedtls_md5_update( &md5, session->master, 48 );
4814 mbedtls_md5_update( &md5, padbuf, 48 );
4815 mbedtls_md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004816
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004817 mbedtls_sha1_update( &sha1, (const unsigned char *) sender, 4 );
4818 mbedtls_sha1_update( &sha1, session->master, 48 );
4819 mbedtls_sha1_update( &sha1, padbuf, 40 );
4820 mbedtls_sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004821
Paul Bakker1ef83d62012-04-11 12:09:53 +00004822 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004823
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004824 mbedtls_md5_starts( &md5 );
4825 mbedtls_md5_update( &md5, session->master, 48 );
4826 mbedtls_md5_update( &md5, padbuf, 48 );
4827 mbedtls_md5_update( &md5, md5sum, 16 );
4828 mbedtls_md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004829
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004830 mbedtls_sha1_starts( &sha1 );
4831 mbedtls_sha1_update( &sha1, session->master, 48 );
4832 mbedtls_sha1_update( &sha1, padbuf , 40 );
4833 mbedtls_sha1_update( &sha1, sha1sum, 20 );
4834 mbedtls_sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004837
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004838 mbedtls_md5_free( &md5 );
4839 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
4842 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
4843 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004846}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004850static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004852{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004853 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004854 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004855 mbedtls_md5_context md5;
4856 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004857 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004860 if( !session )
4861 session = ssl->session;
4862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004864
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004865 mbedtls_md5_init( &md5 );
4866 mbedtls_sha1_init( &sha1 );
4867
4868 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
4869 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004870
Paul Bakker1ef83d62012-04-11 12:09:53 +00004871 /*
4872 * TLSv1:
4873 * hash = PRF( master, finished_label,
4874 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4875 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004878 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4879 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004880#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004882#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004883 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4884 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004885#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004888 ? "client finished"
4889 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004890
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004891 mbedtls_md5_finish( &md5, padbuf );
4892 mbedtls_sha1_finish( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004893
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004894 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004895 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004898
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004899 mbedtls_md5_free( &md5 );
4900 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004905}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4909#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004910static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00004912{
4913 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004914 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004915 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004916 unsigned char padbuf[32];
4917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004919 if( !session )
4920 session = ssl->session;
4921
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004922 mbedtls_sha256_init( &sha256 );
4923
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004925
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004926 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004927
4928 /*
4929 * TLSv1.2:
4930 * hash = PRF( master, finished_label,
4931 * Hash( handshake ) )[0.11]
4932 */
4933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#if !defined(MBEDTLS_SHA256_ALT)
4935 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004936 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004937#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004940 ? "client finished"
4941 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004942
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004943 mbedtls_sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004944
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004945 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004946 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004949
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004950 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004955}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004959static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00004961{
4962 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004963 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004964 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004965 unsigned char padbuf[48];
4966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004968 if( !session )
4969 session = ssl->session;
4970
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004971 mbedtls_sha512_init( &sha512 );
4972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004974
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004975 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004976
4977 /*
4978 * TLSv1.2:
4979 * hash = PRF( master, finished_label,
4980 * Hash( handshake ) )[0.11]
4981 */
4982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004984 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4985 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004986#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004989 ? "client finished"
4990 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004991
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004992 mbedtls_sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004993
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004994 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004995 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004997 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004998
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004999 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005001 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005#endif /* MBEDTLS_SHA512_C */
5006#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005011
5012 /*
5013 * Free our handshake params
5014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 mbedtls_ssl_handshake_free( ssl->handshake );
5016 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005017 ssl->handshake = NULL;
5018
5019 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005020 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005021 */
5022 if( ssl->transform )
5023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 mbedtls_ssl_transform_free( ssl->transform );
5025 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005026 }
5027 ssl->transform = ssl->transform_negotiate;
5028 ssl->transform_negotiate = NULL;
5029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005031}
5032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005033void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005034{
5035 int resume = ssl->handshake->resume;
5036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039#if defined(MBEDTLS_SSL_RENEGOTIATION)
5040 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005042 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005043 ssl->renego_records_seen = 0;
5044 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005045#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005046
5047 /*
5048 * Free the previous session and switch in the current one
5049 */
Paul Bakker0a597072012-09-25 21:55:46 +00005050 if( ssl->session )
5051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005053 /* RFC 7366 3.1: keep the EtM state */
5054 ssl->session_negotiate->encrypt_then_mac =
5055 ssl->session->encrypt_then_mac;
5056#endif
5057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 mbedtls_ssl_session_free( ssl->session );
5059 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005060 }
5061 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005062 ssl->session_negotiate = NULL;
5063
Paul Bakker0a597072012-09-25 21:55:46 +00005064 /*
5065 * Add cache entry
5066 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005067 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005068 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005069 resume == 0 )
5070 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005071 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005073 }
Paul Bakker0a597072012-09-25 21:55:46 +00005074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005077 ssl->handshake->flight != NULL )
5078 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005079 /* Cancel handshake timer */
5080 ssl_set_timer( ssl, 0 );
5081
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005082 /* Keep last flight around in case we need to resend it:
5083 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005085 }
5086 else
5087#endif
5088 ssl_handshake_wrapup_free_hs_transform( ssl );
5089
Paul Bakker48916f92012-09-16 19:57:18 +00005090 ssl->state++;
5091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005093}
5094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005096{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005097 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005100
Paul Bakker92be97b2013-01-02 17:30:03 +01005101 /*
5102 * Set the out_msg pointer to the correct location based on IV length
5103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005105 {
5106 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5107 ssl->transform_negotiate->fixed_ivlen;
5108 }
5109 else
5110 ssl->out_msg = ssl->out_iv;
5111
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005112 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005113
5114 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005118 ssl->verify_data_len = hash_len;
5119 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005120#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005121
Paul Bakker5121ce52009-01-03 21:22:43 +00005122 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5124 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005125
5126 /*
5127 * In case of session resuming, invert the client and server
5128 * ChangeCipherSpec messages order.
5129 */
Paul Bakker0a597072012-09-25 21:55:46 +00005130 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005133 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005137 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005139#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005140 }
5141 else
5142 ssl->state++;
5143
Paul Bakker48916f92012-09-16 19:57:18 +00005144 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005145 * Switch to our negotiated transform and session parameters for outbound
5146 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005151 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005152 {
5153 unsigned char i;
5154
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005155 /* Remember current epoch settings for resending */
5156 ssl->handshake->alt_transform_out = ssl->transform_out;
5157 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5158
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005159 /* Set sequence_number to zero */
5160 memset( ssl->out_ctr + 2, 0, 6 );
5161
5162 /* Increment epoch */
5163 for( i = 2; i > 0; i-- )
5164 if( ++ssl->out_ctr[i - 1] != 0 )
5165 break;
5166
5167 /* The loop goes to its end iff the counter is wrapping */
5168 if( i == 0 )
5169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5171 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005172 }
5173 }
5174 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005175#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005176 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005177
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005178 ssl->transform_out = ssl->transform_negotiate;
5179 ssl->session_out = ssl->session_negotiate;
5180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005181#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5182 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5187 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005188 }
5189 }
5190#endif
5191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005193 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005194 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005195#endif
5196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005200 return( ret );
5201 }
5202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005204
5205 return( 0 );
5206}
5207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005209#define SSL_MAX_HASH_LEN 36
5210#else
5211#define SSL_MAX_HASH_LEN 12
5212#endif
5213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005215{
Paul Bakker23986e52011-04-24 08:57:21 +00005216 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005217 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005218 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005221
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005222 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005227 return( ret );
5228 }
5229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5233 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005234 }
5235
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005236 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237#if defined(MBEDTLS_SSL_PROTO_SSL3)
5238 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005239 hash_len = 36;
5240 else
5241#endif
5242 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5245 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5248 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005249 }
5250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005252 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5255 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005256 }
5257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005259 ssl->verify_data_len = hash_len;
5260 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005261#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005262
Paul Bakker0a597072012-09-25 21:55:46 +00005263 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005266 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005270 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005272#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005273 }
5274 else
5275 ssl->state++;
5276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005278 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005280#endif
5281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005283
5284 return( 0 );
5285}
5286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5292 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5293 mbedtls_md5_init( &handshake->fin_md5 );
5294 mbedtls_sha1_init( &handshake->fin_sha1 );
5295 mbedtls_md5_starts( &handshake->fin_md5 );
5296 mbedtls_sha1_starts( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5299#if defined(MBEDTLS_SHA256_C)
5300 mbedtls_sha256_init( &handshake->fin_sha256 );
5301 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303#if defined(MBEDTLS_SHA512_C)
5304 mbedtls_sha512_init( &handshake->fin_sha512 );
5305 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005308
5309 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01005310
5311#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5312 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5313 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5314#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005316#if defined(MBEDTLS_DHM_C)
5317 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319#if defined(MBEDTLS_ECDH_C)
5320 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005321#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005322
5323#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5324 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5325#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005326}
5327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005330 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5333 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335 mbedtls_md_init( &transform->md_ctx_enc );
5336 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005337}
5338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005342}
5343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005345{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005346 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005347 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005349 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005351 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005353
5354 /*
5355 * Either the pointers are now NULL or cleared properly and can be freed.
5356 * Now allocate missing structures.
5357 */
5358 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005359 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005360 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005361 }
Paul Bakker48916f92012-09-16 19:57:18 +00005362
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005363 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005364 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005365 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005366 }
Paul Bakker48916f92012-09-16 19:57:18 +00005367
Paul Bakker82788fb2014-10-20 13:59:19 +02005368 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005369 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005370 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005371 }
Paul Bakker48916f92012-09-16 19:57:18 +00005372
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005373 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005374 if( ssl->handshake == NULL ||
5375 ssl->transform_negotiate == NULL ||
5376 ssl->session_negotiate == NULL )
5377 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 mbedtls_free( ssl->handshake );
5381 mbedtls_free( ssl->transform_negotiate );
5382 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005383
5384 ssl->handshake = NULL;
5385 ssl->transform_negotiate = NULL;
5386 ssl->session_negotiate = NULL;
5387
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005388 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005389 }
5390
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005391 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005393 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005394 ssl_handshake_params_init( ssl->handshake );
5395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005397 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5398 {
5399 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005400
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005401 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5402 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5403 else
5404 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005405
5406 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005407 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005408#endif
5409
Paul Bakker48916f92012-09-16 19:57:18 +00005410 return( 0 );
5411}
5412
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005413#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005414/* Dummy cookie callbacks for defaults */
5415static int ssl_cookie_write_dummy( void *ctx,
5416 unsigned char **p, unsigned char *end,
5417 const unsigned char *cli_id, size_t cli_id_len )
5418{
5419 ((void) ctx);
5420 ((void) p);
5421 ((void) end);
5422 ((void) cli_id);
5423 ((void) cli_id_len);
5424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005426}
5427
5428static int ssl_cookie_check_dummy( void *ctx,
5429 const unsigned char *cookie, size_t cookie_len,
5430 const unsigned char *cli_id, size_t cli_id_len )
5431{
5432 ((void) ctx);
5433 ((void) cookie);
5434 ((void) cookie_len);
5435 ((void) cli_id);
5436 ((void) cli_id_len);
5437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005438 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005439}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005440#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005441
Paul Bakker5121ce52009-01-03 21:22:43 +00005442/*
5443 * Initialize an SSL context
5444 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005445void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5446{
5447 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5448}
5449
5450/*
5451 * Setup an SSL context
5452 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005453int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005454 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005455{
Paul Bakker48916f92012-09-16 19:57:18 +00005456 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005457 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005458
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005459 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005460
5461 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005462 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005463 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005464 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5465 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005466 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005469 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005470 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005471 }
5472
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005473#if defined(MBEDTLS_SSL_PROTO_DTLS)
5474 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5475 {
5476 ssl->out_hdr = ssl->out_buf;
5477 ssl->out_ctr = ssl->out_buf + 3;
5478 ssl->out_len = ssl->out_buf + 11;
5479 ssl->out_iv = ssl->out_buf + 13;
5480 ssl->out_msg = ssl->out_buf + 13;
5481
5482 ssl->in_hdr = ssl->in_buf;
5483 ssl->in_ctr = ssl->in_buf + 3;
5484 ssl->in_len = ssl->in_buf + 11;
5485 ssl->in_iv = ssl->in_buf + 13;
5486 ssl->in_msg = ssl->in_buf + 13;
5487 }
5488 else
5489#endif
5490 {
5491 ssl->out_ctr = ssl->out_buf;
5492 ssl->out_hdr = ssl->out_buf + 8;
5493 ssl->out_len = ssl->out_buf + 11;
5494 ssl->out_iv = ssl->out_buf + 13;
5495 ssl->out_msg = ssl->out_buf + 13;
5496
5497 ssl->in_ctr = ssl->in_buf;
5498 ssl->in_hdr = ssl->in_buf + 8;
5499 ssl->in_len = ssl->in_buf + 11;
5500 ssl->in_iv = ssl->in_buf + 13;
5501 ssl->in_msg = ssl->in_buf + 13;
5502 }
5503
Paul Bakker48916f92012-09-16 19:57:18 +00005504 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5505 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005506
5507 return( 0 );
5508}
5509
5510/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005511 * Reset an initialized and used SSL context for re-use while retaining
5512 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005513 *
5514 * If partial is non-zero, keep data in the input buffer and client ID.
5515 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00005516 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005517static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005518{
Paul Bakker48916f92012-09-16 19:57:18 +00005519 int ret;
5520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005522
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005523 /* Cancel any possibly running timer */
5524 ssl_set_timer( ssl, 0 );
5525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005526#if defined(MBEDTLS_SSL_RENEGOTIATION)
5527 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005528 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005529
5530 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
5532 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005535
Paul Bakker7eb013f2011-10-06 12:37:39 +00005536 ssl->in_offt = NULL;
5537
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005538 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005539 ssl->in_msgtype = 0;
5540 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005541 if( partial == 0 )
5542 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005543#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005544 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005545 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005546#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005548 ssl_dtls_replay_reset( ssl );
5549#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005550
5551 ssl->in_hslen = 0;
5552 ssl->nb_zero = 0;
Hanno Becker704f4932017-06-08 13:08:45 +01005553 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005554
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005555 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005556 ssl->out_msgtype = 0;
5557 ssl->out_msglen = 0;
5558 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
5560 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005561 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005562#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005563
Paul Bakker48916f92012-09-16 19:57:18 +00005564 ssl->transform_in = NULL;
5565 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005568 if( partial == 0 )
5569 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5572 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
5575 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
5578 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005579 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005580 }
5581#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005582
Paul Bakker48916f92012-09-16 19:57:18 +00005583 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 mbedtls_ssl_transform_free( ssl->transform );
5586 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005587 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005588 }
Paul Bakker48916f92012-09-16 19:57:18 +00005589
Paul Bakkerc0463502013-02-14 11:19:38 +01005590 if( ssl->session )
5591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 mbedtls_ssl_session_free( ssl->session );
5593 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005594 ssl->session = NULL;
5595 }
5596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005598 ssl->alpn_chosen = NULL;
5599#endif
5600
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005601#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005602 if( partial == 0 )
5603 {
5604 mbedtls_free( ssl->cli_id );
5605 ssl->cli_id = NULL;
5606 ssl->cli_id_len = 0;
5607 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005608#endif
5609
Paul Bakker48916f92012-09-16 19:57:18 +00005610 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5611 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005612
5613 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005614}
5615
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005616/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005617 * Reset an initialized and used SSL context for re-use while retaining
5618 * all application-set variables, function pointers and data.
5619 */
5620int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
5621{
5622 return( ssl_session_reset_int( ssl, 0 ) );
5623}
5624
5625/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005626 * SSL set accessors
5627 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005628void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00005629{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005630 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00005631}
5632
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02005633void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005635 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005636}
5637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005638#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005639void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005640{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005641 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005642}
5643#endif
5644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005646void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005648 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005649}
5650#endif
5651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005653void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005655 conf->hs_timeout_min = min;
5656 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005657}
5658#endif
5659
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005660void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00005661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005662 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00005663}
5664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005666void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005667 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005668 void *p_vrfy )
5669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005670 conf->f_vrfy = f_vrfy;
5671 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005674
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005675void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005676 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005677 void *p_rng )
5678{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01005679 conf->f_rng = f_rng;
5680 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00005681}
5682
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005683void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02005684 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 void *p_dbg )
5686{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005687 conf->f_dbg = f_dbg;
5688 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00005689}
5690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005692 void *p_bio,
5693 int (*f_send)(void *, const unsigned char *, size_t),
5694 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005695 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005696{
5697 ssl->p_bio = p_bio;
5698 ssl->f_send = f_send;
5699 ssl->f_recv = f_recv;
5700 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005701}
5702
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005703void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005704{
5705 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005706}
5707
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005708void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
5709 void *p_timer,
5710 void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms),
5711 int (*f_get_timer)(void *) )
5712{
5713 ssl->p_timer = p_timer;
5714 ssl->f_set_timer = f_set_timer;
5715 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005716
5717 /* Make sure we start with no timer running */
5718 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005719}
5720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005722void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005723 void *p_cache,
5724 int (*f_get_cache)(void *, mbedtls_ssl_session *),
5725 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005726{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005727 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005728 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005729 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005730}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005733#if defined(MBEDTLS_SSL_CLI_C)
5734int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005735{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005736 int ret;
5737
5738 if( ssl == NULL ||
5739 session == NULL ||
5740 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005741 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005744 }
5745
5746 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5747 return( ret );
5748
Paul Bakker0a597072012-09-25 21:55:46 +00005749 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005750
5751 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005754
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005755void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005756 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005757{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005758 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
5759 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
5760 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
5761 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005762}
5763
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005764void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005765 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005766 int major, int minor )
5767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005769 return;
5770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005772 return;
5773
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005774 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005775}
5776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005778void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01005779 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005780{
5781 conf->cert_profile = profile;
5782}
5783
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005784/* Append a new keycert entry to a (possibly empty) list */
5785static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
5786 mbedtls_x509_crt *cert,
5787 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005788{
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005789 mbedtls_ssl_key_cert *new;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005790
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005791 new = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005792 if( new == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005793 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005794
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005795 new->cert = cert;
5796 new->key = key;
5797 new->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005798
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005799 /* Update head is the list was null, else add to the end */
5800 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005801 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005802 *head = new;
Paul Bakker0333b972013-11-04 17:08:28 +01005803 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005804 else
5805 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005806 mbedtls_ssl_key_cert *cur = *head;
5807 while( cur->next != NULL )
5808 cur = cur->next;
5809 cur->next = new;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005810 }
5811
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005812 return( 0 );
5813}
5814
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005815int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005816 mbedtls_x509_crt *own_cert,
5817 mbedtls_pk_context *pk_key )
5818{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02005819 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005820}
5821
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005822void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01005823 mbedtls_x509_crt *ca_chain,
5824 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005825{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01005826 conf->ca_chain = ca_chain;
5827 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005830
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02005831#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5832int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
5833 mbedtls_x509_crt *own_cert,
5834 mbedtls_pk_context *pk_key )
5835{
5836 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
5837 own_cert, pk_key ) );
5838}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005839
5840void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
5841 mbedtls_x509_crt *ca_chain,
5842 mbedtls_x509_crl *ca_crl )
5843{
5844 ssl->handshake->sni_ca_chain = ca_chain;
5845 ssl->handshake->sni_ca_crl = ca_crl;
5846}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005847
5848void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
5849 int authmode )
5850{
5851 ssl->handshake->sni_authmode = authmode;
5852}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02005853#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
5854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005856int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01005857 const unsigned char *psk, size_t psk_len,
5858 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005859{
Paul Bakker6db455e2013-09-18 17:29:31 +02005860 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02005862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 if( psk_len > MBEDTLS_PSK_MAX_LEN )
5864 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005865
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02005866 /* Identity len will be encoded on two bytes */
5867 if( ( psk_identity_len >> 16 ) != 0 ||
5868 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
5869 {
5870 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5871 }
5872
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005873 if( conf->psk != NULL || conf->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005874 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005875 mbedtls_free( conf->psk );
5876 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnardffb81802015-10-20 19:56:45 +02005877 conf->psk = NULL;
5878 conf->psk_identity = NULL;
Paul Bakker6db455e2013-09-18 17:29:31 +02005879 }
5880
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005881 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
5882 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005883 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005884 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02005885 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005886 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02005887 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005888 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05005889 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005890
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005891 conf->psk_len = psk_len;
5892 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005893
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005894 memcpy( conf->psk, psk, conf->psk_len );
5895 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005896
5897 return( 0 );
5898}
5899
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01005900int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
5901 const unsigned char *psk, size_t psk_len )
5902{
5903 if( psk == NULL || ssl->handshake == NULL )
5904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5905
5906 if( psk_len > MBEDTLS_PSK_MAX_LEN )
5907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5908
5909 if( ssl->handshake->psk != NULL )
Simon Butcher5b8d1d62015-10-04 22:06:51 +01005910 mbedtls_free( ssl->handshake->psk );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01005911
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005912 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005913 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01005914
5915 ssl->handshake->psk_len = psk_len;
5916 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
5917
5918 return( 0 );
5919}
5920
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005921void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02005923 size_t),
5924 void *p_psk )
5925{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005926 conf->f_psk = f_psk;
5927 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005930
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005931#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005932int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005933{
5934 int ret;
5935
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01005936 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
5937 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
5938 {
5939 mbedtls_mpi_free( &conf->dhm_P );
5940 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005941 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01005942 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005943
5944 return( 0 );
5945}
5946
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005947int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00005948{
5949 int ret;
5950
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01005951 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
5952 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
5953 {
5954 mbedtls_mpi_free( &conf->dhm_P );
5955 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00005956 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01005957 }
Paul Bakker1b57b062011-01-06 15:48:19 +00005958
5959 return( 0 );
5960}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005961#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005962
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005963#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5964/*
5965 * Set the minimum length for Diffie-Hellman parameters
5966 */
5967void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
5968 unsigned int bitlen )
5969{
5970 conf->dhm_min_bitlen = bitlen;
5971}
5972#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
5973
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02005974#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02005975/*
5976 * Set allowed/preferred hashes for handshake signatures
5977 */
5978void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
5979 const int *hashes )
5980{
5981 conf->sig_hashes = hashes;
5982}
Hanno Becker593b0d32017-04-07 13:25:49 +01005983#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02005984
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005985#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005986/*
5987 * Set the allowed elliptic curves
5988 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005989void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005990 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005991{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005992 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005993}
Hanno Becker593b0d32017-04-07 13:25:49 +01005994#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005995
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01005996#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005997int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005998{
Hanno Becker593b0d32017-04-07 13:25:49 +01005999 /* Initialize to suppress unnecessary compiler warning */
6000 size_t hostname_len = 0;
6001
6002 /* Check if new hostname is valid before
6003 * making any change to current one */
6004
6005 if( hostname != NULL )
6006 {
6007 hostname_len = strlen( hostname );
6008
6009 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
6010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6011 }
6012
6013 /* Now it's clear that we will overwrite the old hostname,
6014 * so we can free it safely */
6015
6016 if( ssl->hostname != NULL )
6017 {
6018 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
6019 mbedtls_free( ssl->hostname );
6020 }
6021
6022 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006023
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 if( hostname == NULL )
Hanno Becker593b0d32017-04-07 13:25:49 +01006025 {
6026 ssl->hostname = NULL;
6027 }
6028 else
6029 {
6030 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006031
Hanno Becker593b0d32017-04-07 13:25:49 +01006032 if( ssl->hostname == NULL )
6033 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006034
Hanno Becker593b0d32017-04-07 13:25:49 +01006035 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006036
Hanno Becker593b0d32017-04-07 13:25:49 +01006037 ssl->hostname[hostname_len] = '\0';
6038 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006039
6040 return( 0 );
6041}
Hanno Beckerc7845e52017-04-07 13:02:16 +01006042#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006043
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006044#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006045void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006047 const unsigned char *, size_t),
6048 void *p_sni )
6049{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006050 conf->f_sni = f_sni;
6051 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006052}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006056int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006057{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006058 size_t cur_len, tot_len;
6059 const char **p;
6060
6061 /*
Brian J Murraye7f8dc32016-11-06 04:45:15 -08006062 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6063 * MUST NOT be truncated."
6064 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006065 */
6066 tot_len = 0;
6067 for( p = protos; *p != NULL; p++ )
6068 {
6069 cur_len = strlen( *p );
6070 tot_len += cur_len;
6071
6072 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006074 }
6075
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006076 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006077
6078 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006079}
6080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006082{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006083 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006084}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006086
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006087void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006088{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006089 conf->max_major_ver = major;
6090 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006091}
6092
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006093void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006094{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006095 conf->min_major_ver = major;
6096 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006097}
6098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006100void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006101{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006102 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006103}
6104#endif
6105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006107void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006108{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006109 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006110}
6111#endif
6112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006114void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006116 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006117}
6118#endif
6119
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006120#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006121void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006123 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006124}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006125#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006128int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6131 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006134 }
6135
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006136 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006137
6138 return( 0 );
6139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006143void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006145 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006150void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006151{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006152 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006153}
6154#endif
6155
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006156void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006158 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006159}
6160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006162void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006163{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006164 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006165}
6166
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006167void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006169 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006170}
6171
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006172void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006173 const unsigned char period[8] )
6174{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006175 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006180#if defined(MBEDTLS_SSL_CLI_C)
6181void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006182{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006183 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006184}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006185#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006186
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006187#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006188void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6189 mbedtls_ssl_ticket_write_t *f_ticket_write,
6190 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6191 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006192{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006193 conf->f_ticket_write = f_ticket_write;
6194 conf->f_ticket_parse = f_ticket_parse;
6195 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006196}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006199
Paul Bakker5121ce52009-01-03 21:22:43 +00006200/*
6201 * SSL get accessors
6202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006204{
6205 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6206}
6207
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006208uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006209{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006210 if( ssl->session != NULL )
6211 return( ssl->session->verify_result );
6212
6213 if( ssl->session_negotiate != NULL )
6214 return( ssl->session_negotiate->verify_result );
6215
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006216 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006217}
6218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006220{
Paul Bakker926c8e42013-03-06 10:23:34 +01006221 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006222 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006225}
6226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006228{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006231 {
6232 switch( ssl->minor_ver )
6233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006235 return( "DTLSv1.0" );
6236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006238 return( "DTLSv1.2" );
6239
6240 default:
6241 return( "unknown (DTLS)" );
6242 }
6243 }
6244#endif
6245
Paul Bakker43ca69c2011-01-15 17:35:19 +00006246 switch( ssl->minor_ver )
6247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006249 return( "SSLv3.0" );
6250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006252 return( "TLSv1.0" );
6253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006255 return( "TLSv1.1" );
6256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006258 return( "TLSv1.2" );
6259
Paul Bakker43ca69c2011-01-15 17:35:19 +00006260 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006261 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006262 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006263}
6264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006266{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006267 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270#if defined(MBEDTLS_ZLIB_SUPPORT)
6271 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6272 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006273#endif
6274
6275 if( transform == NULL )
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006276 return( (int) mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 case MBEDTLS_MODE_GCM:
6281 case MBEDTLS_MODE_CCM:
6282 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006283 transform_expansion = transform->minlen;
6284 break;
6285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 case MBEDTLS_MODE_CBC:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006287 transform_expansion = transform->maclen
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 + mbedtls_cipher_get_block_size( &transform->cipher_ctx_enc );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006289 break;
6290
6291 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006294 }
6295
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006296 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006297}
6298
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006299#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6300size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6301{
6302 size_t max_len;
6303
6304 /*
6305 * Assume mfl_code is correct since it was checked when set
6306 */
6307 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6308
6309 /*
6310 * Check if a smaller max length was negotiated
6311 */
6312 if( ssl->session_out != NULL &&
6313 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6314 {
6315 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6316 }
6317
6318 return max_len;
6319}
6320#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322#if defined(MBEDTLS_X509_CRT_PARSE_C)
6323const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006324{
6325 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006326 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006327
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006328 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332#if defined(MBEDTLS_SSL_CLI_C)
6333int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006334{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006335 if( ssl == NULL ||
6336 dst == NULL ||
6337 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006338 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006341 }
6342
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006343 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006346
Paul Bakker5121ce52009-01-03 21:22:43 +00006347/*
Paul Bakker1961b702013-01-25 14:49:24 +01006348 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006353
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006354 if( ssl == NULL || ssl->conf == NULL )
6355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006362 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006364#endif
6365
Paul Bakker1961b702013-01-25 14:49:24 +01006366 return( ret );
6367}
6368
6369/*
6370 * Perform the SSL handshake
6371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006373{
6374 int ret = 0;
6375
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006376 if( ssl == NULL || ssl->conf == NULL )
6377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006384
6385 if( ret != 0 )
6386 break;
6387 }
6388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006390
6391 return( ret );
6392}
6393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394#if defined(MBEDTLS_SSL_RENEGOTIATION)
6395#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006396/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006397 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006400{
6401 int ret;
6402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006404
6405 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6407 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006412 return( ret );
6413 }
6414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006416
6417 return( 0 );
6418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006420
6421/*
6422 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 * - any side: calling mbedtls_ssl_renegotiate(),
6424 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
6425 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02006426 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006427 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006430static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006431{
6432 int ret;
6433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006435
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006436 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6437 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006438
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006439 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
6440 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006442 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006443 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006444 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006445 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006446 ssl->handshake->out_msg_seq = 1;
6447 else
6448 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006449 }
6450#endif
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
6453 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00006454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006458 return( ret );
6459 }
6460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006462
6463 return( 0 );
6464}
6465
6466/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006467 * Renegotiate current connection on client,
6468 * or request renegotiation on server
6469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006470int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006473
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006474 if( ssl == NULL || ssl->conf == NULL )
6475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006478 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006479 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006485
6486 /* Did we already try/start sending HelloRequest? */
6487 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006489
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006490 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006495 /*
6496 * On client, either start the renegotiation process or,
6497 * if already in progress, continue the handshake
6498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006503
6504 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006507 return( ret );
6508 }
6509 }
6510 else
6511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006515 return( ret );
6516 }
6517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006519
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006520 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006521}
6522
6523/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006524 * Check record counters and renegotiate if they're above the limit.
6525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006527{
Andres AG7fa66d42016-12-15 17:01:16 +00006528 size_t ep_len = ssl_ep_len( ssl );
6529 int in_ctr_cmp;
6530 int out_ctr_cmp;
6531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
6533 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006534 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006535 {
6536 return( 0 );
6537 }
6538
Andres AG7fa66d42016-12-15 17:01:16 +00006539 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
6540 ssl->conf->renego_period + ep_len, 8 - ep_len );
6541 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
6542 ssl->conf->renego_period + ep_len, 8 - ep_len );
6543
6544 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006545 {
6546 return( 0 );
6547 }
6548
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006553
6554/*
6555 * Receive application data decrypted from the SSL layer
6556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006558{
Hanno Becker6a582e82017-06-08 13:38:05 +01006559 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006560 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006561
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006562 if( ssl == NULL || ssl->conf == NULL )
6563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006571 return( ret );
6572
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006573 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006577 return( ret );
6578 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006579 }
6580#endif
6581
Hanno Becker6a582e82017-06-08 13:38:05 +01006582 /*
6583 * Check if renegotiation is necessary and/or handshake is
6584 * in process. If yes, perform/continue, and fall through
6585 * if an unexpected packet is received while the client
6586 * is waiting for the ServerHello.
6587 *
6588 * (There is no equivalent to the last condition on
6589 * the server-side as it is not treated as within
6590 * a handshake while waiting for the ClientHello
6591 * after a renegotiation request.)
6592 */
6593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006594#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker6a582e82017-06-08 13:38:05 +01006595 ret = ssl_check_ctr_renegotiate( ssl );
6596 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6597 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006600 return( ret );
6601 }
6602#endif
6603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker6a582e82017-06-08 13:38:05 +01006607 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6608 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006611 return( ret );
6612 }
6613 }
6614
6615 if( ssl->in_offt == NULL )
6616 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006617 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006618 if( ssl->f_get_timer != NULL &&
6619 ssl->f_get_timer( ssl->p_timer ) == -1 )
6620 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006621 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006622 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006623
Hanno Becker6a582e82017-06-08 13:38:05 +01006624 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006625 {
Hanno Becker6a582e82017-06-08 13:38:05 +01006626 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
6627 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006628
Hanno Becker6a582e82017-06-08 13:38:05 +01006629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6630 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006631 }
6632
6633 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006635 {
6636 /*
6637 * OpenSSL sends empty messages to randomize the IV
6638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00006642 return( 0 );
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006645 return( ret );
6646 }
6647 }
6648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649#if defined(MBEDTLS_SSL_RENEGOTIATION)
6650 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00006651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
6657 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006660
6661 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006664 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006665#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006667 }
Hanno Becker6a582e82017-06-08 13:38:05 +01006668#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006669
Hanno Becker6a582e82017-06-08 13:38:05 +01006670#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006671 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006675
6676 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006678 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006679 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006680#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00006682 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006683#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006684
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006685 if( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006687 ssl->conf->allow_legacy_renegotiation ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006688 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692#if defined(MBEDTLS_SSL_PROTO_SSL3)
6693 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006694 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006695 /*
6696 * SSLv3 does not have a "no_renegotiation" alert
6697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006699 return( ret );
6700 }
6701 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6703#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6704 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6705 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6708 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
6709 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006710 {
6711 return( ret );
6712 }
Paul Bakker48916f92012-09-16 19:57:18 +00006713 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006714 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
6716 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006720 }
Paul Bakker48916f92012-09-16 19:57:18 +00006721 }
6722 else
6723 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006724 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006726 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6727 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006730 }
6731#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006732 ret = ssl_start_renegotiation( ssl );
Hanno Becker6a582e82017-06-08 13:38:05 +01006733 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6734 ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006737 return( ret );
6738 }
Paul Bakker48916f92012-09-16 19:57:18 +00006739 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006740
Hanno Becker6a582e82017-06-08 13:38:05 +01006741 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006742 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006744 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006745 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006746 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006747 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006750 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006752 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006753 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006754 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
6758 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006761 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006762 }
6763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
6767 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 }
6769
6770 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006771
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006772 /* We're going to return something now, cancel timer,
6773 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006775 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006776
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006778 /* If we requested renego but received AppData, resend HelloRequest.
6779 * Do it now, after setting in_offt, to avoid taking this branch
6780 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006782 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006784 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006785 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006788 return( ret );
6789 }
6790 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006792#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 }
6794
6795 n = ( len < ssl->in_msglen )
6796 ? len : ssl->in_msglen;
6797
6798 memcpy( buf, ssl->in_offt, n );
6799 ssl->in_msglen -= n;
6800
6801 if( ssl->in_msglen == 0 )
Hanno Beckercc019082017-06-09 10:51:37 +01006802 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 /* all bytes consumed */
6804 ssl->in_offt = NULL;
Hanno Beckercc019082017-06-09 10:51:37 +01006805 ssl->keep_current_message = 0;
6806 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 else
6808 /* more data available */
6809 ssl->in_offt += n;
6810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006812
Paul Bakker23986e52011-04-24 08:57:21 +00006813 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006814}
6815
6816/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006817 * Send application data to be encrypted by the SSL layer,
6818 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00006819 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006820static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006821 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822{
Paul Bakker23986e52011-04-24 08:57:21 +00006823 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006825 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Florina3604112017-07-22 09:01:44 +02006826#else
6827 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
6828#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006829 if( len > max_len )
6830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006835 "maximum fragment length: %d > %d",
6836 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006838 }
6839 else
6840#endif
6841 len = max_len;
6842 }
Paul Bakker887bd502011-06-08 13:10:54 +00006843
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 if( ssl->out_left != 0 )
6845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006849 return( ret );
6850 }
6851 }
Paul Bakker887bd502011-06-08 13:10:54 +00006852 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006853 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006854 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006856 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00006859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00006861 return( ret );
6862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006863 }
6864
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006865 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006866}
6867
6868/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006869 * Write application data, doing 1/n-1 splitting if necessary.
6870 *
6871 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006872 * then the caller will call us again with the same arguments, so
Hanno Beckere298c8b2017-09-18 14:58:11 +01006873 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006876static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006877 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006878{
6879 int ret;
6880
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006881 if( ssl->conf->cbc_record_splitting ==
6882 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006883 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
6885 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6886 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006887 {
6888 return( ssl_write_real( ssl, buf, len ) );
6889 }
6890
6891 if( ssl->split_done == 0 )
6892 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006893 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006894 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006895 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006896 }
6897
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006898 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6899 return( ret );
6900 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006901
6902 return( ret + 1 );
6903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006905
6906/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006907 * Write application data (public-facing wrapper)
6908 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006909int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006910{
6911 int ret;
6912
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006914
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006915 if( ssl == NULL || ssl->conf == NULL )
6916 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6917
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006918#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006919 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6920 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006921 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006922 return( ret );
6923 }
6924#endif
6925
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006926 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006927 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006928 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006929 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02006930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006931 return( ret );
6932 }
6933 }
6934
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006935#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006936 ret = ssl_write_split( ssl, buf, len );
6937#else
6938 ret = ssl_write_real( ssl, buf, len );
6939#endif
6940
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006942
6943 return( ret );
6944}
6945
6946/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 * Notify the peer that the connection is being closed
6948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006950{
6951 int ret;
6952
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006953 if( ssl == NULL || ssl->conf == NULL )
6954 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006957
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006958 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6964 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
6965 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006968 return( ret );
6969 }
6970 }
6971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006973
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006974 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975}
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00006978{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006979 if( transform == NULL )
6980 return;
6981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00006983 deflateEnd( &transform->ctx_deflate );
6984 inflateEnd( &transform->ctx_inflate );
6985#endif
6986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 mbedtls_cipher_free( &transform->cipher_ctx_enc );
6988 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 mbedtls_md_free( &transform->md_ctx_enc );
6991 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006994}
6995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996#if defined(MBEDTLS_X509_CRT_PARSE_C)
6997static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007000
7001 while( cur != NULL )
7002 {
7003 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007005 cur = next;
7006 }
7007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007010void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007011{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007012 if( handshake == NULL )
7013 return;
7014
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007015#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7016 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7017 mbedtls_md5_free( &handshake->fin_md5 );
7018 mbedtls_sha1_free( &handshake->fin_sha1 );
7019#endif
7020#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7021#if defined(MBEDTLS_SHA256_C)
7022 mbedtls_sha256_free( &handshake->fin_sha256 );
7023#endif
7024#if defined(MBEDTLS_SHA512_C)
7025 mbedtls_sha512_free( &handshake->fin_sha512 );
7026#endif
7027#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029#if defined(MBEDTLS_DHM_C)
7030 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#if defined(MBEDTLS_ECDH_C)
7033 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007034#endif
7035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007036#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02007037 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007039#endif
7040
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007041#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7042 if( handshake->psk != NULL )
7043 {
7044 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7045 mbedtls_free( handshake->psk );
7046 }
7047#endif
7048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7050 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007051 /*
7052 * Free only the linked list wrapper, not the keys themselves
7053 * since the belong to the SNI callback
7054 */
7055 if( handshake->sni_key_cert != NULL )
7056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007058
7059 while( cur != NULL )
7060 {
7061 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007063 cur = next;
7064 }
7065 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#if defined(MBEDTLS_SSL_PROTO_DTLS)
7069 mbedtls_free( handshake->verify_cookie );
7070 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007071 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007072#endif
7073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007075}
7076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007078{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007079 if( session == NULL )
7080 return;
7081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007083 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 mbedtls_x509_crt_free( session->peer_cert );
7086 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007087 }
Paul Bakkered27a042013-04-18 22:46:23 +02007088#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007089
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007090#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007092#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007095}
7096
Paul Bakker5121ce52009-01-03 21:22:43 +00007097/*
7098 * Free an SSL context
7099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007101{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007102 if( ssl == NULL )
7103 return;
7104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007106
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007107 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7110 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007111 }
7112
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007113 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7116 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007117 }
7118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007120 if( ssl->compress_buf != NULL )
7121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7123 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007124 }
7125#endif
7126
Paul Bakker48916f92012-09-16 19:57:18 +00007127 if( ssl->transform )
7128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 mbedtls_ssl_transform_free( ssl->transform );
7130 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007131 }
7132
7133 if( ssl->handshake )
7134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 mbedtls_ssl_handshake_free( ssl->handshake );
7136 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7137 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 mbedtls_free( ssl->handshake );
7140 mbedtls_free( ssl->transform_negotiate );
7141 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007142 }
7143
Paul Bakkerc0463502013-02-14 11:19:38 +01007144 if( ssl->session )
7145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 mbedtls_ssl_session_free( ssl->session );
7147 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007148 }
7149
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007150#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007151 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007152 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007153 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007155 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007156#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7159 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7162 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007163 }
7164#endif
7165
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007166#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007168#endif
7169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007171
Paul Bakker86f04f42013-02-14 11:20:09 +01007172 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007174}
7175
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007176/*
7177 * Initialze mbedtls_ssl_config
7178 */
7179void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7180{
7181 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7182}
7183
Simon Butcherc941b6c2015-12-28 01:29:10 +00007184#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007185static int ssl_preset_default_hashes[] = {
7186#if defined(MBEDTLS_SHA512_C)
7187 MBEDTLS_MD_SHA512,
7188 MBEDTLS_MD_SHA384,
7189#endif
7190#if defined(MBEDTLS_SHA256_C)
7191 MBEDTLS_MD_SHA256,
7192 MBEDTLS_MD_SHA224,
7193#endif
Gilles Peskine7344e1b2017-05-12 13:16:40 +02007194#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007195 MBEDTLS_MD_SHA1,
7196#endif
7197 MBEDTLS_MD_NONE
7198};
Simon Butcherc941b6c2015-12-28 01:29:10 +00007199#endif
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007200
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007201static int ssl_preset_suiteb_ciphersuites[] = {
7202 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7203 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7204 0
7205};
7206
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007207#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007208static int ssl_preset_suiteb_hashes[] = {
7209 MBEDTLS_MD_SHA256,
7210 MBEDTLS_MD_SHA384,
7211 MBEDTLS_MD_NONE
7212};
7213#endif
7214
7215#if defined(MBEDTLS_ECP_C)
7216static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
7217 MBEDTLS_ECP_DP_SECP256R1,
7218 MBEDTLS_ECP_DP_SECP384R1,
7219 MBEDTLS_ECP_DP_NONE
7220};
7221#endif
7222
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007223/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007224 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007225 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007226int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007227 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007228{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007229#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007230 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007231#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007232
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007233 /* Use the functions here so that they are covered in tests,
7234 * but otherwise access member directly for efficiency */
7235 mbedtls_ssl_conf_endpoint( conf, endpoint );
7236 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007237
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007238 /*
7239 * Things that are common to all presets
7240 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007241#if defined(MBEDTLS_SSL_CLI_C)
7242 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7243 {
7244 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7245#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7246 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7247#endif
7248 }
7249#endif
7250
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007251#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007252 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007253#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007254
7255#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7256 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7257#endif
7258
7259#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7260 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7261#endif
7262
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007263#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7264 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7265#endif
7266
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007267#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007268 conf->f_cookie_write = ssl_cookie_write_dummy;
7269 conf->f_cookie_check = ssl_cookie_check_dummy;
7270#endif
7271
7272#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7273 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7274#endif
7275
7276#if defined(MBEDTLS_SSL_PROTO_DTLS)
7277 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7278 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7279#endif
7280
7281#if defined(MBEDTLS_SSL_RENEGOTIATION)
7282 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG7fa66d42016-12-15 17:01:16 +00007283 memset( conf->renego_period, 0x00, 2 );
7284 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007285#endif
7286
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007287#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7288 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7289 {
7290 if( ( ret = mbedtls_ssl_conf_dh_param( conf,
7291 MBEDTLS_DHM_RFC5114_MODP_2048_P,
7292 MBEDTLS_DHM_RFC5114_MODP_2048_G ) ) != 0 )
7293 {
7294 return( ret );
7295 }
7296 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007297#endif
7298
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007299 /*
7300 * Preset-specific defaults
7301 */
7302 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007303 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007304 /*
7305 * NSA Suite B
7306 */
7307 case MBEDTLS_SSL_PRESET_SUITEB:
7308 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7309 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7310 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7311 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7312
7313 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7314 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7315 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7316 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7317 ssl_preset_suiteb_ciphersuites;
7318
7319#if defined(MBEDTLS_X509_CRT_PARSE_C)
7320 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007321#endif
7322
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007323#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007324 conf->sig_hashes = ssl_preset_suiteb_hashes;
7325#endif
7326
7327#if defined(MBEDTLS_ECP_C)
7328 conf->curve_list = ssl_preset_suiteb_curves;
7329#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007330 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007331
7332 /*
7333 * Default
7334 */
7335 default:
7336 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7337 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_1; /* TLS 1.0 */
7338 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7339 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7340
7341#if defined(MBEDTLS_SSL_PROTO_DTLS)
7342 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7343 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
7344#endif
7345
7346 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7347 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7348 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7349 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7350 mbedtls_ssl_list_ciphersuites();
7351
7352#if defined(MBEDTLS_X509_CRT_PARSE_C)
7353 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7354#endif
7355
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007356#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007357 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007358#endif
7359
7360#if defined(MBEDTLS_ECP_C)
7361 conf->curve_list = mbedtls_ecp_grp_id_list();
7362#endif
7363
7364#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7365 conf->dhm_min_bitlen = 1024;
7366#endif
7367 }
7368
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007369 return( 0 );
7370}
7371
7372/*
7373 * Free mbedtls_ssl_config
7374 */
7375void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
7376{
7377#if defined(MBEDTLS_DHM_C)
7378 mbedtls_mpi_free( &conf->dhm_P );
7379 mbedtls_mpi_free( &conf->dhm_G );
7380#endif
7381
7382#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7383 if( conf->psk != NULL )
7384 {
7385 mbedtls_zeroize( conf->psk, conf->psk_len );
7386 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
7387 mbedtls_free( conf->psk );
7388 mbedtls_free( conf->psk_identity );
7389 conf->psk_len = 0;
7390 conf->psk_identity_len = 0;
7391 }
7392#endif
7393
7394#if defined(MBEDTLS_X509_CRT_PARSE_C)
7395 ssl_key_cert_free( conf->key_cert );
7396#endif
7397
7398 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
7399}
7400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007402/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_RSA_C)
7408 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
7409 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_ECDSA_C)
7412 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
7413 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007416}
7417
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007418unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
7419{
7420 switch( type ) {
7421 case MBEDTLS_PK_RSA:
7422 return( MBEDTLS_SSL_SIG_RSA );
7423 case MBEDTLS_PK_ECDSA:
7424 case MBEDTLS_PK_ECKEY:
7425 return( MBEDTLS_SSL_SIG_ECDSA );
7426 default:
7427 return( MBEDTLS_SSL_SIG_ANON );
7428 }
7429}
7430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007432{
7433 switch( sig )
7434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_RSA_C)
7436 case MBEDTLS_SSL_SIG_RSA:
7437 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#if defined(MBEDTLS_ECDSA_C)
7440 case MBEDTLS_SSL_SIG_ECDSA:
7441 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007442#endif
7443 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007445 }
7446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#endif /* MBEDTLS_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007448
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007449#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7450 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7451
7452/* Find an entry in a signature-hash set matching a given hash algorithm. */
7453mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7454 mbedtls_pk_type_t sig_alg )
7455{
7456 switch( sig_alg )
7457 {
7458 case MBEDTLS_PK_RSA:
7459 return( set->rsa );
7460 case MBEDTLS_PK_ECDSA:
7461 return( set->ecdsa );
7462 default:
7463 return( MBEDTLS_MD_NONE );
7464 }
7465}
7466
7467/* Add a signature-hash-pair to a signature-hash set */
7468void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7469 mbedtls_pk_type_t sig_alg,
7470 mbedtls_md_type_t md_alg )
7471{
7472 switch( sig_alg )
7473 {
7474 case MBEDTLS_PK_RSA:
7475 if( set->rsa == MBEDTLS_MD_NONE )
7476 set->rsa = md_alg;
7477 break;
7478
7479 case MBEDTLS_PK_ECDSA:
7480 if( set->ecdsa == MBEDTLS_MD_NONE )
7481 set->ecdsa = md_alg;
7482 break;
7483
7484 default:
7485 break;
7486 }
7487}
7488
7489/* Allow exactly one hash algorithm for each signature. */
7490void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7491 mbedtls_md_type_t md_alg )
7492{
7493 set->rsa = md_alg;
7494 set->ecdsa = md_alg;
7495}
7496
7497#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
7498 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
7499
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007500/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007501 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007504{
7505 switch( hash )
7506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507#if defined(MBEDTLS_MD5_C)
7508 case MBEDTLS_SSL_HASH_MD5:
7509 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511#if defined(MBEDTLS_SHA1_C)
7512 case MBEDTLS_SSL_HASH_SHA1:
7513 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515#if defined(MBEDTLS_SHA256_C)
7516 case MBEDTLS_SSL_HASH_SHA224:
7517 return( MBEDTLS_MD_SHA224 );
7518 case MBEDTLS_SSL_HASH_SHA256:
7519 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007520#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#if defined(MBEDTLS_SHA512_C)
7522 case MBEDTLS_SSL_HASH_SHA384:
7523 return( MBEDTLS_MD_SHA384 );
7524 case MBEDTLS_SSL_HASH_SHA512:
7525 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007526#endif
7527 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007529 }
7530}
7531
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007532/*
7533 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7534 */
7535unsigned char mbedtls_ssl_hash_from_md_alg( int md )
7536{
7537 switch( md )
7538 {
7539#if defined(MBEDTLS_MD5_C)
7540 case MBEDTLS_MD_MD5:
7541 return( MBEDTLS_SSL_HASH_MD5 );
7542#endif
7543#if defined(MBEDTLS_SHA1_C)
7544 case MBEDTLS_MD_SHA1:
7545 return( MBEDTLS_SSL_HASH_SHA1 );
7546#endif
7547#if defined(MBEDTLS_SHA256_C)
7548 case MBEDTLS_MD_SHA224:
7549 return( MBEDTLS_SSL_HASH_SHA224 );
7550 case MBEDTLS_MD_SHA256:
7551 return( MBEDTLS_SSL_HASH_SHA256 );
7552#endif
7553#if defined(MBEDTLS_SHA512_C)
7554 case MBEDTLS_MD_SHA384:
7555 return( MBEDTLS_SSL_HASH_SHA384 );
7556 case MBEDTLS_MD_SHA512:
7557 return( MBEDTLS_SSL_HASH_SHA512 );
7558#endif
7559 default:
7560 return( MBEDTLS_SSL_HASH_NONE );
7561 }
7562}
7563
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007564#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007565/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007566 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007567 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007568 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007569int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007570{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007572
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007573 if( ssl->conf->curve_list == NULL )
7574 return( -1 );
7575
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007576 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007577 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007578 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007579
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007580 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007581}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007582#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007583
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007584#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007585/*
7586 * Check if a hash proposed by the peer is in our list.
7587 * Return 0 if we're willing to use it, -1 otherwise.
7588 */
7589int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
7590 mbedtls_md_type_t md )
7591{
7592 const int *cur;
7593
7594 if( ssl->conf->sig_hashes == NULL )
7595 return( -1 );
7596
7597 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
7598 if( *cur == (int) md )
7599 return( 0 );
7600
7601 return( -1 );
7602}
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007603#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_X509_CRT_PARSE_C)
7606int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
7607 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007608 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007609 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007610{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007611 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007613 int usage = 0;
7614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007616 const char *ext_oid;
7617 size_t ext_len;
7618#endif
7619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7621 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007622 ((void) cert);
7623 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007624 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007625#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
7628 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007629 {
7630 /* Server part of the key exchange */
7631 switch( ciphersuite->key_exchange )
7632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 case MBEDTLS_KEY_EXCHANGE_RSA:
7634 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007635 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007636 break;
7637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7639 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7640 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7641 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007642 break;
7643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7645 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007646 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007647 break;
7648
7649 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650 case MBEDTLS_KEY_EXCHANGE_NONE:
7651 case MBEDTLS_KEY_EXCHANGE_PSK:
7652 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7653 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007654 usage = 0;
7655 }
7656 }
7657 else
7658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7660 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007661 }
7662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007664 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007665 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007666 ret = -1;
7667 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007668#else
7669 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
7673 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675 ext_oid = MBEDTLS_OID_SERVER_AUTH;
7676 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007677 }
7678 else
7679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
7681 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007682 }
7683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007685 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007686 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007687 ret = -1;
7688 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007690
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007691 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007694
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007695/*
7696 * Convert version numbers to/from wire format
7697 * and, for DTLS, to/from TLS equivalent.
7698 *
7699 * For TLS this is the identity.
Brian J Murraye7f8dc32016-11-06 04:45:15 -08007700 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007701 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
7702 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
7703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007705 unsigned char ver[2] )
7706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_SSL_PROTO_DTLS)
7708 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007711 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7712
7713 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
7714 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
7715 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007716 else
7717#else
7718 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007719#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007720 {
7721 ver[0] = (unsigned char) major;
7722 ver[1] = (unsigned char) minor;
7723 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007724}
7725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007727 const unsigned char ver[2] )
7728{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729#if defined(MBEDTLS_SSL_PROTO_DTLS)
7730 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007731 {
7732 *major = 255 - ver[0] + 2;
7733 *minor = 255 - ver[1] + 1;
7734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007736 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7737 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007738 else
7739#else
7740 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007741#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007742 {
7743 *major = ver[0];
7744 *minor = ver[1];
7745 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007746}
7747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#endif /* MBEDTLS_SSL_TLS_C */