blob: 906d6ac4d67898d403f805590f1d4ed16222314c [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
Hanno Beckeraede1832017-09-18 10:55:31 +01001262 if( ssl->out_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
1263 {
1264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content too large, maximum %d",
1265 MBEDTLS_SSL_MAX_CONTENT_LEN ) );
1266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1267 }
1268
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001270 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001272#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 if( mode == MBEDTLS_MODE_STREAM ||
1274 ( mode == MBEDTLS_MODE_CBC
1275#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1276 && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001277#endif
1278 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#if defined(MBEDTLS_SSL_PROTO_SSL3)
1281 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001282 {
1283 ssl_mac( &ssl->transform_out->md_ctx_enc,
1284 ssl->transform_out->mac_enc,
1285 ssl->out_msg, ssl->out_msglen,
1286 ssl->out_ctr, ssl->out_msgtype );
1287 }
1288 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001289#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1291 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1292 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1295 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1296 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
1297 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001298 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001300 ssl->out_msg + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001302 }
1303 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001304#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001308 }
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac",
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001311 ssl->out_msg + ssl->out_msglen,
1312 ssl->transform_out->maclen );
1313
1314 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001315 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001316 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001317#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001318
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001319 /*
1320 * Encrypt
1321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1323 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001325 int ret;
1326 size_t olen = 0;
1327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 "including %d bytes of padding",
1330 ssl->out_msglen, 0 ) );
1331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001333 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001334 ssl->transform_out->ivlen,
1335 ssl->out_msg, ssl->out_msglen,
1336 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001339 return( ret );
1340 }
1341
1342 if( ssl->out_msglen != olen )
1343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001346 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 }
Paul Bakker68884e32013-01-07 18:20:04 +01001348 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1350#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1351 if( mode == MBEDTLS_MODE_GCM ||
1352 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001354 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001355 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 unsigned char *enc_msg;
1357 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001358 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001360
Paul Bakkerca4ab492012-04-18 14:23:57 +00001361 memcpy( add_data, ssl->out_ctr, 8 );
1362 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001364 ssl->conf->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001365 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1366 add_data[12] = ssl->out_msglen & 0xFF;
1367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakkerca4ab492012-04-18 14:23:57 +00001369 add_data, 13 );
1370
Paul Bakker68884e32013-01-07 18:20:04 +01001371 /*
1372 * Generate IV
1373 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001374 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1375 {
1376 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001379 }
1380
1381 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1382 ssl->out_ctr, 8 );
1383 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001386 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001387
Paul Bakker68884e32013-01-07 18:20:04 +01001388 /*
1389 * Fix pointer positions and message length with added IV
1390 */
1391 enc_msg = ssl->out_msg;
1392 enc_msglen = ssl->out_msglen;
1393 ssl->out_msglen += ssl->transform_out->ivlen -
1394 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker68884e32013-01-07 18:20:04 +01001397 "including %d bytes of padding",
1398 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001399
Paul Bakker68884e32013-01-07 18:20:04 +01001400 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001401 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001404 ssl->transform_out->iv_enc,
1405 ssl->transform_out->ivlen,
1406 add_data, 13,
1407 enc_msg, enc_msglen,
1408 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001409 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001412 return( ret );
1413 }
1414
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001415 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001419 }
1420
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001421 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001422 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1428#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1429 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1430 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001432 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001434 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001435
Paul Bakker48916f92012-09-16 19:57:18 +00001436 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1437 ssl->transform_out->ivlen;
1438 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 padlen = 0;
1440
1441 for( i = 0; i <= padlen; i++ )
1442 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1443
1444 ssl->out_msglen += padlen + 1;
1445
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001446 enc_msglen = ssl->out_msglen;
1447 enc_msg = ssl->out_msg;
1448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001450 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001451 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1452 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001455 {
1456 /*
1457 * Generate IV
1458 */
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +02001459 ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001460 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001461 if( ret != 0 )
1462 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001463
Paul Bakker92be97b2013-01-02 17:30:03 +01001464 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001465 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001466
1467 /*
1468 * Fix pointer positions and message length with added IV
1469 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001470 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001471 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001472 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001477 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001478 ssl->out_msglen, ssl->transform_out->ivlen,
1479 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001482 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001483 ssl->transform_out->ivlen,
1484 enc_msg, enc_msglen,
1485 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001488 return( ret );
1489 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001490
Paul Bakkercca5b812013-08-31 17:40:26 +02001491 if( enc_msglen != olen )
1492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001495 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1498 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001499 {
1500 /*
1501 * Save IV in SSL3 and TLS1
1502 */
1503 memcpy( ssl->transform_out->iv_enc,
1504 ssl->transform_out->cipher_ctx_enc.iv,
1505 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001507#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001510 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001511 {
1512 /*
1513 * MAC(MAC_write_key, seq_num +
1514 * TLSCipherText.type +
1515 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001516 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001517 * IV + // except for TLS 1.0
1518 * ENC(content + padding + padding_length));
1519 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001520 unsigned char pseudo_hdr[13];
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001523
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001524 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1525 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001526 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1527 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1532 mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001533 ssl->out_iv, ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001535 ssl->out_iv + ssl->out_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001537
1538 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001539 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001540 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001543 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1545 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001546 {
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é-Gonnardf7dc3782013-09-13 14:10:44 +02001549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001551 /* Make extra sure authentication was performed, exactly once */
1552 if( auth_done != 1 )
1553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001556 }
1557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
1560 return( 0 );
1561}
1562
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001563#define SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565static int ssl_decrypt_buf( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001566{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001567 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001569 int auth_done = 0;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001570#if defined(SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001571 size_t padlen = 0, correct = 1;
1572#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001576 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001580 }
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001583
Paul Bakker48916f92012-09-16 19:57:18 +00001584 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001587 ssl->in_msglen, ssl->transform_in->minlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 }
1590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1592 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001593 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001594 int ret;
1595 size_t olen = 0;
1596
Paul Bakker68884e32013-01-07 18:20:04 +01001597 padlen = 0;
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001600 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001601 ssl->transform_in->ivlen,
1602 ssl->in_msg, ssl->in_msglen,
1603 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001606 return( ret );
1607 }
1608
1609 if( ssl->in_msglen != olen )
1610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 }
Paul Bakker68884e32013-01-07 18:20:04 +01001615 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
1617#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
1618 if( mode == MBEDTLS_MODE_GCM ||
1619 mode == MBEDTLS_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001620 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001621 int ret;
1622 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001623 unsigned char *dec_msg;
1624 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001625 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001626 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001628 size_t explicit_iv_len = ssl->transform_in->ivlen -
1629 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001630
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02001631 if( ssl->in_msglen < explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001634 "+ taglen (%d)", ssl->in_msglen,
1635 explicit_iv_len, taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001637 }
1638 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1639
Paul Bakker68884e32013-01-07 18:20:04 +01001640 dec_msg = ssl->in_msg;
1641 dec_msg_result = ssl->in_msg;
1642 ssl->in_msglen = dec_msglen;
1643
1644 memcpy( add_data, ssl->in_ctr, 8 );
1645 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001647 ssl->conf->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001648 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1649 add_data[12] = ssl->in_msglen & 0xFF;
1650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Paul Bakker68884e32013-01-07 18:20:04 +01001652 add_data, 13 );
1653
1654 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1655 ssl->in_iv,
1656 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
Paul Bakker68884e32013-01-07 18:20:04 +01001659 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001661
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001662 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001663 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001666 ssl->transform_in->iv_dec,
1667 ssl->transform_in->ivlen,
1668 add_data, 13,
1669 dec_msg, dec_msglen,
1670 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001671 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1676 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001677
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001678 return( ret );
1679 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001680 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001681
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001682 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001686 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001687 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
1690#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
1691 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) )
1692 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001693 {
Paul Bakker45829992013-01-03 14:52:21 +01001694 /*
1695 * Decrypt and check the padding
1696 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001697 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001698 unsigned char *dec_msg;
1699 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001700 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001701 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001702 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001703
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 /*
Paul Bakker45829992013-01-03 14:52:21 +01001705 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1708 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker45829992013-01-03 14:52:21 +01001709 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001710#endif
Paul Bakker45829992013-01-03 14:52:21 +01001711
1712 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1713 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001716 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1717 ssl->transform_in->ivlen,
1718 ssl->transform_in->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001720 }
1721
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001722 dec_msglen = ssl->in_msglen;
1723 dec_msg = ssl->in_msg;
1724 dec_msg_result = ssl->in_msg;
1725
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001726 /*
1727 * Authenticate before decrypt if enabled
1728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1730 if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001731 {
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001732 unsigned char computed_mac[SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001733 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001736
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001737 dec_msglen -= ssl->transform_in->maclen;
1738 ssl->in_msglen -= ssl->transform_in->maclen;
1739
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001740 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1741 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1742 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1743 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
1748 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001749 ssl->in_iv, ssl->in_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1751 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001754 ssl->transform_in->maclen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001756 ssl->transform_in->maclen );
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001759 ssl->transform_in->maclen ) != 0 )
1760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001764 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001765 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001768
1769 /*
1770 * Check length sanity
1771 */
1772 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001775 ssl->in_msglen, ssl->transform_in->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001777 }
1778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001780 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001781 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001784 {
Paul Bakker48916f92012-09-16 19:57:18 +00001785 dec_msglen -= ssl->transform_in->ivlen;
1786 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001787
Paul Bakker48916f92012-09-16 19:57:18 +00001788 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001789 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001790 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001794 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001795 ssl->transform_in->ivlen,
1796 dec_msg, dec_msglen,
1797 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001800 return( ret );
1801 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001802
Paul Bakkercca5b812013-08-31 17:40:26 +02001803 if( dec_msglen != olen )
1804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001807 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1810 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001811 {
1812 /*
1813 * Save IV in SSL3 and TLS1
1814 */
1815 memcpy( ssl->transform_in->iv_dec,
1816 ssl->transform_in->cipher_ctx_dec.iv,
1817 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001819#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
1821 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001822
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001823 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001824 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_SSL_DEBUG_ALL)
1827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
Paul Bakker45829992013-01-03 14:52:21 +01001828 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001829#endif
Paul Bakker45829992013-01-03 14:52:21 +01001830 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001831 correct = 0;
1832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_SSL_PROTO_SSL3)
1835 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 {
Paul Bakker48916f92012-09-16 19:57:18 +00001837 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839#if defined(MBEDTLS_SSL_DEBUG_ALL)
1840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001842 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001843#endif
Paul Bakker45829992013-01-03 14:52:21 +01001844 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 }
1846 }
1847 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1849#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1850 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1851 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 {
1853 /*
Paul Bakker45829992013-01-03 14:52:21 +01001854 * TLSv1+: always check the padding up to the first failure
1855 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001857 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001858 size_t padding_idx = ssl->in_msglen - padlen - 1;
1859
Paul Bakker956c9e02013-12-19 14:42:28 +01001860 /*
1861 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001862 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001863 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 * 2. 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 * In both cases we reset padding_idx to a safe value (0) to
1868 * prevent out-of-buffer reads.
1869 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001870 correct &= ( ssl->in_msglen >= padlen + 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 correct &= ( padding_idx < MBEDTLS_SSL_MAX_CONTENT_LEN +
Paul Bakker61885c72014-04-25 12:59:03 +02001872 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001873
1874 padding_idx *= correct;
1875
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001876 for( i = 1; i <= 256; i++ )
1877 {
1878 real_count &= ( i <= padlen );
1879 pad_count += real_count *
1880 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1881 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001882
1883 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001886 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001888#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001889 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1893 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001897 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001898
1899 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001901 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_CIPHER_MODE_CBC &&
1903 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001907 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 ssl->in_msg, ssl->in_msglen );
1911
1912 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001913 * Authenticate if not done yet.
1914 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 */
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001916#if defined(SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001917 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001918 {
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001919 unsigned char tmp[SSL_MAX_MAC_SIZE];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001920
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001921 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001923 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1924 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001926 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928#if defined(MBEDTLS_SSL_PROTO_SSL3)
1929 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001930 {
1931 ssl_mac( &ssl->transform_in->md_ctx_dec,
1932 ssl->transform_in->mac_dec,
1933 ssl->in_msg, ssl->in_msglen,
1934 ssl->in_ctr, ssl->in_msgtype );
1935 }
1936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1938#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1939 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1940 if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001941 {
1942 /*
1943 * Process MAC and always update for padlen afterwards to make
1944 * total time independent of padlen
1945 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001946 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001947 *
1948 * Known timing attacks:
1949 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1950 *
1951 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1952 * correctly. (We round down instead of up, so -56 is the correct
1953 * value for our calculations instead of -55)
1954 */
1955 size_t j, extra_run = 0;
1956 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1957 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001958
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001959 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1962 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1963 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
1964 mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001965 ssl->in_msglen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02001968 /* Call mbedtls_md_process at least once due to cache attacks */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001969 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 }
1974 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1976 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1983 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001984 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 if( mbedtls_ssl_safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001987 ssl->transform_in->maclen ) != 0 )
1988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989#if defined(MBEDTLS_SSL_DEBUG_ALL)
1990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001991#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001992 correct = 0;
1993 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001994 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001996 /*
1997 * Finally check the correct flag
1998 */
1999 if( correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002001 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002002#endif /* SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002003
2004 /* Make extra sure authentication was performed, exactly once */
2005 if( auth_done != 1 )
2006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2008 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
2011 if( ssl->in_msglen == 0 )
2012 {
2013 ssl->nb_zero++;
2014
2015 /*
2016 * Three or more empty messages may be a DoS attack
2017 * (excessive CPU consumption).
2018 */
2019 if( ssl->nb_zero > 3 )
2020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 "messages, possible DoS attack" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 }
2025 }
2026 else
2027 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002030 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002031 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002032 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002033 }
2034 else
2035#endif
2036 {
2037 for( i = 8; i > ssl_ep_len( ssl ); i-- )
2038 if( ++ssl->in_ctr[i - 1] != 0 )
2039 break;
2040
2041 /* The loop goes to its end iff the counter is wrapping */
2042 if( i == ssl_ep_len( ssl ) )
2043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
2045 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02002046 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01002047 }
2048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
2051 return( 0 );
2052}
2053
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002054#undef MAC_NONE
2055#undef MAC_PLAINTEXT
2056#undef MAC_CIPHERTEXT
2057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002059/*
2060 * Compression/decompression functions
2061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002063{
2064 int ret;
2065 unsigned char *msg_post = ssl->out_msg;
2066 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002067 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002070
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002071 if( len_pre == 0 )
2072 return( 0 );
2073
Paul Bakker2770fbd2012-07-03 13:30:23 +00002074 memcpy( msg_pre, ssl->out_msg, len_pre );
2075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002077 ssl->out_msglen ) );
2078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002080 ssl->out_msg, ssl->out_msglen );
2081
Paul Bakker48916f92012-09-16 19:57:18 +00002082 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2083 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2084 ssl->transform_out->ctx_deflate.next_out = msg_post;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002086
Paul Bakker48916f92012-09-16 19:57:18 +00002087 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002088 if( ret != Z_OK )
2089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2091 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002092 }
2093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094 ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN -
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002095 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002098 ssl->out_msglen ) );
2099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002101 ssl->out_msg, ssl->out_msglen );
2102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002104
2105 return( 0 );
2106}
2107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002109{
2110 int ret;
2111 unsigned char *msg_post = ssl->in_msg;
2112 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002113 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002116
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002117 if( len_pre == 0 )
2118 return( 0 );
2119
Paul Bakker2770fbd2012-07-03 13:30:23 +00002120 memcpy( msg_pre, ssl->in_msg, len_pre );
2121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002123 ssl->in_msglen ) );
2124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002126 ssl->in_msg, ssl->in_msglen );
2127
Paul Bakker48916f92012-09-16 19:57:18 +00002128 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2129 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2130 ssl->transform_in->ctx_inflate.next_out = msg_post;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002132
Paul Bakker48916f92012-09-16 19:57:18 +00002133 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002134 if( ret != Z_OK )
2135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2137 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002138 }
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 ssl->in_msglen = MBEDTLS_SSL_MAX_CONTENT_LEN -
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002141 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002144 ssl->in_msglen ) );
2145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002147 ssl->in_msg, ssl->in_msglen );
2148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002150
2151 return( 0 );
2152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2156static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158#if defined(MBEDTLS_SSL_PROTO_DTLS)
2159static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002160{
2161 /* If renegotiation is not enforced, retransmit until we would reach max
2162 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002163 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002164 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002165 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002166 unsigned char doublings = 1;
2167
2168 while( ratio != 0 )
2169 {
2170 ++doublings;
2171 ratio >>= 1;
2172 }
2173
2174 if( ++ssl->renego_records_seen > doublings )
2175 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002177 return( 0 );
2178 }
2179 }
2180
2181 return( ssl_write_hello_request( ssl ) );
2182}
2183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002185
Paul Bakker5121ce52009-01-03 21:22:43 +00002186/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002187 * Fill the input message buffer by appending data to it.
2188 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002189 *
2190 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2191 * available (from this read and/or a previous one). Otherwise, an error code
2192 * is returned (possibly EOF or WANT_READ).
2193 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002194 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2195 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2196 * since we always read a whole datagram at once.
2197 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002198 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002199 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002202{
Paul Bakker23986e52011-04-24 08:57:21 +00002203 int ret;
2204 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002208 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002211 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002213 }
2214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002219 }
2220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002224 uint32_t timeout;
2225
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02002226 /* Just to be sure */
2227 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
2228 {
2229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
2230 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
2231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2232 }
2233
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002234 /*
2235 * The point is, we need to always read a full datagram at once, so we
2236 * sometimes read more then requested, and handle the additional data.
2237 * It could be the rest of the current record (while fetching the
2238 * header) and/or some other records in the same datagram.
2239 */
2240
2241 /*
2242 * Move to the next record in the already read datagram if applicable
2243 */
2244 if( ssl->next_record_offset != 0 )
2245 {
2246 if( ssl->in_left < ssl->next_record_offset )
2247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002250 }
2251
2252 ssl->in_left -= ssl->next_record_offset;
2253
2254 if( ssl->in_left != 0 )
2255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002257 ssl->next_record_offset ) );
2258 memmove( ssl->in_hdr,
2259 ssl->in_hdr + ssl->next_record_offset,
2260 ssl->in_left );
2261 }
2262
2263 ssl->next_record_offset = 0;
2264 }
2265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002268
2269 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002270 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002271 */
2272 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002275 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002276 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002277
2278 /*
2279 * A record can't be split accross datagrams. If we need to read but
2280 * are not at the beginning of a new record, the caller did something
2281 * wrong.
2282 */
2283 if( ssl->in_left != 0 )
2284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002287 }
2288
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002289 /*
2290 * Don't even try to read if time's out already.
2291 * This avoids by-passing the timer when repeatedly receiving messages
2292 * that will end up being dropped.
2293 */
2294 if( ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002295 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002296 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002301 timeout = ssl->handshake->retransmit_timeout;
2302 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002303 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002306
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002307 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002308 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2309 timeout );
2310 else
2311 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002314
2315 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002317 }
2318
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002319 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002322 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002325 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002326 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002329 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002330 }
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002335 return( ret );
2336 }
2337
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002338 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002341 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002343 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002344 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002347 return( ret );
2348 }
2349
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002350 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002351 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002353 }
2354
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 if( ret < 0 )
2356 return( ret );
2357
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002358 ssl->in_left = ret;
2359 }
2360 else
2361#endif
2362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002364 ssl->in_left, nb_want ) );
2365
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002366 while( ssl->in_left < nb_want )
2367 {
2368 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002369
2370 if( ssl_check_timer( ssl ) != 0 )
2371 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2372 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002373 {
2374 if( ssl->f_recv_timeout != NULL )
2375 {
2376 ret = ssl->f_recv_timeout( ssl->p_bio,
2377 ssl->in_hdr + ssl->in_left, len,
2378 ssl->conf->read_timeout );
2379 }
2380 else
2381 {
2382 ret = ssl->f_recv( ssl->p_bio,
2383 ssl->in_hdr + ssl->in_left, len );
2384 }
2385 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002388 ssl->in_left, nb_want ) );
2389 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002390
2391 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002393
2394 if( ret < 0 )
2395 return( ret );
2396
2397 ssl->in_left += ret;
2398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 }
2400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
2403 return( 0 );
2404}
2405
2406/*
2407 * Flush any data not yet written
2408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002411 int ret;
2412 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002416 if( ssl->f_send == NULL )
2417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002419 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002421 }
2422
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002423 /* Avoid incrementing counter if data is flushed */
2424 if( ssl->out_left == 0 )
2425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002427 return( 0 );
2428 }
2429
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 while( ssl->out_left > 0 )
2431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2433 mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) +
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002436 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002437 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002440
2441 if( ret <= 0 )
2442 return( ret );
2443
2444 ssl->out_left -= ret;
2445 }
2446
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002447 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002448 if( ++ssl->out_ctr[i - 1] != 0 )
2449 break;
2450
2451 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002452 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2455 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002456 }
2457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002459
2460 return( 0 );
2461}
2462
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002463/*
2464 * Functions to handle the DTLS retransmission state machine
2465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002467/*
2468 * Append current handshake message to current outgoing flight
2469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002470static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002471{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472 mbedtls_ssl_flight_item *msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002473
2474 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002475 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == 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",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 sizeof( mbedtls_ssl_flight_item ) ) );
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
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002482 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002483 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002486 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002487 }
2488
2489 /* Copy current handshake message with headers */
2490 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2491 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002492 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002493 msg->next = NULL;
2494
2495 /* Append to the current flight */
2496 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002497 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002498 else
2499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002501 while( cur->next != NULL )
2502 cur = cur->next;
2503 cur->next = msg;
2504 }
2505
2506 return( 0 );
2507}
2508
2509/*
2510 * Free the current flight of handshake messages
2511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 mbedtls_ssl_flight_item *cur = flight;
2515 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002516
2517 while( cur != NULL )
2518 {
2519 next = cur->next;
2520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 mbedtls_free( cur->p );
2522 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002523
2524 cur = next;
2525 }
2526}
2527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
2529static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002530#endif
2531
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002532/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002533 * Swap transform_out and out_ctr with the alternative ones
2534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002538 unsigned char tmp_out_ctr[8];
2539
2540 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002543 return;
2544 }
2545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002547
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002548 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002549 tmp_transform = ssl->transform_out;
2550 ssl->transform_out = ssl->handshake->alt_transform_out;
2551 ssl->handshake->alt_transform_out = tmp_transform;
2552
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002553 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002554 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2555 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2556 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002557
2558 /* Adjust to the newly activated transform */
2559 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002561 {
2562 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2563 ssl->transform_out->fixed_ivlen;
2564 }
2565 else
2566 ssl->out_msg = ssl->out_iv;
2567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2569 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2574 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002575 }
2576 }
2577#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002578}
2579
2580/*
2581 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002582 *
2583 * Need to remember the current message in case flush_output returns
2584 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002585 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002594
2595 ssl->handshake->cur_msg = ssl->handshake->flight;
2596 ssl_swap_epochs( ssl );
2597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002599 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002600
2601 while( ssl->handshake->cur_msg != NULL )
2602 {
2603 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002605
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002606 /* Swap epochs before sending Finished: we can't do it after
2607 * sending ChangeCipherSpec, in case write returns WANT_READ.
2608 * Must be done before copying, may change out_msg pointer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2610 cur->p[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002611 {
2612 ssl_swap_epochs( ssl );
2613 }
2614
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002615 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002616 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002617 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002618
2619 ssl->handshake->cur_msg = cur->next;
2620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002626 return( ret );
2627 }
2628 }
2629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2631 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002632 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002634 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002635 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2636 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002639
2640 return( 0 );
2641}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002642
2643/*
2644 * To be called when the last message of an incoming flight is received.
2645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002647{
2648 /* We won't need to resend that one any more */
2649 ssl_flight_free( ssl->handshake->flight );
2650 ssl->handshake->flight = NULL;
2651 ssl->handshake->cur_msg = NULL;
2652
2653 /* The next incoming flight will start with this msg_seq */
2654 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2655
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002656 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002657 ssl_set_timer( ssl, 0 );
2658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2660 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002663 }
2664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002666}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002667
2668/*
2669 * To be called when the last message of an outgoing flight is send.
2670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002672{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002673 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002674 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2677 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002680 }
2681 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002685
Paul Bakker5121ce52009-01-03 21:22:43 +00002686/*
2687 * Record layer functions
2688 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002689
2690/*
2691 * Write current record.
2692 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002695{
Paul Bakker05ef8352012-05-08 09:17:57 +00002696 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002697 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002702 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002703 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002705 {
2706 ; /* Skip special handshake treatment when resending */
2707 }
2708 else
2709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 {
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002712 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST &&
2713 ssl->handshake == NULL )
2714 {
2715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2717 }
2718
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2720 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2721 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2722
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002723 /*
2724 * DTLS has additional fields in the Handshake layer,
2725 * between the length field and the actual payload:
2726 * uint16 message_seq;
2727 * uint24 fragment_offset;
2728 * uint24 fragment_length;
2729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002731 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002732 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002733 /* Make room for the additional DTLS fields */
Hanno Becker0983dc42017-09-18 10:55:54 +01002734 if( MBEDTLS_SSL_MAX_CONTENT_LEN - ssl->out_msglen < 8 )
2735 {
2736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
2737 "size %u, maximum %u",
2738 (unsigned) ( ssl->in_hslen - 4 ),
2739 (unsigned) ( MBEDTLS_SSL_MAX_CONTENT_LEN - 12 ) ) );
2740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2741 }
2742
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002743 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002744 ssl->out_msglen += 8;
2745 len += 8;
2746
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002747 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002749 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002750 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2751 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2752 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002753 }
2754 else
2755 {
2756 ssl->out_msg[4] = 0;
2757 ssl->out_msg[5] = 0;
2758 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002759
2760 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2761 memset( ssl->out_msg + 6, 0x00, 3 );
2762 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002764#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002767 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 }
2769
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002770 /* Save handshake and CCS messages for resending */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002772 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002773 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING &&
2775 ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ||
2776 ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002777 {
2778 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002781 return( ret );
2782 }
2783 }
2784#endif
2785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002787 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002789 {
2790 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002793 return( ret );
2794 }
2795
2796 len = ssl->out_msglen;
2797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2801 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 ret = mbedtls_ssl_hw_record_write( ssl );
2806 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2809 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002810 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002811
2812 if( ret == 0 )
2813 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002816 if( !done )
2817 {
2818 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002820 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002821
2822 ssl->out_len[0] = (unsigned char)( len >> 8 );
2823 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002824
Paul Bakker48916f92012-09-16 19:57:18 +00002825 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002826 {
2827 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002830 return( ret );
2831 }
2832
2833 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002834 ssl->out_len[0] = (unsigned char)( len >> 8 );
2835 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002836 }
2837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Paul Bakker05ef8352012-05-08 09:17:57 +00002841 "version = [%d:%d], msglen = %d",
2842 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002843 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
2846 ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002847 }
2848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 return( ret );
2853 }
2854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
2857 return( 0 );
2858}
2859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002861/*
2862 * Mark bits in bitmask (used for DTLS HS reassembly)
2863 */
2864static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2865{
2866 unsigned int start_bits, end_bits;
2867
2868 start_bits = 8 - ( offset % 8 );
2869 if( start_bits != 8 )
2870 {
2871 size_t first_byte_idx = offset / 8;
2872
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002873 /* Special case */
2874 if( len <= start_bits )
2875 {
2876 for( ; len != 0; len-- )
2877 mask[first_byte_idx] |= 1 << ( start_bits - len );
2878
2879 /* Avoid potential issues with offset or len becoming invalid */
2880 return;
2881 }
2882
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002883 offset += start_bits; /* Now offset % 8 == 0 */
2884 len -= start_bits;
2885
2886 for( ; start_bits != 0; start_bits-- )
2887 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2888 }
2889
2890 end_bits = len % 8;
2891 if( end_bits != 0 )
2892 {
2893 size_t last_byte_idx = ( offset + len ) / 8;
2894
2895 len -= end_bits; /* Now len % 8 == 0 */
2896
2897 for( ; end_bits != 0; end_bits-- )
2898 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2899 }
2900
2901 memset( mask + offset / 8, 0xFF, len / 8 );
2902}
2903
2904/*
2905 * Check that bitmask is full
2906 */
2907static int ssl_bitmask_check( unsigned char *mask, size_t len )
2908{
2909 size_t i;
2910
2911 for( i = 0; i < len / 8; i++ )
2912 if( mask[i] != 0xFF )
2913 return( -1 );
2914
2915 for( i = 0; i < len % 8; i++ )
2916 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2917 return( -1 );
2918
2919 return( 0 );
2920}
2921
2922/*
2923 * Reassemble fragmented DTLS handshake messages.
2924 *
2925 * Use a temporary buffer for reassembly, divided in two parts:
2926 * - the first holds the reassembled message (including handshake header),
2927 * - the second holds a bitmask indicating which parts of the message
2928 * (excluding headers) have been received so far.
2929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002931{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002932 unsigned char *msg, *bitmask;
2933 size_t frag_len, frag_off;
2934 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2935
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002936 if( ssl->handshake == NULL )
2937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2939 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002940 }
2941
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002942 /*
2943 * For first fragment, check size and allocate buffer
2944 */
2945 if( ssl->handshake->hs_msg == NULL )
2946 {
2947 size_t alloc_len;
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002950 msg_len ) );
2951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2955 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002956 }
2957
2958 /* The bitmask needs one bit per byte of message excluding header */
2959 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2960
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002961 ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002962 if( ssl->handshake->hs_msg == NULL )
2963 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02002964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002965 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002966 }
2967
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002968 /* Prepare final header: copy msg_type, length and message_seq,
2969 * then add standardised fragment_offset and fragment_length */
2970 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2971 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2972 memcpy( ssl->handshake->hs_msg + 9,
2973 ssl->handshake->hs_msg + 1, 3 );
2974 }
2975 else
2976 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002977 /* Make sure msg_type and length are consistent */
2978 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2981 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002982 }
2983 }
2984
2985 msg = ssl->handshake->hs_msg + 12;
2986 bitmask = msg + msg_len;
2987
2988 /*
2989 * Check and copy current fragment
2990 */
2991 frag_off = ( ssl->in_msg[6] << 16 ) |
2992 ( ssl->in_msg[7] << 8 ) |
2993 ssl->in_msg[8];
2994 frag_len = ( ssl->in_msg[9] << 16 ) |
2995 ( ssl->in_msg[10] << 8 ) |
2996 ssl->in_msg[11];
2997
2998 if( frag_off + frag_len > msg_len )
2999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003001 frag_off, frag_len, msg_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003003 }
3004
3005 if( frag_len + 12 > ssl->in_msglen )
3006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003008 frag_len, ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
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, ( "adding fragment, offset = %d, length = %d",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003013 frag_off, frag_len ) );
3014
3015 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3016 ssl_bitmask_set( bitmask, frag_off, frag_len );
3017
3018 /*
3019 * Do we have the complete message by now?
3020 * If yes, finalize it, else ask to read the next record.
3021 */
3022 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
3023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003025 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003026 }
3027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003029
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003030 if( frag_len + 12 < ssl->in_msglen )
3031 {
3032 /*
3033 * We'got more handshake messages in the same record.
3034 * This case is not handled now because no know implementation does
3035 * that and it's hard to test, so we prefer to fail cleanly for now.
3036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
3038 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02003039 }
3040
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003041 if( ssl->in_left > ssl->next_record_offset )
3042 {
3043 /*
3044 * We've got more data in the buffer after the current record,
3045 * that we don't want to overwrite. Move it before writing the
3046 * reassembled message, and adjust in_left and next_record_offset.
3047 */
3048 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
3049 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
3050 size_t remain_len = ssl->in_left - ssl->next_record_offset;
3051
3052 /* First compute and check new lengths */
3053 ssl->next_record_offset = new_remain - ssl->in_hdr;
3054 ssl->in_left = ssl->next_record_offset + remain_len;
3055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN -
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003057 (size_t)( ssl->in_hdr - ssl->in_buf ) )
3058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
3060 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003061 }
3062
3063 memmove( new_remain, cur_remain, remain_len );
3064 }
3065
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003066 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
3067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 mbedtls_free( ssl->handshake->hs_msg );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003069 ssl->handshake->hs_msg = NULL;
3070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message",
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003072 ssl->in_msg, ssl->in_hslen );
3073
3074 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003075}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078static int ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003083 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003085 }
3086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + (
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003088 ( ssl->in_msg[1] << 16 ) |
3089 ( ssl->in_msg[2] << 8 ) |
3090 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003093 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003094 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003098 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003099 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003100 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003101
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003102 /* ssl->handshake is NULL when receiving ClientHello for renego */
3103 if( ssl->handshake != NULL &&
3104 recv_msg_seq != ssl->handshake->in_msg_seq )
3105 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003106 /* Retransmit only on last message from previous flight, to avoid
3107 * too many retransmissions.
3108 * Besides, No sane server ever retransmits HelloVerifyRequest */
3109 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003113 "message_seq = %d, start_of_flight = %d",
3114 recv_msg_seq,
3115 ssl->handshake->in_flight_start_seq ) );
3116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003120 return( ret );
3121 }
3122 }
3123 else
3124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003126 "message_seq = %d, expected = %d",
3127 recv_msg_seq,
3128 ssl->handshake->in_msg_seq ) );
3129 }
3130
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003131 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003132 }
3133 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003134
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003135 /* Reassemble if current message is fragmented or reassembly is
3136 * already in progress */
3137 if( ssl->in_msglen < ssl->in_hslen ||
3138 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3139 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3140 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003143
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003144 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003147 return( ret );
3148 }
3149 }
3150 }
3151 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003153 /* With TLS we don't handle fragmentation (for now) */
3154 if( ssl->in_msglen < ssl->in_hslen )
3155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3157 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003158 }
3159
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003160 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3161 ssl->handshake != NULL )
3162 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003163 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003164 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003165
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003166 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003168 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003169 ssl->handshake != NULL )
3170 {
3171 ssl->handshake->in_msg_seq++;
3172 }
3173#endif
3174
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003175 return( 0 );
3176}
3177
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003178/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003179 * DTLS anti-replay: RFC 6347 4.1.2.6
3180 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003181 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3182 * Bit n is set iff record number in_window_top - n has been seen.
3183 *
3184 * Usually, in_window_top is the last record number seen and the lsb of
3185 * in_window is set. The only exception is the initial state (record number 0
3186 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3189static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003190{
3191 ssl->in_window_top = 0;
3192 ssl->in_window = 0;
3193}
3194
3195static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3196{
3197 return( ( (uint64_t) buf[0] << 40 ) |
3198 ( (uint64_t) buf[1] << 32 ) |
3199 ( (uint64_t) buf[2] << 24 ) |
3200 ( (uint64_t) buf[3] << 16 ) |
3201 ( (uint64_t) buf[4] << 8 ) |
3202 ( (uint64_t) buf[5] ) );
3203}
3204
3205/*
3206 * Return 0 if sequence number is acceptable, -1 otherwise
3207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003209{
3210 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3211 uint64_t bit;
3212
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003213 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003214 return( 0 );
3215
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003216 if( rec_seqnum > ssl->in_window_top )
3217 return( 0 );
3218
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003219 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003220
3221 if( bit >= 64 )
3222 return( -1 );
3223
3224 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3225 return( -1 );
3226
3227 return( 0 );
3228}
3229
3230/*
3231 * Update replay window on new validated record
3232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003234{
3235 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3236
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003237 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003238 return;
3239
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003240 if( rec_seqnum > ssl->in_window_top )
3241 {
3242 /* Update window_top and the contents of the window */
3243 uint64_t shift = rec_seqnum - ssl->in_window_top;
3244
3245 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003246 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003247 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003248 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003249 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003250 ssl->in_window |= 1;
3251 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003252
3253 ssl->in_window_top = rec_seqnum;
3254 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003255 else
3256 {
3257 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003258 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003259
3260 if( bit < 64 ) /* Always true, but be extra sure */
3261 ssl->in_window |= (uint64_t) 1 << bit;
3262 }
3263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003265
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003266#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003267/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02003268static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
3269
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003270/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003271 * Without any SSL context, check if a datagram looks like a ClientHello with
3272 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003273 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003274 *
3275 * - if cookie is valid, return 0
3276 * - if ClientHello looks superficially valid but cookie is not,
3277 * fill obuf and set olen, then
3278 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3279 * - otherwise return a specific error code
3280 */
3281static int ssl_check_dtls_clihlo_cookie(
3282 mbedtls_ssl_cookie_write_t *f_cookie_write,
3283 mbedtls_ssl_cookie_check_t *f_cookie_check,
3284 void *p_cookie,
3285 const unsigned char *cli_id, size_t cli_id_len,
3286 const unsigned char *in, size_t in_len,
3287 unsigned char *obuf, size_t buf_len, size_t *olen )
3288{
3289 size_t sid_len, cookie_len;
3290 unsigned char *p;
3291
3292 if( f_cookie_write == NULL || f_cookie_check == NULL )
3293 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3294
3295 /*
3296 * Structure of ClientHello with record and handshake headers,
3297 * and expected values. We don't need to check a lot, more checks will be
3298 * done when actually parsing the ClientHello - skipping those checks
3299 * avoids code duplication and does not make cookie forging any easier.
3300 *
3301 * 0-0 ContentType type; copied, must be handshake
3302 * 1-2 ProtocolVersion version; copied
3303 * 3-4 uint16 epoch; copied, must be 0
3304 * 5-10 uint48 sequence_number; copied
3305 * 11-12 uint16 length; (ignored)
3306 *
3307 * 13-13 HandshakeType msg_type; (ignored)
3308 * 14-16 uint24 length; (ignored)
3309 * 17-18 uint16 message_seq; copied
3310 * 19-21 uint24 fragment_offset; copied, must be 0
3311 * 22-24 uint24 fragment_length; (ignored)
3312 *
3313 * 25-26 ProtocolVersion client_version; (ignored)
3314 * 27-58 Random random; (ignored)
3315 * 59-xx SessionID session_id; 1 byte len + sid_len content
3316 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3317 * ...
3318 *
3319 * Minimum length is 61 bytes.
3320 */
3321 if( in_len < 61 ||
3322 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3323 in[3] != 0 || in[4] != 0 ||
3324 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3325 {
3326 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3327 }
3328
3329 sid_len = in[59];
3330 if( sid_len > in_len - 61 )
3331 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3332
3333 cookie_len = in[60 + sid_len];
3334 if( cookie_len > in_len - 60 )
3335 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3336
3337 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3338 cli_id, cli_id_len ) == 0 )
3339 {
3340 /* Valid cookie */
3341 return( 0 );
3342 }
3343
3344 /*
3345 * If we get here, we've got an invalid cookie, let's prepare HVR.
3346 *
3347 * 0-0 ContentType type; copied
3348 * 1-2 ProtocolVersion version; copied
3349 * 3-4 uint16 epoch; copied
3350 * 5-10 uint48 sequence_number; copied
3351 * 11-12 uint16 length; olen - 13
3352 *
3353 * 13-13 HandshakeType msg_type; hello_verify_request
3354 * 14-16 uint24 length; olen - 25
3355 * 17-18 uint16 message_seq; copied
3356 * 19-21 uint24 fragment_offset; copied
3357 * 22-24 uint24 fragment_length; olen - 25
3358 *
3359 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3360 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3361 *
3362 * Minimum length is 28.
3363 */
3364 if( buf_len < 28 )
3365 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3366
3367 /* Copy most fields and adapt others */
3368 memcpy( obuf, in, 25 );
3369 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3370 obuf[25] = 0xfe;
3371 obuf[26] = 0xff;
3372
3373 /* Generate and write actual cookie */
3374 p = obuf + 28;
3375 if( f_cookie_write( p_cookie,
3376 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3377 {
3378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3379 }
3380
3381 *olen = p - obuf;
3382
3383 /* Go back and fill length fields */
3384 obuf[27] = (unsigned char)( *olen - 28 );
3385
3386 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3387 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3388 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3389
3390 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3391 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3392
3393 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3394}
3395
3396/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003397 * Handle possible client reconnect with the same UDP quadruplet
3398 * (RFC 6347 Section 4.2.8).
3399 *
3400 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3401 * that looks like a ClientHello.
3402 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003403 * - if the input looks like a ClientHello without cookies,
3404 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003405 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003406 * - if the input looks like a ClientHello with a valid cookie,
3407 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003408 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003409 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003410 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003411 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01003412 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
3413 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003414 */
3415static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3416{
3417 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003418 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003419
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003420 ret = ssl_check_dtls_clihlo_cookie(
3421 ssl->conf->f_cookie_write,
3422 ssl->conf->f_cookie_check,
3423 ssl->conf->p_cookie,
3424 ssl->cli_id, ssl->cli_id_len,
3425 ssl->in_buf, ssl->in_left,
3426 ssl->out_buf, MBEDTLS_SSL_MAX_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003427
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003428 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3429
3430 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003431 {
Brian J Murraye7f8dc32016-11-06 04:45:15 -08003432 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003433 * If the error is permanent we'll catch it later,
3434 * if it's not, then hopefully it'll work next time. */
3435 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3436
3437 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003438 }
3439
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003440 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003441 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003442 /* Got a valid cookie, partially reset context */
3443 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
3444 {
3445 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3446 return( ret );
3447 }
3448
3449 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003450 }
3451
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003452 return( ret );
3453}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003454#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003455
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003456/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003457 * ContentType type;
3458 * ProtocolVersion version;
3459 * uint16 epoch; // DTLS only
3460 * uint48 sequence_number; // DTLS only
3461 * uint16 length;
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003462 *
3463 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butchere103aa82015-12-16 01:51:01 +00003464 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003465 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3466 *
3467 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butchere103aa82015-12-16 01:51:01 +00003468 * 1. proceed with the record if this function returns 0
3469 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3470 * 3. return CLIENT_RECONNECT if this function returns that value
3471 * 4. drop the whole datagram if this function returns anything else.
3472 * Point 2 is needed when the peer is resending, and we have already received
3473 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003476{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003477 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 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 +02003480
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003482 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003483 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00003486 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003487 ssl->in_msgtype,
3488 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003489
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003490 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
3492 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
3493 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3494 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003497
Andres Amaya Garcia1042d862017-07-05 13:26:34 +01003498#if defined(MBEDTLS_SSL_PROTO_DTLS)
3499 /* Silently ignore invalid DTLS records as recommended by RFC 6347
3500 * Section 4.1.2.7 */
3501 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3502#endif /* MBEDTLS_SSL_PROTO_DTLS */
3503 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3504 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003507 }
3508
3509 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003510 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3513 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003514 }
3515
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003516 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3519 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 }
3521
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003522 /* Check length against the size of our buffer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003524 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3527 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003528 }
3529
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003530 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003531 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003532 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003533 if( ssl->in_msglen < 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 ssl->in_msglen > 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 }
3539 }
3540 else
3541 {
Paul Bakker48916f92012-09-16 19:57:18 +00003542 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3545 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 }
3547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548#if defined(MBEDTLS_SSL_PROTO_SSL3)
3549 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3550 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3553 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003554 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003555#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3557 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 /*
3559 * TLS encrypted messages can have up to 256 bytes of padding
3560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003562 ssl->in_msglen > ssl->transform_in->minlen +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 MBEDTLS_SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3566 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003567 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003568#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 }
3570
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003571 /*
3572 * DTLS-related tests done last, because most of them may result in
3573 * silently dropping the record (but not the whole datagram), and we only
3574 * want to consider that after ensuring that the "basic" fields (type,
3575 * version, length) are sane.
3576 */
3577#if defined(MBEDTLS_SSL_PROTO_DTLS)
3578 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3579 {
3580 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3581
3582 /* Drop unexpected ChangeCipherSpec messages */
3583 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3584 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3585 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3586 {
3587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3588 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3589 }
3590
3591 /* Drop unexpected ApplicationData records,
3592 * except at the beginning of renegotiations */
3593 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
3594 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
3595#if defined(MBEDTLS_SSL_RENEGOTIATION)
3596 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
3597 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
3598#endif
3599 )
3600 {
3601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3602 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3603 }
3604
3605 /* Check epoch (and sequence number) with DTLS */
3606 if( rec_epoch != ssl->in_epoch )
3607 {
3608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
3609 "expected %d, received %d",
3610 ssl->in_epoch, rec_epoch ) );
3611
3612#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3613 /*
3614 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3615 * access the first byte of record content (handshake type), as we
3616 * have an active transform (possibly iv_len != 0), so use the
3617 * fact that the record header len is 13 instead.
3618 */
3619 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3620 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3621 rec_epoch == 0 &&
3622 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3623 ssl->in_left > 13 &&
3624 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3625 {
3626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3627 "from the same port" ) );
3628 return( ssl_handle_possible_reconnect( ssl ) );
3629 }
3630 else
3631#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
3632 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3633 }
3634
3635#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3636 /* Replay detection only works for the current epoch */
3637 if( rec_epoch == ssl->in_epoch &&
3638 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
3639 {
3640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3641 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3642 }
3643#endif
3644 }
3645#endif /* MBEDTLS_SSL_PROTO_DTLS */
3646
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003647 return( 0 );
3648}
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003650/*
3651 * If applicable, decrypt (and decompress) record content
3652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003654{
3655 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
3658 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3661 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 ret = mbedtls_ssl_hw_record_read( ssl );
3666 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3669 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003670 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003671
3672 if( ret == 0 )
3673 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003676 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 {
3678 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 return( ret );
3682 }
3683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 ssl->in_msg, ssl->in_msglen );
3686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687 if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3690 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003691 }
3692 }
3693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003695 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003697 {
3698 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003701 return( ret );
3702 }
3703
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003704 // TODO: what's the purpose of these lines? is in_len used?
3705 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3706 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003707 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003714 }
3715#endif
3716
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003717 return( 0 );
3718}
3719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003721
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003722/*
3723 * Read a record.
3724 *
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02003725 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3726 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3727 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003729int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003730{
3731 int ret;
3732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003734
Hanno Becker704f4932017-06-08 13:08:45 +01003735 if( ssl->keep_current_message == 1 )
3736 {
3737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
3738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3739 ssl->keep_current_message = 0;
3740
3741 return( 0 );
3742 }
3743
Hanno Becker6a582e82017-06-08 13:38:05 +01003744 /*
3745 * Step A
3746 *
3747 * Consume last content-layer message and potentially
3748 * update in_msglen which keeps track of the contents'
3749 * consumption state.
3750 *
3751 * (1) Handshake messages:
3752 * Remove last handshake message, move content
3753 * and adapt in_msglen.
3754 *
3755 * (2) Alert messages:
3756 * Consume whole record content, in_msglen = 0.
3757 *
3758 * NOTE: This needs to be fixed, since like for
3759 * handshake messages it is allowed to have
3760 * multiple alerts witin a single record.
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003761 * Internal reference IOTSSL-1321.
Hanno Becker6a582e82017-06-08 13:38:05 +01003762 *
3763 * (3) Change cipher spec:
3764 * Consume whole record content, in_msglen = 0.
3765 *
3766 * (4) Application data:
3767 * Don't do anything - the record layer provides
3768 * the application data as a stream transport
3769 * and consumes through mbedtls_ssl_read only.
3770 *
3771 */
3772
3773 /* Case (1): Handshake messages */
3774
3775 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003776 {
Hanno Beckerbfbc4942017-06-08 13:39:23 +01003777 if( ssl->in_offt != NULL )
3778 {
3779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3781 }
3782
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003783 /*
3784 * Get next Handshake message in the current record
3785 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003786
Hanno Becker6a582e82017-06-08 13:38:05 +01003787 /* Notes:
3788 * (1) in_hslen is *NOT* necessarily the size of the
3789 * current handshake content: If DTLS handshake
3790 * fragmentation is used, that's the fragment
3791 * size instead. Using the total handshake message
3792 * size here is FAULTY and should be changed at
3793 * some point. Internal reference IOTSSL-1414.
3794 * (2) While it doesn't seem to cause problems, one
3795 * has to be very careful not to assume that in_hslen
3796 * is always <= in_msglen in a sensible communication.
3797 * Again, it's wrong for DTLS handshake fragmentation.
3798 * The following check is therefore mandatory, and
3799 * should not be treated as a silently corrected assertion.
3800 */
3801 if( ssl->in_hslen < ssl->in_msglen )
3802 {
3803 ssl->in_msglen -= ssl->in_hslen;
3804 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3805 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003806
Hanno Becker6a582e82017-06-08 13:38:05 +01003807 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
3808 ssl->in_msg, ssl->in_msglen );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003809
Hanno Becker6a582e82017-06-08 13:38:05 +01003810 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3811 return( ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003812
Hanno Becker6a582e82017-06-08 13:38:05 +01003813 return( 0 );
3814 }
3815 else
3816 {
3817 ssl->in_msglen = 0;
3818 }
3819
3820 ssl->in_hslen = 0;
3821 }
3822 /* Case (4): Application data */
3823 else if( ssl->in_offt != NULL )
3824 {
3825 return( 0 );
3826 }
3827 /* Everything else (CCS & Alerts) */
3828 else
3829 {
3830 ssl->in_msglen = 0;
3831 }
3832
3833 /*
3834 * Step B
3835 *
3836 * Fetch and decode new record if current one is fully consumed.
3837 *
3838 */
3839
3840 if( ssl->in_msglen > 0 )
3841 {
3842 /* There's something left to be processed in the current record. */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003843 return( 0 );
3844 }
3845
Hanno Becker6a582e82017-06-08 13:38:05 +01003846 /* Need to fetch a new record */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003847
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003848read_record_header:
Hanno Becker6a582e82017-06-08 13:38:05 +01003849
3850 /* Current record either fully processed or to be discarded. */
3851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003855 return( ret );
3856 }
3857
3858 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3862 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003863 {
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003864 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
3865 {
3866 /* Skip unexpected record (but not whole datagram) */
3867 ssl->next_record_offset = ssl->in_msglen
3868 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003869
Manuel Pégourié-Gonnard013198f2015-12-03 16:13:17 +01003870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
3871 "(header)" ) );
3872 }
3873 else
3874 {
3875 /* Skip invalid record and the rest of the datagram */
3876 ssl->next_record_offset = 0;
3877 ssl->in_left = 0;
3878
3879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
3880 "(header)" ) );
3881 }
3882
3883 /* Get next record */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003884 goto read_record_header;
3885 }
3886#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003887 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003888 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003889
3890 /*
3891 * Read and optionally decrypt the message contents
3892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 if( ( ret = mbedtls_ssl_fetch_input( ssl,
3894 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003897 return( ret );
3898 }
3899
3900 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003902 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003904 else
3905#endif
3906 ssl->in_left = 0;
3907
3908 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003912 {
3913 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
3915 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003916 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02003917 /* Except when waiting for Finished as a bad mac here
3918 * probably means something went wrong in the handshake
3919 * (eg wrong psk used, mitm downgrade attempt, etc.) */
3920 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
3921 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
3922 {
3923#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
3924 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
3925 {
3926 mbedtls_ssl_send_alert_message( ssl,
3927 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3928 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
3929 }
3930#endif
3931 return( ret );
3932 }
3933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003935 if( ssl->conf->badmac_limit != 0 &&
3936 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3939 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003940 }
3941#endif
3942
Hanno Becker6a582e82017-06-08 13:38:05 +01003943 /* As above, invalid records cause
3944 * dismissal of the whole datagram. */
3945
3946 ssl->next_record_offset = 0;
3947 ssl->in_left = 0;
3948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003950 goto read_record_header;
3951 }
3952
3953 return( ret );
3954 }
3955 else
3956#endif
3957 {
3958 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
3960 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 mbedtls_ssl_send_alert_message( ssl,
3963 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3964 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003965 }
3966#endif
3967 return( ret );
3968 }
3969 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003970
3971 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003972 * When we sent the last flight of the handshake, we MUST respond to a
3973 * retransmit of the peer's previous flight with a retransmit. (In
3974 * practice, only the Finished message will make it, other messages
3975 * including CCS use the old transform so they're dropped as invalid.)
3976 *
3977 * If the record we received is not a handshake message, however, it
3978 * means the peer received our last flight so we can clean up
3979 * handshake info.
3980 *
3981 * This check needs to be done before prepare_handshake() due to an edge
3982 * case: if the client immediately requests renegotiation, this
3983 * finishes the current handshake first, avoiding the new ClientHello
3984 * being mistaken for an ancient message in the current handshake.
3985 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003988 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3992 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003999 return( ret );
4000 }
4001
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01004002 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004003 }
4004 else
4005 {
4006 ssl_handshake_wrapup_free_hs_transform( ssl );
4007 }
4008 }
4009#endif
4010
4011 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004012 * Handle particular types of records
4013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004016 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
4017 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 }
4019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004023 ssl->in_msg[0], ssl->in_msg[1] ) );
4024
4025 /*
Simon Butcher94c5e3c2015-10-27 16:09:03 +00004026 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004028 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004031 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004033 }
4034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4036 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4039 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 }
Manuel Pégourié-Gonnarda3140762015-10-23 11:13:28 +02004041
4042#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4043 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4044 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4045 {
4046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4047 /* Will be handled when trying to parse ServerHello */
4048 return( 0 );
4049 }
4050#endif
4051
4052#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
4053 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4054 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4055 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4056 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
4057 {
4058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
4059 /* Will be handled in mbedtls_ssl_parse_certificate() */
4060 return( 0 );
4061 }
4062#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4063
4064 /* Silently ignore: fetch new message */
4065 goto read_record_header;
Paul Bakker5121ce52009-01-03 21:22:43 +00004066 }
4067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004069
4070 return( 0 );
4071}
4072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004074{
4075 int ret;
4076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4078 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4079 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004080 {
4081 return( ret );
4082 }
4083
4084 return( 0 );
4085}
4086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004088 unsigned char level,
4089 unsigned char message )
4090{
4091 int ret;
4092
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004093 if( ssl == NULL || ssl->conf == NULL )
4094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004099 ssl->out_msglen = 2;
4100 ssl->out_msg[0] = level;
4101 ssl->out_msg[1] = message;
4102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004106 return( ret );
4107 }
4108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004110
4111 return( 0 );
4112}
4113
Paul Bakker5121ce52009-01-03 21:22:43 +00004114/*
4115 * Handshake functions
4116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117#if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
4118 !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
4119 !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
4120 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
4121 !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
4122 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
4123 !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
4124int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00004127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4131 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4132 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004135 ssl->state++;
4136 return( 0 );
4137 }
4138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004141}
4142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4150 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4151 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004154 ssl->state++;
4155 return( 0 );
4156 }
4157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004160}
4161#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004165 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 const mbedtls_x509_crt *crt;
4167 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004171 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4172 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4173 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02004176 ssl->state++;
4177 return( 0 );
4178 }
4179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004181 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004182 {
4183 if( ssl->client_auth == 0 )
4184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004186 ssl->state++;
4187 return( 0 );
4188 }
4189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 /*
4192 * If using SSLv3 and got no cert, send an Alert message
4193 * (otherwise an empty Certificate message will be sent).
4194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
4196 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004197 {
4198 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
4200 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
4201 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00004202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004204 goto write_msg;
4205 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208#endif /* MBEDTLS_SSL_CLI_C */
4209#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004210 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
4215 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004216 }
4217 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004218#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004221
4222 /*
4223 * 0 . 0 handshake type
4224 * 1 . 3 handshake length
4225 * 4 . 6 length of all certs
4226 * 7 . 9 length of cert. 1
4227 * 10 . n-1 peer certificate
4228 * n . n+2 length of cert. 2
4229 * n+3 . ... upper level cert, etc.
4230 */
4231 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004232 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004233
Paul Bakker29087132010-03-21 21:03:34 +00004234 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004235 {
4236 n = crt->raw.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00004238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
4240 i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) );
4241 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 }
4243
4244 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
4245 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
4246 ssl->out_msg[i + 2] = (unsigned char)( n );
4247
4248 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
4249 i += n; crt = crt->next;
4250 }
4251
4252 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
4253 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
4254 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
4255
4256 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4258 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004259
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004260#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004261write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004263
4264 ssl->state++;
4265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004269 return( ret );
4270 }
4271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
Paul Bakkered27a042013-04-18 22:46:23 +02004274 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004275}
4276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004279 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00004280 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004282 int authmode = ssl->conf->authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004286 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
4287 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
4288 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004291 ssl->state++;
4292 return( 0 );
4293 }
4294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004296 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004297 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
4298 {
4299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
4300 ssl->state++;
4301 return( 0 );
4302 }
4303
4304#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4305 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
4306 authmode = ssl->handshake->sni_authmode;
4307#endif
4308
4309 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4310 authmode == MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004312 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 ssl->state++;
4315 return( 0 );
4316 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004319 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 return( ret );
4323 }
4324
4325 ssl->state++;
4326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327#if defined(MBEDTLS_SSL_SRV_C)
4328#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 /*
4330 * Check if the client sent an empty certificate
4331 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004332 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00004335 if( ssl->in_msglen == 2 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4337 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4338 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004341
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004342 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004343 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004344 return( 0 );
4345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004347 }
4348 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4352 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004353 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
4357 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4358 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
4359 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004362
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01004363 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004364 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 return( 0 );
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004366 else
4367 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 }
4369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
4371 MBEDTLS_SSL_PROTO_TLS1_2 */
4372#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4377 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004378 }
4379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004380 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
4381 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4384 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004385 }
4386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004387 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004388
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00004391 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004392 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00004393
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004394 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004395 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4398 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004399 }
4400
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004401 /* In case we tried to reuse a session but it failed */
4402 if( ssl->session_negotiate->peer_cert != NULL )
4403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
4405 mbedtls_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02004406 }
4407
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004408 if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 sizeof( mbedtls_x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004410 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 sizeof( mbedtls_x509_crt ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004413 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004414 }
4415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004417
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00004418 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00004419
4420 while( i < ssl->in_hslen )
4421 {
4422 if( ssl->in_msg[i] != 0 )
4423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4425 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004426 }
4427
4428 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
4429 | (unsigned int) ssl->in_msg[i + 2];
4430 i += 3;
4431
4432 if( n < 128 || i + n > ssl->in_hslen )
4433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
4435 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004436 }
4437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Paul Bakkerddf26b42013-09-18 13:46:23 +02004439 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004440 if( ret != 0 )
4441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004443 return( ret );
4444 }
4445
4446 i += n;
4447 }
4448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00004450
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004451 /*
4452 * On client, make sure the server cert doesn't change during renego to
4453 * avoid "triple handshake" attack: https://secure-resumption.com/
4454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004456 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004458 {
4459 if( ssl->session->peer_cert == NULL )
4460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
4462 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004463 }
4464
4465 if( ssl->session->peer_cert->raw.len !=
4466 ssl->session_negotiate->peer_cert->raw.len ||
4467 memcmp( ssl->session->peer_cert->raw.p,
4468 ssl->session_negotiate->peer_cert->raw.p,
4469 ssl->session->peer_cert->raw.len ) != 0 )
4470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
4472 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004473 }
4474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01004476
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004477 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004479 mbedtls_x509_crt *ca_chain;
4480 mbedtls_x509_crl *ca_crl;
4481
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004482#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004483 if( ssl->handshake->sni_ca_chain != NULL )
4484 {
4485 ca_chain = ssl->handshake->sni_ca_chain;
4486 ca_crl = ssl->handshake->sni_ca_crl;
4487 }
4488 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02004489#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004490 {
4491 ca_chain = ssl->conf->ca_chain;
4492 ca_crl = ssl->conf->ca_crl;
4493 }
4494
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004495 /*
4496 * Main check: verify certificate
4497 */
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004498 ret = mbedtls_x509_crt_verify_with_profile(
4499 ssl->session_negotiate->peer_cert,
4500 ca_chain, ca_crl,
4501 ssl->conf->cert_profile,
4502 ssl->hostname,
4503 &ssl->session_negotiate->verify_result,
4504 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00004505
4506 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004509 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004510
4511 /*
4512 * Secondary checks: always done, but change 'ret' only if it was 0
4513 */
4514
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004515#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004517 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004518
4519 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004520 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02004521 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004522 {
Hanno Beckera3929ba2017-05-08 16:31:14 +01004523 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
4524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004526 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004528 }
4529 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004530#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Beckera3929ba2017-05-08 16:31:14 +01004533 ciphersuite_info,
4534 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01004535 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004538 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004540 }
4541
Hanno Beckera3929ba2017-05-08 16:31:14 +01004542 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
4543 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
4544 * with details encoded in the verification flags. All other kinds
4545 * of error codes, including those from the user provided f_vrfy
4546 * functions, are treated as fatal and lead to a failure of
4547 * ssl_parse_certificate even if verification was optional. */
4548 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
4549 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
4550 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
4551 {
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 ret = 0;
Hanno Beckera3929ba2017-05-08 16:31:14 +01004553 }
4554
4555 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
4556 {
4557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
4558 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
4559 }
4560
Hanno Becker61c0c702017-05-15 16:05:15 +01004561#if defined(MBEDTLS_DEBUG_C)
4562 if( ssl->session_negotiate->verify_result != 0 )
4563 {
4564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
4565 ssl->session_negotiate->verify_result ) );
4566 }
4567 else
4568 {
4569 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
4570 }
4571#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004572 }
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004575
4576 return( ret );
4577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578#endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
4579 !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
4580 !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
4581 !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4582 !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4583 !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
4584 !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004587{
4588 int ret;
4589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004592 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 ssl->out_msglen = 1;
4594 ssl->out_msg[0] = 1;
4595
Paul Bakker5121ce52009-01-03 21:22:43 +00004596 ssl->state++;
4597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 return( ret );
4602 }
4603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004605
4606 return( 0 );
4607}
4608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004610{
4611 int ret;
4612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 return( ret );
4619 }
4620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4624 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004625 }
4626
4627 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
4630 return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 }
4632
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004633 /*
4634 * Switch to our negotiated transform and session parameters for inbound
4635 * data.
4636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004638 ssl->transform_in = ssl->transform_negotiate;
4639 ssl->session_in = ssl->session_negotiate;
4640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004642 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004645 ssl_dtls_replay_reset( ssl );
4646#endif
4647
4648 /* Increment epoch */
4649 if( ++ssl->in_epoch == 0 )
4650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4652 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004653 }
4654 }
4655 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004657 memset( ssl->in_ctr, 0, 8 );
4658
4659 /*
4660 * Set the in_msg pointer to the correct location based on IV length
4661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004663 {
4664 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4665 ssl->transform_negotiate->fixed_ivlen;
4666 }
4667 else
4668 ssl->in_msg = ssl->in_iv;
4669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4671 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004673 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4676 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004677 }
4678 }
4679#endif
4680
Paul Bakker5121ce52009-01-03 21:22:43 +00004681 ssl->state++;
4682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004684
4685 return( 0 );
4686}
4687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
4689 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004690{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004691 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4694 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4695 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004696 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004697 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4700#if defined(MBEDTLS_SHA512_C)
4701 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004702 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4703 else
4704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705#if defined(MBEDTLS_SHA256_C)
4706 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004707 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004708 else
4709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004713 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004714 }
Paul Bakker380da532012-04-18 16:10:25 +00004715}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4720 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4721 mbedtls_md5_starts( &ssl->handshake->fin_md5 );
4722 mbedtls_sha1_starts( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4725#if defined(MBEDTLS_SHA256_C)
4726 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728#if defined(MBEDTLS_SHA512_C)
4729 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004732}
4733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004735 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4738 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4739 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4740 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4743#if defined(MBEDTLS_SHA256_C)
4744 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004745#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746#if defined(MBEDTLS_SHA512_C)
4747 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004748#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004750}
4751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
4753 defined(MBEDTLS_SSL_PROTO_TLS1_1)
4754static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004755 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757 mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len );
4758 mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004759}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004760#endif
Paul Bakker380da532012-04-18 16:10:25 +00004761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4763#if defined(MBEDTLS_SHA256_C)
4764static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004765 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767 mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004768}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004769#endif
Paul Bakker380da532012-04-18 16:10:25 +00004770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004771#if defined(MBEDTLS_SHA512_C)
4772static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004773 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004776}
Paul Bakker769075d2012-11-24 11:26:46 +01004777#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004778#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004781static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004783{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004784 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004785 mbedtls_md5_context md5;
4786 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004787
Paul Bakker5121ce52009-01-03 21:22:43 +00004788 unsigned char padbuf[48];
4789 unsigned char md5sum[16];
4790 unsigned char sha1sum[20];
4791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004793 if( !session )
4794 session = ssl->session;
4795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004797
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004798 mbedtls_md5_init( &md5 );
4799 mbedtls_sha1_init( &sha1 );
4800
4801 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
4802 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004803
4804 /*
4805 * SSLv3:
4806 * hash =
4807 * MD5( master + pad2 +
4808 * MD5( handshake + sender + master + pad1 ) )
4809 * + SHA1( master + pad2 +
4810 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004811 */
4812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004814 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4815 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004819 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4820 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004823 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02004824 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004825
Paul Bakker1ef83d62012-04-11 12:09:53 +00004826 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004827
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004828 mbedtls_md5_update( &md5, (const unsigned char *) sender, 4 );
4829 mbedtls_md5_update( &md5, session->master, 48 );
4830 mbedtls_md5_update( &md5, padbuf, 48 );
4831 mbedtls_md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004832
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004833 mbedtls_sha1_update( &sha1, (const unsigned char *) sender, 4 );
4834 mbedtls_sha1_update( &sha1, session->master, 48 );
4835 mbedtls_sha1_update( &sha1, padbuf, 40 );
4836 mbedtls_sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004837
Paul Bakker1ef83d62012-04-11 12:09:53 +00004838 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004839
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004840 mbedtls_md5_starts( &md5 );
4841 mbedtls_md5_update( &md5, session->master, 48 );
4842 mbedtls_md5_update( &md5, padbuf, 48 );
4843 mbedtls_md5_update( &md5, md5sum, 16 );
4844 mbedtls_md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004845
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004846 mbedtls_sha1_starts( &sha1 );
4847 mbedtls_sha1_update( &sha1, session->master, 48 );
4848 mbedtls_sha1_update( &sha1, padbuf , 40 );
4849 mbedtls_sha1_update( &sha1, sha1sum, 20 );
4850 mbedtls_sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004853
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004854 mbedtls_md5_free( &md5 );
4855 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
4858 mbedtls_zeroize( md5sum, sizeof( md5sum ) );
4859 mbedtls_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004866static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004868{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004869 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004870 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004871 mbedtls_md5_context md5;
4872 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004873 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004876 if( !session )
4877 session = ssl->session;
4878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004880
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004881 mbedtls_md5_init( &md5 );
4882 mbedtls_sha1_init( &sha1 );
4883
4884 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
4885 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004886
Paul Bakker1ef83d62012-04-11 12:09:53 +00004887 /*
4888 * TLSv1:
4889 * hash = PRF( master, finished_label,
4890 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4891 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004894 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4895 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004896#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004899 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4900 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004901#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004904 ? "client finished"
4905 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004906
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004907 mbedtls_md5_finish( &md5, padbuf );
4908 mbedtls_sha1_finish( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004909
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004910 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004911 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004914
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004915 mbedtls_md5_free( &md5 );
4916 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004922#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4925#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004926static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004927 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00004928{
4929 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004930 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004931 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004932 unsigned char padbuf[32];
4933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004935 if( !session )
4936 session = ssl->session;
4937
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004938 mbedtls_sha256_init( &sha256 );
4939
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004940 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004941
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004942 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004943
4944 /*
4945 * TLSv1.2:
4946 * hash = PRF( master, finished_label,
4947 * Hash( handshake ) )[0.11]
4948 */
4949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950#if !defined(MBEDTLS_SHA256_ALT)
4951 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004952 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004953#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004955 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004956 ? "client finished"
4957 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004958
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004959 mbedtls_sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004960
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004961 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004962 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004965
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004966 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004975static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00004977{
4978 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004979 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02004980 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004981 unsigned char padbuf[48];
4982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004984 if( !session )
4985 session = ssl->session;
4986
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004987 mbedtls_sha512_init( &sha512 );
4988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004990
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02004991 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004992
4993 /*
4994 * TLSv1.2:
4995 * hash = PRF( master, finished_label,
4996 * Hash( handshake ) )[0.11]
4997 */
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005000 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
5001 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02005002#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00005003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02005005 ? "client finished"
5006 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00005007
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005008 mbedtls_sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005009
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02005010 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00005011 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005014
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02005015 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 mbedtls_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00005020}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021#endif /* MBEDTLS_SHA512_C */
5022#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00005023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005027
5028 /*
5029 * Free our handshake params
5030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005031 mbedtls_ssl_handshake_free( ssl->handshake );
5032 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00005033 ssl->handshake = NULL;
5034
5035 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005036 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00005037 */
5038 if( ssl->transform )
5039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040 mbedtls_ssl_transform_free( ssl->transform );
5041 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005042 }
5043 ssl->transform = ssl->transform_negotiate;
5044 ssl->transform_negotiate = NULL;
5045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005047}
5048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005049void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005050{
5051 int resume = ssl->handshake->resume;
5052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055#if defined(MBEDTLS_SSL_RENEGOTIATION)
5056 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005059 ssl->renego_records_seen = 0;
5060 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005061#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005062
5063 /*
5064 * Free the previous session and switch in the current one
5065 */
Paul Bakker0a597072012-09-25 21:55:46 +00005066 if( ssl->session )
5067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01005069 /* RFC 7366 3.1: keep the EtM state */
5070 ssl->session_negotiate->encrypt_then_mac =
5071 ssl->session->encrypt_then_mac;
5072#endif
5073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 mbedtls_ssl_session_free( ssl->session );
5075 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00005076 }
5077 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00005078 ssl->session_negotiate = NULL;
5079
Paul Bakker0a597072012-09-25 21:55:46 +00005080 /*
5081 * Add cache entry
5082 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005083 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02005084 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005085 resume == 0 )
5086 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005087 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02005089 }
Paul Bakker0a597072012-09-25 21:55:46 +00005090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005092 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005093 ssl->handshake->flight != NULL )
5094 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005095 /* Cancel handshake timer */
5096 ssl_set_timer( ssl, 0 );
5097
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005098 /* Keep last flight around in case we need to resend it:
5099 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005101 }
5102 else
5103#endif
5104 ssl_handshake_wrapup_free_hs_transform( ssl );
5105
Paul Bakker48916f92012-09-16 19:57:18 +00005106 ssl->state++;
5107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005109}
5110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00005112{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005113 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00005114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005116
Paul Bakker92be97b2013-01-02 17:30:03 +01005117 /*
5118 * Set the out_msg pointer to the correct location based on IV length
5119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker92be97b2013-01-02 17:30:03 +01005121 {
5122 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
5123 ssl->transform_negotiate->fixed_ivlen;
5124 }
5125 else
5126 ssl->out_msg = ssl->out_iv;
5127
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005128 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00005129
5130 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005134 ssl->verify_data_len = hash_len;
5135 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005136#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005137
Paul Bakker5121ce52009-01-03 21:22:43 +00005138 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5140 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005141
5142 /*
5143 * In case of session resuming, invert the client and server
5144 * ChangeCipherSpec messages order.
5145 */
Paul Bakker0a597072012-09-25 21:55:46 +00005146 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005149 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005153 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005155#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005156 }
5157 else
5158 ssl->state++;
5159
Paul Bakker48916f92012-09-16 19:57:18 +00005160 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005161 * Switch to our negotiated transform and session parameters for outbound
5162 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00005163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01005165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005167 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005168 {
5169 unsigned char i;
5170
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005171 /* Remember current epoch settings for resending */
5172 ssl->handshake->alt_transform_out = ssl->transform_out;
5173 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
5174
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005175 /* Set sequence_number to zero */
5176 memset( ssl->out_ctr + 2, 0, 6 );
5177
5178 /* Increment epoch */
5179 for( i = 2; i > 0; i-- )
5180 if( ++ssl->out_ctr[i - 1] != 0 )
5181 break;
5182
5183 /* The loop goes to its end iff the counter is wrapping */
5184 if( i == 0 )
5185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
5187 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005188 }
5189 }
5190 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005192 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005193
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005194 ssl->transform_out = ssl->transform_negotiate;
5195 ssl->session_out = ssl->session_negotiate;
5196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005197#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5198 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01005201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
5203 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005204 }
5205 }
5206#endif
5207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005209 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02005211#endif
5212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005216 return( ret );
5217 }
5218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005220
5221 return( 0 );
5222}
5223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005225#define SSL_MAX_HASH_LEN 36
5226#else
5227#define SSL_MAX_HASH_LEN 12
5228#endif
5229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005231{
Paul Bakker23986e52011-04-24 08:57:21 +00005232 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02005233 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005234 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00005235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005237
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005238 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005243 return( ret );
5244 }
5245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005246 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5249 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005250 }
5251
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005252 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005253#if defined(MBEDTLS_SSL_PROTO_SSL3)
5254 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00005255 hash_len = 36;
5256 else
5257#endif
5258 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00005259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005260 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
5261 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5264 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005265 }
5266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00005268 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
5271 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005272 }
5273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00005275 ssl->verify_data_len = hash_len;
5276 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005277#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005278
Paul Bakker0a597072012-09-25 21:55:46 +00005279 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005282 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005284#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005286 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005289 }
5290 else
5291 ssl->state++;
5292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005295 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005296#endif
5297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005299
5300 return( 0 );
5301}
5302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
5308 defined(MBEDTLS_SSL_PROTO_TLS1_1)
5309 mbedtls_md5_init( &handshake->fin_md5 );
5310 mbedtls_sha1_init( &handshake->fin_sha1 );
5311 mbedtls_md5_starts( &handshake->fin_md5 );
5312 mbedtls_sha1_starts( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5315#if defined(MBEDTLS_SHA256_C)
5316 mbedtls_sha256_init( &handshake->fin_sha256 );
5317 mbedtls_sha256_starts( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319#if defined(MBEDTLS_SHA512_C)
5320 mbedtls_sha512_init( &handshake->fin_sha512 );
5321 mbedtls_sha512_starts( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005324
5325 handshake->update_checksum = ssl_update_checksum_start;
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01005326
5327#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
5328 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
5329 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
5330#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332#if defined(MBEDTLS_DHM_C)
5333 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335#if defined(MBEDTLS_ECDH_C)
5336 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005337#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005338
5339#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5340 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
5341#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005342}
5343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344static void ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 mbedtls_cipher_init( &transform->cipher_ctx_enc );
5349 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02005350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005351 mbedtls_md_init( &transform->md_ctx_enc );
5352 mbedtls_md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005353}
5354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005356{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005358}
5359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005361{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005362 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00005363 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005365 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005367 if( ssl->handshake )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 mbedtls_ssl_handshake_free( ssl->handshake );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005369
5370 /*
5371 * Either the pointers are now NULL or cleared properly and can be freed.
5372 * Now allocate missing structures.
5373 */
5374 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005375 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005376 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005377 }
Paul Bakker48916f92012-09-16 19:57:18 +00005378
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005379 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005380 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005381 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005382 }
Paul Bakker48916f92012-09-16 19:57:18 +00005383
Paul Bakker82788fb2014-10-20 13:59:19 +02005384 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005385 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005386 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005387 }
Paul Bakker48916f92012-09-16 19:57:18 +00005388
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005389 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00005390 if( ssl->handshake == NULL ||
5391 ssl->transform_negotiate == NULL ||
5392 ssl->session_negotiate == NULL )
5393 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 mbedtls_free( ssl->handshake );
5397 mbedtls_free( ssl->transform_negotiate );
5398 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005399
5400 ssl->handshake = NULL;
5401 ssl->transform_negotiate = NULL;
5402 ssl->session_negotiate = NULL;
5403
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00005405 }
5406
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005407 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408 mbedtls_ssl_session_init( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005409 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02005410 ssl_handshake_params_init( ssl->handshake );
5411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005412#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005413 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5414 {
5415 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005416
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005417 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5418 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
5419 else
5420 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005421
5422 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02005423 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02005424#endif
5425
Paul Bakker48916f92012-09-16 19:57:18 +00005426 return( 0 );
5427}
5428
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005429#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005430/* Dummy cookie callbacks for defaults */
5431static int ssl_cookie_write_dummy( void *ctx,
5432 unsigned char **p, unsigned char *end,
5433 const unsigned char *cli_id, size_t cli_id_len )
5434{
5435 ((void) ctx);
5436 ((void) p);
5437 ((void) end);
5438 ((void) cli_id);
5439 ((void) cli_id_len);
5440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005441 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005442}
5443
5444static int ssl_cookie_check_dummy( void *ctx,
5445 const unsigned char *cookie, size_t cookie_len,
5446 const unsigned char *cli_id, size_t cli_id_len )
5447{
5448 ((void) ctx);
5449 ((void) cookie);
5450 ((void) cookie_len);
5451 ((void) cli_id);
5452 ((void) cli_id_len);
5453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005455}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005456#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02005457
Paul Bakker5121ce52009-01-03 21:22:43 +00005458/*
5459 * Initialize an SSL context
5460 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005461void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
5462{
5463 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
5464}
5465
5466/*
5467 * Setup an SSL context
5468 */
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005469int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02005470 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00005471{
Paul Bakker48916f92012-09-16 19:57:18 +00005472 int ret;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005473 const size_t len = MBEDTLS_SSL_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005474
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02005475 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00005476
5477 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005478 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00005479 */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005480 if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL ||
5481 ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005482 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02005483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 mbedtls_free( ssl->in_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005485 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005486 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 }
5488
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005489#if defined(MBEDTLS_SSL_PROTO_DTLS)
5490 if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5491 {
5492 ssl->out_hdr = ssl->out_buf;
5493 ssl->out_ctr = ssl->out_buf + 3;
5494 ssl->out_len = ssl->out_buf + 11;
5495 ssl->out_iv = ssl->out_buf + 13;
5496 ssl->out_msg = ssl->out_buf + 13;
5497
5498 ssl->in_hdr = ssl->in_buf;
5499 ssl->in_ctr = ssl->in_buf + 3;
5500 ssl->in_len = ssl->in_buf + 11;
5501 ssl->in_iv = ssl->in_buf + 13;
5502 ssl->in_msg = ssl->in_buf + 13;
5503 }
5504 else
5505#endif
5506 {
5507 ssl->out_ctr = ssl->out_buf;
5508 ssl->out_hdr = ssl->out_buf + 8;
5509 ssl->out_len = ssl->out_buf + 11;
5510 ssl->out_iv = ssl->out_buf + 13;
5511 ssl->out_msg = ssl->out_buf + 13;
5512
5513 ssl->in_ctr = ssl->in_buf;
5514 ssl->in_hdr = ssl->in_buf + 8;
5515 ssl->in_len = ssl->in_buf + 11;
5516 ssl->in_iv = ssl->in_buf + 13;
5517 ssl->in_msg = ssl->in_buf + 13;
5518 }
5519
Paul Bakker48916f92012-09-16 19:57:18 +00005520 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5521 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005522
5523 return( 0 );
5524}
5525
5526/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00005527 * Reset an initialized and used SSL context for re-use while retaining
5528 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005529 *
5530 * If partial is non-zero, keep data in the input buffer and client ID.
5531 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00005532 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005533static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00005534{
Paul Bakker48916f92012-09-16 19:57:18 +00005535 int ret;
5536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005538
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005539 /* Cancel any possibly running timer */
5540 ssl_set_timer( ssl, 0 );
5541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542#if defined(MBEDTLS_SSL_RENEGOTIATION)
5543 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005544 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005545
5546 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
5548 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005549#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005551
Paul Bakker7eb013f2011-10-06 12:37:39 +00005552 ssl->in_offt = NULL;
5553
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005554 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005555 ssl->in_msgtype = 0;
5556 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005557 if( partial == 0 )
5558 ssl->in_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005559#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005560 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005561 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005564 ssl_dtls_replay_reset( ssl );
5565#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005566
5567 ssl->in_hslen = 0;
5568 ssl->nb_zero = 0;
Hanno Becker704f4932017-06-08 13:08:45 +01005569 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005570
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005571 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005572 ssl->out_msgtype = 0;
5573 ssl->out_msglen = 0;
5574 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
5576 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005577 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005578#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005579
Paul Bakker48916f92012-09-16 19:57:18 +00005580 ssl->transform_in = NULL;
5581 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005584 if( partial == 0 )
5585 memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5588 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
5591 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
5594 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005595 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005596 }
5597#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005598
Paul Bakker48916f92012-09-16 19:57:18 +00005599 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 mbedtls_ssl_transform_free( ssl->transform );
5602 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005603 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005604 }
Paul Bakker48916f92012-09-16 19:57:18 +00005605
Paul Bakkerc0463502013-02-14 11:19:38 +01005606 if( ssl->session )
5607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 mbedtls_ssl_session_free( ssl->session );
5609 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005610 ssl->session = NULL;
5611 }
5612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005614 ssl->alpn_chosen = NULL;
5615#endif
5616
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005617#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005618 if( partial == 0 )
5619 {
5620 mbedtls_free( ssl->cli_id );
5621 ssl->cli_id = NULL;
5622 ssl->cli_id_len = 0;
5623 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005624#endif
5625
Paul Bakker48916f92012-09-16 19:57:18 +00005626 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5627 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005628
5629 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005630}
5631
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005632/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005633 * Reset an initialized and used SSL context for re-use while retaining
5634 * all application-set variables, function pointers and data.
5635 */
5636int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
5637{
5638 return( ssl_session_reset_int( ssl, 0 ) );
5639}
5640
5641/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 * SSL set accessors
5643 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005644void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00005645{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005646 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00005647}
5648
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02005649void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005651 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005652}
5653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005655void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005657 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005658}
5659#endif
5660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005662void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005664 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005665}
5666#endif
5667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005669void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005671 conf->hs_timeout_min = min;
5672 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005673}
5674#endif
5675
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005676void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00005677{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005678 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00005679}
5680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005681#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005682void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02005683 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005684 void *p_vrfy )
5685{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005686 conf->f_vrfy = f_vrfy;
5687 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005690
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005691void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005692 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005693 void *p_rng )
5694{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01005695 conf->f_rng = f_rng;
5696 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00005697}
5698
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005699void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02005700 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005701 void *p_dbg )
5702{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005703 conf->f_dbg = f_dbg;
5704 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00005705}
5706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005708 void *p_bio,
5709 int (*f_send)(void *, const unsigned char *, size_t),
5710 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005711 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005712{
5713 ssl->p_bio = p_bio;
5714 ssl->f_send = f_send;
5715 ssl->f_recv = f_recv;
5716 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005717}
5718
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005719void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01005720{
5721 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005722}
5723
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005724void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
5725 void *p_timer,
5726 void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms),
5727 int (*f_get_timer)(void *) )
5728{
5729 ssl->p_timer = p_timer;
5730 ssl->f_set_timer = f_set_timer;
5731 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005732
5733 /* Make sure we start with no timer running */
5734 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02005735}
5736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005738void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005739 void *p_cache,
5740 int (*f_get_cache)(void *, mbedtls_ssl_session *),
5741 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005742{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01005743 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005744 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005745 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749#if defined(MBEDTLS_SSL_CLI_C)
5750int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005751{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005752 int ret;
5753
5754 if( ssl == NULL ||
5755 session == NULL ||
5756 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005757 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005760 }
5761
5762 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5763 return( ret );
5764
Paul Bakker0a597072012-09-25 21:55:46 +00005765 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005766
5767 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005770
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005771void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005772 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005773{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005774 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
5775 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
5776 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
5777 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005778}
5779
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005780void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005781 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005782 int major, int minor )
5783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005785 return;
5786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005787 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005788 return;
5789
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005790 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005791}
5792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005794void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01005795 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02005796{
5797 conf->cert_profile = profile;
5798}
5799
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005800/* Append a new keycert entry to a (possibly empty) list */
5801static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
5802 mbedtls_x509_crt *cert,
5803 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005804{
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005805 mbedtls_ssl_key_cert *new;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005806
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005807 new = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005808 if( new == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005809 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005810
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005811 new->cert = cert;
5812 new->key = key;
5813 new->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005814
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005815 /* Update head is the list was null, else add to the end */
5816 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005817 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005818 *head = new;
Paul Bakker0333b972013-11-04 17:08:28 +01005819 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005820 else
5821 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005822 mbedtls_ssl_key_cert *cur = *head;
5823 while( cur->next != NULL )
5824 cur = cur->next;
5825 cur->next = new;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005826 }
5827
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005828 return( 0 );
5829}
5830
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005831int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02005832 mbedtls_x509_crt *own_cert,
5833 mbedtls_pk_context *pk_key )
5834{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02005835 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005836}
5837
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005838void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01005839 mbedtls_x509_crt *ca_chain,
5840 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005841{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01005842 conf->ca_chain = ca_chain;
5843 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005846
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02005847#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
5848int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
5849 mbedtls_x509_crt *own_cert,
5850 mbedtls_pk_context *pk_key )
5851{
5852 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
5853 own_cert, pk_key ) );
5854}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02005855
5856void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
5857 mbedtls_x509_crt *ca_chain,
5858 mbedtls_x509_crl *ca_crl )
5859{
5860 ssl->handshake->sni_ca_chain = ca_chain;
5861 ssl->handshake->sni_ca_crl = ca_crl;
5862}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02005863
5864void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
5865 int authmode )
5866{
5867 ssl->handshake->sni_authmode = authmode;
5868}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02005869#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
5870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005872int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01005873 const unsigned char *psk, size_t psk_len,
5874 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005875{
Paul Bakker6db455e2013-09-18 17:29:31 +02005876 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02005878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 if( psk_len > MBEDTLS_PSK_MAX_LEN )
5880 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005881
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02005882 /* Identity len will be encoded on two bytes */
5883 if( ( psk_identity_len >> 16 ) != 0 ||
5884 psk_identity_len > MBEDTLS_SSL_MAX_CONTENT_LEN )
5885 {
5886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5887 }
5888
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005889 if( conf->psk != NULL || conf->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005890 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005891 mbedtls_free( conf->psk );
5892 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnardffb81802015-10-20 19:56:45 +02005893 conf->psk = NULL;
5894 conf->psk_identity = NULL;
Paul Bakker6db455e2013-09-18 17:29:31 +02005895 }
5896
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005897 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
5898 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005899 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005900 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02005901 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005902 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02005903 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005904 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05005905 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005906
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005907 conf->psk_len = psk_len;
5908 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005909
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01005910 memcpy( conf->psk, psk, conf->psk_len );
5911 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005912
5913 return( 0 );
5914}
5915
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01005916int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
5917 const unsigned char *psk, size_t psk_len )
5918{
5919 if( psk == NULL || ssl->handshake == NULL )
5920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5921
5922 if( psk_len > MBEDTLS_PSK_MAX_LEN )
5923 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5924
5925 if( ssl->handshake->psk != NULL )
Simon Butcher5b8d1d62015-10-04 22:06:51 +01005926 mbedtls_free( ssl->handshake->psk );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01005927
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02005928 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02005929 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01005930
5931 ssl->handshake->psk_len = psk_len;
5932 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
5933
5934 return( 0 );
5935}
5936
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005937void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02005939 size_t),
5940 void *p_psk )
5941{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02005942 conf->f_psk = f_psk;
5943 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005946
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005947#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005948int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005949{
5950 int ret;
5951
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01005952 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
5953 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
5954 {
5955 mbedtls_mpi_free( &conf->dhm_P );
5956 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005957 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01005958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005959
5960 return( 0 );
5961}
5962
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02005963int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00005964{
5965 int ret;
5966
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01005967 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
5968 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
5969 {
5970 mbedtls_mpi_free( &conf->dhm_P );
5971 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00005972 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01005973 }
Paul Bakker1b57b062011-01-06 15:48:19 +00005974
5975 return( 0 );
5976}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02005977#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005978
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005979#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5980/*
5981 * Set the minimum length for Diffie-Hellman parameters
5982 */
5983void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
5984 unsigned int bitlen )
5985{
5986 conf->dhm_min_bitlen = bitlen;
5987}
5988#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
5989
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02005990#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02005991/*
5992 * Set allowed/preferred hashes for handshake signatures
5993 */
5994void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
5995 const int *hashes )
5996{
5997 conf->sig_hashes = hashes;
5998}
5999#endif
6000
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006001#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006002/*
6003 * Set the allowed elliptic curves
6004 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006005void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006006 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006007{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006008 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01006009}
6010#endif
6011
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006012#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00006014{
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006015 size_t hostname_len;
6016
Paul Bakker5121ce52009-01-03 21:22:43 +00006017 if( hostname == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00006019
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006020 hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006021
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006022 if( hostname_len + 1 == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02006024
Simon Butcher9f812312015-09-28 19:22:33 +01006025 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
Simon Butcher89f77622015-09-27 22:50:49 +01006026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6027
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02006028 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006029
Paul Bakkerb15b8512012-01-13 13:44:06 +00006030 if( ssl->hostname == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02006031 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakkerb15b8512012-01-13 13:44:06 +00006032
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006033 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02006034
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01006035 ssl->hostname[hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00006036
6037 return( 0 );
6038}
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006039#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006040
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01006041#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006042void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00006044 const unsigned char *, size_t),
6045 void *p_sni )
6046{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006047 conf->f_sni = f_sni;
6048 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00006049}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00006051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006053int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006054{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006055 size_t cur_len, tot_len;
6056 const char **p;
6057
6058 /*
Brian J Murraye7f8dc32016-11-06 04:45:15 -08006059 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
6060 * MUST NOT be truncated."
6061 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006062 */
6063 tot_len = 0;
6064 for( p = protos; *p != NULL; p++ )
6065 {
6066 cur_len = strlen( *p );
6067 tot_len += cur_len;
6068
6069 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006071 }
6072
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006073 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02006074
6075 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006076}
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006079{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006080 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02006083
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006084void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00006085{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006086 conf->max_major_ver = major;
6087 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00006088}
6089
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006090void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00006091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006092 conf->min_major_ver = major;
6093 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00006094}
6095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006097void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006098{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01006099 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02006100}
6101#endif
6102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006104void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006106 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01006107}
6108#endif
6109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006111void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006113 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02006114}
6115#endif
6116
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006117#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006118void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006120 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006121}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02006122#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01006123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006125int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
6128 mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006131 }
6132
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01006133 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006134
6135 return( 0 );
6136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02006138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02006140void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006141{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006142 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02006145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006147void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006148{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006149 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006150}
6151#endif
6152
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006153void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00006154{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006155 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00006156}
6157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006159void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006160{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006161 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006162}
6163
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006164void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006165{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006166 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006167}
6168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02006169void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006170 const unsigned char period[8] )
6171{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02006172 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006177#if defined(MBEDTLS_SSL_CLI_C)
6178void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006179{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01006180 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006181}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006182#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02006183
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006184#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006185void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
6186 mbedtls_ssl_ticket_write_t *f_ticket_write,
6187 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
6188 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02006189{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02006190 conf->f_ticket_write = f_ticket_write;
6191 conf->f_ticket_parse = f_ticket_parse;
6192 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02006193}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02006196
Paul Bakker5121ce52009-01-03 21:22:43 +00006197/*
6198 * SSL get accessors
6199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006201{
6202 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
6203}
6204
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02006205uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006206{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00006207 if( ssl->session != NULL )
6208 return( ssl->session->verify_result );
6209
6210 if( ssl->session_negotiate != NULL )
6211 return( ssl->session_negotiate->verify_result );
6212
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02006213 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00006214}
6215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00006217{
Paul Bakker926c8e42013-03-06 10:23:34 +01006218 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006219 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01006220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00006222}
6223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00006225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006228 {
6229 switch( ssl->minor_ver )
6230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006232 return( "DTLSv1.0" );
6233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006235 return( "DTLSv1.2" );
6236
6237 default:
6238 return( "unknown (DTLS)" );
6239 }
6240 }
6241#endif
6242
Paul Bakker43ca69c2011-01-15 17:35:19 +00006243 switch( ssl->minor_ver )
6244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006246 return( "SSLv3.0" );
6247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006249 return( "TLSv1.0" );
6250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00006252 return( "TLSv1.1" );
6253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00006255 return( "TLSv1.2" );
6256
Paul Bakker43ca69c2011-01-15 17:35:19 +00006257 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01006258 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00006259 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00006260}
6261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006263{
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006264 size_t transform_expansion;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267#if defined(MBEDTLS_ZLIB_SUPPORT)
6268 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
6269 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006270#endif
6271
6272 if( transform == NULL )
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006273 return( (int) mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006277 case MBEDTLS_MODE_GCM:
6278 case MBEDTLS_MODE_CCM:
6279 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006280 transform_expansion = transform->minlen;
6281 break;
6282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283 case MBEDTLS_MODE_CBC:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006284 transform_expansion = transform->maclen
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 + mbedtls_cipher_get_block_size( &transform->cipher_ctx_enc );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006286 break;
6287
6288 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006291 }
6292
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02006293 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02006294}
6295
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006296#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
6297size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
6298{
6299 size_t max_len;
6300
6301 /*
6302 * Assume mfl_code is correct since it was checked when set
6303 */
6304 max_len = mfl_code_to_length[ssl->conf->mfl_code];
6305
6306 /*
6307 * Check if a smaller max length was negotiated
6308 */
6309 if( ssl->session_out != NULL &&
6310 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6311 {
6312 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6313 }
6314
6315 return max_len;
6316}
6317#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
6318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319#if defined(MBEDTLS_X509_CRT_PARSE_C)
6320const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00006321{
6322 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006323 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006324
Paul Bakkerd8bb8262014-06-17 14:06:49 +02006325 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00006326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00006328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329#if defined(MBEDTLS_SSL_CLI_C)
6330int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006331{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006332 if( ssl == NULL ||
6333 dst == NULL ||
6334 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006335 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006338 }
6339
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02006340 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006341}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02006343
Paul Bakker5121ce52009-01-03 21:22:43 +00006344/*
Paul Bakker1961b702013-01-25 14:49:24 +01006345 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00006346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006350
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006351 if( ssl == NULL || ssl->conf == NULL )
6352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006355 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006357#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006359 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006361#endif
6362
Paul Bakker1961b702013-01-25 14:49:24 +01006363 return( ret );
6364}
6365
6366/*
6367 * Perform the SSL handshake
6368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01006370{
6371 int ret = 0;
6372
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006373 if( ssl == NULL || ssl->conf == NULL )
6374 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01006377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01006379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01006381
6382 if( ret != 0 )
6383 break;
6384 }
6385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006387
6388 return( ret );
6389}
6390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006391#if defined(MBEDTLS_SSL_RENEGOTIATION)
6392#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006393/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006394 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00006395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006396static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006397{
6398 int ret;
6399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006401
6402 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006403 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6404 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006409 return( ret );
6410 }
6411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006413
6414 return( 0 );
6415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006417
6418/*
6419 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 * - any side: calling mbedtls_ssl_renegotiate(),
6421 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
6422 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02006423 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006424 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00006428{
6429 int ret;
6430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006432
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006433 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
6434 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006435
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006436 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
6437 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006439 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006441 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006442 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006443 ssl->handshake->out_msg_seq = 1;
6444 else
6445 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02006446 }
6447#endif
6448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
6450 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00006451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006455 return( ret );
6456 }
6457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006459
6460 return( 0 );
6461}
6462
6463/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006464 * Renegotiate current connection on client,
6465 * or request renegotiation on server
6466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006470
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006471 if( ssl == NULL || ssl->conf == NULL )
6472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006475 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006476 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006482
6483 /* Did we already try/start sending HelloRequest? */
6484 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006486
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006487 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006492 /*
6493 * On client, either start the renegotiation process or,
6494 * if already in progress, continue the handshake
6495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
6499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006500
6501 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006504 return( ret );
6505 }
6506 }
6507 else
6508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006512 return( ret );
6513 }
6514 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006516
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006517 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006518}
6519
6520/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006521 * Check record counters and renegotiate if they're above the limit.
6522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006524{
Andres AG7fa66d42016-12-15 17:01:16 +00006525 size_t ep_len = ssl_ep_len( ssl );
6526 int in_ctr_cmp;
6527 int out_ctr_cmp;
6528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006529 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
6530 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006531 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006532 {
6533 return( 0 );
6534 }
6535
Andres AG7fa66d42016-12-15 17:01:16 +00006536 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
6537 ssl->conf->renego_period + ep_len, 8 - ep_len );
6538 out_ctr_cmp = memcmp( ssl->out_ctr + ep_len,
6539 ssl->conf->renego_period + ep_len, 8 - ep_len );
6540
6541 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006542 {
6543 return( 0 );
6544 }
6545
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02006546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006548}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006550
6551/*
6552 * Receive application data decrypted from the SSL layer
6553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006555{
Hanno Becker6a582e82017-06-08 13:38:05 +01006556 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00006557 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006558
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006559 if( ssl == NULL || ssl->conf == NULL )
6560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006565 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006568 return( ret );
6569
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006570 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006571 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006574 return( ret );
6575 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006576 }
6577#endif
6578
Hanno Becker6a582e82017-06-08 13:38:05 +01006579 /*
6580 * Check if renegotiation is necessary and/or handshake is
6581 * in process. If yes, perform/continue, and fall through
6582 * if an unexpected packet is received while the client
6583 * is waiting for the ServerHello.
6584 *
6585 * (There is no equivalent to the last condition on
6586 * the server-side as it is not treated as within
6587 * a handshake while waiting for the ClientHello
6588 * after a renegotiation request.)
6589 */
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker6a582e82017-06-08 13:38:05 +01006592 ret = ssl_check_ctr_renegotiate( ssl );
6593 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6594 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006597 return( ret );
6598 }
6599#endif
6600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006603 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker6a582e82017-06-08 13:38:05 +01006604 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6605 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 return( ret );
6609 }
6610 }
6611
6612 if( ssl->in_offt == NULL )
6613 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006614 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006615 if( ssl->f_get_timer != NULL &&
6616 ssl->f_get_timer( ssl->p_timer ) == -1 )
6617 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006618 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02006619 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006620
Hanno Becker6a582e82017-06-08 13:38:05 +01006621 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006622 {
Hanno Becker6a582e82017-06-08 13:38:05 +01006623 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
6624 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006625
Hanno Becker6a582e82017-06-08 13:38:05 +01006626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6627 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006628 }
6629
6630 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006631 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006632 {
6633 /*
6634 * OpenSSL sends empty messages to randomize the IV
6635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006636 if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00006639 return( 0 );
6640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 return( ret );
6643 }
6644 }
6645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646#if defined(MBEDTLS_SSL_RENEGOTIATION)
6647 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00006648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006652 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
6654 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006657
6658 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006660 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006661 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006662#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006664 }
Hanno Becker6a582e82017-06-08 13:38:05 +01006665#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006666
Hanno Becker6a582e82017-06-08 13:38:05 +01006667#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006668 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006672
6673 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006676 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006677#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00006679 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006680#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006681
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006682 if( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006684 ssl->conf->allow_legacy_renegotiation ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689#if defined(MBEDTLS_SSL_PROTO_SSL3)
6690 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006691 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006692 /*
6693 * SSLv3 does not have a "no_renegotiation" alert
6694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695 if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006696 return( ret );
6697 }
6698 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6700#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6701 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6702 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6705 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
6706 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006707 {
6708 return( ret );
6709 }
Paul Bakker48916f92012-09-16 19:57:18 +00006710 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006711 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
6713 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006717 }
Paul Bakker48916f92012-09-16 19:57:18 +00006718 }
6719 else
6720 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006721 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006723 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6724 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006727 }
6728#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006729 ret = ssl_start_renegotiation( ssl );
Hanno Becker6a582e82017-06-08 13:38:05 +01006730 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
6731 ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006734 return( ret );
6735 }
Paul Bakker48916f92012-09-16 19:57:18 +00006736 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006737
Hanno Becker6a582e82017-06-08 13:38:05 +01006738 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006739 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006741 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006742 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006743 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006744 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006747 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006749 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006750 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006751 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
6755 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01006758 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006759 }
6760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
6764 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006765 }
6766
6767 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006768
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006769 /* We're going to return something now, cancel timer,
6770 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006772 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006773
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006775 /* If we requested renego but received AppData, resend HelloRequest.
6776 * Do it now, after setting in_offt, to avoid taking this branch
6777 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006779 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006781 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006782 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006785 return( ret );
6786 }
6787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006789#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006790 }
6791
6792 n = ( len < ssl->in_msglen )
6793 ? len : ssl->in_msglen;
6794
6795 memcpy( buf, ssl->in_offt, n );
6796 ssl->in_msglen -= n;
6797
6798 if( ssl->in_msglen == 0 )
Hanno Beckercc019082017-06-09 10:51:37 +01006799 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 /* all bytes consumed */
6801 ssl->in_offt = NULL;
Hanno Beckercc019082017-06-09 10:51:37 +01006802 ssl->keep_current_message = 0;
6803 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 else
6805 /* more data available */
6806 ssl->in_offt += n;
6807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006809
Paul Bakker23986e52011-04-24 08:57:21 +00006810 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006811}
6812
6813/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006814 * Send application data to be encrypted by the SSL layer,
6815 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00006816 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006817static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006818 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006819{
Paul Bakker23986e52011-04-24 08:57:21 +00006820 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02006822 size_t max_len = mbedtls_ssl_get_max_frag_len( ssl );
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006823
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006824 if( len > max_len )
6825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006827 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006830 "maximum fragment length: %d > %d",
6831 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006833 }
6834 else
6835#endif
6836 len = max_len;
6837 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006839
Paul Bakker5121ce52009-01-03 21:22:43 +00006840 if( ssl->out_left != 0 )
6841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 return( ret );
6846 }
6847 }
Paul Bakker887bd502011-06-08 13:10:54 +00006848 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006849 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006850 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006852 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00006855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00006857 return( ret );
6858 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006859 }
6860
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006861 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862}
6863
6864/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006865 * Write application data, doing 1/n-1 splitting if necessary.
6866 *
6867 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006868 * then the caller will call us again with the same arguments, so
6869 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006872static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006873 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006874{
6875 int ret;
6876
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01006877 if( ssl->conf->cbc_record_splitting ==
6878 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006879 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
6881 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6882 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006883 {
6884 return( ssl_write_real( ssl, buf, len ) );
6885 }
6886
6887 if( ssl->split_done == 0 )
6888 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006889 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006890 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006891 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006892 }
6893
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006894 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6895 return( ret );
6896 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006897
6898 return( ret + 1 );
6899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006901
6902/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006903 * Write application data (public-facing wrapper)
6904 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006905int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006906{
6907 int ret;
6908
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006910
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006911 if( ssl == NULL || ssl->conf == NULL )
6912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6913
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006914#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006915 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6916 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006918 return( ret );
6919 }
6920#endif
6921
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006922 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006923 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006924 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006925 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02006926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006927 return( ret );
6928 }
6929 }
6930
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006931#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006932 ret = ssl_write_split( ssl, buf, len );
6933#else
6934 ret = ssl_write_real( ssl, buf, len );
6935#endif
6936
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006938
6939 return( ret );
6940}
6941
6942/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006943 * Notify the peer that the connection is being closed
6944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006946{
6947 int ret;
6948
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006949 if( ssl == NULL || ssl->conf == NULL )
6950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006953
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006954 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6960 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
6961 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006964 return( ret );
6965 }
6966 }
6967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006969
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006970 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006971}
6972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00006974{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006975 if( transform == NULL )
6976 return;
6977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00006979 deflateEnd( &transform->ctx_deflate );
6980 inflateEnd( &transform->ctx_inflate );
6981#endif
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 mbedtls_cipher_free( &transform->cipher_ctx_enc );
6984 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 mbedtls_md_free( &transform->md_ctx_enc );
6987 mbedtls_md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006990}
6991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992#if defined(MBEDTLS_X509_CRT_PARSE_C)
6993static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006994{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006996
6997 while( cur != NULL )
6998 {
6999 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007000 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007001 cur = next;
7002 }
7003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake )
Paul Bakker48916f92012-09-16 19:57:18 +00007007{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007008 if( handshake == NULL )
7009 return;
7010
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02007011#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7012 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7013 mbedtls_md5_free( &handshake->fin_md5 );
7014 mbedtls_sha1_free( &handshake->fin_sha1 );
7015#endif
7016#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7017#if defined(MBEDTLS_SHA256_C)
7018 mbedtls_sha256_free( &handshake->fin_sha256 );
7019#endif
7020#if defined(MBEDTLS_SHA512_C)
7021 mbedtls_sha512_free( &handshake->fin_sha512 );
7022#endif
7023#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if defined(MBEDTLS_DHM_C)
7026 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +00007027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028#if defined(MBEDTLS_ECDH_C)
7029 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +02007030#endif
7031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02007033 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02007035#endif
7036
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01007037#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7038 if( handshake->psk != NULL )
7039 {
7040 mbedtls_zeroize( handshake->psk, handshake->psk_len );
7041 mbedtls_free( handshake->psk );
7042 }
7043#endif
7044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7046 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007047 /*
7048 * Free only the linked list wrapper, not the keys themselves
7049 * since the belong to the SNI callback
7050 */
7051 if( handshake->sni_key_cert != NULL )
7052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007054
7055 while( cur != NULL )
7056 {
7057 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02007059 cur = next;
7060 }
7061 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02007063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064#if defined(MBEDTLS_SSL_PROTO_DTLS)
7065 mbedtls_free( handshake->verify_cookie );
7066 mbedtls_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02007067 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02007068#endif
7069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007070 mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007071}
7072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +00007074{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007075 if( session == NULL )
7076 return;
7077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00007079 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00007080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 mbedtls_x509_crt_free( session->peer_cert );
7082 mbedtls_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00007083 }
Paul Bakkered27a042013-04-18 22:46:23 +02007084#endif
Paul Bakker0a597072012-09-25 21:55:46 +00007085
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02007086#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02007088#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02007089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007091}
7092
Paul Bakker5121ce52009-01-03 21:22:43 +00007093/*
7094 * Free an SSL context
7095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007097{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007098 if( ssl == NULL )
7099 return;
7100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007102
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007103 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN );
7106 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007107 }
7108
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01007109 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN );
7112 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 }
7114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +02007116 if( ssl->compress_buf != NULL )
7117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN );
7119 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +02007120 }
7121#endif
7122
Paul Bakker48916f92012-09-16 19:57:18 +00007123 if( ssl->transform )
7124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 mbedtls_ssl_transform_free( ssl->transform );
7126 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007127 }
7128
7129 if( ssl->handshake )
7130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 mbedtls_ssl_handshake_free( ssl->handshake );
7132 mbedtls_ssl_transform_free( ssl->transform_negotiate );
7133 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 mbedtls_free( ssl->handshake );
7136 mbedtls_free( ssl->transform_negotiate );
7137 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00007138 }
7139
Paul Bakkerc0463502013-02-14 11:19:38 +01007140 if( ssl->session )
7141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 mbedtls_ssl_session_free( ssl->session );
7143 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007144 }
7145
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02007146#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +02007147 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007148 {
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01007149 mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00007151 }
Paul Bakker0be444a2013-08-27 21:55:01 +02007152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7155 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
7158 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +00007159 }
7160#endif
7161
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007162#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007164#endif
7165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00007167
Paul Bakker86f04f42013-02-14 11:20:09 +01007168 /* Actually clear after last debug message */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007170}
7171
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007172/*
7173 * Initialze mbedtls_ssl_config
7174 */
7175void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
7176{
7177 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
7178}
7179
Simon Butcherc941b6c2015-12-28 01:29:10 +00007180#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007181static int ssl_preset_default_hashes[] = {
7182#if defined(MBEDTLS_SHA512_C)
7183 MBEDTLS_MD_SHA512,
7184 MBEDTLS_MD_SHA384,
7185#endif
7186#if defined(MBEDTLS_SHA256_C)
7187 MBEDTLS_MD_SHA256,
7188 MBEDTLS_MD_SHA224,
7189#endif
Gilles Peskine7344e1b2017-05-12 13:16:40 +02007190#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007191 MBEDTLS_MD_SHA1,
7192#endif
7193 MBEDTLS_MD_NONE
7194};
Simon Butcherc941b6c2015-12-28 01:29:10 +00007195#endif
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007196
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007197static int ssl_preset_suiteb_ciphersuites[] = {
7198 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
7199 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
7200 0
7201};
7202
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007203#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007204static int ssl_preset_suiteb_hashes[] = {
7205 MBEDTLS_MD_SHA256,
7206 MBEDTLS_MD_SHA384,
7207 MBEDTLS_MD_NONE
7208};
7209#endif
7210
7211#if defined(MBEDTLS_ECP_C)
7212static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
7213 MBEDTLS_ECP_DP_SECP256R1,
7214 MBEDTLS_ECP_DP_SECP384R1,
7215 MBEDTLS_ECP_DP_NONE
7216};
7217#endif
7218
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007219/*
Tillmann Karras588ad502015-09-25 04:27:22 +02007220 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007221 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007222int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007223 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007224{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007225#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007226 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007227#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007228
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007229 /* Use the functions here so that they are covered in tests,
7230 * but otherwise access member directly for efficiency */
7231 mbedtls_ssl_conf_endpoint( conf, endpoint );
7232 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007233
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007234 /*
7235 * Things that are common to all presets
7236 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007237#if defined(MBEDTLS_SSL_CLI_C)
7238 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
7239 {
7240 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7241#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7242 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7243#endif
7244 }
7245#endif
7246
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007247#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007248 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007249#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007250
7251#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7252 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7253#endif
7254
7255#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7256 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7257#endif
7258
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007259#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7260 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7261#endif
7262
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007263#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007264 conf->f_cookie_write = ssl_cookie_write_dummy;
7265 conf->f_cookie_check = ssl_cookie_check_dummy;
7266#endif
7267
7268#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7269 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7270#endif
7271
7272#if defined(MBEDTLS_SSL_PROTO_DTLS)
7273 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7274 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7275#endif
7276
7277#if defined(MBEDTLS_SSL_RENEGOTIATION)
7278 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG7fa66d42016-12-15 17:01:16 +00007279 memset( conf->renego_period, 0x00, 2 );
7280 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007281#endif
7282
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007283#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
7284 if( endpoint == MBEDTLS_SSL_IS_SERVER )
7285 {
7286 if( ( ret = mbedtls_ssl_conf_dh_param( conf,
7287 MBEDTLS_DHM_RFC5114_MODP_2048_P,
7288 MBEDTLS_DHM_RFC5114_MODP_2048_G ) ) != 0 )
7289 {
7290 return( ret );
7291 }
7292 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007293#endif
7294
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007295 /*
7296 * Preset-specific defaults
7297 */
7298 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007299 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007300 /*
7301 * NSA Suite B
7302 */
7303 case MBEDTLS_SSL_PRESET_SUITEB:
7304 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7305 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7306 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7307 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7308
7309 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7310 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7311 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7312 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7313 ssl_preset_suiteb_ciphersuites;
7314
7315#if defined(MBEDTLS_X509_CRT_PARSE_C)
7316 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007317#endif
7318
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007319#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007320 conf->sig_hashes = ssl_preset_suiteb_hashes;
7321#endif
7322
7323#if defined(MBEDTLS_ECP_C)
7324 conf->curve_list = ssl_preset_suiteb_curves;
7325#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007326 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007327
7328 /*
7329 * Default
7330 */
7331 default:
7332 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7333 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_1; /* TLS 1.0 */
7334 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7335 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7336
7337#if defined(MBEDTLS_SSL_PROTO_DTLS)
7338 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7339 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
7340#endif
7341
7342 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
7343 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7344 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7345 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7346 mbedtls_ssl_list_ciphersuites();
7347
7348#if defined(MBEDTLS_X509_CRT_PARSE_C)
7349 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7350#endif
7351
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007352#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb39528e2015-12-04 15:02:56 +01007353 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007354#endif
7355
7356#if defined(MBEDTLS_ECP_C)
7357 conf->curve_list = mbedtls_ecp_grp_id_list();
7358#endif
7359
7360#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7361 conf->dhm_min_bitlen = 1024;
7362#endif
7363 }
7364
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007365 return( 0 );
7366}
7367
7368/*
7369 * Free mbedtls_ssl_config
7370 */
7371void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
7372{
7373#if defined(MBEDTLS_DHM_C)
7374 mbedtls_mpi_free( &conf->dhm_P );
7375 mbedtls_mpi_free( &conf->dhm_G );
7376#endif
7377
7378#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
7379 if( conf->psk != NULL )
7380 {
7381 mbedtls_zeroize( conf->psk, conf->psk_len );
7382 mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len );
7383 mbedtls_free( conf->psk );
7384 mbedtls_free( conf->psk_identity );
7385 conf->psk_len = 0;
7386 conf->psk_identity_len = 0;
7387 }
7388#endif
7389
7390#if defined(MBEDTLS_X509_CRT_PARSE_C)
7391 ssl_key_cert_free( conf->key_cert );
7392#endif
7393
7394 mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) );
7395}
7396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007398/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_RSA_C)
7404 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
7405 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007406#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_ECDSA_C)
7408 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
7409 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007412}
7413
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007414unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
7415{
7416 switch( type ) {
7417 case MBEDTLS_PK_RSA:
7418 return( MBEDTLS_SSL_SIG_RSA );
7419 case MBEDTLS_PK_ECDSA:
7420 case MBEDTLS_PK_ECKEY:
7421 return( MBEDTLS_SSL_SIG_ECDSA );
7422 default:
7423 return( MBEDTLS_SSL_SIG_ANON );
7424 }
7425}
7426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007428{
7429 switch( sig )
7430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431#if defined(MBEDTLS_RSA_C)
7432 case MBEDTLS_SSL_SIG_RSA:
7433 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007434#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_ECDSA_C)
7436 case MBEDTLS_SSL_SIG_ECDSA:
7437 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007438#endif
7439 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007441 }
7442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443#endif /* MBEDTLS_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007444
Hanno Beckeraa8a2bd2017-04-28 17:15:26 +01007445#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7446 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7447
7448/* Find an entry in a signature-hash set matching a given hash algorithm. */
7449mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
7450 mbedtls_pk_type_t sig_alg )
7451{
7452 switch( sig_alg )
7453 {
7454 case MBEDTLS_PK_RSA:
7455 return( set->rsa );
7456 case MBEDTLS_PK_ECDSA:
7457 return( set->ecdsa );
7458 default:
7459 return( MBEDTLS_MD_NONE );
7460 }
7461}
7462
7463/* Add a signature-hash-pair to a signature-hash set */
7464void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
7465 mbedtls_pk_type_t sig_alg,
7466 mbedtls_md_type_t md_alg )
7467{
7468 switch( sig_alg )
7469 {
7470 case MBEDTLS_PK_RSA:
7471 if( set->rsa == MBEDTLS_MD_NONE )
7472 set->rsa = md_alg;
7473 break;
7474
7475 case MBEDTLS_PK_ECDSA:
7476 if( set->ecdsa == MBEDTLS_MD_NONE )
7477 set->ecdsa = md_alg;
7478 break;
7479
7480 default:
7481 break;
7482 }
7483}
7484
7485/* Allow exactly one hash algorithm for each signature. */
7486void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
7487 mbedtls_md_type_t md_alg )
7488{
7489 set->rsa = md_alg;
7490 set->ecdsa = md_alg;
7491}
7492
7493#endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
7494 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
7495
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007496/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007497 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007500{
7501 switch( hash )
7502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503#if defined(MBEDTLS_MD5_C)
7504 case MBEDTLS_SSL_HASH_MD5:
7505 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507#if defined(MBEDTLS_SHA1_C)
7508 case MBEDTLS_SSL_HASH_SHA1:
7509 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511#if defined(MBEDTLS_SHA256_C)
7512 case MBEDTLS_SSL_HASH_SHA224:
7513 return( MBEDTLS_MD_SHA224 );
7514 case MBEDTLS_SSL_HASH_SHA256:
7515 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if defined(MBEDTLS_SHA512_C)
7518 case MBEDTLS_SSL_HASH_SHA384:
7519 return( MBEDTLS_MD_SHA384 );
7520 case MBEDTLS_SSL_HASH_SHA512:
7521 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007522#endif
7523 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007525 }
7526}
7527
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007528/*
7529 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7530 */
7531unsigned char mbedtls_ssl_hash_from_md_alg( int md )
7532{
7533 switch( md )
7534 {
7535#if defined(MBEDTLS_MD5_C)
7536 case MBEDTLS_MD_MD5:
7537 return( MBEDTLS_SSL_HASH_MD5 );
7538#endif
7539#if defined(MBEDTLS_SHA1_C)
7540 case MBEDTLS_MD_SHA1:
7541 return( MBEDTLS_SSL_HASH_SHA1 );
7542#endif
7543#if defined(MBEDTLS_SHA256_C)
7544 case MBEDTLS_MD_SHA224:
7545 return( MBEDTLS_SSL_HASH_SHA224 );
7546 case MBEDTLS_MD_SHA256:
7547 return( MBEDTLS_SSL_HASH_SHA256 );
7548#endif
7549#if defined(MBEDTLS_SHA512_C)
7550 case MBEDTLS_MD_SHA384:
7551 return( MBEDTLS_SSL_HASH_SHA384 );
7552 case MBEDTLS_MD_SHA512:
7553 return( MBEDTLS_SSL_HASH_SHA512 );
7554#endif
7555 default:
7556 return( MBEDTLS_SSL_HASH_NONE );
7557 }
7558}
7559
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007560#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007561/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007562 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007563 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007564 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007565int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007568
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007569 if( ssl->conf->curve_list == NULL )
7570 return( -1 );
7571
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007572 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007573 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007574 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007575
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007576 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007577}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007578#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007579
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007580#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007581/*
7582 * Check if a hash proposed by the peer is in our list.
7583 * Return 0 if we're willing to use it, -1 otherwise.
7584 */
7585int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
7586 mbedtls_md_type_t md )
7587{
7588 const int *cur;
7589
7590 if( ssl->conf->sig_hashes == NULL )
7591 return( -1 );
7592
7593 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
7594 if( *cur == (int) md )
7595 return( 0 );
7596
7597 return( -1 );
7598}
Manuel Pégourié-Gonnardf9945bc2015-10-22 17:01:15 +02007599#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601#if defined(MBEDTLS_X509_CRT_PARSE_C)
7602int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
7603 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007604 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007605 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007606{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007607 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007609 int usage = 0;
7610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007612 const char *ext_oid;
7613 size_t ext_len;
7614#endif
7615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7617 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007618 ((void) cert);
7619 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007620 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007621#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
7624 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007625 {
7626 /* Server part of the key exchange */
7627 switch( ciphersuite->key_exchange )
7628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 case MBEDTLS_KEY_EXCHANGE_RSA:
7630 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007631 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007632 break;
7633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7635 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7636 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7637 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007638 break;
7639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7641 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007642 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007643 break;
7644
7645 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 case MBEDTLS_KEY_EXCHANGE_NONE:
7647 case MBEDTLS_KEY_EXCHANGE_PSK:
7648 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7649 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007650 usage = 0;
7651 }
7652 }
7653 else
7654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7656 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007657 }
7658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007660 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007661 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007662 ret = -1;
7663 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007664#else
7665 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
7669 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671 ext_oid = MBEDTLS_OID_SERVER_AUTH;
7672 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007673 }
7674 else
7675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
7677 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007678 }
7679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007681 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007682 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007683 ret = -1;
7684 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007686
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007687 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007690
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007691/*
7692 * Convert version numbers to/from wire format
7693 * and, for DTLS, to/from TLS equivalent.
7694 *
7695 * For TLS this is the identity.
Brian J Murraye7f8dc32016-11-06 04:45:15 -08007696 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007697 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
7698 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
7699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007701 unsigned char ver[2] )
7702{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703#if defined(MBEDTLS_SSL_PROTO_DTLS)
7704 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007707 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7708
7709 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
7710 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
7711 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007712 else
7713#else
7714 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007715#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007716 {
7717 ver[0] = (unsigned char) major;
7718 ver[1] = (unsigned char) minor;
7719 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007720}
7721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007723 const unsigned char ver[2] )
7724{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SSL_PROTO_DTLS)
7726 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007727 {
7728 *major = 255 - ver[0] + 2;
7729 *minor = 255 - ver[1] + 1;
7730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007732 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
7733 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007734 else
7735#else
7736 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007737#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01007738 {
7739 *major = ver[0];
7740 *minor = ver[1];
7741 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01007742}
7743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744#endif /* MBEDTLS_SSL_TLS_C */