| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSLv3/TLSv1 shared functions | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  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 Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ | 
|  | 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_SSL_TLS_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #if defined(MBEDTLS_PLATFORM_C) | 
|  | 39 | #include "mbedtls/platform.h" | 
|  | 40 | #else | 
|  | 41 | #include <stdlib.h> | 
|  | 42 | #define mbedtls_calloc    calloc | 
|  | 43 | #define mbedtls_free      free | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 44 | #endif | 
|  | 45 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/debug.h" | 
|  | 47 | #include "mbedtls/ssl.h" | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 48 | #include "mbedtls/ssl_internal.h" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 49 | #include "mbedtls/platform_util.h" | 
| Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 50 | #include "mbedtls/version.h" | 
| Jarno Lamsa | af60cd7 | 2019-12-19 16:45:23 +0200 | [diff] [blame] | 51 | #include "mbedtls/platform.h" | 
|  | 52 |  | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 53 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 54 | #include <string.h> | 
|  | 55 |  | 
| Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 56 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 57 | #include "mbedtls/oid.h" | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 58 | #endif | 
|  | 59 |  | 
| Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 60 | #if defined(MBEDTLS_USE_TINYCRYPT) | 
|  | 61 | static int uecc_rng_wrapper( uint8_t *dest, unsigned int size ) | 
|  | 62 | { | 
| Hanno Becker | d089fad | 2019-07-24 09:05:05 +0100 | [diff] [blame] | 63 | int ret; | 
|  | 64 | ret = mbedtls_ssl_conf_rng_func( NULL, dest, size ); | 
|  | 65 | if( ret == 0 ) | 
|  | 66 | return( (int) size ); | 
|  | 67 |  | 
|  | 68 | return( 0 ); | 
| Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 69 | } | 
| Hanno Becker | 75f12d1 | 2019-07-23 16:16:15 +0100 | [diff] [blame] | 70 |  | 
|  | 71 | int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl, | 
|  | 72 | unsigned char **p, unsigned char *end ) | 
|  | 73 | { | 
|  | 74 | size_t const secp256r1_uncompressed_point_length = | 
|  | 75 | 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */; | 
|  | 76 |  | 
|  | 77 | if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length ) | 
|  | 78 | { | 
|  | 79 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) ); | 
| Hanno Becker | c64d5af | 2019-08-23 13:14:36 +0100 | [diff] [blame] | 80 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Hanno Becker | 75f12d1 | 2019-07-23 16:16:15 +0100 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 83 | if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 || | 
|  | 84 | (*p)[1] != 0x04 ) | 
|  | 85 | { | 
|  | 86 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }", | 
|  | 87 | 2 * NUM_ECC_BYTES + 1, | 
|  | 88 | 0x04, | 
|  | 89 | (unsigned) (*p)[0], | 
|  | 90 | (unsigned) (*p)[1] ) ); | 
| Hanno Becker | c64d5af | 2019-08-23 13:14:36 +0100 | [diff] [blame] | 91 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Hanno Becker | 75f12d1 | 2019-07-23 16:16:15 +0100 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 94 | mbedtls_platform_memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES ); | 
| Hanno Becker | 75f12d1 | 2019-07-23 16:16:15 +0100 | [diff] [blame] | 95 |  | 
|  | 96 | *p += secp256r1_uncompressed_point_length; | 
|  | 97 | return( 0 ); | 
|  | 98 | } | 
| Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 99 | #endif /* MBEDTLS_USE_TINYCRYPT */ | 
|  | 100 |  | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 101 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 102 | static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl ); | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 103 |  | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 104 | /* Length of the "epoch" field in the record header */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 106 | { | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 107 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) | 
| Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 108 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 109 | #endif | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 110 |  | 
|  | 111 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 112 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
|  | 113 | return( 2 ); | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 114 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 115 | #endif | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 116 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 117 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 118 | #endif | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
| Hanno Becker | b82350b | 2019-07-26 07:24:05 +0100 | [diff] [blame] | 121 | static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl ) | 
|  | 122 | { | 
|  | 123 | if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE ) | 
|  | 124 | return; | 
|  | 125 |  | 
|  | 126 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 127 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 128 | ssl->pending_fatal_alert_msg ); | 
|  | 129 | ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE; | 
|  | 130 | } | 
|  | 131 |  | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 132 | /* | 
|  | 133 | * Start a timer. | 
|  | 134 | * Passing millisecs = 0 cancels a running timer. | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 135 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs ) | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 137 | { | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 138 | if( mbedtls_ssl_get_set_timer( ssl ) == NULL ) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 139 | return; | 
|  | 140 |  | 
|  | 141 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 142 | mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer, | 
|  | 143 | millisecs / 4, | 
|  | 144 | millisecs ); | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
|  | 147 | /* | 
|  | 148 | * Return -1 is timer is expired, 0 if it isn't. | 
|  | 149 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 151 | { | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 152 | if( mbedtls_ssl_get_get_timer( ssl ) == NULL ) | 
| Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 153 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 154 |  | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 155 | if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 ) | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 156 | { | 
|  | 157 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 158 | return( -1 ); | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 159 | } | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 160 |  | 
|  | 161 | return( 0 ); | 
|  | 162 | } | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 163 |  | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 164 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, | 
|  | 165 | mbedtls_ssl_transform *transform ); | 
| Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 166 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 167 |  | 
| Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 168 | #if defined(MBEDTLS_SSL_RECORD_CHECKING) | 
| Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 169 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, | 
|  | 170 | unsigned char *buf, | 
|  | 171 | size_t len, | 
|  | 172 | mbedtls_record *rec ); | 
|  | 173 |  | 
| Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 174 | int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl, | 
|  | 175 | unsigned char *buf, | 
|  | 176 | size_t buflen ) | 
|  | 177 | { | 
| Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 178 | int ret = 0; | 
|  | 179 | mbedtls_record rec; | 
|  | 180 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) ); | 
|  | 181 | MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen ); | 
|  | 182 |  | 
|  | 183 | /* We don't support record checking in TLS because | 
|  | 184 | * (a) there doesn't seem to be a usecase for it, and | 
|  | 185 | * (b) In SSLv3 and TLS 1.0, CBC record decryption has state | 
|  | 186 | *     and we'd need to backup the transform here. | 
|  | 187 | */ | 
|  | 188 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 189 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) | 
|  | 190 | { | 
|  | 191 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
|  | 192 | goto exit; | 
|  | 193 | } | 
|  | 194 | MBEDTLS_SSL_TRANSPORT_ELSE | 
|  | 195 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
|  | 196 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 197 | { | 
|  | 198 | ret = ssl_parse_record_header( ssl, buf, buflen, &rec ); | 
|  | 199 | if( ret != 0 ) | 
|  | 200 | { | 
|  | 201 | MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret ); | 
|  | 202 | goto exit; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | if( ssl->transform_in != NULL ) | 
|  | 206 | { | 
|  | 207 | ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec ); | 
|  | 208 | if( ret != 0 ) | 
|  | 209 | { | 
|  | 210 | MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret ); | 
|  | 211 | goto exit; | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 | } | 
|  | 215 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 216 |  | 
|  | 217 | exit: | 
|  | 218 | /* On success, we have decrypted the buffer in-place, so make | 
|  | 219 | * sure we don't leak any plaintext data. */ | 
|  | 220 | mbedtls_platform_zeroize( buf, buflen ); | 
|  | 221 |  | 
|  | 222 | /* For the purpose of this API, treat messages with unexpected CID | 
|  | 223 | * as well as such from future epochs as unexpected. */ | 
|  | 224 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || | 
|  | 225 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) | 
|  | 226 | { | 
|  | 227 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) ); | 
|  | 231 | return( ret ); | 
| Hanno Becker | 02f2609 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 232 | } | 
|  | 233 | #endif /* MBEDTLS_SSL_RECORD_CHECKING */ | 
|  | 234 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 235 | #define SSL_DONT_FORCE_FLUSH 0 | 
|  | 236 | #define SSL_FORCE_FLUSH      1 | 
|  | 237 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 238 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 239 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 240 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 241 | /* Top-level Connection ID API */ | 
|  | 242 |  | 
| Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 243 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \ | 
|  | 244 | !defined(MBEDTLS_SSL_CONF_CID_LEN) &&      \ | 
|  | 245 | !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID) | 
| Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 246 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, | 
|  | 247 | size_t len, | 
|  | 248 | int ignore_other_cid ) | 
| Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 249 | { | 
|  | 250 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) | 
|  | 251 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 252 |  | 
| Hanno Becker | 791ec6b | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 253 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && | 
|  | 254 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) | 
|  | 255 | { | 
|  | 256 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | conf->ignore_unexpected_cid = ignore_other_cid; | 
| Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 260 | conf->cid_len = len; | 
|  | 261 | return( 0 ); | 
|  | 262 | } | 
| Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 263 | #else  /* MBEDTLS_SSL_DTLS_CONNECTION_ID && | 
|  | 264 | !MBEDTLS_SSL_CONF_CID_LEN && | 
|  | 265 | !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ | 
|  | 266 |  | 
|  | 267 | #if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX | 
|  | 268 | #error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN" | 
|  | 269 | #endif | 
|  | 270 | #if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \ | 
|  | 271 | MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL | 
|  | 272 | #error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID" | 
|  | 273 | #endif | 
|  | 274 |  | 
|  | 275 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID && | 
|  | 276 | !MBEDTLS_SSL_CONF_CID_LEN && | 
|  | 277 | !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ | 
| Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 278 |  | 
| Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 279 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, | 
|  | 280 | int enable, | 
|  | 281 | unsigned char const *own_cid, | 
|  | 282 | size_t own_cid_len ) | 
|  | 283 | { | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 284 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) | 
| Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 285 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 286 |  | 
| Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 287 | ssl->negotiate_cid = enable; | 
|  | 288 | if( enable == MBEDTLS_SSL_CID_DISABLED ) | 
|  | 289 | { | 
|  | 290 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); | 
|  | 291 | return( 0 ); | 
|  | 292 | } | 
|  | 293 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); | 
| Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 294 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); | 
| Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 295 |  | 
| Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 296 | if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) | 
| Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 297 | { | 
| Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 298 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", | 
|  | 299 | (unsigned) own_cid_len, | 
| Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 300 | (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) ); | 
| Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 301 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 302 | } | 
|  | 303 |  | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 304 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 305 | memcpy( ssl->own_cid, own_cid, own_cid_len ); | 
| Hanno Becker | b4a5606 | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 306 | /* Truncation is not an issue here because | 
|  | 307 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ | 
|  | 308 | ssl->own_cid_len = (uint8_t) own_cid_len; | 
| Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 309 |  | 
| Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 310 | return( 0 ); | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, | 
|  | 314 | int *enabled, | 
|  | 315 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], | 
|  | 316 | size_t *peer_cid_len ) | 
|  | 317 | { | 
| Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 318 | *enabled = MBEDTLS_SSL_CID_DISABLED; | 
| Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 319 |  | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 320 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || | 
| Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 321 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 322 | { | 
| Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 323 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 324 | } | 
| Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 325 |  | 
| Hanno Becker | cb063f5 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 326 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions | 
|  | 327 | * were used, but client and server requested the empty CID. | 
|  | 328 | * This is indistinguishable from not using the CID extension | 
|  | 329 | * in the first place. */ | 
| Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 330 | if( ssl->transform_in->in_cid_len  == 0 && | 
|  | 331 | ssl->transform_in->out_cid_len == 0 ) | 
|  | 332 | { | 
|  | 333 | return( 0 ); | 
|  | 334 | } | 
|  | 335 |  | 
| Hanno Becker | 633d604 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 336 | if( peer_cid_len != NULL ) | 
|  | 337 | { | 
|  | 338 | *peer_cid_len = ssl->transform_in->out_cid_len; | 
|  | 339 | if( peer_cid != NULL ) | 
|  | 340 | { | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 341 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 342 | memcpy( peer_cid, ssl->transform_in->out_cid, | 
| Hanno Becker | 633d604 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 343 | ssl->transform_in->out_cid_len ); | 
|  | 344 | } | 
|  | 345 | } | 
| Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 346 |  | 
|  | 347 | *enabled = MBEDTLS_SSL_CID_ENABLED; | 
|  | 348 |  | 
| Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 349 | return( 0 ); | 
|  | 350 | } | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 351 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 352 |  | 
| Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 353 | /* Forward declarations for functions related to message buffering. */ | 
|  | 354 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); | 
|  | 355 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, | 
|  | 356 | uint8_t slot ); | 
|  | 357 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); | 
|  | 358 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); | 
|  | 359 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); | 
|  | 360 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 361 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, | 
|  | 362 | mbedtls_record const *rec ); | 
| Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 363 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 364 |  | 
| Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 365 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 366 | static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl ) | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 367 | { | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 368 | size_t mtu = ssl_get_current_mtu( ssl ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 369 |  | 
|  | 370 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 371 | return( mtu ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 372 |  | 
|  | 373 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
|  | 374 | } | 
|  | 375 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 376 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) | 
|  | 377 | { | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 378 | size_t const bytes_written = ssl->out_left; | 
|  | 379 | size_t const mtu           = ssl_get_maximum_datagram_size( ssl ); | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 380 |  | 
|  | 381 | /* Double-check that the write-index hasn't gone | 
|  | 382 | * past what we can transmit in a single datagram. */ | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 383 | if( bytes_written > mtu ) | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 384 | { | 
|  | 385 | /* Should never happen... */ | 
|  | 386 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | return( (int) ( mtu - bytes_written ) ); | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) | 
|  | 393 | { | 
|  | 394 | int ret; | 
|  | 395 | size_t remaining, expansion; | 
| Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 396 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 397 |  | 
|  | 398 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 399 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); | 
|  | 400 |  | 
|  | 401 | if( max_len > mfl ) | 
|  | 402 | max_len = mfl; | 
| Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 403 |  | 
|  | 404 | /* By the standard (RFC 6066 Sect. 4), the MFL extension | 
|  | 405 | * only limits the maximum record payload size, so in theory | 
|  | 406 | * we would be allowed to pack multiple records of payload size | 
|  | 407 | * MFL into a single datagram. However, this would mean that there's | 
|  | 408 | * no way to explicitly communicate MTU restrictions to the peer. | 
|  | 409 | * | 
|  | 410 | * The following reduction of max_len makes sure that we never | 
|  | 411 | * write datagrams larger than MFL + Record Expansion Overhead. | 
|  | 412 | */ | 
|  | 413 | if( max_len <= ssl->out_left ) | 
|  | 414 | return( 0 ); | 
|  | 415 |  | 
|  | 416 | max_len -= ssl->out_left; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 417 | #endif | 
|  | 418 |  | 
|  | 419 | ret = ssl_get_remaining_space_in_datagram( ssl ); | 
|  | 420 | if( ret < 0 ) | 
|  | 421 | return( ret ); | 
|  | 422 | remaining = (size_t) ret; | 
|  | 423 |  | 
|  | 424 | ret = mbedtls_ssl_get_record_expansion( ssl ); | 
|  | 425 | if( ret < 0 ) | 
|  | 426 | return( ret ); | 
|  | 427 | expansion = (size_t) ret; | 
|  | 428 |  | 
|  | 429 | if( remaining <= expansion ) | 
|  | 430 | return( 0 ); | 
|  | 431 |  | 
|  | 432 | remaining -= expansion; | 
|  | 433 | if( remaining >= max_len ) | 
|  | 434 | remaining = max_len; | 
|  | 435 |  | 
|  | 436 | return( (int) remaining ); | 
|  | 437 | } | 
|  | 438 |  | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 439 | /* | 
|  | 440 | * Double the retransmit timeout value, within the allowed range, | 
|  | 441 | * returning -1 if the maximum value has already been reached. | 
|  | 442 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 444 | { | 
|  | 445 | uint32_t new_timeout; | 
|  | 446 |  | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 447 | if( ssl->handshake->retransmit_timeout >= | 
|  | 448 | mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) ) | 
|  | 449 | { | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 450 | return( -1 ); | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 451 | } | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 452 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 453 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 | 
|  | 454 | * in the following way: after the initial transmission and a first | 
|  | 455 | * retransmission, back off to a temporary estimated MTU of 508 bytes. | 
|  | 456 | * This value is guaranteed to be deliverable (if not guaranteed to be | 
|  | 457 | * delivered) of any compliant IPv4 (and IPv6) network, and should work | 
|  | 458 | * on most non-IP stacks too. */ | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 459 | if( ssl->handshake->retransmit_timeout != | 
|  | 460 | mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) ) | 
| Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 461 | { | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 462 | ssl->handshake->mtu = 508; | 
| Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 463 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); | 
|  | 464 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 465 |  | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 466 | new_timeout = 2 * ssl->handshake->retransmit_timeout; | 
|  | 467 |  | 
|  | 468 | /* Avoid arithmetic overflow and range overflow */ | 
|  | 469 | if( new_timeout < ssl->handshake->retransmit_timeout || | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 470 | new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 471 | { | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 472 | new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ); | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 473 | } | 
|  | 474 |  | 
|  | 475 | ssl->handshake->retransmit_timeout = new_timeout; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs", | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 477 | ssl->handshake->retransmit_timeout ) ); | 
|  | 478 |  | 
|  | 479 | return( 0 ); | 
|  | 480 | } | 
|  | 481 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 483 | { | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 484 | ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 485 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs", | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 486 | ssl->handshake->retransmit_timeout ) ); | 
|  | 487 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 488 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 489 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 491 | /* | 
|  | 492 | * Convert max_fragment_length codes to length. | 
|  | 493 | * RFC 6066 says: | 
|  | 494 | *    enum{ | 
|  | 495 | *        2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) | 
|  | 496 | *    } MaxFragmentLength; | 
|  | 497 | * and we add 0 -> extension unused | 
|  | 498 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 499 | static unsigned int ssl_mfl_code_to_length( int mfl ) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 500 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 501 | switch( mfl ) | 
|  | 502 | { | 
|  | 503 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 504 | return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 505 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: | 
|  | 506 | return 512; | 
|  | 507 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: | 
|  | 508 | return 1024; | 
|  | 509 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: | 
|  | 510 | return 2048; | 
|  | 511 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: | 
|  | 512 | return 4096; | 
|  | 513 | default: | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 514 | return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 515 | } | 
|  | 516 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 518 |  | 
| Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 519 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, | 
|  | 520 | const mbedtls_ssl_session *src ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 521 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 522 | mbedtls_ssl_session_free( dst ); | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 523 | mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 524 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | d5258fa | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 526 |  | 
|  | 527 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 528 | if( src->peer_cert != NULL ) | 
|  | 529 | { | 
| Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 530 | int ret; | 
|  | 531 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 532 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 533 | if( dst->peer_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 534 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 535 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | mbedtls_x509_crt_init( dst->peer_cert ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 537 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, | 
| Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 539 | src->peer_cert->raw.len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 540 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 541 | mbedtls_free( dst->peer_cert ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 542 | dst->peer_cert = NULL; | 
|  | 543 | return( ret ); | 
|  | 544 | } | 
|  | 545 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 546 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 547 | if( src->peer_cert_digest != NULL ) | 
|  | 548 | { | 
| Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 549 | dst->peer_cert_digest = | 
| Hanno Becker | 9d64b78 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 550 | mbedtls_calloc( 1, src->peer_cert_digest_len ); | 
| Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 551 | if( dst->peer_cert_digest == NULL ) | 
|  | 552 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 553 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 554 | mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest, | 
| Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 555 | src->peer_cert_digest_len ); | 
|  | 556 | dst->peer_cert_digest_type = src->peer_cert_digest_type; | 
| Hanno Becker | 9d64b78 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 557 | dst->peer_cert_digest_len = src->peer_cert_digest_len; | 
| Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 558 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 559 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ | 
| Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 560 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 561 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 562 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 563 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 564 | if( src->ticket != NULL ) | 
|  | 565 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 566 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 567 | if( dst->ticket == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 568 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 569 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 570 | mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 571 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 572 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 573 |  | 
|  | 574 | return( 0 ); | 
|  | 575 | } | 
|  | 576 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 577 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 578 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 579 | const unsigned char *key_enc, const unsigned char *key_dec, | 
|  | 580 | size_t keylen, | 
|  | 581 | const unsigned char *iv_enc,  const unsigned char *iv_dec, | 
|  | 582 | size_t ivlen, | 
|  | 583 | const unsigned char *mac_enc, const unsigned char *mac_dec, | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 584 | size_t maclen ) = NULL; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 585 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; | 
|  | 586 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; | 
|  | 587 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; | 
|  | 588 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; | 
|  | 589 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; | 
|  | 590 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 591 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 592 | /* | 
|  | 593 | * Key material generation | 
|  | 594 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 595 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 596 | MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 597 | const char *label, | 
|  | 598 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 599 | unsigned char *dstbuf, size_t dlen ) | 
|  | 600 | { | 
| Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 601 | int ret = 0; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 602 | size_t i; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 603 | mbedtls_md5_context md5; | 
|  | 604 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 605 | unsigned char padding[16]; | 
|  | 606 | unsigned char sha1sum[20]; | 
|  | 607 | ((void)label); | 
|  | 608 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 609 | mbedtls_md5_init(  &md5  ); | 
|  | 610 | mbedtls_sha1_init( &sha1 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 611 |  | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 612 | /* | 
|  | 613 | *  SSLv3: | 
|  | 614 | *    block = | 
|  | 615 | *      MD5( secret + SHA1( 'A'    + secret + random ) ) + | 
|  | 616 | *      MD5( secret + SHA1( 'BB'   + secret + random ) ) + | 
|  | 617 | *      MD5( secret + SHA1( 'CCC'  + secret + random ) ) + | 
|  | 618 | *      ... | 
|  | 619 | */ | 
|  | 620 | for( i = 0; i < dlen / 16; i++ ) | 
|  | 621 | { | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 622 | mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 623 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 624 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 625 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 626 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 627 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 628 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 629 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 630 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 631 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 632 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 633 | goto exit; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 634 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 635 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 636 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 637 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 638 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 639 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 640 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 641 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 642 | goto exit; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 643 | } | 
|  | 644 |  | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 645 | exit: | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 646 | mbedtls_md5_free(  &md5  ); | 
|  | 647 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 648 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 649 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); | 
|  | 650 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 651 |  | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 652 | return( ret ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 653 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 655 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 657 | MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 658 | const char *label, | 
|  | 659 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 660 | unsigned char *dstbuf, size_t dlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 661 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 662 | size_t nb, hs; | 
|  | 663 | size_t i, j, k; | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 664 | const unsigned char *S1, *S2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 665 | unsigned char tmp[128]; | 
|  | 666 | unsigned char h_i[20]; | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 667 | mbedtls_md_handle_t md_info; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | mbedtls_md_context_t md_ctx; | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 669 | int ret; | 
|  | 670 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | mbedtls_md_init( &md_ctx ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 672 |  | 
|  | 673 | if( sizeof( tmp ) < 20 + strlen( label ) + rlen ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 675 |  | 
|  | 676 | hs = ( slen + 1 ) / 2; | 
|  | 677 | S1 = secret; | 
|  | 678 | S2 = secret + slen - hs; | 
|  | 679 |  | 
|  | 680 | nb = strlen( label ); | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 681 | mbedtls_platform_memcpy( tmp + 20, label, nb ); | 
|  | 682 | mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 683 | nb += rlen; | 
|  | 684 |  | 
|  | 685 | /* | 
|  | 686 | * First compute P_md5(secret,label+random)[0..dlen] | 
|  | 687 | */ | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 688 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == | 
|  | 689 | MBEDTLS_MD_INVALID_HANDLE ) | 
|  | 690 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 692 | } | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 693 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 695 | return( ret ); | 
|  | 696 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); | 
|  | 698 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); | 
|  | 699 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 700 |  | 
|  | 701 | for( i = 0; i < dlen; i += 16 ) | 
|  | 702 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 704 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); | 
|  | 705 | mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 706 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 707 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 708 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); | 
|  | 709 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 710 |  | 
|  | 711 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; | 
|  | 712 |  | 
|  | 713 | for( j = 0; j < k; j++ ) | 
|  | 714 | dstbuf[i + j]  = h_i[j]; | 
|  | 715 | } | 
|  | 716 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 718 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 719 | /* | 
|  | 720 | * XOR out with P_sha1(secret,label+random)[0..dlen] | 
|  | 721 | */ | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 722 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == | 
|  | 723 | MBEDTLS_MD_INVALID_HANDLE ) | 
|  | 724 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 725 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 726 | } | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 727 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 729 | return( ret ); | 
|  | 730 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 731 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); | 
|  | 732 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); | 
|  | 733 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 734 |  | 
|  | 735 | for( i = 0; i < dlen; i += 20 ) | 
|  | 736 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 737 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 738 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); | 
|  | 739 | mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 740 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 742 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); | 
|  | 743 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 744 |  | 
|  | 745 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; | 
|  | 746 |  | 
|  | 747 | for( j = 0; j < k; j++ ) | 
|  | 748 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); | 
|  | 749 | } | 
|  | 750 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 751 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 752 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 753 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
|  | 754 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 755 |  | 
|  | 756 | return( 0 ); | 
|  | 757 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 758 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 759 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 760 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | f6cc742 | 2019-08-16 14:34:52 +0100 | [diff] [blame] | 761 | #if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) ) | 
|  | 762 | MBEDTLS_ALWAYS_INLINE static inline | 
|  | 763 | #else | 
|  | 764 | static | 
|  | 765 | #endif | 
|  | 766 | int tls_prf_generic( mbedtls_md_type_t md_type, | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 767 | const unsigned char *secret, size_t slen, | 
|  | 768 | const char *label, | 
|  | 769 | const unsigned char *random, size_t rlen, | 
|  | 770 | unsigned char *dstbuf, size_t dlen ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 771 | { | 
|  | 772 | size_t nb; | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 773 | size_t i, j, k, md_len; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 774 | unsigned char tmp[128]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 775 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 776 | mbedtls_md_handle_t md_info; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | mbedtls_md_context_t md_ctx; | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 778 | int ret; | 
|  | 779 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 780 | mbedtls_md_init( &md_ctx ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 781 |  | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 782 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == | 
|  | 783 | MBEDTLS_MD_INVALID_HANDLE ) | 
|  | 784 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 785 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 786 | } | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 787 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 788 | md_len = mbedtls_md_get_size( md_info ); | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 789 |  | 
|  | 790 | if( sizeof( tmp ) < md_len + strlen( label ) + rlen ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 791 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 792 |  | 
|  | 793 | nb = strlen( label ); | 
| Teppo Järvelin | 8f7e36f | 2020-01-02 10:40:19 +0200 | [diff] [blame] | 794 | (void)mbedtls_platform_memcpy( tmp + md_len, label, nb ); | 
|  | 795 | (void)mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 796 | nb += rlen; | 
|  | 797 |  | 
|  | 798 | /* | 
|  | 799 | * Compute P_<hash>(secret, label + random)[0..dlen] | 
|  | 800 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 801 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 802 | return( ret ); | 
|  | 803 |  | 
| Teppo Järvelin | 8f7e36f | 2020-01-02 10:40:19 +0200 | [diff] [blame] | 804 | if ( ( ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ) ) != 0 ) | 
|  | 805 | return( ret ); | 
|  | 806 | if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ) ) != 0 ) | 
|  | 807 | return( ret ); | 
|  | 808 | if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 ) | 
|  | 809 | return( ret ); | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 810 |  | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 811 | for( i = 0; i < dlen; i += md_len ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 812 | { | 
| Teppo Järvelin | 8f7e36f | 2020-01-02 10:40:19 +0200 | [diff] [blame] | 813 | if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 ) | 
|  | 814 | return( ret ); | 
|  | 815 | if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ) ) != 0 ) | 
|  | 816 | return( ret ); | 
|  | 817 | if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, h_i ) ) != 0 ) | 
|  | 818 | return( ret ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 819 |  | 
| Teppo Järvelin | 8f7e36f | 2020-01-02 10:40:19 +0200 | [diff] [blame] | 820 | if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 ) | 
|  | 821 | return( ret ); | 
|  | 822 | if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ) ) != 0 ) | 
|  | 823 | return( ret ); | 
|  | 824 | if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 ) | 
|  | 825 | return( ret ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 826 |  | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 827 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 828 |  | 
|  | 829 | for( j = 0; j < k; j++ ) | 
|  | 830 | dstbuf[i + j]  = h_i[j]; | 
|  | 831 | } | 
|  | 832 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 833 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 834 |  | 
| Teppo Järvelin | 8f7e36f | 2020-01-02 10:40:19 +0200 | [diff] [blame] | 835 | (void)mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
|  | 836 | (void)mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 837 |  | 
|  | 838 | return( 0 ); | 
|  | 839 | } | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 840 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 841 | #if defined(MBEDTLS_SHA256_C) | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 842 | MBEDTLS_NO_INLINE static int tls_prf_sha256( | 
|  | 843 | const unsigned char *secret, size_t slen, | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 844 | const char *label, | 
|  | 845 | const unsigned char *random, size_t rlen, | 
|  | 846 | unsigned char *dstbuf, size_t dlen ) | 
|  | 847 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 848 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 849 | label, random, rlen, dstbuf, dlen ) ); | 
|  | 850 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | #endif /* MBEDTLS_SHA256_C */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 852 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | #if defined(MBEDTLS_SHA512_C) | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 854 | MBEDTLS_NO_INLINE static int tls_prf_sha384( | 
|  | 855 | const unsigned char *secret, size_t slen, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 856 | const char *label, | 
|  | 857 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 858 | unsigned char *dstbuf, size_t dlen ) | 
|  | 859 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 861 | label, random, rlen, dstbuf, dlen ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 862 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 863 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 864 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 865 |  | 
| Hanno Becker | 39c7f7e | 2019-08-15 16:17:34 +0100 | [diff] [blame] | 866 | /* | 
|  | 867 | * Call the appropriate PRF function | 
|  | 868 | */ | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 869 | MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver, | 
| Hanno Becker | 39c7f7e | 2019-08-15 16:17:34 +0100 | [diff] [blame] | 870 | mbedtls_md_type_t hash, | 
|  | 871 | const unsigned char *secret, size_t slen, | 
|  | 872 | const char *label, | 
|  | 873 | const unsigned char *random, size_t rlen, | 
|  | 874 | unsigned char *dstbuf, size_t dlen ) | 
|  | 875 | { | 
|  | 876 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) | 
|  | 877 | (void) hash; | 
|  | 878 | #endif | 
|  | 879 |  | 
|  | 880 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 881 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
|  | 882 | return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); | 
|  | 883 | else | 
|  | 884 | #endif | 
|  | 885 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 886 | if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) ) | 
| Hanno Becker | 39c7f7e | 2019-08-15 16:17:34 +0100 | [diff] [blame] | 887 | return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); | 
|  | 888 | else | 
|  | 889 | #endif | 
|  | 890 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 891 | #if defined(MBEDTLS_SHA512_C) | 
|  | 892 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 893 | hash == MBEDTLS_MD_SHA384 ) | 
|  | 894 | { | 
|  | 895 | return( tls_prf_sha384( secret, slen, label, random, rlen, | 
|  | 896 | dstbuf, dlen ) ); | 
|  | 897 | } | 
|  | 898 | else | 
|  | 899 | #endif | 
|  | 900 | #if defined(MBEDTLS_SHA256_C) | 
|  | 901 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 902 | { | 
|  | 903 | return( tls_prf_sha256( secret, slen, label, random, rlen, | 
|  | 904 | dstbuf, dlen ) ); | 
|  | 905 | } | 
|  | 906 | #endif | 
|  | 907 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 908 |  | 
|  | 909 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 910 | } | 
| Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 911 |  | 
| Hanno Becker | fc7429e | 2019-08-16 10:12:21 +0100 | [diff] [blame] | 912 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 913 | MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl( | 
| Hanno Becker | fc7429e | 2019-08-16 10:12:21 +0100 | [diff] [blame] | 914 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
|  | 915 | { | 
|  | 916 | const char *sender; | 
|  | 917 | mbedtls_md5_context  md5; | 
|  | 918 | mbedtls_sha1_context sha1; | 
|  | 919 |  | 
|  | 920 | unsigned char padbuf[48]; | 
|  | 921 | unsigned char md5sum[16]; | 
|  | 922 | unsigned char sha1sum[20]; | 
|  | 923 |  | 
|  | 924 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 925 | if( !session ) | 
|  | 926 | session = ssl->session; | 
|  | 927 |  | 
|  | 928 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished ssl" ) ); | 
|  | 929 |  | 
|  | 930 | mbedtls_md5_init( &md5 ); | 
|  | 931 | mbedtls_sha1_init( &sha1 ); | 
|  | 932 |  | 
|  | 933 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 934 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
|  | 935 |  | 
|  | 936 | /* | 
|  | 937 | * SSLv3: | 
|  | 938 | *   hash = | 
|  | 939 | *      MD5( master + pad2 + | 
|  | 940 | *          MD5( handshake + sender + master + pad1 ) ) | 
|  | 941 | *   + SHA1( master + pad2 + | 
|  | 942 | *         SHA1( handshake + sender + master + pad1 ) ) | 
|  | 943 | */ | 
|  | 944 |  | 
|  | 945 | #if !defined(MBEDTLS_MD5_ALT) | 
|  | 946 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished  md5 state", (unsigned char *) | 
|  | 947 | md5.state, sizeof(  md5.state ) ); | 
|  | 948 | #endif | 
|  | 949 |  | 
|  | 950 | #if !defined(MBEDTLS_SHA1_ALT) | 
|  | 951 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) | 
|  | 952 | sha1.state, sizeof( sha1.state ) ); | 
|  | 953 | #endif | 
|  | 954 |  | 
|  | 955 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" | 
|  | 956 | : "SRVR"; | 
|  | 957 |  | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 958 | mbedtls_platform_memset( padbuf, 0x36, 48 ); | 
| Hanno Becker | fc7429e | 2019-08-16 10:12:21 +0100 | [diff] [blame] | 959 |  | 
|  | 960 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); | 
|  | 961 | mbedtls_md5_update_ret( &md5, session->master, 48 ); | 
|  | 962 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); | 
|  | 963 | mbedtls_md5_finish_ret( &md5, md5sum ); | 
|  | 964 |  | 
|  | 965 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); | 
|  | 966 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); | 
|  | 967 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); | 
|  | 968 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); | 
|  | 969 |  | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 970 | mbedtls_platform_memset( padbuf, 0x5C, 48 ); | 
| Hanno Becker | fc7429e | 2019-08-16 10:12:21 +0100 | [diff] [blame] | 971 |  | 
|  | 972 | mbedtls_md5_starts_ret( &md5 ); | 
|  | 973 | mbedtls_md5_update_ret( &md5, session->master, 48 ); | 
|  | 974 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); | 
|  | 975 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); | 
|  | 976 | mbedtls_md5_finish_ret( &md5, buf ); | 
|  | 977 |  | 
|  | 978 | mbedtls_sha1_starts_ret( &sha1 ); | 
|  | 979 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); | 
|  | 980 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); | 
|  | 981 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); | 
|  | 982 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); | 
|  | 983 |  | 
|  | 984 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); | 
|  | 985 |  | 
|  | 986 | mbedtls_md5_free(  &md5  ); | 
|  | 987 | mbedtls_sha1_free( &sha1 ); | 
|  | 988 |  | 
|  | 989 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
|  | 990 | mbedtls_platform_zeroize(  md5sum, sizeof(  md5sum ) ); | 
|  | 991 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); | 
|  | 992 |  | 
|  | 993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
|  | 994 | } | 
|  | 995 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 996 |  | 
|  | 997 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 998 | MBEDTLS_NO_INLINE static void ssl_calc_finished_tls( | 
| Hanno Becker | fc7429e | 2019-08-16 10:12:21 +0100 | [diff] [blame] | 999 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
|  | 1000 | { | 
|  | 1001 | int len = 12; | 
|  | 1002 | const char *sender; | 
|  | 1003 | mbedtls_md5_context  md5; | 
|  | 1004 | mbedtls_sha1_context sha1; | 
|  | 1005 | unsigned char padbuf[36]; | 
|  | 1006 |  | 
|  | 1007 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 1008 | if( !session ) | 
|  | 1009 | session = ssl->session; | 
|  | 1010 |  | 
|  | 1011 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls" ) ); | 
|  | 1012 |  | 
|  | 1013 | mbedtls_md5_init( &md5 ); | 
|  | 1014 | mbedtls_sha1_init( &sha1 ); | 
|  | 1015 |  | 
|  | 1016 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 1017 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
|  | 1018 |  | 
|  | 1019 | /* | 
|  | 1020 | * TLSv1: | 
|  | 1021 | *   hash = PRF( master, finished_label, | 
|  | 1022 | *               MD5( handshake ) + SHA1( handshake ) )[0..11] | 
|  | 1023 | */ | 
|  | 1024 |  | 
|  | 1025 | #if !defined(MBEDTLS_MD5_ALT) | 
|  | 1026 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished  md5 state", (unsigned char *) | 
|  | 1027 | md5.state, sizeof(  md5.state ) ); | 
|  | 1028 | #endif | 
|  | 1029 |  | 
|  | 1030 | #if !defined(MBEDTLS_SHA1_ALT) | 
|  | 1031 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) | 
|  | 1032 | sha1.state, sizeof( sha1.state ) ); | 
|  | 1033 | #endif | 
|  | 1034 |  | 
|  | 1035 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 1036 | ? "client finished" | 
|  | 1037 | : "server finished"; | 
|  | 1038 |  | 
|  | 1039 | mbedtls_md5_finish_ret(  &md5, padbuf ); | 
|  | 1040 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); | 
|  | 1041 |  | 
|  | 1042 | ssl_prf( mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 1043 | mbedtls_ssl_suite_get_mac( | 
|  | 1044 | mbedtls_ssl_ciphersuite_from_id( | 
|  | 1045 | mbedtls_ssl_session_get_ciphersuite( session ) ) ), | 
|  | 1046 | session->master, 48, sender, | 
|  | 1047 | padbuf, 36, buf, len ); | 
|  | 1048 |  | 
|  | 1049 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
|  | 1050 |  | 
|  | 1051 | mbedtls_md5_free(  &md5  ); | 
|  | 1052 | mbedtls_sha1_free( &sha1 ); | 
|  | 1053 |  | 
|  | 1054 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
|  | 1055 |  | 
|  | 1056 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
|  | 1057 | } | 
|  | 1058 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 1059 |  | 
|  | 1060 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1061 | #if defined(MBEDTLS_SHA256_C) | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 1062 | MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256( | 
| Hanno Becker | fc7429e | 2019-08-16 10:12:21 +0100 | [diff] [blame] | 1063 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
|  | 1064 | { | 
|  | 1065 | int len = 12; | 
|  | 1066 | const char *sender; | 
|  | 1067 | mbedtls_sha256_context sha256; | 
|  | 1068 | unsigned char padbuf[32]; | 
|  | 1069 |  | 
|  | 1070 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 1071 | if( !session ) | 
|  | 1072 | session = ssl->session; | 
|  | 1073 |  | 
|  | 1074 | mbedtls_sha256_init( &sha256 ); | 
|  | 1075 |  | 
|  | 1076 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha256" ) ); | 
|  | 1077 |  | 
|  | 1078 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
|  | 1079 |  | 
|  | 1080 | /* | 
|  | 1081 | * TLSv1.2: | 
|  | 1082 | *   hash = PRF( master, finished_label, | 
|  | 1083 | *               Hash( handshake ) )[0.11] | 
|  | 1084 | */ | 
|  | 1085 |  | 
|  | 1086 | #if !defined(MBEDTLS_SHA256_ALT) | 
|  | 1087 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) | 
|  | 1088 | sha256.state, sizeof( sha256.state ) ); | 
|  | 1089 | #endif | 
|  | 1090 |  | 
|  | 1091 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 1092 | ? "client finished" | 
|  | 1093 | : "server finished"; | 
|  | 1094 |  | 
|  | 1095 | mbedtls_sha256_finish_ret( &sha256, padbuf ); | 
|  | 1096 |  | 
|  | 1097 | ssl_prf( mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 1098 | mbedtls_ssl_suite_get_mac( | 
|  | 1099 | mbedtls_ssl_ciphersuite_from_id( | 
|  | 1100 | mbedtls_ssl_session_get_ciphersuite( session ) ) ), | 
|  | 1101 | session->master, 48, sender, | 
|  | 1102 | padbuf, 32, buf, len ); | 
|  | 1103 |  | 
|  | 1104 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
|  | 1105 |  | 
|  | 1106 | mbedtls_sha256_free( &sha256 ); | 
|  | 1107 |  | 
|  | 1108 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
|  | 1109 |  | 
|  | 1110 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
|  | 1111 | } | 
|  | 1112 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 1113 |  | 
|  | 1114 | #if defined(MBEDTLS_SHA512_C) | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 1115 | MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384( | 
| Hanno Becker | fc7429e | 2019-08-16 10:12:21 +0100 | [diff] [blame] | 1116 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
|  | 1117 | { | 
|  | 1118 | int len = 12; | 
|  | 1119 | const char *sender; | 
|  | 1120 | mbedtls_sha512_context sha512; | 
|  | 1121 | unsigned char padbuf[48]; | 
|  | 1122 |  | 
|  | 1123 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 1124 | if( !session ) | 
|  | 1125 | session = ssl->session; | 
|  | 1126 |  | 
|  | 1127 | mbedtls_sha512_init( &sha512 ); | 
|  | 1128 |  | 
|  | 1129 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha384" ) ); | 
|  | 1130 |  | 
|  | 1131 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
|  | 1132 |  | 
|  | 1133 | /* | 
|  | 1134 | * TLSv1.2: | 
|  | 1135 | *   hash = PRF( master, finished_label, | 
|  | 1136 | *               Hash( handshake ) )[0.11] | 
|  | 1137 | */ | 
|  | 1138 |  | 
|  | 1139 | #if !defined(MBEDTLS_SHA512_ALT) | 
|  | 1140 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) | 
|  | 1141 | sha512.state, sizeof( sha512.state ) ); | 
|  | 1142 | #endif | 
|  | 1143 |  | 
|  | 1144 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 1145 | ? "client finished" | 
|  | 1146 | : "server finished"; | 
|  | 1147 |  | 
|  | 1148 | mbedtls_sha512_finish_ret( &sha512, padbuf ); | 
|  | 1149 |  | 
|  | 1150 | ssl_prf( mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 1151 | mbedtls_ssl_suite_get_mac( | 
|  | 1152 | mbedtls_ssl_ciphersuite_from_id( | 
|  | 1153 | mbedtls_ssl_session_get_ciphersuite( session ) ) ), | 
|  | 1154 | session->master, 48, sender, | 
|  | 1155 | padbuf, 48, buf, len ); | 
|  | 1156 |  | 
|  | 1157 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
|  | 1158 |  | 
|  | 1159 | mbedtls_sha512_free( &sha512 ); | 
|  | 1160 |  | 
|  | 1161 | mbedtls_platform_zeroize(  padbuf, sizeof( padbuf ) ); | 
|  | 1162 |  | 
|  | 1163 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
|  | 1164 | } | 
|  | 1165 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 1166 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 1167 |  | 
| Hanno Becker | 2793f74 | 2019-08-16 14:28:43 +0100 | [diff] [blame] | 1168 | MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished( | 
|  | 1169 | int minor_ver, | 
| Hanno Becker | c2fb759 | 2019-08-15 16:31:23 +0100 | [diff] [blame] | 1170 | mbedtls_md_type_t hash, | 
|  | 1171 | mbedtls_ssl_context *ssl, | 
|  | 1172 | unsigned char *buf, | 
|  | 1173 | int from ) | 
|  | 1174 | { | 
|  | 1175 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) | 
|  | 1176 | (void) hash; | 
|  | 1177 | #endif | 
|  | 1178 |  | 
|  | 1179 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 1180 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
|  | 1181 | ssl_calc_finished_ssl( ssl, buf, from ); | 
|  | 1182 | else | 
|  | 1183 | #endif | 
|  | 1184 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1185 | if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) ) | 
| Hanno Becker | c2fb759 | 2019-08-15 16:31:23 +0100 | [diff] [blame] | 1186 | ssl_calc_finished_tls( ssl, buf, from ); | 
|  | 1187 | else | 
|  | 1188 | #endif | 
|  | 1189 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1190 | #if defined(MBEDTLS_SHA512_C) | 
|  | 1191 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 1192 | hash == MBEDTLS_MD_SHA384 ) | 
|  | 1193 | { | 
|  | 1194 | ssl_calc_finished_tls_sha384( ssl, buf, from ); | 
|  | 1195 | } | 
|  | 1196 | else | 
|  | 1197 | #endif | 
|  | 1198 | #if defined(MBEDTLS_SHA256_C) | 
|  | 1199 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 1200 | ssl_calc_finished_tls_sha256( ssl, buf, from ); | 
|  | 1201 | else | 
|  | 1202 | #endif | 
|  | 1203 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 1204 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 1205 |  | 
|  | 1206 | return( 0 ); | 
|  | 1207 | } | 
|  | 1208 |  | 
| Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1209 | /* | 
| Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1210 | * Populate a transform structure with session keys and all the other | 
|  | 1211 | * necessary information. | 
| Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1212 | * | 
| Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1213 | * Parameters: | 
|  | 1214 | * - [in/out]: transform: structure to populate | 
|  | 1215 | *      [in] must be just initialised with mbedtls_ssl_transform_init() | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1216 | *      [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1217 | * - [in] ciphersuite | 
|  | 1218 | * - [in] master | 
|  | 1219 | * - [in] encrypt_then_mac | 
|  | 1220 | * - [in] trunc_hmac | 
|  | 1221 | * - [in] compression | 
| Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1222 | * - [in] tls_prf: pointer to PRF to use for key derivation | 
|  | 1223 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1224 | * - [in] minor_ver: SSL/TLS minor version | 
|  | 1225 | * - [in] endpoint: client or server | 
|  | 1226 | * - [in] ssl: optionally used for: | 
|  | 1227 | *        - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context | 
|  | 1228 | *        - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys | 
|  | 1229 | *        - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg | 
| Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1230 | */ | 
| Hanno Becker | 298a470 | 2019-08-16 10:21:32 +0100 | [diff] [blame] | 1231 | /* Force compilers to inline this function if it's used only | 
|  | 1232 | * from one place, because at least ARMC5 doesn't do that | 
|  | 1233 | * automatically. */ | 
|  | 1234 | #if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
|  | 1235 | MBEDTLS_ALWAYS_INLINE static inline | 
|  | 1236 | #else | 
|  | 1237 | static | 
|  | 1238 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
|  | 1239 | int ssl_populate_transform( mbedtls_ssl_transform *transform, | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1240 | int ciphersuite, | 
|  | 1241 | const unsigned char master[48], | 
| Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1242 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1243 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 1244 | int encrypt_then_mac, | 
|  | 1245 | #endif | 
|  | 1246 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 1247 | int trunc_hmac, | 
|  | 1248 | #endif | 
| Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1249 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1250 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 1251 | int compression, | 
|  | 1252 | #endif | 
| Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1253 | const unsigned char randbytes[64], | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1254 | int minor_ver, | 
|  | 1255 | unsigned endpoint, | 
| Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1256 | const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1257 | { | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1258 | int ret = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1259 | unsigned char keyblk[256]; | 
|  | 1260 | unsigned char *key1; | 
|  | 1261 | unsigned char *key2; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1262 | unsigned char *mac_enc; | 
|  | 1263 | unsigned char *mac_dec; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1264 | size_t mac_key_len; | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1265 | size_t iv_copy_len; | 
| Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1266 | unsigned keylen; | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1267 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1268 | const mbedtls_cipher_info_t *cipher_info; | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1269 | mbedtls_md_handle_t md_info; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1270 |  | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1271 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ | 
|  | 1272 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ | 
|  | 1273 | !defined(MBEDTLS_DEBUG_C) | 
| Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1274 | ssl = NULL; /* make sure we don't use it except for those cases */ | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1275 | (void) ssl; | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1276 | #endif | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1277 |  | 
| Manuel Pégourié-Gonnard | a3024ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 1278 | /* | 
|  | 1279 | * Some data just needs copying into the structure | 
|  | 1280 | */ | 
| Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1281 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ | 
|  | 1282 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1283 | transform->encrypt_then_mac = encrypt_then_mac; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1284 | #endif | 
| Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 1285 |  | 
|  | 1286 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1287 | transform->minor_ver = minor_ver; | 
| Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 1288 | #else | 
|  | 1289 | ((void) minor_ver); | 
|  | 1290 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1291 |  | 
| Manuel Pégourié-Gonnard | a3024ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 1292 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1293 | mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); | 
| Manuel Pégourié-Gonnard | a3024ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 1294 | #endif | 
|  | 1295 |  | 
| Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1296 | /* | 
|  | 1297 | * Get various info structures | 
|  | 1298 | */ | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1299 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1300 | if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE ) | 
| Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1301 | { | 
|  | 1302 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1303 | ciphersuite ) ); | 
| Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1304 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1305 | } | 
|  | 1306 |  | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1307 | cipher_info = mbedtls_cipher_info_from_type( | 
|  | 1308 | mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1309 | if( cipher_info == NULL ) | 
|  | 1310 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1311 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1312 | mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1313 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1314 | } | 
|  | 1315 |  | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1316 | md_info = mbedtls_md_info_from_type( | 
|  | 1317 | mbedtls_ssl_suite_get_mac( ciphersuite_info ) ); | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 1318 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1319 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1321 | mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1322 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1323 | } | 
|  | 1324 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1325 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1326 | /* Copy own and peer's CID if the use of the CID | 
|  | 1327 | * extension has been negotiated. */ | 
|  | 1328 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) | 
|  | 1329 | { | 
|  | 1330 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); | 
| Hanno Becker | d91dc37 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 1331 |  | 
| Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 1332 | transform->in_cid_len = ssl->own_cid_len; | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 1333 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 1334 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); | 
| Hanno Becker | 8013b27 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 1335 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, | 
| Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1336 | transform->in_cid_len ); | 
| Hanno Becker | e582d12 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 1337 |  | 
|  | 1338 | transform->out_cid_len = ssl->handshake->peer_cid_len; | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 1339 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 1340 | memcpy( transform->out_cid, ssl->handshake->peer_cid, | 
| Hanno Becker | e582d12 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 1341 | ssl->handshake->peer_cid_len ); | 
|  | 1342 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, | 
|  | 1343 | transform->out_cid_len ); | 
| Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1344 | } | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1345 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 1346 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1347 | /* | 
| Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1348 | * Compute key block using the PRF | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1349 | */ | 
| Hanno Becker | 39c7f7e | 2019-08-15 16:17:34 +0100 | [diff] [blame] | 1350 | ret = ssl_prf( minor_ver, | 
|  | 1351 | mbedtls_ssl_suite_get_mac( ciphersuite_info ), | 
|  | 1352 | master, 48, "key expansion", randbytes, 64, | 
|  | 1353 | keyblk, 256 ); | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1354 | if( ret != 0 ) | 
|  | 1355 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1356 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1357 | return( ret ); | 
|  | 1358 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1359 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", | 
| Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1361 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1362 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); | 
| Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1363 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1364 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1365 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1366 | /* | 
|  | 1367 | * Determine the appropriate key, IV and MAC length. | 
|  | 1368 | */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1369 |  | 
| Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1370 | keylen = cipher_info->key_bitlen / 8; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1371 |  | 
| Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1372 | #if defined(MBEDTLS_GCM_C) ||                           \ | 
|  | 1373 | defined(MBEDTLS_CCM_C) ||                           \ | 
|  | 1374 | defined(MBEDTLS_CHACHAPOLY_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1375 | if( cipher_info->mode == MBEDTLS_MODE_GCM || | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1376 | cipher_info->mode == MBEDTLS_MODE_CCM || | 
|  | 1377 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1378 | { | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1379 | transform->maclen = 0; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1380 | mac_key_len = 0; | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 1381 | transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) & | 
|  | 1382 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1383 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1384 | /* All modes haves 96-bit IVs; | 
|  | 1385 | * GCM and CCM has 4 implicit and 8 explicit bytes | 
|  | 1386 | * ChachaPoly has all 12 bytes implicit | 
|  | 1387 | */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1388 | transform->ivlen = 12; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1389 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) | 
|  | 1390 | transform->fixed_ivlen = 12; | 
|  | 1391 | else | 
|  | 1392 | transform->fixed_ivlen = 4; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1393 | } | 
|  | 1394 | else | 
| Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1395 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ | 
|  | 1396 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
|  | 1397 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || | 
|  | 1398 | cipher_info->mode == MBEDTLS_MODE_CBC ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1399 | { | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1400 | /* Initialize HMAC contexts */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1401 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || | 
|  | 1402 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1403 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1404 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1405 | return( ret ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1406 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1407 |  | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1408 | /* Get MAC length */ | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1409 | mac_key_len = mbedtls_md_get_size( md_info ); | 
|  | 1410 | transform->maclen = mac_key_len; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1411 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1412 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1413 | /* | 
|  | 1414 | * If HMAC is to be truncated, we shall keep the leftmost bytes, | 
|  | 1415 | * (rfc 6066 page 13 or rfc 2104 section 4), | 
|  | 1416 | * so we only need to adjust the length here. | 
|  | 1417 | */ | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1418 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1419 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1420 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1421 |  | 
|  | 1422 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) | 
|  | 1423 | /* Fall back to old, non-compliant version of the truncated | 
| Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1424 | * HMAC implementation which also truncates the key | 
|  | 1425 | * (Mbed TLS versions from 1.3 to 2.6.0) */ | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1426 | mac_key_len = transform->maclen; | 
|  | 1427 | #endif | 
|  | 1428 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1429 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1430 |  | 
|  | 1431 | /* IV length */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1432 | transform->ivlen = cipher_info->iv_size; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1433 | } | 
| Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1434 | else | 
|  | 1435 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
|  | 1436 | { | 
|  | 1437 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1438 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 1439 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1440 |  | 
| Hanno Becker | a9d5c45 | 2019-07-25 16:47:12 +0100 | [diff] [blame] | 1441 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u", | 
| Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1442 | (unsigned) keylen, | 
| Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1443 | (unsigned) transform->ivlen, | 
|  | 1444 | (unsigned) transform->maclen ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1445 |  | 
|  | 1446 | /* | 
|  | 1447 | * Finally setup the cipher contexts, IVs and MAC secrets. | 
|  | 1448 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1450 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1451 | { | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1452 | key1 = keyblk + mac_key_len * 2; | 
| Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1453 | key2 = keyblk + mac_key_len * 2 + keylen; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1454 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1455 | mac_enc = keyblk; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1456 | mac_dec = keyblk + mac_key_len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1457 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1458 | /* | 
|  | 1459 | * This is not used in TLS v1.1. | 
|  | 1460 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1461 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 1462 | transform->fixed_ivlen : transform->ivlen; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1463 | mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen,  iv_copy_len ); | 
|  | 1464 | mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1465 | iv_copy_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1466 | } | 
|  | 1467 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1468 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 1469 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1470 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1471 | { | 
| Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1472 | key1 = keyblk + mac_key_len * 2 + keylen; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1473 | key2 = keyblk + mac_key_len * 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1474 |  | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1475 | mac_enc = keyblk + mac_key_len; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1476 | mac_dec = keyblk; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1477 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1478 | /* | 
|  | 1479 | * This is not used in TLS v1.1. | 
|  | 1480 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1481 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 1482 | transform->fixed_ivlen : transform->ivlen; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1483 | mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen,  iv_copy_len ); | 
|  | 1484 | mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1485 | iv_copy_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1486 | } | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1487 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1489 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1491 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1492 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1493 |  | 
| Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1494 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1495 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1496 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1497 | { | 
| Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1498 | if( mac_key_len > sizeof( transform->mac_enc ) ) | 
| Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1499 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1500 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1501 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1502 | } | 
|  | 1503 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1504 | mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len ); | 
|  | 1505 | mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1506 | } | 
|  | 1507 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1508 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 1509 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 1510 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1511 | if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1512 | { | 
| Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1513 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. | 
|  | 1514 | For AEAD-based ciphersuites, there is nothing to do here. */ | 
|  | 1515 | if( mac_key_len != 0 ) | 
|  | 1516 | { | 
|  | 1517 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); | 
|  | 1518 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); | 
|  | 1519 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1520 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1521 | else | 
|  | 1522 | #endif | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1523 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1524 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1525 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1526 | } | 
| Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1527 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1528 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1529 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 1530 | if( mbedtls_ssl_hw_record_init != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1531 | { | 
|  | 1532 | int ret = 0; | 
|  | 1533 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1534 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1535 |  | 
| Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1536 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1537 | transform->iv_enc, transform->iv_dec, | 
|  | 1538 | iv_copy_len, | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1539 | mac_enc, mac_dec, | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1540 | mac_key_len ) ) != 0 ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1541 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); | 
|  | 1543 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1544 | } | 
|  | 1545 | } | 
| Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1546 | #else | 
|  | 1547 | ((void) mac_dec); | 
|  | 1548 | ((void) mac_enc); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1550 |  | 
| Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1551 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) | 
|  | 1552 | if( ssl->conf->f_export_keys != NULL ) | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1553 | { | 
| Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1554 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1555 | master, keyblk, | 
| Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1556 | mac_key_len, keylen, | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1557 | iv_copy_len ); | 
|  | 1558 | } | 
|  | 1559 | #endif | 
|  | 1560 |  | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1561 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1562 | cipher_info ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1563 | { | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1564 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1565 | return( ret ); | 
|  | 1566 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1567 |  | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1568 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1569 | cipher_info ) ) != 0 ) | 
|  | 1570 | { | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1571 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1572 | return( ret ); | 
|  | 1573 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1574 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1575 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, | 
| Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1576 | cipher_info->key_bitlen, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1577 | MBEDTLS_ENCRYPT ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1578 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1579 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1580 | return( ret ); | 
|  | 1581 | } | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1582 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1583 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, | 
| Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1584 | cipher_info->key_bitlen, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1585 | MBEDTLS_DECRYPT ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1586 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1587 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1588 | return( ret ); | 
|  | 1589 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1590 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1591 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
|  | 1592 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1593 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, | 
|  | 1595 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1596 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1597 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1598 | return( ret ); | 
| Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1599 | } | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1600 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1601 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, | 
|  | 1602 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1603 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1604 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1605 | return( ret ); | 
|  | 1606 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1607 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1608 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1609 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1610 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1611 |  | 
| Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1612 | /* Initialize Zlib contexts */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1613 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1614 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1615 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1617 |  | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 1618 | mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); | 
|  | 1619 | mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1620 |  | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1621 | if( deflateInit( &transform->ctx_deflate, | 
|  | 1622 | Z_DEFAULT_COMPRESSION )   != Z_OK || | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1623 | inflateInit( &transform->ctx_inflate ) != Z_OK ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1624 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); | 
|  | 1626 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1627 | } | 
|  | 1628 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1629 | #endif /* MBEDTLS_ZLIB_SUPPORT */ | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1630 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1631 | return( 0 ); | 
|  | 1632 | } | 
|  | 1633 |  | 
| Hanno Becker | cf87c5e | 2019-08-16 10:11:21 +0100 | [diff] [blame] | 1634 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 1635 | static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, | 
|  | 1636 | unsigned char hash[36], | 
|  | 1637 | size_t *hlen ) | 
|  | 1638 | { | 
|  | 1639 | mbedtls_md5_context md5; | 
|  | 1640 | mbedtls_sha1_context sha1; | 
|  | 1641 | unsigned char pad_1[48]; | 
|  | 1642 | unsigned char pad_2[48]; | 
|  | 1643 |  | 
|  | 1644 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); | 
|  | 1645 |  | 
|  | 1646 | mbedtls_md5_init( &md5 ); | 
|  | 1647 | mbedtls_sha1_init( &sha1 ); | 
|  | 1648 |  | 
|  | 1649 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 1650 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
|  | 1651 |  | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 1652 | mbedtls_platform_memset( pad_1, 0x36, 48 ); | 
|  | 1653 | mbedtls_platform_memset( pad_2, 0x5C, 48 ); | 
| Hanno Becker | cf87c5e | 2019-08-16 10:11:21 +0100 | [diff] [blame] | 1654 |  | 
|  | 1655 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); | 
|  | 1656 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); | 
|  | 1657 | mbedtls_md5_finish_ret( &md5, hash ); | 
|  | 1658 |  | 
|  | 1659 | mbedtls_md5_starts_ret( &md5 ); | 
|  | 1660 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); | 
|  | 1661 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); | 
|  | 1662 | mbedtls_md5_update_ret( &md5, hash,  16 ); | 
|  | 1663 | mbedtls_md5_finish_ret( &md5, hash ); | 
|  | 1664 |  | 
|  | 1665 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); | 
|  | 1666 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); | 
|  | 1667 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
|  | 1668 |  | 
|  | 1669 | mbedtls_sha1_starts_ret( &sha1 ); | 
|  | 1670 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); | 
|  | 1671 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); | 
|  | 1672 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); | 
|  | 1673 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
|  | 1674 |  | 
|  | 1675 | *hlen = 36; | 
|  | 1676 |  | 
|  | 1677 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
|  | 1678 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
|  | 1679 |  | 
|  | 1680 | mbedtls_md5_free(  &md5  ); | 
|  | 1681 | mbedtls_sha1_free( &sha1 ); | 
|  | 1682 |  | 
|  | 1683 | return; | 
|  | 1684 | } | 
|  | 1685 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 1686 |  | 
|  | 1687 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 1688 | static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, | 
|  | 1689 | unsigned char hash[36], | 
|  | 1690 | size_t *hlen ) | 
|  | 1691 | { | 
|  | 1692 | mbedtls_md5_context md5; | 
|  | 1693 | mbedtls_sha1_context sha1; | 
|  | 1694 |  | 
|  | 1695 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); | 
|  | 1696 |  | 
|  | 1697 | mbedtls_md5_init( &md5 ); | 
|  | 1698 | mbedtls_sha1_init( &sha1 ); | 
|  | 1699 |  | 
|  | 1700 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 1701 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
|  | 1702 |  | 
|  | 1703 | mbedtls_md5_finish_ret( &md5,  hash ); | 
|  | 1704 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
|  | 1705 |  | 
|  | 1706 | *hlen = 36; | 
|  | 1707 |  | 
|  | 1708 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
|  | 1709 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
|  | 1710 |  | 
|  | 1711 | mbedtls_md5_free(  &md5  ); | 
|  | 1712 | mbedtls_sha1_free( &sha1 ); | 
|  | 1713 |  | 
|  | 1714 | return; | 
|  | 1715 | } | 
|  | 1716 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 1717 |  | 
|  | 1718 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1719 | #if defined(MBEDTLS_SHA256_C) | 
|  | 1720 | static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, | 
|  | 1721 | unsigned char hash[32], | 
|  | 1722 | size_t *hlen ) | 
|  | 1723 | { | 
|  | 1724 | mbedtls_sha256_context sha256; | 
|  | 1725 |  | 
|  | 1726 | mbedtls_sha256_init( &sha256 ); | 
|  | 1727 |  | 
|  | 1728 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); | 
|  | 1729 |  | 
|  | 1730 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
|  | 1731 | mbedtls_sha256_finish_ret( &sha256, hash ); | 
|  | 1732 |  | 
|  | 1733 | *hlen = 32; | 
|  | 1734 |  | 
|  | 1735 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
|  | 1736 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
|  | 1737 |  | 
|  | 1738 | mbedtls_sha256_free( &sha256 ); | 
|  | 1739 |  | 
|  | 1740 | return; | 
|  | 1741 | } | 
|  | 1742 | #endif /* MBEDTLS_SHA256_C */ | 
|  | 1743 |  | 
|  | 1744 | #if defined(MBEDTLS_SHA512_C) | 
|  | 1745 | static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, | 
|  | 1746 | unsigned char hash[48], | 
|  | 1747 | size_t *hlen ) | 
|  | 1748 | { | 
|  | 1749 | mbedtls_sha512_context sha512; | 
|  | 1750 |  | 
|  | 1751 | mbedtls_sha512_init( &sha512 ); | 
|  | 1752 |  | 
|  | 1753 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); | 
|  | 1754 |  | 
|  | 1755 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
|  | 1756 | mbedtls_sha512_finish_ret( &sha512, hash ); | 
|  | 1757 |  | 
|  | 1758 | *hlen = 48; | 
|  | 1759 |  | 
|  | 1760 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); | 
|  | 1761 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
|  | 1762 |  | 
|  | 1763 | mbedtls_sha512_free( &sha512 ); | 
|  | 1764 |  | 
|  | 1765 | return; | 
|  | 1766 | } | 
|  | 1767 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 1768 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 1769 |  | 
| Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 1770 | int mbedtls_ssl_calc_verify( int minor_ver, | 
|  | 1771 | mbedtls_md_type_t hash, | 
|  | 1772 | mbedtls_ssl_context const *ssl, | 
|  | 1773 | unsigned char *dst, | 
|  | 1774 | size_t *hlen ) | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1775 | { | 
| Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1776 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) | 
|  | 1777 | (void) hash; | 
|  | 1778 | #endif | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1779 |  | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1780 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1781 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 1782 | ssl_calc_verify_ssl( ssl, dst, hlen ); | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1783 | else | 
|  | 1784 | #endif | 
|  | 1785 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 1786 | if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) ) | 
| Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 1787 | ssl_calc_verify_tls( ssl, dst, hlen ); | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1788 | else | 
|  | 1789 | #endif | 
|  | 1790 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1791 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1792 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 1793 | hash == MBEDTLS_MD_SHA384 ) | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1794 | { | 
| Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 1795 | ssl_calc_verify_tls_sha384( ssl, dst, hlen ); | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1796 | } | 
|  | 1797 | else | 
|  | 1798 | #endif | 
|  | 1799 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1800 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1801 | { | 
| Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 1802 | ssl_calc_verify_tls_sha256( ssl, dst, hlen ); | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1803 | } | 
|  | 1804 | else | 
|  | 1805 | #endif | 
|  | 1806 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 1807 | { | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1808 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 1809 | } | 
|  | 1810 |  | 
|  | 1811 | return( 0 ); | 
|  | 1812 | } | 
|  | 1813 |  | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1814 | /* | 
| Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1815 | * Compute master secret if needed | 
| Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1816 | * | 
|  | 1817 | * Parameters: | 
|  | 1818 | * [in/out] handshake | 
|  | 1819 | *          [in] resume, premaster, extended_ms, calc_verify, tls_prf | 
|  | 1820 | *          [out] premaster (cleared) | 
| Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1821 | * [out] master | 
|  | 1822 | * [in] ssl: optionally used for debugging and calc_verify | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1823 | */ | 
| Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1824 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, | 
| Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1825 | unsigned char *master, | 
| Manuel Pégourié-Gonnard | ed3b7a9 | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1826 | const mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1827 | { | 
|  | 1828 | int ret; | 
| Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1829 |  | 
| Hanno Becker | 39c7f7e | 2019-08-15 16:17:34 +0100 | [diff] [blame] | 1830 | /* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */ | 
|  | 1831 | /*     ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */ | 
|  | 1832 | /*     (void) ssl; */ | 
|  | 1833 | /* #endif */ | 
|  | 1834 |  | 
|  | 1835 | mbedtls_ssl_ciphersuite_handle_t const ciphersuite = | 
|  | 1836 | mbedtls_ssl_handshake_get_ciphersuite( handshake ); | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1837 |  | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 1838 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) | 
| Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame] | 1839 | if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET ) | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1840 | { | 
| Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1841 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); | 
|  | 1842 | return( 0 ); | 
|  | 1843 | } | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 1844 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ | 
| Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1845 |  | 
|  | 1846 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, | 
|  | 1847 | handshake->pmslen ); | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1848 |  | 
|  | 1849 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Hanno Becker | a49ec56 | 2019-06-11 14:47:55 +0100 | [diff] [blame] | 1850 | if( mbedtls_ssl_hs_get_extended_ms( handshake ) | 
|  | 1851 | == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) | 
| Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1852 | { | 
|  | 1853 | unsigned char session_hash[48]; | 
|  | 1854 | size_t hash_len; | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1855 |  | 
| Hanno Becker | 2f41b24 | 2019-08-15 17:29:43 +0100 | [diff] [blame] | 1856 | mbedtls_ssl_calc_verify( | 
|  | 1857 | mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 1858 | mbedtls_ssl_suite_get_mac( ciphersuite ), | 
|  | 1859 | ssl, session_hash, &hash_len ); | 
| Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1860 |  | 
| Manuel Pégourié-Gonnard | 9c5bcc9 | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1861 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", | 
|  | 1862 | session_hash, hash_len ); | 
| Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1863 |  | 
| Hanno Becker | 39c7f7e | 2019-08-15 16:17:34 +0100 | [diff] [blame] | 1864 | ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 1865 | mbedtls_ssl_suite_get_mac( ciphersuite ), | 
|  | 1866 | handshake->premaster, handshake->pmslen, | 
|  | 1867 | "extended master secret", | 
|  | 1868 | session_hash, hash_len, | 
|  | 1869 | master, 48 ); | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1870 | } | 
|  | 1871 | else | 
| Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1872 | #endif | 
| Manuel Pégourié-Gonnard | bcf258e | 2019-05-03 11:46:27 +0200 | [diff] [blame] | 1873 | { | 
| Hanno Becker | 39c7f7e | 2019-08-15 16:17:34 +0100 | [diff] [blame] | 1874 | ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 1875 | mbedtls_ssl_suite_get_mac( ciphersuite ), | 
|  | 1876 | handshake->premaster, handshake->pmslen, | 
|  | 1877 | "master secret", | 
|  | 1878 | handshake->randbytes, 64, | 
|  | 1879 | master, 48 ); | 
| Manuel Pégourié-Gonnard | bcf258e | 2019-05-03 11:46:27 +0200 | [diff] [blame] | 1880 | } | 
| Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1881 | if( ret != 0 ) | 
|  | 1882 | { | 
|  | 1883 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
|  | 1884 | return( ret ); | 
|  | 1885 | } | 
|  | 1886 |  | 
|  | 1887 | mbedtls_platform_zeroize( handshake->premaster, | 
|  | 1888 | sizeof(handshake->premaster) ); | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1889 |  | 
|  | 1890 | return( 0 ); | 
|  | 1891 | } | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1892 |  | 
| Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1893 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) | 
|  | 1894 | { | 
| Jarno Lamsa | 5aa4c07 | 2019-12-20 12:42:49 +0200 | [diff] [blame] | 1895 | volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1896 |  | 
| Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1897 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); | 
| Jarno Lamsa | 4031a45 | 2019-12-19 08:11:12 +0200 | [diff] [blame] | 1898 | ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET; | 
| Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1899 | /* Compute master secret if needed */ | 
| Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1900 | ret = ssl_compute_master( ssl->handshake, | 
| Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1901 | ssl->session_negotiate->master, | 
|  | 1902 | ssl ); | 
| Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1903 | if( ret != 0 ) | 
|  | 1904 | { | 
|  | 1905 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); | 
|  | 1906 | return( ret ); | 
|  | 1907 | } | 
|  | 1908 |  | 
| Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1909 | /* Swap the client and server random values: | 
|  | 1910 | * - MS derivation wanted client+server (RFC 5246 8.1) | 
|  | 1911 | * - key derivation wants server+client (RFC 5246 6.3) */ | 
|  | 1912 | { | 
|  | 1913 | unsigned char tmp[64]; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 1914 | mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 ); | 
|  | 1915 | mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); | 
|  | 1916 | mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); | 
| Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1917 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
|  | 1918 | } | 
|  | 1919 |  | 
|  | 1920 | /* Populate transform structure */ | 
| Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1921 | ret = ssl_populate_transform( ssl->transform_negotiate, | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1922 | mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ), | 
|  | 1923 | ssl->session_negotiate->master, | 
| Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1924 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1925 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1926 | ssl->session_negotiate->encrypt_then_mac, | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1927 | #endif | 
|  | 1928 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1929 | ssl->session_negotiate->trunc_hmac, | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1930 | #endif | 
| Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1931 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1932 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1933 | ssl->session_negotiate->compression, | 
| Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1934 | #endif | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1935 | ssl->handshake->randbytes, | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 1936 | mbedtls_ssl_get_minor_ver( ssl ), | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 1937 | mbedtls_ssl_conf_get_endpoint( ssl->conf ), | 
|  | 1938 | ssl ); | 
| Jarno Lamsa | 4031a45 | 2019-12-19 08:11:12 +0200 | [diff] [blame] | 1939 | if( ret == 0 ) | 
|  | 1940 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 1941 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | 4031a45 | 2019-12-19 08:11:12 +0200 | [diff] [blame] | 1942 | if( ret == 0 ) | 
|  | 1943 | { | 
|  | 1944 | ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET; | 
|  | 1945 | } | 
|  | 1946 | else | 
|  | 1947 | { | 
|  | 1948 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
|  | 1949 | } | 
|  | 1950 | } | 
|  | 1951 | else | 
| Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1952 | { | 
|  | 1953 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); | 
|  | 1954 | return( ret ); | 
|  | 1955 | } | 
|  | 1956 |  | 
|  | 1957 | /* We no longer need Server/ClientHello.random values */ | 
|  | 1958 | mbedtls_platform_zeroize( ssl->handshake->randbytes, | 
|  | 1959 | sizeof( ssl->handshake->randbytes ) ); | 
|  | 1960 |  | 
| Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1961 | /* Allocate compression buffer */ | 
|  | 1962 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| jiblime | 92af9a9 | 2019-12-18 21:40:01 -0800 | [diff] [blame] | 1963 | if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && | 
| Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1964 | ssl->compress_buf == NULL ) | 
|  | 1965 | { | 
|  | 1966 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); | 
|  | 1967 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); | 
|  | 1968 | if( ssl->compress_buf == NULL ) | 
|  | 1969 | { | 
|  | 1970 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
| Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1971 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); | 
| Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1972 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 1973 | } | 
|  | 1974 | } | 
|  | 1975 | #endif | 
|  | 1976 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1977 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); | 
|  | 1978 |  | 
|  | 1979 | return( 0 ); | 
|  | 1980 | } | 
|  | 1981 |  | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1982 | int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl ) | 
|  | 1983 | { | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 1984 | volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 1985 |  | 
|  | 1986 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = | 
|  | 1987 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); | 
| Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame] | 1988 | #if defined(MBEDTLS_USE_TINYCRYPT) | 
|  | 1989 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
| Hanno Becker | ecf5d3f | 2019-09-01 07:47:29 +0100 | [diff] [blame] | 1990 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA                       || | 
| Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame] | 1991 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
| Hanno Becker | ecf5d3f | 2019-09-01 07:47:29 +0100 | [diff] [blame] | 1992 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA                     || | 
|  | 1993 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
|  | 1994 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA                        || | 
|  | 1995 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
|  | 1996 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) | 
| Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame] | 1997 | { | 
| Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1998 | ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey, | 
|  | 1999 | ssl->handshake->ecdh_privkey, | 
|  | 2000 | ssl->handshake->premaster ); | 
|  | 2001 | if( ret == UECC_FAULT_DETECTED ) | 
|  | 2002 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
|  | 2003 | if( ret != UECC_SUCCESS ) | 
| Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame] | 2004 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2005 | ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET; | 
| Hanno Becker | a3c2c17 | 2019-07-23 16:51:57 +0100 | [diff] [blame] | 2006 |  | 
|  | 2007 | ssl->handshake->pmslen = NUM_ECC_BYTES; | 
|  | 2008 | } | 
|  | 2009 | else | 
|  | 2010 | #endif | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2011 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) | 
|  | 2012 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
|  | 2013 | == MBEDTLS_KEY_EXCHANGE_DHE_RSA ) | 
|  | 2014 | { | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2015 | ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2016 | ssl->handshake->premaster, | 
|  | 2017 | MBEDTLS_PREMASTER_SIZE, | 
|  | 2018 | &ssl->handshake->pmslen, | 
|  | 2019 | mbedtls_ssl_conf_get_frng( ssl->conf ), | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2020 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); | 
|  | 2021 | if( ret == 0 ) | 
|  | 2022 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 2023 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2024 | if( ret == 0 ) | 
|  | 2025 | { | 
|  | 2026 | ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET; | 
|  | 2027 | } | 
|  | 2028 | else | 
|  | 2029 | { | 
|  | 2030 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
|  | 2031 | return( ret ); | 
|  | 2032 | } | 
|  | 2033 | } | 
|  | 2034 | else | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2035 | { | 
|  | 2036 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
|  | 2037 | return( ret ); | 
|  | 2038 | } | 
|  | 2039 |  | 
|  | 2040 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  ); | 
|  | 2041 | } | 
|  | 2042 | else | 
|  | 2043 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ | 
| Hanno Becker | 29d1655 | 2019-07-24 11:11:45 +0100 | [diff] [blame] | 2044 | #if defined(MBEDTLS_ECDH_C) &&                                          \ | 
|  | 2045 | ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)   ||              \ | 
|  | 2046 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) ||              \ | 
|  | 2047 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED)    ||              \ | 
|  | 2048 | defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) ) | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2049 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
|  | 2050 | == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA                       || | 
|  | 2051 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
|  | 2052 | == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA                     || | 
|  | 2053 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
|  | 2054 | == MBEDTLS_KEY_EXCHANGE_ECDH_RSA                        || | 
|  | 2055 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
|  | 2056 | == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA ) | 
|  | 2057 | { | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2058 | ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2059 | &ssl->handshake->pmslen, | 
|  | 2060 | ssl->handshake->premaster, | 
|  | 2061 | MBEDTLS_MPI_MAX_SIZE, | 
|  | 2062 | mbedtls_ssl_conf_get_frng( ssl->conf ), | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2063 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); | 
|  | 2064 | if( ret == 0 ) | 
|  | 2065 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 2066 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2067 | if( ret == 0 ) | 
|  | 2068 | { | 
|  | 2069 | ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET; | 
|  | 2070 | } | 
|  | 2071 | else | 
|  | 2072 | { | 
|  | 2073 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
| Jarno Lamsa | 5aa4c07 | 2019-12-20 12:42:49 +0200 | [diff] [blame] | 2074 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2075 | } | 
|  | 2076 | } | 
|  | 2077 | else | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2078 | { | 
|  | 2079 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
|  | 2080 | return( ret ); | 
|  | 2081 | } | 
|  | 2082 |  | 
|  | 2083 | MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z ); | 
|  | 2084 | } | 
|  | 2085 | else | 
|  | 2086 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || | 
|  | 2087 | MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED || | 
|  | 2088 | MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED || | 
|  | 2089 | MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
|  | 2090 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 2091 | if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) ) | 
|  | 2092 | { | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2093 | ret = mbedtls_ssl_psk_derive_premaster( ssl, | 
|  | 2094 | mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ); | 
|  | 2095 | if( ret == 0 ) | 
|  | 2096 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 2097 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2098 | if( ret == 0 ) | 
|  | 2099 | { | 
|  | 2100 | ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET; | 
|  | 2101 | } | 
|  | 2102 | else | 
|  | 2103 | { | 
|  | 2104 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); | 
| Jarno Lamsa | 5aa4c07 | 2019-12-20 12:42:49 +0200 | [diff] [blame] | 2105 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2106 | } | 
|  | 2107 | } | 
|  | 2108 | else | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2109 | { | 
|  | 2110 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret ); | 
|  | 2111 | return( ret ); | 
|  | 2112 | } | 
|  | 2113 | } | 
|  | 2114 | else | 
|  | 2115 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ | 
|  | 2116 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
|  | 2117 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == | 
|  | 2118 | MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 2119 | { | 
|  | 2120 | ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx, | 
|  | 2121 | ssl->handshake->premaster, 32, &ssl->handshake->pmslen, | 
|  | 2122 | mbedtls_ssl_conf_get_frng( ssl->conf ), | 
| Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2123 | mbedtls_ssl_conf_get_prng( ssl->conf ) ); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2124 | if( ret == 0 ) | 
|  | 2125 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 2126 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2127 | if( ret == 0 ) | 
|  | 2128 | { | 
|  | 2129 | ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET; | 
|  | 2130 | } | 
|  | 2131 | else | 
|  | 2132 | { | 
|  | 2133 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); | 
| Jarno Lamsa | 5aa4c07 | 2019-12-20 12:42:49 +0200 | [diff] [blame] | 2134 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2135 | } | 
|  | 2136 | } | 
|  | 2137 | else | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2138 | { | 
|  | 2139 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret ); | 
|  | 2140 | return( ret ); | 
|  | 2141 | } | 
|  | 2142 | } | 
|  | 2143 | else | 
|  | 2144 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 2145 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) | 
|  | 2146 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) | 
|  | 2147 | == MBEDTLS_KEY_EXCHANGE_RSA ) | 
|  | 2148 | { | 
|  | 2149 | ((void) ret); | 
| Manuel Pégourié-Gonnard | 8793fab | 2019-08-01 10:44:07 +0200 | [diff] [blame] | 2150 | /* The premaster secret has already been set by | 
| Hanno Becker | 09d2364 | 2019-07-22 17:18:18 +0100 | [diff] [blame] | 2151 | * ssl_rsa_generate_partial_pms(). Only the | 
|  | 2152 | * PMS length needs to be set. */ | 
|  | 2153 | ssl->handshake->pmslen = 48; | 
|  | 2154 | } | 
|  | 2155 | else | 
|  | 2156 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
|  | 2157 | { | 
|  | 2158 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2159 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 2160 | } | 
|  | 2161 |  | 
|  | 2162 | return( 0 ); | 
|  | 2163 | } | 
|  | 2164 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2165 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 2166 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2167 | { | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2168 | unsigned char *p = ssl->handshake->premaster; | 
|  | 2169 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2170 | const unsigned char *psk = ssl->conf->psk; | 
|  | 2171 | size_t psk_len = ssl->conf->psk_len; | 
|  | 2172 |  | 
|  | 2173 | /* If the psk callback was called, use its result */ | 
|  | 2174 | if( ssl->handshake->psk != NULL ) | 
|  | 2175 | { | 
|  | 2176 | psk = ssl->handshake->psk; | 
|  | 2177 | psk_len = ssl->handshake->psk_len; | 
|  | 2178 | } | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2179 |  | 
|  | 2180 | /* | 
|  | 2181 | * PMS = struct { | 
|  | 2182 | *     opaque other_secret<0..2^16-1>; | 
|  | 2183 | *     opaque psk<0..2^16-1>; | 
|  | 2184 | * }; | 
|  | 2185 | * with "other_secret" depending on the particular key exchange | 
|  | 2186 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2187 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 2188 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2189 | { | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2190 | if( end - p < 2 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2192 |  | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2193 | p = mbedtls_platform_put_uint16_be( p, psk_len ); | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2194 |  | 
|  | 2195 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 2196 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2197 |  | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 2198 | mbedtls_platform_memset( p, 0, psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2199 | p += psk_len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2200 | } | 
|  | 2201 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2202 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 2203 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 2204 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2205 | { | 
|  | 2206 | /* | 
|  | 2207 | * other_secret already set by the ClientKeyExchange message, | 
|  | 2208 | * and is 48 bytes long | 
|  | 2209 | */ | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 2210 | if( end - p < 2 ) | 
|  | 2211 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2212 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 2213 | *p++ = 0; | 
|  | 2214 | *p++ = 48; | 
|  | 2215 | p += 48; | 
|  | 2216 | } | 
|  | 2217 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2218 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 2219 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 2220 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2221 | { | 
| Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 2222 | int ret; | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 2223 | size_t len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2224 |  | 
| Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 2225 | /* Write length only when we know the actual value */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2226 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 2227 | p + 2, end - ( p + 2 ), &len, | 
| Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2228 | mbedtls_ssl_conf_get_frng( ssl->conf ), | 
| Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2229 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2230 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2231 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2232 | return( ret ); | 
|  | 2233 | } | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2234 | p = mbedtls_platform_put_uint16_be( p, len ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2235 | p += len; | 
|  | 2236 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2237 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K  ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2238 | } | 
|  | 2239 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2240 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 2241 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 2242 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2243 | { | 
| Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 2244 | int ret; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2245 | size_t zlen; | 
|  | 2246 |  | 
| Hanno Becker | 982da7e | 2019-09-02 09:47:39 +0100 | [diff] [blame] | 2247 | #if defined(MBEDTLS_USE_TINYCRYPT) | 
| Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 2248 | ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey, | 
|  | 2249 | ssl->handshake->ecdh_privkey, | 
|  | 2250 | p + 2 ); | 
|  | 2251 | if( ret == UECC_FAULT_DETECTED ) | 
|  | 2252 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
|  | 2253 | if( ret != UECC_SUCCESS ) | 
| Hanno Becker | 982da7e | 2019-09-02 09:47:39 +0100 | [diff] [blame] | 2254 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Hanno Becker | 982da7e | 2019-09-02 09:47:39 +0100 | [diff] [blame] | 2255 |  | 
|  | 2256 | zlen = NUM_ECC_BYTES; | 
|  | 2257 | #else /* MBEDTLS_USE_TINYCRYPT */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2258 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2259 | p + 2, end - ( p + 2 ), | 
| Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 2260 | mbedtls_ssl_conf_get_frng( ssl->conf ), | 
| Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 2261 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2262 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2263 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2264 | return( ret ); | 
|  | 2265 | } | 
|  | 2266 |  | 
| Hanno Becker | 982da7e | 2019-09-02 09:47:39 +0100 | [diff] [blame] | 2267 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 2268 | MBEDTLS_DEBUG_ECDH_Z ); | 
|  | 2269 | #endif /* MBEDTLS_USE_TINYCRYPT */ | 
|  | 2270 |  | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2271 | p = mbedtls_platform_put_uint16_be( p, zlen ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2272 | p += zlen; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2273 | } | 
|  | 2274 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2275 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2276 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2277 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2278 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2279 | } | 
|  | 2280 |  | 
|  | 2281 | /* opaque psk<0..2^16-1>; */ | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2282 | if( end - p < 2 ) | 
|  | 2283 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 2284 |  | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 2285 | p = mbedtls_platform_put_uint16_be( p, psk_len ); | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 2286 |  | 
|  | 2287 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 2288 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2289 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2290 | mbedtls_platform_memcpy( p, psk, psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 2291 | p += psk_len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2292 |  | 
|  | 2293 | ssl->handshake->pmslen = p - ssl->handshake->premaster; | 
|  | 2294 |  | 
| Jarno Lamsa | 67f0a1e | 2019-12-18 16:28:51 +0200 | [diff] [blame] | 2295 | ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET; | 
|  | 2296 |  | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2297 | return( 0 ); | 
|  | 2298 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2299 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 2300 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2301 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2302 | /* | 
|  | 2303 | * SSLv3.0 MAC functions | 
|  | 2304 | */ | 
| Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2305 | #define SSL_MAC_MAX_BYTES   20  /* MD-5 or SHA-1 */ | 
| Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2306 | static void ssl_mac( mbedtls_md_context_t *md_ctx, | 
|  | 2307 | const unsigned char *secret, | 
|  | 2308 | const unsigned char *buf, size_t len, | 
|  | 2309 | const unsigned char *ctr, int type, | 
| Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2310 | unsigned char out[SSL_MAC_MAX_BYTES] ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2311 | { | 
|  | 2312 | unsigned char header[11]; | 
|  | 2313 | unsigned char padding[48]; | 
| Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2314 | int padlen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2315 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); | 
|  | 2316 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2317 |  | 
| Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2318 | /* Only MD5 and SHA-1 supported */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2319 | if( md_type == MBEDTLS_MD_MD5 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2320 | padlen = 48; | 
| Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 2321 | else | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2322 | padlen = 40; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2323 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2324 | mbedtls_platform_memcpy( header, ctr, 8 ); | 
| Arto Kinnunen | 6e3f09b | 2019-09-06 17:37:01 +0300 | [diff] [blame] | 2325 | header[8] = (unsigned char) type; | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 2326 | (void)mbedtls_platform_put_uint16_be( &header[9], len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2327 |  | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 2328 | mbedtls_platform_memset( padding, 0x36, padlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2329 | mbedtls_md_starts( md_ctx ); | 
|  | 2330 | mbedtls_md_update( md_ctx, secret,  md_size ); | 
|  | 2331 | mbedtls_md_update( md_ctx, padding, padlen  ); | 
|  | 2332 | mbedtls_md_update( md_ctx, header,  11      ); | 
|  | 2333 | mbedtls_md_update( md_ctx, buf,     len     ); | 
| Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2334 | mbedtls_md_finish( md_ctx, out              ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2335 |  | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 2336 | mbedtls_platform_memset( padding, 0x5C, padlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2337 | mbedtls_md_starts( md_ctx ); | 
|  | 2338 | mbedtls_md_update( md_ctx, secret,    md_size ); | 
|  | 2339 | mbedtls_md_update( md_ctx, padding,   padlen  ); | 
| Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2340 | mbedtls_md_update( md_ctx, out,       md_size ); | 
|  | 2341 | mbedtls_md_finish( md_ctx, out                ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 2342 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2343 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 2344 |  | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2345 | /* The function below is only used in the Lucky 13 counter-measure in | 
| Hanno Becker | 30d02cd | 2018-10-18 15:43:13 +0100 | [diff] [blame] | 2346 | * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */ | 
| Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2347 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2348 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 2349 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 2350 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) | 
|  | 2351 | /* This function makes sure every byte in the memory region is accessed | 
|  | 2352 | * (in ascending addresses order) */ | 
|  | 2353 | static void ssl_read_memory( unsigned char *p, size_t len ) | 
|  | 2354 | { | 
|  | 2355 | unsigned char acc = 0; | 
|  | 2356 | volatile unsigned char force; | 
|  | 2357 |  | 
|  | 2358 | for( ; len != 0; p++, len-- ) | 
|  | 2359 | acc ^= *p; | 
|  | 2360 |  | 
|  | 2361 | force = acc; | 
|  | 2362 | (void) force; | 
|  | 2363 | } | 
|  | 2364 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ | 
|  | 2365 |  | 
| Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2366 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2367 | * Encryption/decryption functions | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2368 | */ | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2369 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2370 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 8969369 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 2371 | /* This functions transforms a DTLS plaintext fragment and a record content | 
|  | 2372 | * type into an instance of the DTLSInnerPlaintext structure: | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2373 | * | 
|  | 2374 | *        struct { | 
|  | 2375 | *            opaque content[DTLSPlaintext.length]; | 
|  | 2376 | *            ContentType real_type; | 
|  | 2377 | *            uint8 zeros[length_of_padding]; | 
|  | 2378 | *        } DTLSInnerPlaintext; | 
|  | 2379 | * | 
|  | 2380 | *  Input: | 
|  | 2381 | *  - `content`: The beginning of the buffer holding the | 
|  | 2382 | *               plaintext to be wrapped. | 
|  | 2383 | *  - `*content_size`: The length of the plaintext in Bytes. | 
|  | 2384 | *  - `max_len`: The number of Bytes available starting from | 
|  | 2385 | *               `content`. This must be `>= *content_size`. | 
|  | 2386 | *  - `rec_type`: The desired record content type. | 
|  | 2387 | * | 
|  | 2388 | *  Output: | 
|  | 2389 | *  - `content`: The beginning of the resulting DTLSInnerPlaintext structure. | 
|  | 2390 | *  - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. | 
|  | 2391 | * | 
|  | 2392 | *  Returns: | 
|  | 2393 | *  - `0` on success. | 
|  | 2394 | *  - A negative error code if `max_len` didn't offer enough space | 
|  | 2395 | *    for the expansion. | 
|  | 2396 | */ | 
|  | 2397 | static int ssl_cid_build_inner_plaintext( unsigned char *content, | 
|  | 2398 | size_t *content_size, | 
|  | 2399 | size_t remaining, | 
|  | 2400 | uint8_t rec_type ) | 
|  | 2401 | { | 
|  | 2402 | size_t len = *content_size; | 
| Hanno Becker | 7842609 | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 2403 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - | 
|  | 2404 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % | 
|  | 2405 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2406 |  | 
|  | 2407 | /* Write real content type */ | 
|  | 2408 | if( remaining == 0 ) | 
|  | 2409 | return( -1 ); | 
|  | 2410 | content[ len ] = rec_type; | 
|  | 2411 | len++; | 
|  | 2412 | remaining--; | 
|  | 2413 |  | 
|  | 2414 | if( remaining < pad ) | 
|  | 2415 | return( -1 ); | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 2416 | mbedtls_platform_memset( content + len, 0, pad ); | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2417 | len += pad; | 
|  | 2418 | remaining -= pad; | 
|  | 2419 |  | 
|  | 2420 | *content_size = len; | 
|  | 2421 | return( 0 ); | 
|  | 2422 | } | 
|  | 2423 |  | 
| Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2424 | /* This function parses a DTLSInnerPlaintext structure. | 
|  | 2425 | * See ssl_cid_build_inner_plaintext() for details. */ | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2426 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, | 
|  | 2427 | size_t *content_size, | 
|  | 2428 | uint8_t *rec_type ) | 
|  | 2429 | { | 
|  | 2430 | size_t remaining = *content_size; | 
|  | 2431 |  | 
|  | 2432 | /* Determine length of padding by skipping zeroes from the back. */ | 
|  | 2433 | do | 
|  | 2434 | { | 
|  | 2435 | if( remaining == 0 ) | 
|  | 2436 | return( -1 ); | 
|  | 2437 | remaining--; | 
|  | 2438 | } while( content[ remaining ] == 0 ); | 
|  | 2439 |  | 
|  | 2440 | *content_size = remaining; | 
|  | 2441 | *rec_type = content[ remaining ]; | 
|  | 2442 |  | 
|  | 2443 | return( 0 ); | 
|  | 2444 | } | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2445 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2446 |  | 
| Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2447 | /* `add_data` must have size 13 Bytes if the CID extension is disabled, | 
| Hanno Becker | acadb0a | 2019-05-08 18:15:21 +0100 | [diff] [blame] | 2448 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2449 | static void ssl_extract_add_data_from_record( unsigned char* add_data, | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2450 | size_t *add_data_len, | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2451 | mbedtls_record *rec ) | 
|  | 2452 | { | 
| Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2453 | /* Quoting RFC 5246 (TLS 1.2): | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2454 | * | 
|  | 2455 | *    additional_data = seq_num + TLSCompressed.type + | 
|  | 2456 | *                      TLSCompressed.version + TLSCompressed.length; | 
|  | 2457 | * | 
| Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2458 | * For the CID extension, this is extended as follows | 
|  | 2459 | * (quoting draft-ietf-tls-dtls-connection-id-05, | 
|  | 2460 | *  https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2461 | * | 
|  | 2462 | *       additional_data = seq_num + DTLSPlaintext.type + | 
|  | 2463 | *                         DTLSPlaintext.version + | 
| Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2464 | *                         cid + | 
|  | 2465 | *                         cid_length + | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2466 | *                         length_of_DTLSInnerPlaintext; | 
|  | 2467 | */ | 
|  | 2468 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2469 | mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2470 | add_data[8] = rec->type; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2471 | mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2472 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2473 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2474 | if( rec->cid_len != 0 ) | 
|  | 2475 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2476 | mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len ); | 
| Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2477 | add_data[11 + rec->cid_len + 0] = rec->cid_len; | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 2478 | (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1], | 
|  | 2479 | rec->data_len ); | 
| Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2480 | *add_data_len = 13 + 1 + rec->cid_len; | 
|  | 2481 | } | 
|  | 2482 | else | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2483 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2484 | { | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 2485 | (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len ); | 
| Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2486 | *add_data_len = 13; | 
|  | 2487 | } | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2488 | } | 
|  | 2489 |  | 
| Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2490 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, | 
|  | 2491 | mbedtls_ssl_transform *transform, | 
|  | 2492 | mbedtls_record *rec, | 
|  | 2493 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 2494 | void *p_rng ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2495 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2496 | mbedtls_cipher_mode_t mode; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2497 | int auth_done = 0; | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2498 | unsigned char * data; | 
| Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2499 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2500 | size_t add_data_len; | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2501 | size_t post_avail; | 
|  | 2502 |  | 
|  | 2503 | /* The SSL context is only used for debugging purposes! */ | 
| Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2504 | #if !defined(MBEDTLS_DEBUG_C) | 
| Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2505 | ssl = NULL; /* make sure we don't use it except for debug */ | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2506 | ((void) ssl); | 
|  | 2507 | #endif | 
|  | 2508 |  | 
|  | 2509 | /* The PRNG is used for dynamic IV generation that's used | 
|  | 2510 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ | 
|  | 2511 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) &&                              \ | 
|  | 2512 | ( defined(MBEDTLS_AES_C)  ||                                     \ | 
|  | 2513 | defined(MBEDTLS_ARIA_C) ||                                     \ | 
|  | 2514 | defined(MBEDTLS_CAMELLIA_C) ) &&                               \ | 
|  | 2515 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) | 
|  | 2516 | ((void) f_rng); | 
|  | 2517 | ((void) p_rng); | 
|  | 2518 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2519 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2520 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2521 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2522 | if( transform == NULL ) | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2523 | { | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2524 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); | 
|  | 2525 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 2526 | } | 
| Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2527 | if( rec == NULL | 
|  | 2528 | || rec->buf == NULL | 
|  | 2529 | || rec->buf_len < rec->data_offset | 
|  | 2530 | || rec->buf_len - rec->data_offset < rec->data_len | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2531 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2532 | || rec->cid_len != 0 | 
|  | 2533 | #endif | 
|  | 2534 | ) | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2535 | { | 
|  | 2536 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2537 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2538 | } | 
|  | 2539 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2540 | data = rec->buf + rec->data_offset; | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2541 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2542 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2543 | data, rec->data_len ); | 
|  | 2544 |  | 
|  | 2545 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); | 
|  | 2546 |  | 
|  | 2547 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
|  | 2548 | { | 
|  | 2549 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", | 
|  | 2550 | (unsigned) rec->data_len, | 
|  | 2551 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | 
|  | 2552 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2553 | } | 
| Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 2554 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2555 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2556 | /* | 
|  | 2557 | * Add CID information | 
|  | 2558 | */ | 
|  | 2559 | rec->cid_len = transform->out_cid_len; | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 2560 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 2561 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2562 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2563 |  | 
|  | 2564 | if( rec->cid_len != 0 ) | 
|  | 2565 | { | 
|  | 2566 | /* | 
| Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2567 | * Wrap plaintext into DTLSInnerPlaintext structure. | 
|  | 2568 | * See ssl_cid_build_inner_plaintext() for more information. | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2569 | * | 
| Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2570 | * Note that this changes `rec->data_len`, and hence | 
|  | 2571 | * `post_avail` needs to be recalculated afterwards. | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2572 | */ | 
|  | 2573 | if( ssl_cid_build_inner_plaintext( data, | 
|  | 2574 | &rec->data_len, | 
|  | 2575 | post_avail, | 
|  | 2576 | rec->type ) != 0 ) | 
|  | 2577 | { | 
|  | 2578 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 2579 | } | 
|  | 2580 |  | 
|  | 2581 | rec->type = MBEDTLS_SSL_MSG_CID; | 
|  | 2582 | } | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2583 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2584 |  | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2585 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); | 
|  | 2586 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2587 | /* | 
| Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2588 | * Add MAC before if needed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2589 | */ | 
| Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2590 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2591 | if( mode == MBEDTLS_MODE_STREAM || | 
|  | 2592 | ( mode == MBEDTLS_MODE_CBC | 
|  | 2593 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2594 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2595 | #endif | 
|  | 2596 | ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2597 | { | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2598 | if( post_avail < transform->maclen ) | 
|  | 2599 | { | 
|  | 2600 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); | 
|  | 2601 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 2602 | } | 
|  | 2603 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2604 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 2605 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == | 
|  | 2606 | MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2607 | { | 
| Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2608 | unsigned char mac[SSL_MAC_MAX_BYTES]; | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2609 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, | 
|  | 2610 | data, rec->data_len, rec->ctr, rec->type, mac ); | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2611 | mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2612 | } | 
|  | 2613 | else | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2614 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2615 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 2616 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 2617 | if( mbedtls_ssl_ver_geq( | 
|  | 2618 | mbedtls_ssl_transform_get_minor_ver( transform ), | 
|  | 2619 | MBEDTLS_SSL_MINOR_VERSION_1 ) ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2620 | { | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2621 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; | 
|  | 2622 |  | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2623 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2624 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2625 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2626 | add_data_len ); | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2627 | mbedtls_md_hmac_update( &transform->md_ctx_enc, | 
|  | 2628 | data, rec->data_len ); | 
|  | 2629 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); | 
|  | 2630 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); | 
|  | 2631 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2632 | mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2633 | } | 
|  | 2634 | else | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2635 | #endif | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2636 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2637 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2638 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2639 | } | 
|  | 2640 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2641 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, | 
|  | 2642 | transform->maclen ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2643 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2644 | rec->data_len += transform->maclen; | 
|  | 2645 | post_avail -= transform->maclen; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2646 | auth_done++; | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2647 | } | 
| Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2648 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2649 |  | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2650 | /* | 
|  | 2651 | * Encrypt | 
|  | 2652 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2653 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) | 
|  | 2654 | if( mode == MBEDTLS_MODE_STREAM ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2655 | { | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2656 | int ret; | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2657 | size_t olen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2658 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2659 | "including %d bytes of padding", | 
|  | 2660 | rec->data_len, 0 ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2661 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2662 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, | 
|  | 2663 | transform->iv_enc, transform->ivlen, | 
|  | 2664 | data, rec->data_len, | 
|  | 2665 | data, &olen ) ) != 0 ) | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2666 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2667 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2668 | return( ret ); | 
|  | 2669 | } | 
|  | 2670 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2671 | if( rec->data_len != olen ) | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2672 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2674 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2675 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2676 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2677 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2678 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2679 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2680 | #if defined(MBEDTLS_GCM_C) || \ | 
|  | 2681 | defined(MBEDTLS_CCM_C) || \ | 
|  | 2682 | defined(MBEDTLS_CHACHAPOLY_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | if( mode == MBEDTLS_MODE_GCM || | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2684 | mode == MBEDTLS_MODE_CCM || | 
|  | 2685 | mode == MBEDTLS_MODE_CHACHAPOLY ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2686 | { | 
| Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2687 | int ret; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2688 | unsigned char iv[12]; | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2689 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2690 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2691 | /* Check that there's space for both the authentication tag | 
|  | 2692 | * and the explicit IV before and after the record content. */ | 
|  | 2693 | if( post_avail < transform->taglen || | 
|  | 2694 | rec->data_offset < explicit_iv_len ) | 
|  | 2695 | { | 
|  | 2696 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); | 
|  | 2697 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 2698 | } | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2699 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2700 | /* | 
|  | 2701 | * Generate IV | 
|  | 2702 | */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2703 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) | 
|  | 2704 | { | 
| Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2705 | /* GCM and CCM: fixed || explicit (=seqnum) */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2706 | mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); | 
|  | 2707 | mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr, | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2708 | explicit_iv_len ); | 
|  | 2709 | /* Prefix record content with explicit IV. */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2710 | mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2711 | } | 
|  | 2712 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) | 
|  | 2713 | { | 
| Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2714 | /* ChachaPoly: fixed XOR sequence number */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2715 | unsigned char i; | 
|  | 2716 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2717 | mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2718 |  | 
|  | 2719 | for( i = 0; i < 8; i++ ) | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2720 | iv[i+4] ^= rec->ctr[i]; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2721 | } | 
|  | 2722 | else | 
| Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2723 | { | 
|  | 2724 | /* Reminder if we ever add an AEAD mode with a different size */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2725 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2726 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2727 | } | 
|  | 2728 |  | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2729 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); | 
| Hanno Becker | 0888581 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2730 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2731 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", | 
|  | 2732 | iv, transform->ivlen ); | 
|  | 2733 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2734 | data - explicit_iv_len, explicit_iv_len ); | 
|  | 2735 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2736 | add_data, add_data_len ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2737 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2738 | "including 0 bytes of padding", | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2739 | rec->data_len ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2740 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2741 | /* | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2742 | * Encrypt and authenticate | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2743 | */ | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2744 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2745 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2746 | iv, transform->ivlen, | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2747 | add_data, add_data_len,       /* add data     */ | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2748 | data, rec->data_len,          /* source       */ | 
|  | 2749 | data, &rec->data_len,         /* destination  */ | 
|  | 2750 | data + rec->data_len, transform->taglen ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2751 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2752 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2753 | return( ret ); | 
|  | 2754 | } | 
|  | 2755 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2756 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", | 
|  | 2757 | data + rec->data_len, transform->taglen ); | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2758 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2759 | rec->data_len    += transform->taglen + explicit_iv_len; | 
|  | 2760 | rec->data_offset -= explicit_iv_len; | 
|  | 2761 | post_avail -= transform->taglen; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2762 | auth_done++; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2763 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2764 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2765 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ | 
|  | 2766 | #if defined(MBEDTLS_CIPHER_MODE_CBC) &&                                    \ | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2767 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2768 | if( mode == MBEDTLS_MODE_CBC ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2769 | { | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2770 | int ret; | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2771 | size_t padlen, i; | 
|  | 2772 | size_t olen; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2773 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2774 | /* Currently we're always using minimal padding | 
|  | 2775 | * (up to 255 bytes would be allowed). */ | 
|  | 2776 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; | 
|  | 2777 | if( padlen == transform->ivlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2778 | padlen = 0; | 
|  | 2779 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2780 | /* Check there's enough space in the buffer for the padding. */ | 
|  | 2781 | if( post_avail < padlen + 1 ) | 
|  | 2782 | { | 
|  | 2783 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); | 
|  | 2784 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 2785 | } | 
|  | 2786 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2787 | for( i = 0; i <= padlen; i++ ) | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2788 | data[rec->data_len + i] = (unsigned char) padlen; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2789 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2790 | rec->data_len += padlen + 1; | 
|  | 2791 | post_avail -= padlen + 1; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2792 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2793 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2794 | /* | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2795 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per | 
|  | 2796 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2797 | */ | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 2798 | if( mbedtls_ssl_ver_geq( | 
|  | 2799 | mbedtls_ssl_transform_get_minor_ver( transform ), | 
|  | 2800 | MBEDTLS_SSL_MINOR_VERSION_2 ) ) | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2801 | { | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2802 | if( f_rng == NULL ) | 
|  | 2803 | { | 
|  | 2804 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); | 
|  | 2805 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 2806 | } | 
|  | 2807 |  | 
|  | 2808 | if( rec->data_offset < transform->ivlen ) | 
|  | 2809 | { | 
|  | 2810 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); | 
|  | 2811 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 2812 | } | 
|  | 2813 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2814 | /* | 
|  | 2815 | * Generate IV | 
|  | 2816 | */ | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2817 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2818 | if( ret != 0 ) | 
|  | 2819 | return( ret ); | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2820 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2821 | mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc, | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2822 | transform->ivlen ); | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2823 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2824 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2825 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2826 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2827 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2828 | "including %d bytes of IV and %d bytes of padding", | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2829 | rec->data_len, transform->ivlen, | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2830 | padlen + 1 ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2831 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2832 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, | 
|  | 2833 | transform->iv_enc, | 
|  | 2834 | transform->ivlen, | 
|  | 2835 | data, rec->data_len, | 
|  | 2836 | data, &olen ) ) != 0 ) | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2837 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2838 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2839 | return( ret ); | 
|  | 2840 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2841 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2842 | if( rec->data_len != olen ) | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2843 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2844 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2845 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2846 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2847 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2848 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 2849 | if( mbedtls_ssl_ver_lt( | 
|  | 2850 | mbedtls_ssl_transform_get_minor_ver( transform ), | 
|  | 2851 | MBEDTLS_SSL_MINOR_VERSION_2 ) ) | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2852 | { | 
|  | 2853 | /* | 
|  | 2854 | * Save IV in SSL3 and TLS1 | 
|  | 2855 | */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2856 | mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2857 | transform->ivlen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2858 | } | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2859 | else | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2860 | #endif | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2861 | { | 
|  | 2862 | data             -= transform->ivlen; | 
|  | 2863 | rec->data_offset -= transform->ivlen; | 
|  | 2864 | rec->data_len    += transform->ivlen; | 
|  | 2865 | } | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2866 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2867 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2868 | if( auth_done == 0 ) | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2869 | { | 
| Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2870 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; | 
|  | 2871 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2872 | /* | 
|  | 2873 | * MAC(MAC_write_key, seq_num + | 
|  | 2874 | *     TLSCipherText.type + | 
|  | 2875 | *     TLSCipherText.version + | 
| Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2876 | *     length_of( (IV +) ENC(...) ) + | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2877 | *     IV + // except for TLS 1.0 | 
|  | 2878 | *     ENC(content + padding + padding_length)); | 
|  | 2879 | */ | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2880 |  | 
|  | 2881 | if( post_avail < transform->maclen) | 
|  | 2882 | { | 
|  | 2883 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); | 
|  | 2884 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 2885 | } | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2886 |  | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2887 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2888 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2889 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2890 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2891 | add_data_len ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2892 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2893 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2894 | add_data_len ); | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2895 | mbedtls_md_hmac_update( &transform->md_ctx_enc, | 
|  | 2896 | data, rec->data_len ); | 
|  | 2897 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); | 
|  | 2898 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); | 
| Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2899 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 2900 | mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2901 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2902 | rec->data_len += transform->maclen; | 
|  | 2903 | post_avail -= transform->maclen; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2904 | auth_done++; | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2905 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2906 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2907 | } | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2908 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2909 | #endif /* MBEDTLS_CIPHER_MODE_CBC && | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2910 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2911 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2912 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2913 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2914 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2915 |  | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2916 | /* Make extra sure authentication was performed, exactly once */ | 
|  | 2917 | if( auth_done != 1 ) | 
|  | 2918 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2919 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2920 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2921 | } | 
|  | 2922 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2923 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2924 |  | 
|  | 2925 | return( 0 ); | 
|  | 2926 | } | 
|  | 2927 |  | 
| Hanno Becker | 40478be | 2019-07-12 08:23:59 +0100 | [diff] [blame] | 2928 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl, | 
| Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2929 | mbedtls_ssl_transform *transform, | 
|  | 2930 | mbedtls_record *rec ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2931 | { | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2932 | size_t olen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2933 | mbedtls_cipher_mode_t mode; | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2934 | int ret, auth_done = 0; | 
| Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2935 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2936 | size_t padlen = 0, correct = 1; | 
|  | 2937 | #endif | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2938 | unsigned char* data; | 
| Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2939 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2940 | size_t add_data_len; | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2941 |  | 
| Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2942 | #if !defined(MBEDTLS_DEBUG_C) | 
| Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2943 | ssl = NULL; /* make sure we don't use it except for debug */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2944 | ((void) ssl); | 
|  | 2945 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2946 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2947 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2948 | if( rec == NULL                     || | 
|  | 2949 | rec->buf == NULL                || | 
|  | 2950 | rec->buf_len < rec->data_offset || | 
|  | 2951 | rec->buf_len - rec->data_offset < rec->data_len ) | 
|  | 2952 | { | 
|  | 2953 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2954 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2955 | } | 
|  | 2956 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2957 | data = rec->buf + rec->data_offset; | 
|  | 2958 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2959 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2960 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2961 | /* | 
|  | 2962 | * Match record's CID with incoming CID. | 
|  | 2963 | */ | 
| Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2964 | if( rec->cid_len != transform->in_cid_len || | 
| Teppo Järvelin | 0efac53 | 2019-10-04 13:21:08 +0300 | [diff] [blame] | 2965 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public | 
| Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2966 | { | 
| Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2967 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); | 
| Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2968 | } | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2969 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2970 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2971 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) | 
|  | 2972 | if( mode == MBEDTLS_MODE_STREAM ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2973 | { | 
|  | 2974 | padlen = 0; | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2975 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, | 
|  | 2976 | transform->iv_dec, | 
|  | 2977 | transform->ivlen, | 
|  | 2978 | data, rec->data_len, | 
|  | 2979 | data, &olen ) ) != 0 ) | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2980 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2981 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2982 | return( ret ); | 
|  | 2983 | } | 
|  | 2984 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2985 | if( rec->data_len != olen ) | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2986 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2988 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2989 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2990 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2991 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2992 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2993 | #if defined(MBEDTLS_GCM_C) || \ | 
|  | 2994 | defined(MBEDTLS_CCM_C) || \ | 
|  | 2995 | defined(MBEDTLS_CHACHAPOLY_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2996 | if( mode == MBEDTLS_MODE_GCM || | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2997 | mode == MBEDTLS_MODE_CCM || | 
|  | 2998 | mode == MBEDTLS_MODE_CHACHAPOLY ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2999 | { | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3000 | unsigned char iv[12]; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3001 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3002 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3003 | /* | 
|  | 3004 | * Prepare IV from explicit and implicit data. | 
|  | 3005 | */ | 
|  | 3006 |  | 
|  | 3007 | /* Check that there's enough space for the explicit IV | 
|  | 3008 | * (at the beginning of the record) and the MAC (at the | 
|  | 3009 | * end of the record). */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3010 | if( rec->data_len < explicit_iv_len + transform->taglen ) | 
| Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 3011 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3013 | "+ taglen (%d)", rec->data_len, | 
|  | 3014 | explicit_iv_len, transform->taglen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3015 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 3016 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 3017 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3018 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3019 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) | 
|  | 3020 | { | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3021 | /* GCM and CCM: fixed || explicit */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 3022 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3023 | /* Fixed */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3024 | mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3025 | /* Explicit */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3026 | mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 ); | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3027 | } | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3028 | else | 
|  | 3029 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ | 
|  | 3030 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 3031 | if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3032 | { | 
| Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 3033 | /* ChachaPoly: fixed XOR sequence number */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3034 | unsigned char i; | 
|  | 3035 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3036 | mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3037 |  | 
|  | 3038 | for( i = 0; i < 8; i++ ) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3039 | iv[i+4] ^= rec->ctr[i]; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3040 | } | 
|  | 3041 | else | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3042 | #endif /* MBEDTLS_CHACHAPOLY_C */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3043 | { | 
|  | 3044 | /* Reminder if we ever add an AEAD mode with a different size */ | 
|  | 3045 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3046 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3047 | } | 
|  | 3048 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3049 | /* Group changes to data, data_len, and add_data, because | 
|  | 3050 | * add_data depends on data_len. */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3051 | data += explicit_iv_len; | 
|  | 3052 | rec->data_offset += explicit_iv_len; | 
|  | 3053 | rec->data_len -= explicit_iv_len + transform->taglen; | 
|  | 3054 |  | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3055 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3056 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3057 | add_data, add_data_len ); | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3058 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3059 | /* Because of the check above, we know that there are | 
|  | 3060 | * explicit_iv_len Bytes preceeding data, and taglen | 
|  | 3061 | * bytes following data + data_len. This justifies | 
| Hanno Becker | 07d420d | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 3062 | * the debug message and the invocation of | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3063 | * mbedtls_cipher_auth_decrypt() below. */ | 
|  | 3064 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 3065 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3066 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, | 
| Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 3067 | transform->taglen ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 3068 |  | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 3069 | /* | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 3070 | * Decrypt and authenticate | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 3071 | */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3072 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, | 
|  | 3073 | iv, transform->ivlen, | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3074 | add_data, add_data_len, | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3075 | data, rec->data_len, | 
|  | 3076 | data, &olen, | 
|  | 3077 | data + rec->data_len, | 
|  | 3078 | transform->taglen ) ) != 0 ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3079 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3080 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 3081 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3082 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) | 
|  | 3083 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 3084 |  | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 3085 | return( ret ); | 
|  | 3086 | } | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3087 | auth_done++; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3088 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3089 | /* Double-check that AEAD decryption doesn't change content length. */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3090 | if( olen != rec->data_len ) | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 3091 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3093 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 3094 | } | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 3095 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3096 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3097 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ | 
|  | 3098 | #if defined(MBEDTLS_CIPHER_MODE_CBC) &&                                    \ | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 3099 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3100 | if( mode == MBEDTLS_MODE_CBC ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3101 | { | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3102 | size_t minlen = 0; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3103 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3104 | /* | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3105 | * Check immediate ciphertext sanity | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3106 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3107 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 3108 | if( mbedtls_ssl_ver_geq( | 
|  | 3109 | mbedtls_ssl_transform_get_minor_ver( transform ), | 
|  | 3110 | MBEDTLS_SSL_MINOR_VERSION_2 ) ) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3111 | { | 
|  | 3112 | /* The ciphertext is prefixed with the CBC IV. */ | 
|  | 3113 | minlen += transform->ivlen; | 
|  | 3114 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3115 | #endif | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3116 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3117 | /* Size considerations: | 
|  | 3118 | * | 
|  | 3119 | * - The CBC cipher text must not be empty and hence | 
|  | 3120 | *   at least of size transform->ivlen. | 
|  | 3121 | * | 
|  | 3122 | * Together with the potential IV-prefix, this explains | 
|  | 3123 | * the first of the two checks below. | 
|  | 3124 | * | 
|  | 3125 | * - The record must contain a MAC, either in plain or | 
|  | 3126 | *   encrypted, depending on whether Encrypt-then-MAC | 
|  | 3127 | *   is used or not. | 
|  | 3128 | *   - If it is, the message contains the IV-prefix, | 
|  | 3129 | *     the CBC ciphertext, and the MAC. | 
|  | 3130 | *   - If it is not, the padded plaintext, and hence | 
|  | 3131 | *     the CBC ciphertext, has at least length maclen + 1 | 
|  | 3132 | *     because there is at least the padding length byte. | 
|  | 3133 | * | 
|  | 3134 | * As the CBC ciphertext is not empty, both cases give the | 
|  | 3135 | * lower bound minlen + maclen + 1 on the record size, which | 
|  | 3136 | * we test for in the second check below. | 
|  | 3137 | */ | 
|  | 3138 | if( rec->data_len < minlen + transform->ivlen || | 
|  | 3139 | rec->data_len < minlen + transform->maclen + 1 ) | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3140 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3141 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3142 | "+ 1 ) ( + expl IV )", rec->data_len, | 
|  | 3143 | transform->ivlen, | 
|  | 3144 | transform->maclen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3145 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3146 | } | 
|  | 3147 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3148 | /* | 
|  | 3149 | * Authenticate before decrypt if enabled | 
|  | 3150 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3151 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3152 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3153 | { | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3154 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3155 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3156 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3157 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3158 | /* Update data_len in tandem with add_data. | 
|  | 3159 | * | 
|  | 3160 | * The subtraction is safe because of the previous check | 
|  | 3161 | * data_len >= minlen + maclen + 1. | 
|  | 3162 | * | 
|  | 3163 | * Afterwards, we know that data + data_len is followed by at | 
|  | 3164 | * least maclen Bytes, which justifies the call to | 
| Teppo Järvelin | 707ceb8 | 2019-10-04 07:49:39 +0300 | [diff] [blame] | 3165 | * mbedtls_platform_memcmp() below. | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3166 | * | 
|  | 3167 | * Further, we still know that data_len > minlen */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3168 | rec->data_len -= transform->maclen; | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3169 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); | 
| Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 3170 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3171 | /* Calculate expected MAC. */ | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3172 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, | 
|  | 3173 | add_data_len ); | 
|  | 3174 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, | 
|  | 3175 | add_data_len ); | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3176 | mbedtls_md_hmac_update( &transform->md_ctx_dec, | 
|  | 3177 | data, rec->data_len ); | 
|  | 3178 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); | 
|  | 3179 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); | 
| Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 3180 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3181 | MBEDTLS_SSL_DEBUG_BUF( 4, "message  mac", data + rec->data_len, | 
|  | 3182 | transform->maclen ); | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3183 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3184 | transform->maclen ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3185 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3186 | /* Compare expected MAC with MAC at the end of the record. */ | 
| Teppo Järvelin | 707ceb8 | 2019-10-04 07:49:39 +0300 | [diff] [blame] | 3187 | if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect, | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3188 | transform->maclen ) != 0 ) | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3189 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3190 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3191 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3192 | } | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3193 | auth_done++; | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3194 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3195 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3196 |  | 
|  | 3197 | /* | 
|  | 3198 | * Check length sanity | 
|  | 3199 | */ | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3200 |  | 
|  | 3201 | /* We know from above that data_len > minlen >= 0, | 
|  | 3202 | * so the following check in particular implies that | 
|  | 3203 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3204 | if( rec->data_len % transform->ivlen != 0 ) | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3205 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3206 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3207 | rec->data_len, transform->ivlen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3208 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3209 | } | 
|  | 3210 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3211 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3212 | /* | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 3213 | * Initialize for prepended IV for block cipher in TLS v1.1 and up | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3214 | */ | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 3215 | if( mbedtls_ssl_ver_geq( | 
|  | 3216 | mbedtls_ssl_transform_get_minor_ver( transform ), | 
|  | 3217 | MBEDTLS_SSL_MINOR_VERSION_2 ) ) | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3218 | { | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3219 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3220 | mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen ); | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3221 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3222 | data += transform->ivlen; | 
|  | 3223 | rec->data_offset += transform->ivlen; | 
|  | 3224 | rec->data_len -= transform->ivlen; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3225 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3226 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3227 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3228 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ | 
|  | 3229 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3230 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, | 
|  | 3231 | transform->iv_dec, transform->ivlen, | 
|  | 3232 | data, rec->data_len, data, &olen ) ) != 0 ) | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 3233 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3234 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3235 | return( ret ); | 
|  | 3236 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 3237 |  | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3238 | /* Double-check that length hasn't changed during decryption. */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3239 | if( rec->data_len != olen ) | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3240 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3241 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3242 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3243 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 3244 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3245 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 3246 | if( mbedtls_ssl_ver_lt( | 
|  | 3247 | mbedtls_ssl_transform_get_minor_ver( transform ), | 
|  | 3248 | MBEDTLS_SSL_MINOR_VERSION_2 ) ) | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3249 | { | 
|  | 3250 | /* | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3251 | * Save IV in SSL3 and TLS1, where CBC decryption of consecutive | 
|  | 3252 | * records is equivalent to CBC decryption of the concatenation | 
|  | 3253 | * of the records; in other words, IVs are maintained across | 
|  | 3254 | * record decryptions. | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3255 | */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3256 | mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3257 | transform->ivlen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3258 | } | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 3259 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3260 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3261 | /* Safe since data_len >= minlen + maclen + 1, so after having | 
|  | 3262 | * subtracted at most minlen and maclen up to this point, | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3263 | * data_len > 0 (because of data_len % ivlen == 0, it's actually | 
|  | 3264 | * >= ivlen ). */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3265 | padlen = data[rec->data_len - 1]; | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3266 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3267 | if( auth_done == 1 ) | 
|  | 3268 | { | 
|  | 3269 | correct *= ( rec->data_len >= padlen + 1 ); | 
|  | 3270 | padlen  *= ( rec->data_len >= padlen + 1 ); | 
|  | 3271 | } | 
|  | 3272 | else | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3273 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3274 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3275 | if( rec->data_len < transform->maclen + padlen + 1 ) | 
|  | 3276 | { | 
|  | 3277 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", | 
|  | 3278 | rec->data_len, | 
|  | 3279 | transform->maclen, | 
|  | 3280 | padlen + 1 ) ); | 
|  | 3281 | } | 
| Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3282 | #endif | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3283 |  | 
|  | 3284 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); | 
|  | 3285 | padlen  *= ( rec->data_len >= transform->maclen + padlen + 1 ); | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3286 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3287 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3288 | padlen++; | 
|  | 3289 |  | 
|  | 3290 | /* Regardless of the validity of the padding, | 
|  | 3291 | * we have data_len >= padlen here. */ | 
|  | 3292 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3293 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 3294 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == | 
|  | 3295 | MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3296 | { | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3297 | if( padlen > transform->ivlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3298 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3299 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
|  | 3300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3301 | "should be no more than %d", | 
|  | 3302 | padlen, transform->ivlen ) ); | 
| Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3303 | #endif | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 3304 | correct = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3305 | } | 
|  | 3306 | } | 
|  | 3307 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3308 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 3309 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 3310 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 3311 | if( mbedtls_ssl_ver_gt( | 
|  | 3312 | mbedtls_ssl_transform_get_minor_ver( transform ), | 
|  | 3313 | MBEDTLS_SSL_MINOR_VERSION_0 ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3314 | { | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3315 | /* The padding check involves a series of up to 256 | 
|  | 3316 | * consecutive memory reads at the end of the record | 
|  | 3317 | * plaintext buffer. In order to hide the length and | 
|  | 3318 | * validity of the padding, always perform exactly | 
|  | 3319 | * `min(256,plaintext_len)` reads (but take into account | 
|  | 3320 | * only the last `padlen` bytes for the padding check). */ | 
|  | 3321 | size_t pad_count = 0; | 
|  | 3322 | size_t real_count = 0; | 
|  | 3323 | volatile unsigned char* const check = data; | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3324 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3325 | /* Index of first padding byte; it has been ensured above | 
|  | 3326 | * that the subtraction is safe. */ | 
|  | 3327 | size_t const padding_idx = rec->data_len - padlen; | 
|  | 3328 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; | 
|  | 3329 | size_t const start_idx = rec->data_len - num_checks; | 
|  | 3330 | size_t idx; | 
| Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 3331 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3332 | for( idx = start_idx; idx < rec->data_len; idx++ ) | 
| Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 3333 | { | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3334 | real_count |= ( idx >= padding_idx ); | 
|  | 3335 | pad_count += real_count * ( check[idx] == padlen - 1 ); | 
| Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 3336 | } | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3337 | correct &= ( pad_count == padlen ); | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3338 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3339 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3340 | if( padlen > 0 && correct == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3341 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); | 
| Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 3342 | #endif | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3343 | padlen &= correct * 0x1FF; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3344 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3345 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3346 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 3347 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3348 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3349 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3350 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 3351 | } | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3352 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3353 | /* If the padding was found to be invalid, padlen == 0 | 
|  | 3354 | * and the subtraction is safe. If the padding was found valid, | 
|  | 3355 | * padlen hasn't been changed and the previous assertion | 
|  | 3356 | * data_len >= padlen still holds. */ | 
|  | 3357 | rec->data_len -= padlen; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3358 | } | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3359 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3360 | #endif /* MBEDTLS_CIPHER_MODE_CBC && | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 3361 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3362 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3363 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3364 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 3365 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3366 |  | 
| Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3367 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3368 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3369 | data, rec->data_len ); | 
| Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 3370 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3371 |  | 
|  | 3372 | /* | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 3373 | * Authenticate if not done yet. | 
|  | 3374 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3375 | */ | 
| Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3376 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3377 | if( auth_done == 0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3378 | { | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 3379 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; | 
| Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 3380 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3381 | /* If the initial value of padlen was such that | 
|  | 3382 | * data_len < maclen + padlen + 1, then padlen | 
|  | 3383 | * got reset to 1, and the initial check | 
|  | 3384 | * data_len >= minlen + maclen + 1 | 
|  | 3385 | * guarantees that at this point we still | 
|  | 3386 | * have at least data_len >= maclen. | 
|  | 3387 | * | 
|  | 3388 | * If the initial value of padlen was such that | 
|  | 3389 | * data_len >= maclen + padlen + 1, then we have | 
|  | 3390 | * subtracted either padlen + 1 (if the padding was correct) | 
|  | 3391 | * or 0 (if the padding was incorrect) since then, | 
|  | 3392 | * hence data_len >= maclen in any case. | 
|  | 3393 | */ | 
|  | 3394 | rec->data_len -= transform->maclen; | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3395 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3396 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3397 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Hanno Becker | 0a92b81 | 2019-06-24 15:46:40 +0100 | [diff] [blame] | 3398 | if( mbedtls_ssl_transform_get_minor_ver( transform ) == | 
|  | 3399 | MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3400 | { | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3401 | ssl_mac( &transform->md_ctx_dec, | 
|  | 3402 | transform->mac_dec, | 
|  | 3403 | data, rec->data_len, | 
|  | 3404 | rec->ctr, rec->type, | 
|  | 3405 | mac_expect ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3406 | } | 
|  | 3407 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3408 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 3409 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 3410 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 3411 | if( mbedtls_ssl_ver_gt( | 
|  | 3412 | mbedtls_ssl_transform_get_minor_ver( transform ), | 
|  | 3413 | MBEDTLS_SSL_MINOR_VERSION_0 ) ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3414 | { | 
|  | 3415 | /* | 
|  | 3416 | * Process MAC and always update for padlen afterwards to make | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3417 | * total time independent of padlen. | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3418 | * | 
|  | 3419 | * Known timing attacks: | 
|  | 3420 | *  - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) | 
|  | 3421 | * | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3422 | * To compensate for different timings for the MAC calculation | 
|  | 3423 | * depending on how much padding was removed (which is determined | 
|  | 3424 | * by padlen), process extra_run more blocks through the hash | 
|  | 3425 | * function. | 
|  | 3426 | * | 
|  | 3427 | * The formula in the paper is | 
|  | 3428 | *   extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) | 
|  | 3429 | * where L1 is the size of the header plus the decrypted message | 
|  | 3430 | * plus CBC padding and L2 is the size of the header plus the | 
|  | 3431 | * decrypted message. This is for an underlying hash function | 
|  | 3432 | * with 64-byte blocks. | 
|  | 3433 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values | 
|  | 3434 | * correctly. We round down instead of up, so -56 is the correct | 
|  | 3435 | * value for our calculations instead of -55. | 
|  | 3436 | * | 
| Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 3437 | * Repeat the formula rather than defining a block_size variable. | 
|  | 3438 | * This avoids requiring division by a variable at runtime | 
|  | 3439 | * (which would be marginally less efficient and would require | 
|  | 3440 | * linking an extra division function in some builds). | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3441 | */ | 
|  | 3442 | size_t j, extra_run = 0; | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3443 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3444 |  | 
|  | 3445 | /* | 
|  | 3446 | * The next two sizes are the minimum and maximum values of | 
|  | 3447 | * in_msglen over all padlen values. | 
|  | 3448 | * | 
|  | 3449 | * They're independent of padlen, since we previously did | 
| Hanno Becker | 6d3db0f | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 3450 | * data_len -= padlen. | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3451 | * | 
|  | 3452 | * Note that max_len + maclen is never more than the buffer | 
|  | 3453 | * length, as we previously did in_msglen -= maclen too. | 
|  | 3454 | */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3455 | const size_t max_len = rec->data_len + padlen; | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3456 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; | 
|  | 3457 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3458 | memset( tmp, 0, sizeof( tmp ) ); | 
|  | 3459 |  | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 3460 | switch( mbedtls_md_get_type( | 
|  | 3461 | mbedtls_md_get_handle( &transform->md_ctx_dec ) ) ) | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3462 | { | 
| Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 3463 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ | 
|  | 3464 | defined(MBEDTLS_SHA256_C) | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3465 | case MBEDTLS_MD_MD5: | 
|  | 3466 | case MBEDTLS_MD_SHA1: | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3467 | case MBEDTLS_MD_SHA256: | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3468 | /* 8 bytes of message size, 64-byte compression blocks */ | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3469 | extra_run = | 
|  | 3470 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - | 
|  | 3471 | ( add_data_len + rec->data_len          + 8 ) / 64; | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3472 | break; | 
|  | 3473 | #endif | 
| Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 3474 | #if defined(MBEDTLS_SHA512_C) | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3475 | case MBEDTLS_MD_SHA384: | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3476 | /* 16 bytes of message size, 128-byte compression blocks */ | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3477 | extra_run = | 
|  | 3478 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - | 
|  | 3479 | ( add_data_len + rec->data_len          + 16 ) / 128; | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3480 | break; | 
|  | 3481 | #endif | 
|  | 3482 | default: | 
| Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 3483 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3484 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3485 | } | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3486 |  | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3487 | extra_run &= correct * 0xFF; | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3488 |  | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3489 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, | 
|  | 3490 | add_data_len ); | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3491 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, | 
|  | 3492 | rec->data_len ); | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3493 | /* Make sure we access everything even when padlen > 0. This | 
|  | 3494 | * makes the synchronisation requirements for just-in-time | 
|  | 3495 | * Prime+Probe attacks much tighter and hopefully impractical. */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3496 | ssl_read_memory( data + rec->data_len, padlen ); | 
|  | 3497 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3498 |  | 
|  | 3499 | /* Call mbedtls_md_process at least once due to cache attacks | 
|  | 3500 | * that observe whether md_process() was called of not */ | 
| Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 3501 | for( j = 0; j < extra_run + 1; j++ ) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3502 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3503 |  | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3504 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3505 |  | 
|  | 3506 | /* Make sure we access all the memory that could contain the MAC, | 
|  | 3507 | * before we check it in the next code block. This makes the | 
|  | 3508 | * synchronisation requirements for just-in-time Prime+Probe | 
|  | 3509 | * attacks much tighter and hopefully impractical. */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3510 | ssl_read_memory( data + min_len, | 
|  | 3511 | max_len - min_len + transform->maclen ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3512 | } | 
|  | 3513 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3514 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 3515 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3516 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3517 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3518 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3519 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3520 |  | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3521 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3522 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); | 
|  | 3523 | MBEDTLS_SSL_DEBUG_BUF( 4, "message  mac", data + rec->data_len, transform->maclen ); | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3524 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3525 |  | 
| Teppo Järvelin | 707ceb8 | 2019-10-04 07:49:39 +0300 | [diff] [blame] | 3526 | if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect, | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3527 | transform->maclen ) != 0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3528 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3529 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
|  | 3530 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3531 | #endif | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3532 | correct = 0; | 
|  | 3533 | } | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3534 | auth_done++; | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3535 | } | 
| Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 3536 |  | 
|  | 3537 | /* | 
|  | 3538 | * Finally check the correct flag | 
|  | 3539 | */ | 
|  | 3540 | if( correct == 0 ) | 
|  | 3541 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3542 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3543 |  | 
|  | 3544 | /* Make extra sure authentication was performed, exactly once */ | 
|  | 3545 | if( auth_done != 1 ) | 
|  | 3546 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3547 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3548 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3549 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3550 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3551 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3552 | if( rec->cid_len != 0 ) | 
|  | 3553 | { | 
|  | 3554 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, | 
|  | 3555 | &rec->type ); | 
|  | 3556 | if( ret != 0 ) | 
|  | 3557 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 3558 | } | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3559 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3560 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3561 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3562 |  | 
|  | 3563 | return( 0 ); | 
|  | 3564 | } | 
|  | 3565 |  | 
| Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 3566 | #undef MAC_NONE | 
|  | 3567 | #undef MAC_PLAINTEXT | 
|  | 3568 | #undef MAC_CIPHERTEXT | 
|  | 3569 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3570 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3571 | /* | 
|  | 3572 | * Compression/decompression functions | 
|  | 3573 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3574 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3575 | { | 
|  | 3576 | int ret; | 
|  | 3577 | unsigned char *msg_post = ssl->out_msg; | 
| Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3578 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3579 | size_t len_pre = ssl->out_msglen; | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3580 | unsigned char *msg_pre = ssl->compress_buf; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3581 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3582 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3583 |  | 
| Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3584 | if( len_pre == 0 ) | 
|  | 3585 | return( 0 ); | 
|  | 3586 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3587 | mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3588 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3589 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3590 | ssl->out_msglen ) ); | 
|  | 3591 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3592 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3593 | ssl->out_msg, ssl->out_msglen ); | 
|  | 3594 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3595 | ssl->transform_out->ctx_deflate.next_in = msg_pre; | 
|  | 3596 | ssl->transform_out->ctx_deflate.avail_in = len_pre; | 
|  | 3597 | ssl->transform_out->ctx_deflate.next_out = msg_post; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3598 | ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3599 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3600 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3601 | if( ret != Z_OK ) | 
|  | 3602 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3603 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); | 
|  | 3604 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3605 | } | 
|  | 3606 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3607 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - | 
| Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3608 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3609 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3610 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3611 | ssl->out_msglen ) ); | 
|  | 3612 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3613 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3614 | ssl->out_msg, ssl->out_msglen ); | 
|  | 3615 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3616 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3617 |  | 
|  | 3618 | return( 0 ); | 
|  | 3619 | } | 
|  | 3620 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3621 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3622 | { | 
|  | 3623 | int ret; | 
|  | 3624 | unsigned char *msg_post = ssl->in_msg; | 
| Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3625 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3626 | size_t len_pre = ssl->in_msglen; | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3627 | unsigned char *msg_pre = ssl->compress_buf; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3628 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3629 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3630 |  | 
| Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3631 | if( len_pre == 0 ) | 
|  | 3632 | return( 0 ); | 
|  | 3633 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 3634 | mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3635 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3636 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3637 | ssl->in_msglen ) ); | 
|  | 3638 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3639 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3640 | ssl->in_msg, ssl->in_msglen ); | 
|  | 3641 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3642 | ssl->transform_in->ctx_inflate.next_in = msg_pre; | 
|  | 3643 | ssl->transform_in->ctx_inflate.avail_in = len_pre; | 
|  | 3644 | ssl->transform_in->ctx_inflate.next_out = msg_post; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3645 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - | 
| Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3646 | header_bytes; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3647 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3648 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3649 | if( ret != Z_OK ) | 
|  | 3650 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3651 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); | 
|  | 3652 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3653 | } | 
|  | 3654 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3655 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - | 
| Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3656 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3657 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3658 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3659 | ssl->in_msglen ) ); | 
|  | 3660 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3661 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3662 | ssl->in_msg, ssl->in_msglen ); | 
|  | 3663 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3664 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3665 |  | 
|  | 3666 | return( 0 ); | 
|  | 3667 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3668 | #endif /* MBEDTLS_ZLIB_SUPPORT */ | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3669 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3670 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 3671 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3672 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3673 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3674 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3675 | { | 
|  | 3676 | /* If renegotiation is not enforced, retransmit until we would reach max | 
|  | 3677 | * timeout if we were using the usual handshake doubling scheme */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3678 | if( ssl->conf->renego_max_records < 0 ) | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3679 | { | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3680 | uint32_t ratio = | 
|  | 3681 | mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) / | 
|  | 3682 | mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1; | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3683 | unsigned char doublings = 1; | 
|  | 3684 |  | 
|  | 3685 | while( ratio != 0 ) | 
|  | 3686 | { | 
|  | 3687 | ++doublings; | 
|  | 3688 | ratio >>= 1; | 
|  | 3689 | } | 
|  | 3690 |  | 
|  | 3691 | if( ++ssl->renego_records_seen > doublings ) | 
|  | 3692 | { | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 3693 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3694 | return( 0 ); | 
|  | 3695 | } | 
|  | 3696 | } | 
|  | 3697 |  | 
|  | 3698 | return( ssl_write_hello_request( ssl ) ); | 
|  | 3699 | } | 
|  | 3700 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3701 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3702 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3703 | /* | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3704 | * Fill the input message buffer by appending data to it. | 
|  | 3705 | * The amount of data already fetched is in ssl->in_left. | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3706 | * | 
|  | 3707 | * If we return 0, is it guaranteed that (at least) nb_want bytes are | 
|  | 3708 | * available (from this read and/or a previous one). Otherwise, an error code | 
|  | 3709 | * is returned (possibly EOF or WANT_READ). | 
|  | 3710 | * | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3711 | * With stream transport (TLS) on success ssl->in_left == nb_want, but | 
|  | 3712 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, | 
|  | 3713 | * since we always read a whole datagram at once. | 
|  | 3714 | * | 
| Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3715 | * For DTLS, it is up to the caller to set ssl->next_record_offset when | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3716 | * they're done reading a record. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3717 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3718 | int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3719 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3720 | int ret; | 
|  | 3721 | size_t len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3722 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3723 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3724 |  | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3725 | if( mbedtls_ssl_get_recv( ssl ) == NULL && | 
|  | 3726 | mbedtls_ssl_get_recv_timeout( ssl ) == NULL ) | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3727 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3728 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() " | 
| Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 3729 | "or mbedtls_ssl_set_bio()" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3730 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3731 | } | 
|  | 3732 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3733 | if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) ) | 
| Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3734 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3735 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); | 
|  | 3736 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3737 | } | 
|  | 3738 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3739 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3740 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3741 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3742 | uint32_t timeout; | 
|  | 3743 |  | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3744 | /* Just to be sure */ | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 3745 | if( mbedtls_ssl_get_set_timer( ssl ) == NULL || | 
|  | 3746 | mbedtls_ssl_get_get_timer( ssl ) == NULL ) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3747 | { | 
|  | 3748 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " | 
|  | 3749 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); | 
|  | 3750 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3751 | } | 
|  | 3752 |  | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3753 | /* | 
|  | 3754 | * The point is, we need to always read a full datagram at once, so we | 
|  | 3755 | * sometimes read more then requested, and handle the additional data. | 
|  | 3756 | * It could be the rest of the current record (while fetching the | 
|  | 3757 | * header) and/or some other records in the same datagram. | 
|  | 3758 | */ | 
|  | 3759 |  | 
|  | 3760 | /* | 
|  | 3761 | * Move to the next record in the already read datagram if applicable | 
|  | 3762 | */ | 
|  | 3763 | if( ssl->next_record_offset != 0 ) | 
|  | 3764 | { | 
|  | 3765 | if( ssl->in_left < ssl->next_record_offset ) | 
|  | 3766 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3767 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3768 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3769 | } | 
|  | 3770 |  | 
|  | 3771 | ssl->in_left -= ssl->next_record_offset; | 
|  | 3772 |  | 
|  | 3773 | if( ssl->in_left != 0 ) | 
|  | 3774 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3775 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d", | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3776 | ssl->next_record_offset ) ); | 
|  | 3777 | memmove( ssl->in_hdr, | 
|  | 3778 | ssl->in_hdr + ssl->next_record_offset, | 
|  | 3779 | ssl->in_left ); | 
|  | 3780 | } | 
|  | 3781 |  | 
|  | 3782 | ssl->next_record_offset = 0; | 
|  | 3783 | } | 
|  | 3784 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3785 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3786 | ssl->in_left, nb_want ) ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3787 |  | 
|  | 3788 | /* | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3789 | * Done if we already have enough data. | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3790 | */ | 
|  | 3791 | if( nb_want <= ssl->in_left) | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3792 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3793 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3794 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3795 | } | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3796 |  | 
|  | 3797 | /* | 
| Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 3798 | * A record can't be split across datagrams. If we need to read but | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3799 | * are not at the beginning of a new record, the caller did something | 
|  | 3800 | * wrong. | 
|  | 3801 | */ | 
|  | 3802 | if( ssl->in_left != 0 ) | 
|  | 3803 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3804 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3805 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3806 | } | 
|  | 3807 |  | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3808 | /* | 
|  | 3809 | * Don't even try to read if time's out already. | 
|  | 3810 | * This avoids by-passing the timer when repeatedly receiving messages | 
|  | 3811 | * that will end up being dropped. | 
|  | 3812 | */ | 
|  | 3813 | if( ssl_check_timer( ssl ) != 0 ) | 
| Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3814 | { | 
|  | 3815 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3816 | ret = MBEDTLS_ERR_SSL_TIMEOUT; | 
| Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3817 | } | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3818 | else | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3819 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3820 | len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3821 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3822 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3823 | timeout = ssl->handshake->retransmit_timeout; | 
|  | 3824 | else | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3825 | timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3826 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3827 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3828 |  | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3829 | if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL ) | 
|  | 3830 | { | 
|  | 3831 | ret = mbedtls_ssl_get_recv_timeout( ssl ) | 
|  | 3832 | ( ssl->p_bio, ssl->in_hdr, len, timeout ); | 
|  | 3833 | } | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3834 | else | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3835 | { | 
|  | 3836 | ret = mbedtls_ssl_get_recv( ssl ) | 
|  | 3837 | ( ssl->p_bio, ssl->in_hdr, len ); | 
|  | 3838 | } | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3839 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3840 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3841 |  | 
|  | 3842 | if( ret == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3843 | return( MBEDTLS_ERR_SSL_CONN_EOF ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3844 | } | 
|  | 3845 |  | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3846 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3847 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3848 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3849 | ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3850 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3851 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3852 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3853 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) | 
|  | 3854 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3855 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3856 | return( MBEDTLS_ERR_SSL_TIMEOUT ); | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3857 | } | 
|  | 3858 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3859 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3860 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3861 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3862 | return( ret ); | 
|  | 3863 | } | 
|  | 3864 |  | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3865 | return( MBEDTLS_ERR_SSL_WANT_READ ); | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3866 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3867 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 3868 | else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 3869 | MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3870 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3871 | { | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3872 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3873 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3874 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3875 | return( ret ); | 
|  | 3876 | } | 
|  | 3877 |  | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3878 | return( MBEDTLS_ERR_SSL_WANT_READ ); | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3879 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3880 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3881 | } | 
|  | 3882 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3883 | if( ret < 0 ) | 
|  | 3884 | return( ret ); | 
|  | 3885 |  | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3886 | ssl->in_left = ret; | 
|  | 3887 | } | 
| Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3888 | MBEDTLS_SSL_TRANSPORT_ELSE | 
|  | 3889 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3890 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3891 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3892 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3893 | ssl->in_left, nb_want ) ); | 
|  | 3894 |  | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3895 | while( ssl->in_left < nb_want ) | 
|  | 3896 | { | 
|  | 3897 | len = nb_want - ssl->in_left; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3898 |  | 
|  | 3899 | if( ssl_check_timer( ssl ) != 0 ) | 
|  | 3900 | ret = MBEDTLS_ERR_SSL_TIMEOUT; | 
|  | 3901 | else | 
| Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3902 | { | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3903 | if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL ) | 
| Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3904 | { | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3905 | ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio, | 
|  | 3906 | ssl->in_hdr + ssl->in_left, len, | 
|  | 3907 | mbedtls_ssl_conf_get_read_timeout( ssl->conf ) ); | 
| Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3908 | } | 
|  | 3909 | else | 
|  | 3910 | { | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3911 | ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio, | 
| Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3912 | ssl->in_hdr + ssl->in_left, len ); | 
|  | 3913 | } | 
|  | 3914 | } | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3915 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3916 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", | 
| Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3917 | ssl->in_left, nb_want ) ); | 
|  | 3918 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3919 |  | 
|  | 3920 | if( ret == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3921 | return( MBEDTLS_ERR_SSL_CONN_EOF ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3922 |  | 
|  | 3923 | if( ret < 0 ) | 
|  | 3924 | return( ret ); | 
|  | 3925 |  | 
| mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3926 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) | 
| mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3927 | { | 
| Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3928 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3929 | ( "f_recv returned %d bytes but only %lu were requested", | 
| mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3930 | ret, (unsigned long)len ) ); | 
| mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3931 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3932 | } | 
|  | 3933 |  | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3934 | ssl->in_left += ret; | 
|  | 3935 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3936 | } | 
| Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3937 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3938 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3939 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3940 |  | 
|  | 3941 | return( 0 ); | 
|  | 3942 | } | 
|  | 3943 |  | 
|  | 3944 | /* | 
|  | 3945 | * Flush any data not yet written | 
|  | 3946 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3947 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3948 | { | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3949 | int ret; | 
| Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3950 | unsigned char *buf; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3951 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3952 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3953 |  | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3954 | if( mbedtls_ssl_get_send( ssl ) == NULL ) | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3955 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3956 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() " | 
| Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 3957 | "or mbedtls_ssl_set_bio()" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3958 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3959 | } | 
|  | 3960 |  | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3961 | /* Avoid incrementing counter if data is flushed */ | 
|  | 3962 | if( ssl->out_left == 0 ) | 
|  | 3963 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3964 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3965 | return( 0 ); | 
|  | 3966 | } | 
|  | 3967 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3968 | while( ssl->out_left > 0 ) | 
|  | 3969 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3970 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", | 
| Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3971 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3972 |  | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3973 | buf = ssl->out_hdr - ssl->out_left; | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 3974 | ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left ); | 
| Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 3975 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3976 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3977 |  | 
|  | 3978 | if( ret <= 0 ) | 
|  | 3979 | return( ret ); | 
|  | 3980 |  | 
| mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3981 | if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) | 
| mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3982 | { | 
| Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3983 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 3984 | ( "f_send returned %d bytes but only %lu bytes were sent", | 
| mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3985 | ret, (unsigned long)ssl->out_left ) ); | 
| mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3986 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3987 | } | 
|  | 3988 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3989 | ssl->out_left -= ret; | 
|  | 3990 | } | 
|  | 3991 |  | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3992 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3993 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3994 | { | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3995 | ssl->out_hdr = ssl->out_buf; | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3996 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3997 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3998 | #endif | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3999 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4000 | { | 
|  | 4001 | ssl->out_hdr = ssl->out_buf + 8; | 
|  | 4002 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4003 | #endif | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4004 | ssl_update_out_pointers( ssl, ssl->transform_out ); | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 4005 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4006 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4007 |  | 
|  | 4008 | return( 0 ); | 
|  | 4009 | } | 
|  | 4010 |  | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4011 | /* | 
|  | 4012 | * Functions to handle the DTLS retransmission state machine | 
|  | 4013 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4014 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4015 | /* | 
|  | 4016 | * Append current handshake message to current outgoing flight | 
|  | 4017 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4018 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4019 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4020 | mbedtls_ssl_flight_item *msg; | 
| Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 4021 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); | 
|  | 4022 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", | 
|  | 4023 | ssl->out_msg, ssl->out_msglen ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4024 |  | 
|  | 4025 | /* Allocate space for current message */ | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4026 | if( ( msg = mbedtls_calloc( 1, sizeof(  mbedtls_ssl_flight_item ) ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4027 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 4028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4029 | sizeof( mbedtls_ssl_flight_item ) ) ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4030 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4031 | } | 
|  | 4032 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4033 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4034 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 4035 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4036 | mbedtls_free( msg ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4037 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4038 | } | 
|  | 4039 |  | 
|  | 4040 | /* Copy current handshake message with headers */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 4041 | mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4042 | msg->len = ssl->out_msglen; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4043 | msg->type = ssl->out_msgtype; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4044 | msg->next = NULL; | 
|  | 4045 |  | 
|  | 4046 | /* Append to the current flight */ | 
|  | 4047 | if( ssl->handshake->flight == NULL ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4048 | ssl->handshake->flight = msg; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4049 | else | 
|  | 4050 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4051 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4052 | while( cur->next != NULL ) | 
|  | 4053 | cur = cur->next; | 
|  | 4054 | cur->next = msg; | 
|  | 4055 | } | 
|  | 4056 |  | 
| Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 4057 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4058 | return( 0 ); | 
|  | 4059 | } | 
|  | 4060 |  | 
|  | 4061 | /* | 
|  | 4062 | * Free the current flight of handshake messages | 
|  | 4063 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4064 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4065 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4066 | mbedtls_ssl_flight_item *cur = flight; | 
|  | 4067 | mbedtls_ssl_flight_item *next; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4068 |  | 
|  | 4069 | while( cur != NULL ) | 
|  | 4070 | { | 
|  | 4071 | next = cur->next; | 
|  | 4072 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4073 | mbedtls_free( cur->p ); | 
|  | 4074 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4075 |  | 
|  | 4076 | cur = next; | 
|  | 4077 | } | 
|  | 4078 | } | 
|  | 4079 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4080 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 4081 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 4082 | #endif | 
|  | 4083 |  | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4084 | /* | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4085 | * Swap transform_out and out_ctr with the alternative ones | 
|  | 4086 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4087 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4088 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4089 | mbedtls_ssl_transform *tmp_transform; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4090 | unsigned char tmp_out_ctr[8]; | 
|  | 4091 |  | 
|  | 4092 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) | 
|  | 4093 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4094 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4095 | return; | 
|  | 4096 | } | 
|  | 4097 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4098 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4099 |  | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4100 | /* Swap transforms */ | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4101 | tmp_transform                     = ssl->transform_out; | 
|  | 4102 | ssl->transform_out                = ssl->handshake->alt_transform_out; | 
|  | 4103 | ssl->handshake->alt_transform_out = tmp_transform; | 
|  | 4104 |  | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4105 | /* Swap epoch + sequence_number */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 4106 | mbedtls_platform_memcpy( tmp_out_ctr,                 ssl->cur_out_ctr,            8 ); | 
|  | 4107 | mbedtls_platform_memcpy( ssl->cur_out_ctr,            ssl->handshake->alt_out_ctr, 8 ); | 
|  | 4108 | mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr,                 8 ); | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4109 |  | 
|  | 4110 | /* Adjust to the newly activated transform */ | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4111 | ssl_update_out_pointers( ssl, ssl->transform_out ); | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4112 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4113 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 4114 | if( mbedtls_ssl_hw_record_activate != NULL ) | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4115 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4116 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4117 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4118 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); | 
|  | 4119 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4120 | } | 
|  | 4121 | } | 
|  | 4122 | #endif | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4123 | } | 
|  | 4124 |  | 
|  | 4125 | /* | 
|  | 4126 | * Retransmit the current flight of messages. | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4127 | */ | 
|  | 4128 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) | 
|  | 4129 | { | 
|  | 4130 | int ret = 0; | 
|  | 4131 |  | 
|  | 4132 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); | 
|  | 4133 |  | 
|  | 4134 | ret = mbedtls_ssl_flight_transmit( ssl ); | 
|  | 4135 |  | 
|  | 4136 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); | 
|  | 4137 |  | 
|  | 4138 | return( ret ); | 
|  | 4139 | } | 
|  | 4140 |  | 
|  | 4141 | /* | 
|  | 4142 | * Transmit or retransmit the current flight of messages. | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4143 | * | 
|  | 4144 | * Need to remember the current message in case flush_output returns | 
|  | 4145 | * WANT_WRITE, causing us to exit this function and come back later. | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4146 | * This function must be called until state is no longer SENDING. | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4147 | */ | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4148 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4149 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4150 | int ret; | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4151 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4152 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4153 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4154 | { | 
| Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 4155 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4156 |  | 
|  | 4157 | ssl->handshake->cur_msg = ssl->handshake->flight; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4158 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4159 | ssl_swap_epochs( ssl ); | 
|  | 4160 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4161 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4162 | } | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4163 |  | 
|  | 4164 | while( ssl->handshake->cur_msg != NULL ) | 
|  | 4165 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4166 | size_t max_frag_len; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4167 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4168 |  | 
| Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4169 | int const is_finished = | 
|  | 4170 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 4171 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); | 
|  | 4172 |  | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4173 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? | 
|  | 4174 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; | 
|  | 4175 |  | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4176 | /* Swap epochs before sending Finished: we can't do it after | 
|  | 4177 | * sending ChangeCipherSpec, in case write returns WANT_READ. | 
|  | 4178 | * Must be done before copying, may change out_msg pointer */ | 
| Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4179 | if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) ) | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4180 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4181 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) ); | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 4182 | ssl_swap_epochs( ssl ); | 
|  | 4183 | } | 
|  | 4184 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4185 | ret = ssl_get_remaining_payload_in_datagram( ssl ); | 
|  | 4186 | if( ret < 0 ) | 
|  | 4187 | return( ret ); | 
|  | 4188 | max_frag_len = (size_t) ret; | 
|  | 4189 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4190 | /* CCS is copied as is, while HS messages may need fragmentation */ | 
|  | 4191 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) | 
|  | 4192 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4193 | if( max_frag_len == 0 ) | 
|  | 4194 | { | 
|  | 4195 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
|  | 4196 | return( ret ); | 
|  | 4197 |  | 
|  | 4198 | continue; | 
|  | 4199 | } | 
|  | 4200 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 4201 | mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len ); | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4202 | ssl->out_msglen  = cur->len; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4203 | ssl->out_msgtype = cur->type; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4204 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4205 | /* Update position inside current message */ | 
|  | 4206 | ssl->handshake->cur_msg_p += cur->len; | 
|  | 4207 | } | 
|  | 4208 | else | 
|  | 4209 | { | 
|  | 4210 | const unsigned char * const p = ssl->handshake->cur_msg_p; | 
|  | 4211 | const size_t hs_len = cur->len - 12; | 
|  | 4212 | const size_t frag_off = p - ( cur->p + 12 ); | 
|  | 4213 | const size_t rem_len = hs_len - frag_off; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4214 | size_t cur_hs_frag_len, max_hs_frag_len; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4215 |  | 
| Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4216 | if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) ) | 
| Manuel Pégourié-Gonnard | a1071a5 | 2018-08-20 11:56:14 +0200 | [diff] [blame] | 4217 | { | 
| Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 4218 | if( is_finished ) | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4219 | ssl_swap_epochs( ssl ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4220 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4221 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
|  | 4222 | return( ret ); | 
|  | 4223 |  | 
|  | 4224 | continue; | 
|  | 4225 | } | 
|  | 4226 | max_hs_frag_len = max_frag_len - 12; | 
|  | 4227 |  | 
|  | 4228 | cur_hs_frag_len = rem_len > max_hs_frag_len ? | 
|  | 4229 | max_hs_frag_len : rem_len; | 
|  | 4230 |  | 
|  | 4231 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) | 
| Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 4232 | { | 
|  | 4233 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4234 | (unsigned) cur_hs_frag_len, | 
|  | 4235 | (unsigned) max_hs_frag_len ) ); | 
| Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 4236 | } | 
| Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 4237 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4238 | /* Messages are stored with handshake headers as if not fragmented, | 
|  | 4239 | * copy beginning of headers then fill fragmentation fields. | 
|  | 4240 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 4241 | mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4242 |  | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 4243 | (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off ); | 
|  | 4244 | (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9], | 
|  | 4245 | cur_hs_frag_len ); | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4246 |  | 
|  | 4247 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); | 
|  | 4248 |  | 
| Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 4249 | /* Copy the handshake message content and set records fields */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 4250 | mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4251 | ssl->out_msglen = cur_hs_frag_len + 12; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4252 | ssl->out_msgtype = cur->type; | 
|  | 4253 |  | 
|  | 4254 | /* Update position inside current message */ | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4255 | ssl->handshake->cur_msg_p += cur_hs_frag_len; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4256 | } | 
|  | 4257 |  | 
|  | 4258 | /* If done with the current message move to the next one if any */ | 
|  | 4259 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) | 
|  | 4260 | { | 
|  | 4261 | if( cur->next != NULL ) | 
|  | 4262 | { | 
|  | 4263 | ssl->handshake->cur_msg = cur->next; | 
|  | 4264 | ssl->handshake->cur_msg_p = cur->next->p + 12; | 
|  | 4265 | } | 
|  | 4266 | else | 
|  | 4267 | { | 
|  | 4268 | ssl->handshake->cur_msg = NULL; | 
|  | 4269 | ssl->handshake->cur_msg_p = NULL; | 
|  | 4270 | } | 
|  | 4271 | } | 
|  | 4272 |  | 
|  | 4273 | /* Actually send the message out */ | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 4274 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4275 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4276 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4277 | return( ret ); | 
|  | 4278 | } | 
|  | 4279 | } | 
|  | 4280 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4281 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
|  | 4282 | return( ret ); | 
|  | 4283 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 4284 | /* Update state and set timer */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4285 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 4286 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; | 
| Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 4287 | else | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 4288 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4289 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 4290 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); | 
|  | 4291 | } | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4292 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4293 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4294 |  | 
|  | 4295 | return( 0 ); | 
|  | 4296 | } | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4297 |  | 
|  | 4298 | /* | 
|  | 4299 | * To be called when the last message of an incoming flight is received. | 
|  | 4300 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4301 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4302 | { | 
|  | 4303 | /* We won't need to resend that one any more */ | 
|  | 4304 | ssl_flight_free( ssl->handshake->flight ); | 
|  | 4305 | ssl->handshake->flight = NULL; | 
|  | 4306 | ssl->handshake->cur_msg = NULL; | 
|  | 4307 |  | 
|  | 4308 | /* The next incoming flight will start with this msg_seq */ | 
|  | 4309 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; | 
|  | 4310 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4311 | /* We don't want to remember CCS's across flight boundaries. */ | 
| Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4312 | ssl->handshake->buffering.seen_ccs = 0; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4313 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4314 | /* Clear future message buffering structure. */ | 
|  | 4315 | ssl_buffering_free( ssl ); | 
|  | 4316 |  | 
| Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 4317 | /* Cancel timer */ | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4318 | ssl_set_timer( ssl, 0 ); | 
|  | 4319 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4320 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 4321 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4322 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4323 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4324 | } | 
|  | 4325 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4326 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4327 | } | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4328 |  | 
|  | 4329 | /* | 
|  | 4330 | * To be called when the last message of an outgoing flight is send. | 
|  | 4331 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4332 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4333 | { | 
| Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 4334 | ssl_reset_retransmit_timeout( ssl ); | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 4335 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4336 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4337 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 4338 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4339 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4340 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4341 | } | 
|  | 4342 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4343 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4344 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4345 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4346 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4347 | /* | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4348 | * Handshake layer functions | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4349 | */ | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4350 |  | 
|  | 4351 | /* | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4352 | * Write (DTLS: or queue) current handshake (including CCS) message. | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4353 | * | 
|  | 4354 | *  - fill in handshake headers | 
|  | 4355 | *  - update handshake checksum | 
|  | 4356 | *  - DTLS: save message for resending | 
|  | 4357 | *  - then pass to the record layer | 
|  | 4358 | * | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4359 | * DTLS: except for HelloRequest, messages are only queued, and will only be | 
|  | 4360 | * actually sent when calling flight_transmit() or resend(). | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4361 | * | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4362 | * Inputs: | 
|  | 4363 | *  - ssl->out_msglen: 4 + actual handshake message len | 
|  | 4364 | *      (4 is the size of handshake headers for TLS) | 
|  | 4365 | *  - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) | 
|  | 4366 | *  - ssl->out_msg + 4: the handshake message body | 
|  | 4367 | * | 
| Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 4368 | * Outputs, ie state before passing to flight_append() or write_record(): | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4369 | *   - ssl->out_msglen: the length of the record contents | 
|  | 4370 | *      (including handshake headers but excluding record headers) | 
|  | 4371 | *   - ssl->out_msg: the record contents (handshake headers + content) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4372 | */ | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4373 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4374 | { | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4375 | int ret; | 
|  | 4376 | const size_t hs_len = ssl->out_msglen - 4; | 
|  | 4377 | const unsigned char hs_type = ssl->out_msg[0]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4378 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4379 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); | 
|  | 4380 |  | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4381 | /* | 
|  | 4382 | * Sanity checks | 
|  | 4383 | */ | 
| Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4384 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE          && | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4385 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) | 
|  | 4386 | { | 
| Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4387 | /* In SSLv3, the client might send a NoCertificate alert. */ | 
|  | 4388 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4389 | if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 && | 
| Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4390 | ssl->out_msgtype    == MBEDTLS_SSL_MSG_ALERT       && | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 4391 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 4392 | MBEDTLS_SSL_IS_CLIENT ) ) | 
| Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 4393 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ | 
|  | 4394 | { | 
|  | 4395 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4396 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4397 | } | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4398 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4399 |  | 
| Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 4400 | /* Whenever we send anything different from a | 
|  | 4401 | * HelloRequest we should be in a handshake - double check. */ | 
|  | 4402 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 4403 | hs_type          == MBEDTLS_SSL_HS_HELLO_REQUEST ) && | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4404 | ssl->handshake == NULL ) | 
|  | 4405 | { | 
|  | 4406 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4407 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4408 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4409 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4410 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4411 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4412 | ssl->handshake != NULL && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4413 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4414 | { | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4415 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4416 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4417 | } | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4418 | #endif | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4419 |  | 
| Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 4420 | /* Double-check that we did not exceed the bounds | 
|  | 4421 | * of the outgoing record buffer. | 
|  | 4422 | * This should never fail as the various message | 
|  | 4423 | * writing functions must obey the bounds of the | 
|  | 4424 | * outgoing record buffer, but better be safe. | 
|  | 4425 | * | 
|  | 4426 | * Note: We deliberately do not check for the MTU or MFL here. | 
|  | 4427 | */ | 
|  | 4428 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
|  | 4429 | { | 
|  | 4430 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " | 
|  | 4431 | "size %u, maximum %u", | 
|  | 4432 | (unsigned) ssl->out_msglen, | 
|  | 4433 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | 
|  | 4434 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4435 | } | 
|  | 4436 |  | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4437 | /* | 
|  | 4438 | * Fill handshake headers | 
|  | 4439 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4440 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4441 | { | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 4442 | (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4443 |  | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4444 | /* | 
|  | 4445 | * DTLS has additional fields in the Handshake layer, | 
|  | 4446 | * between the length field and the actual payload: | 
|  | 4447 | *      uint16 message_seq; | 
|  | 4448 | *      uint24 fragment_offset; | 
|  | 4449 | *      uint24 fragment_length; | 
|  | 4450 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4451 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4452 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4453 | { | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4454 | /* Make room for the additional DTLS fields */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4455 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) | 
| Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4456 | { | 
|  | 4457 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " | 
|  | 4458 | "size %u, maximum %u", | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4459 | (unsigned) ( hs_len ), | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4460 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); | 
| Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4461 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 4462 | } | 
|  | 4463 |  | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4464 | memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len ); | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4465 | ssl->out_msglen += 8; | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4466 |  | 
| Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4467 | /* Write message_seq and update it, except for HelloRequest */ | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4468 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) | 
| Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4469 | { | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 4470 | (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4], | 
|  | 4471 | ssl->handshake->out_msg_seq ); | 
| Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 4472 | ++( ssl->handshake->out_msg_seq ); | 
| Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4473 | } | 
|  | 4474 | else | 
|  | 4475 | { | 
|  | 4476 | ssl->out_msg[4] = 0; | 
|  | 4477 | ssl->out_msg[5] = 0; | 
|  | 4478 | } | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4479 |  | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4480 | /* Handshake hashes are computed without fragmentation, | 
|  | 4481 | * so set frag_offset = 0 and frag_len = hs_len for now */ | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 4482 | mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 ); | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 4483 | mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4484 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4485 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4486 |  | 
| Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 4487 | /* Update running hashes of handshake messages seen */ | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4488 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) | 
| Hanno Becker | 8a4b590 | 2019-08-15 17:04:57 +0100 | [diff] [blame] | 4489 | mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4490 | } | 
|  | 4491 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4492 | /* Either send now, or just save to be sent (and resent) later */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4493 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4494 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 4495 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 4496 | hs_type          == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4497 | { | 
|  | 4498 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) | 
|  | 4499 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4500 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4501 | return( ret ); | 
|  | 4502 | } | 
|  | 4503 | } | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4504 | else | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4505 | #endif | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4506 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4507 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4508 | { | 
|  | 4509 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); | 
|  | 4510 | return( ret ); | 
|  | 4511 | } | 
|  | 4512 | } | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4513 |  | 
|  | 4514 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); | 
|  | 4515 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4516 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4517 | } | 
|  | 4518 |  | 
|  | 4519 | /* | 
|  | 4520 | * Record layer functions | 
|  | 4521 | */ | 
|  | 4522 |  | 
|  | 4523 | /* | 
|  | 4524 | * Write current record. | 
|  | 4525 | * | 
|  | 4526 | * Uses: | 
|  | 4527 | *  - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) | 
|  | 4528 | *  - ssl->out_msglen: length of the record content (excl headers) | 
|  | 4529 | *  - ssl->out_msg: record content | 
|  | 4530 | */ | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4531 | int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush ) | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4532 | { | 
|  | 4533 | int ret, done = 0; | 
|  | 4534 | size_t len = ssl->out_msglen; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4535 | uint8_t flush = force_flush; | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4536 |  | 
|  | 4537 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4538 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4539 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4540 | if( ssl->transform_out != NULL && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4541 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4542 | { | 
|  | 4543 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) | 
|  | 4544 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4545 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4546 | return( ret ); | 
|  | 4547 | } | 
|  | 4548 |  | 
|  | 4549 | len = ssl->out_msglen; | 
|  | 4550 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4551 | #endif /*MBEDTLS_ZLIB_SUPPORT */ | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4552 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4553 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 4554 | if( mbedtls_ssl_hw_record_write != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4555 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4557 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4558 | ret = mbedtls_ssl_hw_record_write( ssl ); | 
|  | 4559 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4560 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4561 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); | 
|  | 4562 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4563 | } | 
| Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4564 |  | 
|  | 4565 | if( ret == 0 ) | 
|  | 4566 | done = 1; | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4567 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4568 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4569 | if( !done ) | 
|  | 4570 | { | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4571 | unsigned i; | 
|  | 4572 | size_t protected_record_size; | 
| Jarno Lamsa | acb5eb0 | 2019-11-14 14:13:10 +0200 | [diff] [blame] | 4573 | volatile int encrypted_fi = 0; | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4574 |  | 
| Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4575 | /* Skip writing the record content type to after the encryption, | 
|  | 4576 | * as it may change when using the CID extension. */ | 
|  | 4577 |  | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4578 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), | 
|  | 4579 | mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 4580 | ssl->conf->transport, ssl->out_hdr + 1 ); | 
| Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4581 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 4582 | mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 4583 | (void)mbedtls_platform_put_uint16_be( ssl->out_len, len ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4584 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4585 | if( ssl->transform_out != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4586 | { | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4587 | mbedtls_record rec; | 
|  | 4588 |  | 
|  | 4589 | rec.buf         = ssl->out_iv; | 
|  | 4590 | rec.buf_len     = MBEDTLS_SSL_OUT_BUFFER_LEN - | 
|  | 4591 | ( ssl->out_iv - ssl->out_buf ); | 
|  | 4592 | rec.data_len    = ssl->out_msglen; | 
|  | 4593 | rec.data_offset = ssl->out_msg - rec.buf; | 
|  | 4594 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 4595 | mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 4596 | mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ), | 
|  | 4597 | mbedtls_ssl_get_minor_ver( ssl ), | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4598 | ssl->conf->transport, rec.ver ); | 
|  | 4599 | rec.type = ssl->out_msgtype; | 
|  | 4600 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4601 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 4602 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4603 | rec.cid_len = 0; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4604 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4605 |  | 
| Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4606 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, | 
| Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 4607 | mbedtls_ssl_conf_get_frng( ssl->conf ), | 
| Hanno Becker | 9a12243 | 2019-07-23 13:24:02 +0100 | [diff] [blame] | 4608 | mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4609 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4610 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4611 | return( ret ); | 
|  | 4612 | } | 
|  | 4613 |  | 
| Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4614 | if( rec.data_offset != 0 ) | 
|  | 4615 | { | 
|  | 4616 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4617 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4618 | } | 
|  | 4619 |  | 
| Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4620 | /* Update the record content type and CID. */ | 
|  | 4621 | ssl->out_msgtype = rec.type; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4622 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 4623 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 4624 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4625 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | c5aee96 | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 4626 | ssl->out_msglen = len = rec.data_len; | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 4627 | (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len ); | 
| Jarno Lamsa | acb5eb0 | 2019-11-14 14:13:10 +0200 | [diff] [blame] | 4628 | encrypted_fi = 1; | 
|  | 4629 | } | 
|  | 4630 |  | 
| Jarno Lamsa | 88db2ae | 2019-12-19 14:51:34 +0200 | [diff] [blame] | 4631 | /* Double check to ensure the encryption has been done */ | 
| Jarno Lamsa | acb5eb0 | 2019-11-14 14:13:10 +0200 | [diff] [blame] | 4632 | if( ssl->transform_out != NULL && encrypted_fi == 0 ) | 
|  | 4633 | { | 
| Jarno Lamsa | 88db2ae | 2019-12-19 14:51:34 +0200 | [diff] [blame] | 4634 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4635 | } | 
|  | 4636 |  | 
| Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4637 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4638 |  | 
|  | 4639 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4640 | /* In case of DTLS, double-check that we don't exceed | 
|  | 4641 | * the remaining space in the datagram. */ | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4642 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4643 | { | 
| Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 4644 | ret = ssl_get_remaining_space_in_datagram( ssl ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4645 | if( ret < 0 ) | 
|  | 4646 | return( ret ); | 
|  | 4647 |  | 
|  | 4648 | if( protected_record_size > (size_t) ret ) | 
|  | 4649 | { | 
|  | 4650 | /* Should never happen */ | 
|  | 4651 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4652 | } | 
|  | 4653 | } | 
|  | 4654 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4655 |  | 
| Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4656 | /* Now write the potentially updated record content type. */ | 
|  | 4657 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; | 
|  | 4658 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4659 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " | 
| Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4660 | "version = [%d:%d], msglen = %d", | 
|  | 4661 | ssl->out_hdr[0], ssl->out_hdr[1], | 
|  | 4662 | ssl->out_hdr[2], len ) ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4663 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4664 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", | 
| Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4665 | ssl->out_hdr, protected_record_size ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4666 |  | 
|  | 4667 | ssl->out_left += protected_record_size; | 
|  | 4668 | ssl->out_hdr  += protected_record_size; | 
|  | 4669 | ssl_update_out_pointers( ssl, ssl->transform_out ); | 
|  | 4670 |  | 
| Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4671 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) | 
|  | 4672 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) | 
|  | 4673 | break; | 
|  | 4674 |  | 
|  | 4675 | /* The loop goes to its end iff the counter is wrapping */ | 
|  | 4676 | if( i == ssl_ep_len( ssl ) ) | 
|  | 4677 | { | 
|  | 4678 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); | 
|  | 4679 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
|  | 4680 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4681 | } | 
|  | 4682 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4683 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4684 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 4685 | flush == SSL_DONT_FORCE_FLUSH ) | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4686 | { | 
| Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 4687 | size_t remaining; | 
|  | 4688 | ret = ssl_get_remaining_payload_in_datagram( ssl ); | 
|  | 4689 | if( ret < 0 ) | 
|  | 4690 | { | 
|  | 4691 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", | 
|  | 4692 | ret ); | 
|  | 4693 | return( ret ); | 
|  | 4694 | } | 
|  | 4695 |  | 
|  | 4696 | remaining = (size_t) ret; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4697 | if( remaining == 0 ) | 
| Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4698 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4699 | flush = SSL_FORCE_FLUSH; | 
| Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4700 | } | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4701 | else | 
|  | 4702 | { | 
| Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 4703 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) ); | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4704 | } | 
|  | 4705 | } | 
|  | 4706 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4707 |  | 
|  | 4708 | if( ( flush == SSL_FORCE_FLUSH ) && | 
|  | 4709 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4710 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4711 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4712 | return( ret ); | 
|  | 4713 | } | 
|  | 4714 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4715 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4716 |  | 
|  | 4717 | return( 0 ); | 
|  | 4718 | } | 
|  | 4719 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4720 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4721 |  | 
|  | 4722 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) | 
|  | 4723 | { | 
|  | 4724 | if( ssl->in_msglen < ssl->in_hslen || | 
| Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 4725 | mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0",        3 ) != 0 || | 
|  | 4726 | mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4727 | { | 
|  | 4728 | return( 1 ); | 
|  | 4729 | } | 
|  | 4730 | return( 0 ); | 
|  | 4731 | } | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4732 |  | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4733 | static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl ) | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4734 | { | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 4735 | return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) ); | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4736 | } | 
|  | 4737 |  | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4738 | static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl ) | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4739 | { | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 4740 | return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) ); | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4741 | } | 
|  | 4742 |  | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4743 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4744 | { | 
|  | 4745 | uint32_t msg_len, frag_off, frag_len; | 
|  | 4746 |  | 
|  | 4747 | msg_len  = ssl_get_hs_total_len( ssl ); | 
|  | 4748 | frag_off = ssl_get_hs_frag_off( ssl ); | 
|  | 4749 | frag_len = ssl_get_hs_frag_len( ssl ); | 
|  | 4750 |  | 
|  | 4751 | if( frag_off > msg_len ) | 
|  | 4752 | return( -1 ); | 
|  | 4753 |  | 
|  | 4754 | if( frag_len > msg_len - frag_off ) | 
|  | 4755 | return( -1 ); | 
|  | 4756 |  | 
|  | 4757 | if( frag_len + 12 > ssl->in_msglen ) | 
|  | 4758 | return( -1 ); | 
|  | 4759 |  | 
|  | 4760 | return( 0 ); | 
|  | 4761 | } | 
|  | 4762 |  | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4763 | /* | 
|  | 4764 | * Mark bits in bitmask (used for DTLS HS reassembly) | 
|  | 4765 | */ | 
|  | 4766 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) | 
|  | 4767 | { | 
|  | 4768 | unsigned int start_bits, end_bits; | 
|  | 4769 |  | 
|  | 4770 | start_bits = 8 - ( offset % 8 ); | 
|  | 4771 | if( start_bits != 8 ) | 
|  | 4772 | { | 
|  | 4773 | size_t first_byte_idx = offset / 8; | 
|  | 4774 |  | 
| Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4775 | /* Special case */ | 
|  | 4776 | if( len <= start_bits ) | 
|  | 4777 | { | 
|  | 4778 | for( ; len != 0; len-- ) | 
|  | 4779 | mask[first_byte_idx] |= 1 << ( start_bits - len ); | 
|  | 4780 |  | 
|  | 4781 | /* Avoid potential issues with offset or len becoming invalid */ | 
|  | 4782 | return; | 
|  | 4783 | } | 
|  | 4784 |  | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4785 | offset += start_bits; /* Now offset % 8 == 0 */ | 
|  | 4786 | len -= start_bits; | 
|  | 4787 |  | 
|  | 4788 | for( ; start_bits != 0; start_bits-- ) | 
|  | 4789 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); | 
|  | 4790 | } | 
|  | 4791 |  | 
|  | 4792 | end_bits = len % 8; | 
|  | 4793 | if( end_bits != 0 ) | 
|  | 4794 | { | 
|  | 4795 | size_t last_byte_idx = ( offset + len ) / 8; | 
|  | 4796 |  | 
|  | 4797 | len -= end_bits; /* Now len % 8 == 0 */ | 
|  | 4798 |  | 
|  | 4799 | for( ; end_bits != 0; end_bits-- ) | 
|  | 4800 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); | 
|  | 4801 | } | 
|  | 4802 |  | 
|  | 4803 | memset( mask + offset / 8, 0xFF, len / 8 ); | 
|  | 4804 | } | 
|  | 4805 |  | 
|  | 4806 | /* | 
|  | 4807 | * Check that bitmask is full | 
|  | 4808 | */ | 
|  | 4809 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) | 
|  | 4810 | { | 
|  | 4811 | size_t i; | 
|  | 4812 |  | 
|  | 4813 | for( i = 0; i < len / 8; i++ ) | 
|  | 4814 | if( mask[i] != 0xFF ) | 
|  | 4815 | return( -1 ); | 
|  | 4816 |  | 
|  | 4817 | for( i = 0; i < len % 8; i++ ) | 
|  | 4818 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) | 
|  | 4819 | return( -1 ); | 
|  | 4820 |  | 
|  | 4821 | return( 0 ); | 
|  | 4822 | } | 
|  | 4823 |  | 
| Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4824 | /* msg_len does not include the handshake header */ | 
| Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4825 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4826 | unsigned add_bitmap ) | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4827 | { | 
| Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4828 | size_t alloc_len; | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4829 |  | 
| Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4830 | alloc_len  = 12;                                 /* Handshake header */ | 
|  | 4831 | alloc_len += msg_len;                            /* Content buffer   */ | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4832 |  | 
| Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4833 | if( add_bitmap ) | 
|  | 4834 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap       */ | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4835 |  | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4836 | return( alloc_len ); | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4837 | } | 
| Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4838 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4839 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4840 |  | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4841 | static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl ) | 
| Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4842 | { | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 4843 | return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) ); | 
| Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4844 | } | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4845 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4846 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4847 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4848 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4849 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4850 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", | 
| Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4851 | ssl->in_msglen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4852 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4853 | } | 
|  | 4854 |  | 
| Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4855 | ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl ); | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4856 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4857 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4858 | " %d, type = %d, hslen = %d", | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4859 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4860 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4861 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4862 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4863 | { | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4864 | int ret; | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 4865 | unsigned int recv_msg_seq = (unsigned int) | 
|  | 4866 | mbedtls_platform_get_uint16_be( &ssl->in_msg[4] ); | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4867 |  | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4868 | if( ssl_check_hs_header( ssl ) != 0 ) | 
|  | 4869 | { | 
|  | 4870 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); | 
|  | 4871 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 4872 | } | 
|  | 4873 |  | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4874 | if( ssl->handshake != NULL && | 
| Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4875 | ( ( ssl->state   != MBEDTLS_SSL_HANDSHAKE_OVER && | 
|  | 4876 | recv_msg_seq != ssl->handshake->in_msg_seq ) || | 
|  | 4877 | ( ssl->state  == MBEDTLS_SSL_HANDSHAKE_OVER && | 
|  | 4878 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4879 | { | 
| Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4880 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) | 
|  | 4881 | { | 
|  | 4882 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", | 
|  | 4883 | recv_msg_seq, | 
|  | 4884 | ssl->handshake->in_msg_seq ) ); | 
|  | 4885 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); | 
|  | 4886 | } | 
|  | 4887 |  | 
| Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4888 | /* Retransmit only on last message from previous flight, to avoid | 
|  | 4889 | * too many retransmissions. | 
|  | 4890 | * Besides, No sane server ever retransmits HelloVerifyRequest */ | 
|  | 4891 | if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4892 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4893 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4894 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4895 | "message_seq = %d, start_of_flight = %d", | 
|  | 4896 | recv_msg_seq, | 
|  | 4897 | ssl->handshake->in_flight_start_seq ) ); | 
|  | 4898 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4899 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4900 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4901 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4902 | return( ret ); | 
|  | 4903 | } | 
|  | 4904 | } | 
|  | 4905 | else | 
|  | 4906 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4907 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4908 | "message_seq = %d, expected = %d", | 
|  | 4909 | recv_msg_seq, | 
|  | 4910 | ssl->handshake->in_msg_seq ) ); | 
|  | 4911 | } | 
|  | 4912 |  | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4913 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4914 | } | 
|  | 4915 | /* Wait until message completion to increment in_msg_seq */ | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4916 |  | 
| Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4917 | /* Message reassembly is handled alongside buffering of future | 
|  | 4918 | * messages; the commonality is that both handshake fragments and | 
| Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4919 | * future messages cannot be forwarded immediately to the | 
| Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4920 | * handshake logic layer. */ | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4921 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4922 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4923 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); | 
| Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4924 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4925 | } | 
|  | 4926 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4927 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4928 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4929 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4930 | { | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4931 | /* With TLS we don't handle fragmentation (for now) */ | 
|  | 4932 | if( ssl->in_msglen < ssl->in_hslen ) | 
|  | 4933 | { | 
|  | 4934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); | 
|  | 4935 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 4936 | } | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4937 | } | 
| Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 4938 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4939 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4940 | return( 0 ); | 
|  | 4941 | } | 
|  | 4942 |  | 
|  | 4943 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) | 
|  | 4944 | { | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4945 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4946 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4947 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) | 
| Hanno Becker | 8a4b590 | 2019-08-15 17:04:57 +0100 | [diff] [blame] | 4948 | mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen ); | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4949 |  | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4950 | /* Handshake message is complete, increment counter */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4951 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4952 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4953 | ssl->handshake != NULL ) | 
|  | 4954 | { | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4955 | unsigned offset; | 
|  | 4956 | mbedtls_ssl_hs_buffer *hs_buf; | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4957 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4958 | /* Increment handshake sequence number */ | 
|  | 4959 | hs->in_msg_seq++; | 
|  | 4960 |  | 
|  | 4961 | /* | 
|  | 4962 | * Clear up handshake buffering and reassembly structure. | 
|  | 4963 | */ | 
|  | 4964 |  | 
|  | 4965 | /* Free first entry */ | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4966 | ssl_buffering_free_slot( ssl, 0 ); | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4967 |  | 
|  | 4968 | /* Shift all other entries */ | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4969 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; | 
|  | 4970 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4971 | offset++, hs_buf++ ) | 
|  | 4972 | { | 
|  | 4973 | *hs_buf = *(hs_buf + 1); | 
|  | 4974 | } | 
|  | 4975 |  | 
|  | 4976 | /* Create a fresh last entry */ | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 4977 | mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4978 | } | 
|  | 4979 | #endif | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4980 | } | 
|  | 4981 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4982 | /* | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4983 | * DTLS anti-replay: RFC 6347 4.1.2.6 | 
|  | 4984 | * | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4985 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). | 
|  | 4986 | * Bit n is set iff record number in_window_top - n has been seen. | 
|  | 4987 | * | 
|  | 4988 | * Usually, in_window_top is the last record number seen and the lsb of | 
|  | 4989 | * in_window is set. The only exception is the initial state (record number 0 | 
|  | 4990 | * not seen yet). | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4991 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4992 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 4993 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4994 | { | 
|  | 4995 | ssl->in_window_top = 0; | 
|  | 4996 | ssl->in_window = 0; | 
|  | 4997 | } | 
|  | 4998 |  | 
|  | 4999 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) | 
|  | 5000 | { | 
|  | 5001 | return( ( (uint64_t) buf[0] << 40 ) | | 
|  | 5002 | ( (uint64_t) buf[1] << 32 ) | | 
|  | 5003 | ( (uint64_t) buf[2] << 24 ) | | 
|  | 5004 | ( (uint64_t) buf[3] << 16 ) | | 
|  | 5005 | ( (uint64_t) buf[4] <<  8 ) | | 
|  | 5006 | ( (uint64_t) buf[5]       ) ); | 
|  | 5007 | } | 
|  | 5008 |  | 
| Arto Kinnunen | 8a8488c | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 5009 | static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr ) | 
|  | 5010 | { | 
|  | 5011 | int ret; | 
|  | 5012 | unsigned char *original_in_ctr; | 
|  | 5013 |  | 
|  | 5014 | // save original in_ctr | 
|  | 5015 | original_in_ctr = ssl->in_ctr; | 
|  | 5016 |  | 
|  | 5017 | // use counter from record | 
|  | 5018 | ssl->in_ctr = record_in_ctr; | 
|  | 5019 |  | 
|  | 5020 | ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl ); | 
|  | 5021 |  | 
|  | 5022 | // restore the counter | 
|  | 5023 | ssl->in_ctr = original_in_ctr; | 
|  | 5024 |  | 
|  | 5025 | return ret; | 
|  | 5026 | } | 
|  | 5027 |  | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5028 | /* | 
|  | 5029 | * Return 0 if sequence number is acceptable, -1 otherwise | 
|  | 5030 | */ | 
| Hanno Becker | fc55172 | 2019-07-12 08:50:37 +0100 | [diff] [blame] | 5031 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl ) | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5032 | { | 
|  | 5033 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); | 
|  | 5034 | uint64_t bit; | 
|  | 5035 |  | 
| Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 5036 | if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) == | 
|  | 5037 | MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) | 
|  | 5038 | { | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 5039 | return( 0 ); | 
| Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 5040 | } | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 5041 |  | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5042 | if( rec_seqnum > ssl->in_window_top ) | 
|  | 5043 | return( 0 ); | 
|  | 5044 |  | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 5045 | bit = ssl->in_window_top - rec_seqnum; | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5046 |  | 
|  | 5047 | if( bit >= 64 ) | 
|  | 5048 | return( -1 ); | 
|  | 5049 |  | 
|  | 5050 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) | 
|  | 5051 | return( -1 ); | 
|  | 5052 |  | 
|  | 5053 | return( 0 ); | 
|  | 5054 | } | 
|  | 5055 |  | 
|  | 5056 | /* | 
|  | 5057 | * Update replay window on new validated record | 
|  | 5058 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5059 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5060 | { | 
|  | 5061 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); | 
|  | 5062 |  | 
| Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 5063 | if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) == | 
|  | 5064 | MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) | 
|  | 5065 | { | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 5066 | return; | 
| Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 5067 | } | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 5068 |  | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5069 | if( rec_seqnum > ssl->in_window_top ) | 
|  | 5070 | { | 
|  | 5071 | /* Update window_top and the contents of the window */ | 
|  | 5072 | uint64_t shift = rec_seqnum - ssl->in_window_top; | 
|  | 5073 |  | 
|  | 5074 | if( shift >= 64 ) | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 5075 | ssl->in_window = 1; | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5076 | else | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 5077 | { | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5078 | ssl->in_window <<= shift; | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 5079 | ssl->in_window |= 1; | 
|  | 5080 | } | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5081 |  | 
|  | 5082 | ssl->in_window_top = rec_seqnum; | 
|  | 5083 | } | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5084 | else | 
|  | 5085 | { | 
|  | 5086 | /* Mark that number as seen in the current window */ | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 5087 | uint64_t bit = ssl->in_window_top - rec_seqnum; | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5088 |  | 
|  | 5089 | if( bit < 64 ) /* Always true, but be extra sure */ | 
|  | 5090 | ssl->in_window |= (uint64_t) 1 << bit; | 
|  | 5091 | } | 
|  | 5092 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5093 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5094 |  | 
| Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 5095 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5096 | /* Forward declaration */ | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 5097 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); | 
|  | 5098 |  | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5099 | /* | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5100 | * Without any SSL context, check if a datagram looks like a ClientHello with | 
|  | 5101 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. | 
| Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 5102 | * Both input and output include full DTLS headers. | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5103 | * | 
|  | 5104 | * - if cookie is valid, return 0 | 
|  | 5105 | * - if ClientHello looks superficially valid but cookie is not, | 
|  | 5106 | *   fill obuf and set olen, then | 
|  | 5107 | *   return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED | 
|  | 5108 | * - otherwise return a specific error code | 
|  | 5109 | */ | 
|  | 5110 | static int ssl_check_dtls_clihlo_cookie( | 
|  | 5111 | mbedtls_ssl_cookie_write_t *f_cookie_write, | 
|  | 5112 | mbedtls_ssl_cookie_check_t *f_cookie_check, | 
|  | 5113 | void *p_cookie, | 
|  | 5114 | const unsigned char *cli_id, size_t cli_id_len, | 
|  | 5115 | const unsigned char *in, size_t in_len, | 
|  | 5116 | unsigned char *obuf, size_t buf_len, size_t *olen ) | 
|  | 5117 | { | 
|  | 5118 | size_t sid_len, cookie_len; | 
|  | 5119 | unsigned char *p; | 
|  | 5120 |  | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5121 | /* | 
|  | 5122 | * Structure of ClientHello with record and handshake headers, | 
|  | 5123 | * and expected values. We don't need to check a lot, more checks will be | 
|  | 5124 | * done when actually parsing the ClientHello - skipping those checks | 
|  | 5125 | * avoids code duplication and does not make cookie forging any easier. | 
|  | 5126 | * | 
|  | 5127 | *  0-0  ContentType type;                  copied, must be handshake | 
|  | 5128 | *  1-2  ProtocolVersion version;           copied | 
|  | 5129 | *  3-4  uint16 epoch;                      copied, must be 0 | 
|  | 5130 | *  5-10 uint48 sequence_number;            copied | 
|  | 5131 | * 11-12 uint16 length;                     (ignored) | 
|  | 5132 | * | 
|  | 5133 | * 13-13 HandshakeType msg_type;            (ignored) | 
|  | 5134 | * 14-16 uint24 length;                     (ignored) | 
|  | 5135 | * 17-18 uint16 message_seq;                copied | 
|  | 5136 | * 19-21 uint24 fragment_offset;            copied, must be 0 | 
|  | 5137 | * 22-24 uint24 fragment_length;            (ignored) | 
|  | 5138 | * | 
|  | 5139 | * 25-26 ProtocolVersion client_version;    (ignored) | 
|  | 5140 | * 27-58 Random random;                     (ignored) | 
|  | 5141 | * 59-xx SessionID session_id;              1 byte len + sid_len content | 
|  | 5142 | * 60+   opaque cookie<0..2^8-1>;           1 byte len + content | 
|  | 5143 | *       ... | 
|  | 5144 | * | 
|  | 5145 | * Minimum length is 61 bytes. | 
|  | 5146 | */ | 
|  | 5147 | if( in_len < 61 || | 
|  | 5148 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || | 
|  | 5149 | in[3] != 0 || in[4] != 0 || | 
|  | 5150 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) | 
|  | 5151 | { | 
|  | 5152 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 5153 | } | 
|  | 5154 |  | 
|  | 5155 | sid_len = in[59]; | 
|  | 5156 | if( sid_len > in_len - 61 ) | 
|  | 5157 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 5158 |  | 
|  | 5159 | cookie_len = in[60 + sid_len]; | 
|  | 5160 | if( cookie_len > in_len - 60 ) | 
|  | 5161 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 5162 |  | 
|  | 5163 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, | 
|  | 5164 | cli_id, cli_id_len ) == 0 ) | 
|  | 5165 | { | 
|  | 5166 | /* Valid cookie */ | 
|  | 5167 | return( 0 ); | 
|  | 5168 | } | 
|  | 5169 |  | 
|  | 5170 | /* | 
|  | 5171 | * If we get here, we've got an invalid cookie, let's prepare HVR. | 
|  | 5172 | * | 
|  | 5173 | *  0-0  ContentType type;                  copied | 
|  | 5174 | *  1-2  ProtocolVersion version;           copied | 
|  | 5175 | *  3-4  uint16 epoch;                      copied | 
|  | 5176 | *  5-10 uint48 sequence_number;            copied | 
|  | 5177 | * 11-12 uint16 length;                     olen - 13 | 
|  | 5178 | * | 
|  | 5179 | * 13-13 HandshakeType msg_type;            hello_verify_request | 
|  | 5180 | * 14-16 uint24 length;                     olen - 25 | 
|  | 5181 | * 17-18 uint16 message_seq;                copied | 
|  | 5182 | * 19-21 uint24 fragment_offset;            copied | 
|  | 5183 | * 22-24 uint24 fragment_length;            olen - 25 | 
|  | 5184 | * | 
|  | 5185 | * 25-26 ProtocolVersion server_version;    0xfe 0xff | 
|  | 5186 | * 27-27 opaque cookie<0..2^8-1>;           cookie_len = olen - 27, cookie | 
|  | 5187 | * | 
|  | 5188 | * Minimum length is 28. | 
|  | 5189 | */ | 
|  | 5190 | if( buf_len < 28 ) | 
|  | 5191 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 5192 |  | 
|  | 5193 | /* Copy most fields and adapt others */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 5194 | mbedtls_platform_memcpy( obuf, in, 25 ); | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5195 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; | 
|  | 5196 | obuf[25] = 0xfe; | 
|  | 5197 | obuf[26] = 0xff; | 
|  | 5198 |  | 
|  | 5199 | /* Generate and write actual cookie */ | 
|  | 5200 | p = obuf + 28; | 
|  | 5201 | if( f_cookie_write( p_cookie, | 
|  | 5202 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) | 
|  | 5203 | { | 
|  | 5204 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5205 | } | 
|  | 5206 |  | 
|  | 5207 | *olen = p - obuf; | 
|  | 5208 |  | 
|  | 5209 | /* Go back and fill length fields */ | 
|  | 5210 | obuf[27] = (unsigned char)( *olen - 28 ); | 
|  | 5211 |  | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 5212 | (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) ); | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 5213 | obuf[22] = obuf[14]; | 
|  | 5214 | obuf[23] = obuf[15]; | 
|  | 5215 | obuf[24] = obuf[16]; | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5216 |  | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 5217 | (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) ); | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5218 |  | 
|  | 5219 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); | 
|  | 5220 | } | 
|  | 5221 |  | 
|  | 5222 | /* | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5223 | * Handle possible client reconnect with the same UDP quadruplet | 
|  | 5224 | * (RFC 6347 Section 4.2.8). | 
|  | 5225 | * | 
|  | 5226 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record | 
|  | 5227 | * that looks like a ClientHello. | 
|  | 5228 | * | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5229 | * - if the input looks like a ClientHello without cookies, | 
|  | 5230 | *   send back HelloVerifyRequest, then | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5231 | *   return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5232 | * - if the input looks like a ClientHello with a valid cookie, | 
|  | 5233 | *   reset the session of the current context, and | 
| Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 5234 | *   return MBEDTLS_ERR_SSL_CLIENT_RECONNECT | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5235 | * - if anything goes wrong, return a specific error code | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5236 | * | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5237 | * mbedtls_ssl_read_record() will ignore the record if anything else than | 
| Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 5238 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function | 
|  | 5239 | * cannot not return 0. | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5240 | */ | 
|  | 5241 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) | 
|  | 5242 | { | 
|  | 5243 | int ret; | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5244 | size_t len; | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5245 |  | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5246 | if( ssl->conf->f_cookie_write == NULL || | 
|  | 5247 | ssl->conf->f_cookie_check == NULL ) | 
|  | 5248 | { | 
|  | 5249 | /* If we can't use cookies to verify reachability of the peer, | 
|  | 5250 | * drop the record. */ | 
|  | 5251 | return( 0 ); | 
|  | 5252 | } | 
|  | 5253 |  | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5254 | ret = ssl_check_dtls_clihlo_cookie( | 
|  | 5255 | ssl->conf->f_cookie_write, | 
|  | 5256 | ssl->conf->f_cookie_check, | 
|  | 5257 | ssl->conf->p_cookie, | 
|  | 5258 | ssl->cli_id, ssl->cli_id_len, | 
|  | 5259 | ssl->in_buf, ssl->in_left, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5260 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5261 |  | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5262 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); | 
|  | 5263 |  | 
|  | 5264 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5265 | { | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 5266 | /* Don't check write errors as we can't do anything here. | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5267 | * If the error is permanent we'll catch it later, | 
|  | 5268 | * if it's not, then hopefully it'll work next time. */ | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 5269 | (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len ); | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5270 | ret = 0; | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5271 | } | 
|  | 5272 |  | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5273 | if( ret == 0 ) | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5274 | { | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 5275 | /* Got a valid cookie, partially reset context */ | 
|  | 5276 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) | 
|  | 5277 | { | 
|  | 5278 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); | 
|  | 5279 | return( ret ); | 
|  | 5280 | } | 
|  | 5281 |  | 
|  | 5282 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5283 | } | 
|  | 5284 |  | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5285 | return( ret ); | 
|  | 5286 | } | 
| Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 5287 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 5288 |  | 
| Hanno Becker | 46483f1 | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 5289 | static int ssl_check_record_type( uint8_t record_type ) | 
|  | 5290 | { | 
|  | 5291 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 5292 | record_type != MBEDTLS_SSL_MSG_ALERT && | 
|  | 5293 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && | 
|  | 5294 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) | 
|  | 5295 | { | 
|  | 5296 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5297 | } | 
|  | 5298 |  | 
|  | 5299 | return( 0 ); | 
|  | 5300 | } | 
|  | 5301 |  | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 5302 | /* | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5303 | * ContentType type; | 
|  | 5304 | * ProtocolVersion version; | 
|  | 5305 | * uint16 epoch;            // DTLS only | 
|  | 5306 | * uint48 sequence_number;  // DTLS only | 
|  | 5307 | * uint16 length; | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5308 | * | 
|  | 5309 | * Return 0 if header looks sane (and, for DTLS, the record is expected) | 
| Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 5310 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5311 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. | 
|  | 5312 | * | 
|  | 5313 | * With DTLS, mbedtls_ssl_read_record() will: | 
| Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 5314 | * 1. proceed with the record if this function returns 0 | 
|  | 5315 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD | 
|  | 5316 | * 3. return CLIENT_RECONNECT if this function return that value | 
|  | 5317 | * 4. drop the whole datagram if this function returns anything else. | 
|  | 5318 | * Point 2 is needed when the peer is resending, and we have already received | 
|  | 5319 | * the first record from a datagram but are still waiting for the others. | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5320 | */ | 
| Hanno Becker | 21fc61c | 2019-07-12 11:10:16 +0100 | [diff] [blame] | 5321 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5322 | unsigned char *buf, | 
|  | 5323 | size_t len, | 
|  | 5324 | mbedtls_record *rec ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5325 | { | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5326 | int major_ver, minor_ver; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5327 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5328 | size_t const rec_hdr_type_offset    = 0; | 
|  | 5329 | size_t const rec_hdr_type_len       = 1; | 
| Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 5330 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5331 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + | 
|  | 5332 | rec_hdr_type_len; | 
|  | 5333 | size_t const rec_hdr_version_len    = 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5334 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5335 | size_t const rec_hdr_ctr_len        = 8; | 
|  | 5336 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 6181761 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 5337 | uint32_t     rec_epoch; | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5338 | size_t const rec_hdr_ctr_offset     = rec_hdr_version_offset + | 
|  | 5339 | rec_hdr_version_len; | 
|  | 5340 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5341 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5342 | size_t const rec_hdr_cid_offset     = rec_hdr_ctr_offset + | 
|  | 5343 | rec_hdr_ctr_len; | 
| Hanno Becker | 6181761 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 5344 | size_t       rec_hdr_cid_len        = 0; | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5345 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 5346 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5347 |  | 
|  | 5348 | size_t       rec_hdr_len_offset; /* To be determined */ | 
|  | 5349 | size_t const rec_hdr_len_len    = 2; | 
|  | 5350 |  | 
|  | 5351 | /* | 
|  | 5352 | * Check minimum lengths for record header. | 
|  | 5353 | */ | 
|  | 5354 |  | 
|  | 5355 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5356 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
|  | 5357 | { | 
|  | 5358 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; | 
|  | 5359 | } | 
|  | 5360 | MBEDTLS_SSL_TRANSPORT_ELSE | 
|  | 5361 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5362 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 5363 | { | 
|  | 5364 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; | 
|  | 5365 | } | 
|  | 5366 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5367 |  | 
|  | 5368 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) | 
|  | 5369 | { | 
|  | 5370 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u", | 
|  | 5371 | (unsigned) len, | 
|  | 5372 | (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) ); | 
|  | 5373 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5374 | } | 
|  | 5375 |  | 
|  | 5376 | /* | 
|  | 5377 | * Parse and validate record content type | 
|  | 5378 | */ | 
|  | 5379 |  | 
|  | 5380 | rec->type = buf[ rec_hdr_type_offset ]; | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5381 |  | 
|  | 5382 | /* Check record content type */ | 
|  | 5383 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 5384 | rec->cid_len = 0; | 
|  | 5385 |  | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5386 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5387 | mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0        && | 
|  | 5388 | rec->type == MBEDTLS_SSL_MSG_CID ) | 
| Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5389 | { | 
|  | 5390 | /* Shift pointers to account for record header including CID | 
|  | 5391 | * struct { | 
|  | 5392 | *   ContentType special_type = tls12_cid; | 
|  | 5393 | *   ProtocolVersion version; | 
|  | 5394 | *   uint16 epoch; | 
|  | 5395 | *   uint48 sequence_number; | 
| Hanno Becker | 3b2bf5b | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 5396 | *   opaque cid[cid_length]; // Additional field compared to | 
|  | 5397 | *                           // default DTLS record format | 
| Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5398 | *   uint16 length; | 
|  | 5399 | *   opaque enc_content[DTLSCiphertext.length]; | 
|  | 5400 | * } DTLSCiphertext; | 
|  | 5401 | */ | 
|  | 5402 |  | 
|  | 5403 | /* So far, we only support static CID lengths | 
|  | 5404 | * fixed in the configuration. */ | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5405 | rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf ); | 
|  | 5406 | rec_hdr_len_offset += rec_hdr_cid_len; | 
| Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5407 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5408 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) | 
| Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5409 | { | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5410 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u", | 
|  | 5411 | (unsigned) len, | 
|  | 5412 | (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) ); | 
| Hanno Becker | 2982346 | 2019-07-10 14:53:43 +0100 | [diff] [blame] | 5413 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Hanno Becker | de7d6d3 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 5414 | } | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5415 |  | 
| Manuel Pégourié-Gonnard | f3a15b3 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 5416 | /* configured CID len is guaranteed at most 255, see | 
|  | 5417 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ | 
|  | 5418 | rec->cid_len = (uint8_t) rec_hdr_cid_len; | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 5419 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 5420 | memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len ); | 
| Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5421 | } | 
|  | 5422 | else | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5423 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5424 | { | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5425 | if( ssl_check_record_type( rec->type ) ) | 
|  | 5426 | { | 
| Hanno Becker | 03e2db6 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 5427 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u", | 
|  | 5428 | (unsigned) rec->type ) ); | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5429 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5430 | } | 
| Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 5431 | } | 
|  | 5432 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5433 | /* | 
|  | 5434 | * Parse and validate record version | 
|  | 5435 | */ | 
|  | 5436 |  | 
| Hanno Becker | 8061c6e | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5437 | rec->ver[0] = buf[ rec_hdr_version_offset + 0 ]; | 
|  | 5438 | rec->ver[1] = buf[ rec_hdr_version_offset + 1 ]; | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5439 | mbedtls_ssl_read_version( &major_ver, &minor_ver, | 
|  | 5440 | ssl->conf->transport, | 
| Hanno Becker | 8061c6e | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 5441 | &rec->ver[0] ); | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5442 |  | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 5443 | if( major_ver != mbedtls_ssl_get_major_ver( ssl ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5444 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5445 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); | 
|  | 5446 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5447 | } | 
|  | 5448 |  | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 5449 | if( mbedtls_ssl_ver_gt( minor_ver, | 
|  | 5450 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5451 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5452 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); | 
|  | 5453 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5454 | } | 
|  | 5455 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5456 | /* | 
|  | 5457 | * Parse/Copy record sequence number. | 
|  | 5458 | */ | 
| Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5459 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5460 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5461 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
|  | 5462 | { | 
|  | 5463 | /* Copy explicit record sequence number from input buffer. */ | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 5464 | /* Not using more secure mbedtls_platform_memcpy as sequence number is public */ | 
|  | 5465 | memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset, | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5466 | rec_hdr_ctr_len ); | 
|  | 5467 | } | 
|  | 5468 | MBEDTLS_SSL_TRANSPORT_ELSE | 
|  | 5469 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5470 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 5471 | { | 
|  | 5472 | /* Copy implicit record sequence number from SSL context structure. */ | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 5473 | /* Not using more secure mbedtls_platform_memcpy as sequence number is public */ | 
|  | 5474 | memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len ); | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5475 | } | 
|  | 5476 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5477 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5478 | /* | 
|  | 5479 | * Parse record length. | 
|  | 5480 | */ | 
|  | 5481 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5482 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 5483 | rec->data_len = mbedtls_platform_get_uint16_be(  &buf[rec_hdr_len_offset] ); | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5484 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset ); | 
|  | 5485 |  | 
| Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5486 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " | 
| Hanno Becker | d8f7c4a | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 5487 | "version = [%d:%d], msglen = %d", | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5488 | rec->type, | 
|  | 5489 | major_ver, minor_ver, rec->data_len ) ); | 
|  | 5490 |  | 
|  | 5491 | rec->buf     = buf; | 
|  | 5492 | rec->buf_len = rec->data_offset + rec->data_len; | 
| Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5493 |  | 
| Hanno Becker | ec01408 | 2019-07-26 08:20:27 +0100 | [diff] [blame] | 5494 | if( rec->data_len == 0 ) | 
|  | 5495 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5496 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5497 | /* | 
| Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5498 | * DTLS-related tests. | 
|  | 5499 | * Check epoch before checking length constraint because | 
|  | 5500 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec | 
|  | 5501 | * message gets duplicated before the corresponding Finished message, | 
|  | 5502 | * the second ChangeCipherSpec should be discarded because it belongs | 
|  | 5503 | * to an old epoch, but not because its length is shorter than | 
|  | 5504 | * the minimum record length for packets using the new record transform. | 
|  | 5505 | * Note that these two kinds of failures are handled differently, | 
|  | 5506 | * as an unexpected record is silently skipped but an invalid | 
|  | 5507 | * record leads to the entire datagram being dropped. | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5508 | */ | 
|  | 5509 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5510 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5511 | { | 
| Arto Kinnunen | a3fa06e | 2019-09-09 12:22:51 +0300 | [diff] [blame] | 5512 | rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr ); | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5513 |  | 
| Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5514 | /* Check that the datagram is large enough to contain a record | 
|  | 5515 | * of the advertised length. */ | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5516 | if( len < rec->data_offset + rec->data_len ) | 
| Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5517 | { | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 5518 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.", | 
|  | 5519 | (unsigned) len, | 
|  | 5520 | (unsigned)( rec->data_offset + rec->data_len ) ) ); | 
| Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5521 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5522 | } | 
| Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5523 |  | 
| Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5524 | /* Records from other, non-matching epochs are silently discarded. | 
|  | 5525 | * (The case of same-port Client reconnects must be considered in | 
|  | 5526 | *  the caller). */ | 
| Hanno Becker | c1c173c | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5527 | if( rec_epoch != ssl->in_epoch ) | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5528 | { | 
|  | 5529 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " | 
|  | 5530 | "expected %d, received %d", | 
|  | 5531 | ssl->in_epoch, rec_epoch ) ); | 
| Hanno Becker | c1c173c | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 5532 |  | 
|  | 5533 | /* Records from the next epoch are considered for buffering | 
|  | 5534 | * (concretely: early Finished messages). */ | 
|  | 5535 | if( rec_epoch == (unsigned) ssl->in_epoch + 1 ) | 
|  | 5536 | { | 
|  | 5537 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); | 
|  | 5538 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); | 
|  | 5539 | } | 
|  | 5540 |  | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5541 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5542 | } | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5543 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Hanno Becker | 6c0e53c | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 5544 | /* For records from the correct epoch, check whether their | 
|  | 5545 | * sequence number has been seen before. */ | 
| Arto Kinnunen | 8a8488c | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 5546 | else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl, | 
|  | 5547 | &rec->ctr[0] ) != 0 ) | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5548 | { | 
|  | 5549 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); | 
|  | 5550 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); | 
|  | 5551 | } | 
|  | 5552 | #endif | 
|  | 5553 | } | 
|  | 5554 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5555 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5556 | return( 0 ); | 
|  | 5557 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5558 |  | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5559 |  | 
|  | 5560 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) | 
|  | 5561 | static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl ) | 
|  | 5562 | { | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 5563 | unsigned int rec_epoch = (unsigned int) | 
|  | 5564 | mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] ); | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 5565 |  | 
|  | 5566 | /* | 
|  | 5567 | * Check for an epoch 0 ClientHello. We can't use in_msg here to | 
|  | 5568 | * access the first byte of record content (handshake type), as we | 
|  | 5569 | * have an active transform (possibly iv_len != 0), so use the | 
|  | 5570 | * fact that the record header len is 13 instead. | 
|  | 5571 | */ | 
|  | 5572 | if( rec_epoch == 0 && | 
|  | 5573 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 5574 | MBEDTLS_SSL_IS_SERVER && | 
|  | 5575 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && | 
|  | 5576 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 5577 | ssl->in_left > 13 && | 
|  | 5578 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) | 
|  | 5579 | { | 
|  | 5580 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " | 
|  | 5581 | "from the same port" ) ); | 
|  | 5582 | return( ssl_handle_possible_reconnect( ssl ) ); | 
|  | 5583 | } | 
|  | 5584 |  | 
|  | 5585 | return( 0 ); | 
|  | 5586 | } | 
|  | 5587 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ | 
|  | 5588 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5589 | /* | 
| Manuel Pégourié-Gonnard | ae48d86 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 5590 | * If applicable, decrypt record content | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5591 | */ | 
| Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5592 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl, | 
|  | 5593 | mbedtls_record *rec ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5594 | { | 
|  | 5595 | int ret, done = 0; | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5596 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5597 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", | 
| Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5598 | rec->buf, rec->buf_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5599 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5600 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 5601 | if( mbedtls_ssl_hw_record_read != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5602 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5603 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5604 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5605 | ret = mbedtls_ssl_hw_record_read( ssl ); | 
|  | 5606 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5607 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5608 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); | 
|  | 5609 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5610 | } | 
| Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 5611 |  | 
|  | 5612 | if( ret == 0 ) | 
|  | 5613 | done = 1; | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5614 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5615 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5616 | if( !done && ssl->transform_in != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5617 | { | 
| Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5618 | unsigned char const old_msg_type = rec->type; | 
|  | 5619 |  | 
| Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 5620 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, | 
| Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 5621 | rec ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5622 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5623 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); | 
| Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5624 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5625 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5626 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && | 
| Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 5627 | mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf ) | 
|  | 5628 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) | 
| Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5629 | { | 
| Hanno Becker | 675c4d6 | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 5630 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); | 
| Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5631 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; | 
| Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5632 | } | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5633 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5634 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5635 | return( ret ); | 
|  | 5636 | } | 
|  | 5637 |  | 
| Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5638 | if( old_msg_type != rec->type ) | 
| Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 5639 | { | 
| Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5640 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", | 
| Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5641 | old_msg_type, rec->type ) ); | 
| Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 5642 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5643 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5644 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", | 
| Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5645 | rec->buf + rec->data_offset, rec->data_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5646 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5647 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5648 | /* We have already checked the record content type | 
|  | 5649 | * in ssl_parse_record_header(), failing or silently | 
|  | 5650 | * dropping the record in the case of an unknown type. | 
|  | 5651 | * | 
|  | 5652 | * Since with the use of CIDs, the record content type | 
|  | 5653 | * might change during decryption, re-check the record | 
|  | 5654 | * content type, but treat a failure as fatal this time. */ | 
| Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5655 | if( ssl_check_record_type( rec->type ) ) | 
| Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5656 | { | 
|  | 5657 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); | 
|  | 5658 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5659 | } | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5660 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5661 |  | 
| Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5662 | if( rec->data_len == 0 ) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5663 | { | 
|  | 5664 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 5665 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3 | 
| Hanno Becker | 106f3da | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 5666 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5667 | { | 
|  | 5668 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ | 
|  | 5669 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); | 
|  | 5670 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5671 | } | 
|  | 5672 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 5673 |  | 
|  | 5674 | ssl->nb_zero++; | 
|  | 5675 |  | 
|  | 5676 | /* | 
|  | 5677 | * Three or more empty messages may be a DoS attack | 
|  | 5678 | * (excessive CPU consumption). | 
|  | 5679 | */ | 
|  | 5680 | if( ssl->nb_zero > 3 ) | 
|  | 5681 | { | 
|  | 5682 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " | 
| Hanno Becker | 70463db | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 5683 | "messages, possible DoS attack" ) ); | 
|  | 5684 | /* Treat the records as if they were not properly authenticated, | 
|  | 5685 | * thereby failing the connection if we see more than allowed | 
|  | 5686 | * by the configured bad MAC threshold. */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5687 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
|  | 5688 | } | 
|  | 5689 | } | 
|  | 5690 | else | 
|  | 5691 | ssl->nb_zero = 0; | 
|  | 5692 |  | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5693 | /* Only needed for TLS, as with DTLS in_ctr is read from the header */ | 
|  | 5694 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 5695 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5696 | { | 
|  | 5697 | unsigned i; | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5698 | for( i = 8; i > 0; i-- ) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5699 | if( ++ssl->in_ctr[i - 1] != 0 ) | 
|  | 5700 | break; | 
|  | 5701 |  | 
| Manuel Pégourié-Gonnard | 8794a42 | 2019-06-11 10:04:57 +0200 | [diff] [blame] | 5702 | /* The loop goes to its end only if the counter is wrapping around */ | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5703 | if( i == 0 ) | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5704 | { | 
|  | 5705 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); | 
|  | 5706 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
|  | 5707 | } | 
|  | 5708 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5709 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5710 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5711 | } | 
|  | 5712 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5713 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5714 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5715 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5716 | mbedtls_ssl_dtls_replay_update( ssl ); | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5717 | } | 
|  | 5718 | #endif | 
|  | 5719 |  | 
| Hanno Becker | f024285 | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 5720 | /* Check actual (decrypted) record content length against | 
|  | 5721 | * configured maximum. */ | 
|  | 5722 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
|  | 5723 | { | 
|  | 5724 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); | 
|  | 5725 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5726 | } | 
|  | 5727 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5728 | return( 0 ); | 
|  | 5729 | } | 
|  | 5730 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5731 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5732 |  | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5733 | /* | 
|  | 5734 | * Read a record. | 
|  | 5735 | * | 
| Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5736 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, | 
|  | 5737 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. | 
|  | 5738 | * | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5739 | */ | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5740 |  | 
|  | 5741 | /* Helper functions for mbedtls_ssl_read_record(). */ | 
|  | 5742 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5743 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); | 
|  | 5744 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 5745 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5746 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, | 
| Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5747 | unsigned update_hs_digest ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5748 | { | 
|  | 5749 | int ret; | 
|  | 5750 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5751 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5752 |  | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5753 | if( ssl->keep_current_message == 0 ) | 
|  | 5754 | { | 
|  | 5755 | do { | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5756 |  | 
| Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5757 | ret = ssl_consume_current_message( ssl ); | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5758 | if( ret != 0 ) | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5759 | return( ret ); | 
| Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5760 |  | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5761 | if( ssl_record_is_in_progress( ssl ) == 0 ) | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5762 | { | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5763 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5764 | int have_buffered = 0; | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5765 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5766 | /* We only check for buffered messages if the | 
|  | 5767 | * current datagram is fully consumed. */ | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5768 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5769 | ssl_next_record_is_in_datagram( ssl ) == 0 ) | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5770 | { | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5771 | if( ssl_load_buffered_message( ssl ) == 0 ) | 
|  | 5772 | have_buffered = 1; | 
|  | 5773 | } | 
|  | 5774 |  | 
|  | 5775 | if( have_buffered == 0 ) | 
|  | 5776 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5777 | { | 
|  | 5778 | ret = ssl_get_next_record( ssl ); | 
|  | 5779 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) | 
|  | 5780 | continue; | 
|  | 5781 |  | 
|  | 5782 | if( ret != 0 ) | 
|  | 5783 | { | 
| Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5784 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); | 
| Hanno Becker | 42a6b04 | 2019-07-26 07:25:20 +0100 | [diff] [blame] | 5785 | ssl_send_pending_fatal_alert( ssl ); | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5786 | return( ret ); | 
|  | 5787 | } | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5788 | } | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5789 | } | 
|  | 5790 |  | 
|  | 5791 | ret = mbedtls_ssl_handle_message_type( ssl ); | 
|  | 5792 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5793 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5794 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) | 
|  | 5795 | { | 
|  | 5796 | /* Buffer future message */ | 
|  | 5797 | ret = ssl_buffer_message( ssl ); | 
|  | 5798 | if( ret != 0 ) | 
|  | 5799 | return( ret ); | 
|  | 5800 |  | 
|  | 5801 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; | 
|  | 5802 | } | 
|  | 5803 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 5804 |  | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5805 | } while( MBEDTLS_ERR_SSL_NON_FATAL           == ret  || | 
|  | 5806 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5807 |  | 
|  | 5808 | if( 0 != ret ) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5809 | { | 
| Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5810 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5811 | return( ret ); | 
|  | 5812 | } | 
|  | 5813 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5814 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
| Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5815 | update_hs_digest == 1 ) | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5816 | { | 
|  | 5817 | mbedtls_ssl_update_handshake_status( ssl ); | 
|  | 5818 | } | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5819 | } | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5820 | else | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5821 | { | 
| Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5822 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5823 | ssl->keep_current_message = 0; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5824 | } | 
|  | 5825 |  | 
|  | 5826 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); | 
|  | 5827 |  | 
|  | 5828 | return( 0 ); | 
|  | 5829 | } | 
|  | 5830 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5831 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5832 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5833 | { | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5834 | if( ssl->in_left > ssl->next_record_offset ) | 
|  | 5835 | return( 1 ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5836 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5837 | return( 0 ); | 
|  | 5838 | } | 
|  | 5839 |  | 
|  | 5840 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) | 
|  | 5841 | { | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5842 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5843 | mbedtls_ssl_hs_buffer * hs_buf; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5844 | int ret = 0; | 
|  | 5845 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5846 | if( hs == NULL ) | 
|  | 5847 | return( -1 ); | 
|  | 5848 |  | 
| Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5849 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); | 
|  | 5850 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5851 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || | 
|  | 5852 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) | 
|  | 5853 | { | 
|  | 5854 | /* Check if we have seen a ChangeCipherSpec before. | 
|  | 5855 | * If yes, synthesize a CCS record. */ | 
| Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5856 | if( !hs->buffering.seen_ccs ) | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5857 | { | 
|  | 5858 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); | 
|  | 5859 | ret = -1; | 
| Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5860 | goto exit; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5861 | } | 
|  | 5862 |  | 
| Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5863 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5864 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; | 
|  | 5865 | ssl->in_msglen = 1; | 
|  | 5866 | ssl->in_msg[0] = 1; | 
|  | 5867 |  | 
|  | 5868 | /* As long as they are equal, the exact value doesn't matter. */ | 
|  | 5869 | ssl->in_left            = 0; | 
|  | 5870 | ssl->next_record_offset = 0; | 
|  | 5871 |  | 
| Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5872 | hs->buffering.seen_ccs = 0; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5873 | goto exit; | 
|  | 5874 | } | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5875 |  | 
| Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5876 | #if defined(MBEDTLS_DEBUG_C) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5877 | /* Debug only */ | 
|  | 5878 | { | 
|  | 5879 | unsigned offset; | 
|  | 5880 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) | 
|  | 5881 | { | 
|  | 5882 | hs_buf = &hs->buffering.hs[offset]; | 
|  | 5883 | if( hs_buf->is_valid == 1 ) | 
|  | 5884 | { | 
|  | 5885 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", | 
|  | 5886 | hs->in_msg_seq + offset, | 
| Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5887 | hs_buf->is_complete ? "fully" : "partially" ) ); | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5888 | } | 
|  | 5889 | } | 
|  | 5890 | } | 
| Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5891 | #endif /* MBEDTLS_DEBUG_C */ | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5892 |  | 
|  | 5893 | /* Check if we have buffered and/or fully reassembled the | 
|  | 5894 | * next handshake message. */ | 
|  | 5895 | hs_buf = &hs->buffering.hs[0]; | 
|  | 5896 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) | 
|  | 5897 | { | 
|  | 5898 | /* Synthesize a record containing the buffered HS message. */ | 
| Arto Kinnunen | 84eeb4f | 2019-09-10 10:32:30 +0300 | [diff] [blame] | 5899 | size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] ); | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5900 |  | 
|  | 5901 | /* Double-check that we haven't accidentally buffered | 
|  | 5902 | * a message that doesn't fit into the input buffer. */ | 
|  | 5903 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
|  | 5904 | { | 
|  | 5905 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5906 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 5907 | } | 
|  | 5908 |  | 
|  | 5909 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); | 
|  | 5910 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", | 
|  | 5911 | hs_buf->data, msg_len + 12 ); | 
|  | 5912 |  | 
|  | 5913 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 5914 | ssl->in_hslen   = msg_len + 12; | 
|  | 5915 | ssl->in_msglen  = msg_len + 12; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 5916 | mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5917 |  | 
|  | 5918 | ret = 0; | 
|  | 5919 | goto exit; | 
|  | 5920 | } | 
|  | 5921 | else | 
|  | 5922 | { | 
|  | 5923 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", | 
|  | 5924 | hs->in_msg_seq ) ); | 
|  | 5925 | } | 
|  | 5926 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5927 | ret = -1; | 
|  | 5928 |  | 
|  | 5929 | exit: | 
|  | 5930 |  | 
|  | 5931 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); | 
|  | 5932 | return( ret ); | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5933 | } | 
|  | 5934 |  | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5935 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, | 
|  | 5936 | size_t desired ) | 
|  | 5937 | { | 
|  | 5938 | int offset; | 
|  | 5939 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
| Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5940 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", | 
|  | 5941 | (unsigned) desired ) ); | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5942 |  | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5943 | /* Get rid of future records epoch first, if such exist. */ | 
|  | 5944 | ssl_free_buffered_record( ssl ); | 
|  | 5945 |  | 
|  | 5946 | /* Check if we have enough space available now. */ | 
|  | 5947 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - | 
|  | 5948 | hs->buffering.total_bytes_buffered ) ) | 
|  | 5949 | { | 
| Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5950 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) ); | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5951 | return( 0 ); | 
|  | 5952 | } | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5953 |  | 
| Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5954 | /* We don't have enough space to buffer the next expected handshake | 
|  | 5955 | * message. Remove buffers used for future messages to gain space, | 
|  | 5956 | * starting with the most distant one. */ | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5957 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; | 
|  | 5958 | offset >= 0; offset-- ) | 
|  | 5959 | { | 
|  | 5960 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", | 
|  | 5961 | offset ) ); | 
|  | 5962 |  | 
| Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5963 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5964 |  | 
|  | 5965 | /* Check if we have enough space available now. */ | 
|  | 5966 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - | 
|  | 5967 | hs->buffering.total_bytes_buffered ) ) | 
|  | 5968 | { | 
| Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5969 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) ); | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5970 | return( 0 ); | 
|  | 5971 | } | 
|  | 5972 | } | 
|  | 5973 |  | 
|  | 5974 | return( -1 ); | 
|  | 5975 | } | 
|  | 5976 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5977 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) | 
|  | 5978 | { | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5979 | int ret = 0; | 
|  | 5980 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 5981 |  | 
|  | 5982 | if( hs == NULL ) | 
|  | 5983 | return( 0 ); | 
|  | 5984 |  | 
|  | 5985 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); | 
|  | 5986 |  | 
|  | 5987 | switch( ssl->in_msgtype ) | 
|  | 5988 | { | 
|  | 5989 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: | 
|  | 5990 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5991 |  | 
| Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5992 | hs->buffering.seen_ccs = 1; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5993 | break; | 
|  | 5994 |  | 
|  | 5995 | case MBEDTLS_SSL_MSG_HANDSHAKE: | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5996 | { | 
|  | 5997 | unsigned recv_msg_seq_offset; | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 5998 | unsigned recv_msg_seq = (unsigned) | 
|  | 5999 | mbedtls_platform_get_uint16_be( &ssl->in_msg[4] ); | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 6000 |  | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6001 | mbedtls_ssl_hs_buffer *hs_buf; | 
|  | 6002 | size_t msg_len = ssl->in_hslen - 12; | 
|  | 6003 |  | 
|  | 6004 | /* We should never receive an old handshake | 
|  | 6005 | * message - double-check nonetheless. */ | 
|  | 6006 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) | 
|  | 6007 | { | 
|  | 6008 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 6009 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 6010 | } | 
|  | 6011 |  | 
|  | 6012 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; | 
|  | 6013 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) | 
|  | 6014 | { | 
|  | 6015 | /* Silently ignore -- message too far in the future */ | 
|  | 6016 | MBEDTLS_SSL_DEBUG_MSG( 2, | 
|  | 6017 | ( "Ignore future HS message with sequence number %u, " | 
|  | 6018 | "buffering window %u - %u", | 
|  | 6019 | recv_msg_seq, ssl->handshake->in_msg_seq, | 
|  | 6020 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); | 
|  | 6021 |  | 
|  | 6022 | goto exit; | 
|  | 6023 | } | 
|  | 6024 |  | 
|  | 6025 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", | 
|  | 6026 | recv_msg_seq, recv_msg_seq_offset ) ); | 
|  | 6027 |  | 
|  | 6028 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; | 
|  | 6029 |  | 
|  | 6030 | /* Check if the buffering for this seq nr has already commenced. */ | 
| Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 6031 | if( !hs_buf->is_valid ) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6032 | { | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 6033 | size_t reassembly_buf_sz; | 
|  | 6034 |  | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6035 | hs_buf->is_fragmented = | 
|  | 6036 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); | 
|  | 6037 |  | 
|  | 6038 | /* We copy the message back into the input buffer | 
|  | 6039 | * after reassembly, so check that it's not too large. | 
|  | 6040 | * This is an implementation-specific limitation | 
|  | 6041 | * and not one from the standard, hence it is not | 
|  | 6042 | * checked in ssl_check_hs_header(). */ | 
| Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 6043 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6044 | { | 
|  | 6045 | /* Ignore message */ | 
|  | 6046 | goto exit; | 
|  | 6047 | } | 
|  | 6048 |  | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 6049 | /* Check if we have enough space to buffer the message. */ | 
|  | 6050 | if( hs->buffering.total_bytes_buffered > | 
|  | 6051 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) | 
|  | 6052 | { | 
|  | 6053 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 6054 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 6055 | } | 
|  | 6056 |  | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 6057 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, | 
|  | 6058 | hs_buf->is_fragmented ); | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 6059 |  | 
|  | 6060 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - | 
|  | 6061 | hs->buffering.total_bytes_buffered ) ) | 
|  | 6062 | { | 
|  | 6063 | if( recv_msg_seq_offset > 0 ) | 
|  | 6064 | { | 
|  | 6065 | /* If we can't buffer a future message because | 
|  | 6066 | * of space limitations -- ignore. */ | 
|  | 6067 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n", | 
|  | 6068 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, | 
|  | 6069 | (unsigned) hs->buffering.total_bytes_buffered ) ); | 
|  | 6070 | goto exit; | 
|  | 6071 | } | 
| Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 6072 | else | 
|  | 6073 | { | 
|  | 6074 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n", | 
|  | 6075 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, | 
|  | 6076 | (unsigned) hs->buffering.total_bytes_buffered ) ); | 
|  | 6077 | } | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 6078 |  | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 6079 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) | 
| Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 6080 | { | 
| Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 6081 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n", | 
|  | 6082 | (unsigned) msg_len, | 
|  | 6083 | (unsigned) reassembly_buf_sz, | 
|  | 6084 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 6085 | (unsigned) hs->buffering.total_bytes_buffered ) ); | 
| Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 6086 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 
|  | 6087 | goto exit; | 
|  | 6088 | } | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 6089 | } | 
|  | 6090 |  | 
|  | 6091 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", | 
|  | 6092 | msg_len ) ); | 
|  | 6093 |  | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 6094 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); | 
|  | 6095 | if( hs_buf->data == NULL ) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6096 | { | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 6097 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6098 | goto exit; | 
|  | 6099 | } | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 6100 | hs_buf->data_len = reassembly_buf_sz; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6101 |  | 
|  | 6102 | /* Prepare final header: copy msg_type, length and message_seq, | 
|  | 6103 | * then add standardised fragment_offset and fragment_length */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 6104 | mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 ); | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 6105 | mbedtls_platform_memset( hs_buf->data + 6, 0, 3 ); | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 6106 | mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6107 |  | 
|  | 6108 | hs_buf->is_valid = 1; | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 6109 |  | 
|  | 6110 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6111 | } | 
|  | 6112 | else | 
|  | 6113 | { | 
|  | 6114 | /* Make sure msg_type and length are consistent */ | 
| Teppo Järvelin | 0efac53 | 2019-10-04 13:21:08 +0300 | [diff] [blame] | 6115 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) // use regular memcmp as msg type is public | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6116 | { | 
|  | 6117 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); | 
|  | 6118 | /* Ignore */ | 
|  | 6119 | goto exit; | 
|  | 6120 | } | 
|  | 6121 | } | 
|  | 6122 |  | 
| Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 6123 | if( !hs_buf->is_complete ) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6124 | { | 
|  | 6125 | size_t frag_len, frag_off; | 
|  | 6126 | unsigned char * const msg = hs_buf->data + 12; | 
|  | 6127 |  | 
|  | 6128 | /* | 
|  | 6129 | * Check and copy current fragment | 
|  | 6130 | */ | 
|  | 6131 |  | 
|  | 6132 | /* Validation of header fields already done in | 
|  | 6133 | * mbedtls_ssl_prepare_handshake_record(). */ | 
|  | 6134 | frag_off = ssl_get_hs_frag_off( ssl ); | 
|  | 6135 | frag_len = ssl_get_hs_frag_len( ssl ); | 
|  | 6136 |  | 
|  | 6137 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", | 
|  | 6138 | frag_off, frag_len ) ); | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 6139 | mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6140 |  | 
|  | 6141 | if( hs_buf->is_fragmented ) | 
|  | 6142 | { | 
|  | 6143 | unsigned char * const bitmask = msg + msg_len; | 
|  | 6144 | ssl_bitmask_set( bitmask, frag_off, frag_len ); | 
|  | 6145 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, | 
|  | 6146 | msg_len ) == 0 ); | 
|  | 6147 | } | 
|  | 6148 | else | 
|  | 6149 | { | 
|  | 6150 | hs_buf->is_complete = 1; | 
|  | 6151 | } | 
|  | 6152 |  | 
|  | 6153 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", | 
|  | 6154 | hs_buf->is_complete ? "" : "not yet " ) ); | 
|  | 6155 | } | 
|  | 6156 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6157 | break; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 6158 | } | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6159 |  | 
|  | 6160 | default: | 
| Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 6161 | /* We don't buffer other types of messages. */ | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6162 | break; | 
|  | 6163 | } | 
|  | 6164 |  | 
|  | 6165 | exit: | 
|  | 6166 |  | 
|  | 6167 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); | 
|  | 6168 | return( ret ); | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 6169 | } | 
|  | 6170 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6171 |  | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6172 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6173 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6174 | /* | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6175 | * Consume last content-layer message and potentially | 
|  | 6176 | * update in_msglen which keeps track of the contents' | 
|  | 6177 | * consumption state. | 
|  | 6178 | * | 
|  | 6179 | * (1) Handshake messages: | 
|  | 6180 | *     Remove last handshake message, move content | 
|  | 6181 | *     and adapt in_msglen. | 
|  | 6182 | * | 
|  | 6183 | * (2) Alert messages: | 
|  | 6184 | *     Consume whole record content, in_msglen = 0. | 
|  | 6185 | * | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6186 | * (3) Change cipher spec: | 
|  | 6187 | *     Consume whole record content, in_msglen = 0. | 
|  | 6188 | * | 
|  | 6189 | * (4) Application data: | 
|  | 6190 | *     Don't do anything - the record layer provides | 
|  | 6191 | *     the application data as a stream transport | 
|  | 6192 | *     and consumes through mbedtls_ssl_read only. | 
|  | 6193 | * | 
|  | 6194 | */ | 
|  | 6195 |  | 
|  | 6196 | /* Case (1): Handshake messages */ | 
|  | 6197 | if( ssl->in_hslen != 0 ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6198 | { | 
| Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 6199 | /* Hard assertion to be sure that no application data | 
|  | 6200 | * is in flight, as corrupting ssl->in_msglen during | 
|  | 6201 | * ssl->in_offt != NULL is fatal. */ | 
|  | 6202 | if( ssl->in_offt != NULL ) | 
|  | 6203 | { | 
|  | 6204 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 6205 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 6206 | } | 
|  | 6207 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6208 | /* | 
|  | 6209 | * Get next Handshake message in the current record | 
|  | 6210 | */ | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6211 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6212 | /* Notes: | 
| Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 6213 | * (1) in_hslen is not necessarily the size of the | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6214 | *     current handshake content: If DTLS handshake | 
|  | 6215 | *     fragmentation is used, that's the fragment | 
|  | 6216 | *     size instead. Using the total handshake message | 
| Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 6217 | *     size here is faulty and should be changed at | 
|  | 6218 | *     some point. | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6219 | * (2) While it doesn't seem to cause problems, one | 
|  | 6220 | *     has to be very careful not to assume that in_hslen | 
|  | 6221 | *     is always <= in_msglen in a sensible communication. | 
|  | 6222 | *     Again, it's wrong for DTLS handshake fragmentation. | 
|  | 6223 | *     The following check is therefore mandatory, and | 
|  | 6224 | *     should not be treated as a silently corrected assertion. | 
| Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 6225 | *     Additionally, ssl->in_hslen might be arbitrarily out of | 
|  | 6226 | *     bounds after handling a DTLS message with an unexpected | 
|  | 6227 | *     sequence number, see mbedtls_ssl_prepare_handshake_record. | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6228 | */ | 
|  | 6229 | if( ssl->in_hslen < ssl->in_msglen ) | 
|  | 6230 | { | 
|  | 6231 | ssl->in_msglen -= ssl->in_hslen; | 
|  | 6232 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, | 
|  | 6233 | ssl->in_msglen ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6234 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6235 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", | 
|  | 6236 | ssl->in_msg, ssl->in_msglen ); | 
|  | 6237 | } | 
|  | 6238 | else | 
|  | 6239 | { | 
|  | 6240 | ssl->in_msglen = 0; | 
|  | 6241 | } | 
| Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 6242 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6243 | ssl->in_hslen   = 0; | 
|  | 6244 | } | 
|  | 6245 | /* Case (4): Application data */ | 
|  | 6246 | else if( ssl->in_offt != NULL ) | 
|  | 6247 | { | 
|  | 6248 | return( 0 ); | 
|  | 6249 | } | 
|  | 6250 | /* Everything else (CCS & Alerts) */ | 
|  | 6251 | else | 
|  | 6252 | { | 
|  | 6253 | ssl->in_msglen = 0; | 
|  | 6254 | } | 
|  | 6255 |  | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6256 | return( 0 ); | 
|  | 6257 | } | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6258 |  | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6259 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) | 
|  | 6260 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6261 | if( ssl->in_msglen > 0 ) | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6262 | return( 1 ); | 
|  | 6263 |  | 
|  | 6264 | return( 0 ); | 
|  | 6265 | } | 
|  | 6266 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6267 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6268 |  | 
|  | 6269 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) | 
|  | 6270 | { | 
|  | 6271 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 6272 | if( hs == NULL ) | 
|  | 6273 | return; | 
|  | 6274 |  | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6275 | if( hs->buffering.future_record.data != NULL ) | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6276 | { | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6277 | hs->buffering.total_bytes_buffered -= | 
|  | 6278 | hs->buffering.future_record.len; | 
|  | 6279 |  | 
|  | 6280 | mbedtls_free( hs->buffering.future_record.data ); | 
|  | 6281 | hs->buffering.future_record.data = NULL; | 
|  | 6282 | } | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6283 | } | 
|  | 6284 |  | 
|  | 6285 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) | 
|  | 6286 | { | 
|  | 6287 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 6288 | unsigned char * rec; | 
|  | 6289 | size_t rec_len; | 
|  | 6290 | unsigned rec_epoch; | 
|  | 6291 |  | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 6292 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6293 | return( 0 ); | 
|  | 6294 |  | 
|  | 6295 | if( hs == NULL ) | 
|  | 6296 | return( 0 ); | 
|  | 6297 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6298 | rec       = hs->buffering.future_record.data; | 
|  | 6299 | rec_len   = hs->buffering.future_record.len; | 
|  | 6300 | rec_epoch = hs->buffering.future_record.epoch; | 
|  | 6301 |  | 
|  | 6302 | if( rec == NULL ) | 
|  | 6303 | return( 0 ); | 
|  | 6304 |  | 
| Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 6305 | /* Only consider loading future records if the | 
|  | 6306 | * input buffer is empty. */ | 
| Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 6307 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) | 
| Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 6308 | return( 0 ); | 
|  | 6309 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6310 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); | 
|  | 6311 |  | 
|  | 6312 | if( rec_epoch != ssl->in_epoch ) | 
|  | 6313 | { | 
|  | 6314 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); | 
|  | 6315 | goto exit; | 
|  | 6316 | } | 
|  | 6317 |  | 
|  | 6318 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); | 
|  | 6319 |  | 
|  | 6320 | /* Double-check that the record is not too large */ | 
|  | 6321 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - | 
|  | 6322 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) | 
|  | 6323 | { | 
|  | 6324 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 6325 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 6326 | } | 
|  | 6327 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 6328 | mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len ); | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6329 | ssl->in_left = rec_len; | 
|  | 6330 | ssl->next_record_offset = 0; | 
|  | 6331 |  | 
|  | 6332 | ssl_free_buffered_record( ssl ); | 
|  | 6333 |  | 
|  | 6334 | exit: | 
|  | 6335 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); | 
|  | 6336 | return( 0 ); | 
|  | 6337 | } | 
|  | 6338 |  | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6339 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, | 
|  | 6340 | mbedtls_record const *rec ) | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6341 | { | 
|  | 6342 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6343 |  | 
|  | 6344 | /* Don't buffer future records outside handshakes. */ | 
|  | 6345 | if( hs == NULL ) | 
|  | 6346 | return( 0 ); | 
|  | 6347 |  | 
|  | 6348 | /* Only buffer handshake records (we are only interested | 
|  | 6349 | * in Finished messages). */ | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6350 | if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6351 | return( 0 ); | 
|  | 6352 |  | 
|  | 6353 | /* Don't buffer more than one future epoch record. */ | 
|  | 6354 | if( hs->buffering.future_record.data != NULL ) | 
|  | 6355 | return( 0 ); | 
|  | 6356 |  | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6357 | /* Don't buffer record if there's not enough buffering space remaining. */ | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6358 | if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6359 | hs->buffering.total_bytes_buffered ) ) | 
|  | 6360 | { | 
|  | 6361 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n", | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6362 | (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 6363 | (unsigned) hs->buffering.total_bytes_buffered ) ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6364 | return( 0 ); | 
|  | 6365 | } | 
|  | 6366 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6367 | /* Buffer record */ | 
|  | 6368 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", | 
|  | 6369 | ssl->in_epoch + 1 ) ); | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6370 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len ); | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6371 |  | 
|  | 6372 | /* ssl_parse_record_header() only considers records | 
|  | 6373 | * of the next epoch as candidates for buffering. */ | 
|  | 6374 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6375 | hs->buffering.future_record.len   = rec->buf_len; | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6376 |  | 
|  | 6377 | hs->buffering.future_record.data = | 
|  | 6378 | mbedtls_calloc( 1, hs->buffering.future_record.len ); | 
|  | 6379 | if( hs->buffering.future_record.data == NULL ) | 
|  | 6380 | { | 
|  | 6381 | /* If we run out of RAM trying to buffer a | 
|  | 6382 | * record from the next epoch, just ignore. */ | 
|  | 6383 | return( 0 ); | 
|  | 6384 | } | 
|  | 6385 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 6386 | mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len ); | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6387 |  | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6388 | hs->buffering.total_bytes_buffered += rec->buf_len; | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6389 | return( 0 ); | 
|  | 6390 | } | 
|  | 6391 |  | 
|  | 6392 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6393 |  | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 6394 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6395 | { | 
|  | 6396 | int ret; | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6397 | mbedtls_record rec; | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 6398 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6399 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6400 | /* We might have buffered a future record; if so, | 
|  | 6401 | * and if the epoch matches now, load it. | 
|  | 6402 | * On success, this call will set ssl->in_left to | 
|  | 6403 | * the length of the buffered record, so that | 
|  | 6404 | * the calls to ssl_fetch_input() below will | 
|  | 6405 | * essentially be no-ops. */ | 
|  | 6406 | ret = ssl_load_buffered_record( ssl ); | 
|  | 6407 | if( ret != 0 ) | 
|  | 6408 | return( ret ); | 
|  | 6409 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6410 |  | 
| Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 6411 | /* Ensure that we have enough space available for the default form | 
|  | 6412 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, | 
|  | 6413 | * with no space for CIDs counted in). */ | 
|  | 6414 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); | 
|  | 6415 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6416 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6417 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6418 | return( ret ); | 
|  | 6419 | } | 
|  | 6420 |  | 
| Hanno Becker | c6e7c57 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 6421 | ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec ); | 
|  | 6422 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6423 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6424 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6425 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6426 | { | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6427 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) | 
|  | 6428 | { | 
| Hanno Becker | af5bcfc | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 6429 | ret = ssl_buffer_future_record( ssl, &rec ); | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 6430 | if( ret != 0 ) | 
|  | 6431 | return( ret ); | 
|  | 6432 |  | 
|  | 6433 | /* Fall through to handling of unexpected records */ | 
|  | 6434 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; | 
|  | 6435 | } | 
|  | 6436 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6437 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) | 
|  | 6438 | { | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6439 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 6837972 | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 6440 | /* Reset in pointers to default state for TLS/DTLS records, | 
|  | 6441 | * assuming no CID and no offset between record content and | 
|  | 6442 | * record plaintext. */ | 
|  | 6443 | ssl_update_in_pointers( ssl ); | 
|  | 6444 |  | 
| Hanno Becker | 6941245 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6445 | /* Setup internal message pointers from record structure. */ | 
|  | 6446 | ssl->in_msgtype = rec.type; | 
|  | 6447 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 6448 | ssl->in_len = ssl->in_cid + rec.cid_len; | 
|  | 6449 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 6450 | ssl->in_msg = ssl->in_len + 2; | 
| Hanno Becker | 6941245 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 6451 | ssl->in_msglen = rec.data_len; | 
|  | 6452 |  | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6453 | ret = ssl_check_client_reconnect( ssl ); | 
|  | 6454 | if( ret != 0 ) | 
|  | 6455 | return( ret ); | 
|  | 6456 | #endif | 
|  | 6457 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6458 | /* Skip unexpected record (but not whole datagram) */ | 
| Hanno Becker | 2528ee0 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 6459 | ssl->next_record_offset = rec.buf_len; | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6460 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 6461 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " | 
|  | 6462 | "(header)" ) ); | 
|  | 6463 | } | 
|  | 6464 | else | 
|  | 6465 | { | 
|  | 6466 | /* Skip invalid record and the rest of the datagram */ | 
|  | 6467 | ssl->next_record_offset = 0; | 
|  | 6468 | ssl->in_left = 0; | 
|  | 6469 |  | 
|  | 6470 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " | 
|  | 6471 | "(header)" ) ); | 
|  | 6472 | } | 
|  | 6473 |  | 
|  | 6474 | /* Get next record */ | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6475 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6476 | } | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6477 | else | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6478 | #endif | 
| Hanno Becker | 87b5626 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 6479 | { | 
|  | 6480 | return( ret ); | 
|  | 6481 | } | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6482 | } | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6483 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6484 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6485 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6486 | { | 
| Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6487 | /* Remember offset of next record within datagram. */ | 
| Hanno Becker | 2720f4c | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 6488 | ssl->next_record_offset = rec.buf_len; | 
| Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 6489 | if( ssl->next_record_offset < ssl->in_left ) | 
|  | 6490 | { | 
|  | 6491 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); | 
|  | 6492 | } | 
|  | 6493 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6494 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6495 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6496 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 6497 | { | 
| Hanno Becker | e045277 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 6498 | /* | 
|  | 6499 | * Fetch record contents from underlying transport. | 
|  | 6500 | */ | 
| Hanno Becker | 9babbf7 | 2019-07-11 12:50:29 +0100 | [diff] [blame] | 6501 | ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len ); | 
| Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6502 | if( ret != 0 ) | 
|  | 6503 | { | 
|  | 6504 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
|  | 6505 | return( ret ); | 
|  | 6506 | } | 
|  | 6507 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6508 | ssl->in_left = 0; | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6509 | } | 
| Hanno Becker | dc4d627 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 6510 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
|  | 6511 |  | 
|  | 6512 | /* | 
|  | 6513 | * Decrypt record contents. | 
|  | 6514 | */ | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6515 |  | 
| Hanno Becker | a89610a | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 6516 | if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6517 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6518 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6519 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6520 | { | 
|  | 6521 | /* Silently discard invalid records */ | 
| Hanno Becker | 16e9ae2 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 6522 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6523 | { | 
| Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6524 | /* Except when waiting for Finished as a bad mac here | 
|  | 6525 | * probably means something went wrong in the handshake | 
|  | 6526 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ | 
|  | 6527 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || | 
|  | 6528 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) | 
|  | 6529 | { | 
|  | 6530 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) | 
|  | 6531 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) | 
|  | 6532 | { | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6533 | mbedtls_ssl_pend_fatal_alert( ssl, | 
| Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6534 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); | 
|  | 6535 | } | 
|  | 6536 | #endif | 
|  | 6537 | return( ret ); | 
|  | 6538 | } | 
|  | 6539 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6540 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
| Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 6541 | if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 && | 
|  | 6542 | ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) ) | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6543 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6544 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); | 
|  | 6545 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6546 | } | 
|  | 6547 | #endif | 
|  | 6548 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6549 | /* As above, invalid records cause | 
|  | 6550 | * dismissal of the whole datagram. */ | 
|  | 6551 |  | 
|  | 6552 | ssl->next_record_offset = 0; | 
|  | 6553 | ssl->in_left = 0; | 
|  | 6554 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6555 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6556 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6557 | } | 
|  | 6558 |  | 
|  | 6559 | return( ret ); | 
|  | 6560 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6561 | MBEDTLS_SSL_TRANSPORT_ELSE | 
|  | 6562 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6563 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6564 | { | 
|  | 6565 | /* Error out (and send alert) on invalid records */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6566 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) | 
|  | 6567 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6568 | { | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 6569 | mbedtls_ssl_pend_fatal_alert( ssl, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6570 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6571 | } | 
|  | 6572 | #endif | 
|  | 6573 | return( ret ); | 
|  | 6574 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6575 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6576 | } | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6577 |  | 
| Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6578 |  | 
|  | 6579 | /* Reset in pointers to default state for TLS/DTLS records, | 
|  | 6580 | * assuming no CID and no offset between record content and | 
|  | 6581 | * record plaintext. */ | 
|  | 6582 | ssl_update_in_pointers( ssl ); | 
| Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6583 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 6584 | ssl->in_len = ssl->in_cid + rec.cid_len; | 
|  | 6585 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 6586 | ssl->in_msg = ssl->in_len + 2; | 
| Hanno Becker | bd70c8e | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 6587 |  | 
| Hanno Becker | bf256cd | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 6588 | /* The record content type may change during decryption, | 
|  | 6589 | * so re-read it. */ | 
|  | 6590 | ssl->in_msgtype = rec.type; | 
|  | 6591 | /* Also update the input buffer, because unfortunately | 
|  | 6592 | * the server-side ssl_parse_client_hello() reparses the | 
|  | 6593 | * record header when receiving a ClientHello initiating | 
|  | 6594 | * a renegotiation. */ | 
|  | 6595 | ssl->in_hdr[0] = rec.type; | 
|  | 6596 | ssl->in_msg    = rec.buf + rec.data_offset; | 
|  | 6597 | ssl->in_msglen = rec.data_len; | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 6598 | (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len ); | 
| Hanno Becker | bf256cd | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 6599 |  | 
| Manuel Pégourié-Gonnard | ae48d86 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 6600 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 6601 | if( ssl->transform_in != NULL && | 
|  | 6602 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
|  | 6603 | { | 
|  | 6604 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) | 
|  | 6605 | { | 
|  | 6606 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); | 
|  | 6607 | return( ret ); | 
|  | 6608 | } | 
|  | 6609 |  | 
|  | 6610 | /* Check actual (decompress) record content length against | 
|  | 6611 | * configured maximum. */ | 
|  | 6612 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
|  | 6613 | { | 
|  | 6614 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); | 
|  | 6615 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 6616 | } | 
|  | 6617 | } | 
|  | 6618 | #endif /* MBEDTLS_ZLIB_SUPPORT */ | 
|  | 6619 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6620 | return( 0 ); | 
|  | 6621 | } | 
|  | 6622 |  | 
|  | 6623 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) | 
|  | 6624 | { | 
|  | 6625 | int ret; | 
|  | 6626 |  | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6627 | /* | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6628 | * Handle particular types of records | 
|  | 6629 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6630 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6631 | { | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6632 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) | 
|  | 6633 | { | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 6634 | return( ret ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6635 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6636 | } | 
|  | 6637 |  | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6638 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6639 | { | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6640 | if( ssl->in_msglen != 1 ) | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6641 | { | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6642 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", | 
|  | 6643 | ssl->in_msglen ) ); | 
|  | 6644 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6645 | } | 
|  | 6646 |  | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6647 | if( ssl->in_msg[0] != 1 ) | 
|  | 6648 | { | 
|  | 6649 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", | 
|  | 6650 | ssl->in_msg[0] ) ); | 
|  | 6651 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 6652 | } | 
|  | 6653 |  | 
|  | 6654 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 6655 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6656 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC    && | 
|  | 6657 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) | 
|  | 6658 | { | 
|  | 6659 | if( ssl->handshake == NULL ) | 
|  | 6660 | { | 
|  | 6661 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); | 
|  | 6662 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); | 
|  | 6663 | } | 
|  | 6664 |  | 
|  | 6665 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); | 
|  | 6666 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); | 
|  | 6667 | } | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6668 | #endif | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6669 | } | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6670 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6671 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6672 | { | 
| Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 6673 | if( ssl->in_msglen != 2 ) | 
|  | 6674 | { | 
|  | 6675 | /* Note: Standard allows for more than one 2 byte alert | 
|  | 6676 | to be packed in a single message, but Mbed TLS doesn't | 
|  | 6677 | currently support this. */ | 
|  | 6678 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", | 
|  | 6679 | ssl->in_msglen ) ); | 
|  | 6680 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 6681 | } | 
|  | 6682 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6683 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6684 | ssl->in_msg[0], ssl->in_msg[1] ) ); | 
|  | 6685 |  | 
|  | 6686 | /* | 
| Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 6687 | * Ignore non-fatal alerts, except close_notify and no_renegotiation | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6688 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6689 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6690 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6691 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 6692 | ssl->in_msg[1] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6693 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6694 | } | 
|  | 6695 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6696 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && | 
|  | 6697 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6698 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6699 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); | 
|  | 6700 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6701 | } | 
| Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6702 |  | 
|  | 6703 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) | 
|  | 6704 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && | 
|  | 6705 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) | 
|  | 6706 | { | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6707 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) ); | 
| Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6708 | /* Will be handled when trying to parse ServerHello */ | 
|  | 6709 | return( 0 ); | 
|  | 6710 | } | 
|  | 6711 | #endif | 
|  | 6712 |  | 
|  | 6713 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 6714 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 && | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6715 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 6716 | MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6717 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && | 
|  | 6718 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) | 
|  | 6719 | { | 
|  | 6720 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); | 
|  | 6721 | /* Will be handled in mbedtls_ssl_parse_certificate() */ | 
|  | 6722 | return( 0 ); | 
|  | 6723 | } | 
|  | 6724 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ | 
|  | 6725 |  | 
|  | 6726 | /* Silently ignore: fetch new message */ | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6727 | return MBEDTLS_ERR_SSL_NON_FATAL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6728 | } | 
|  | 6729 |  | 
| Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6730 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 6731 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6732 | { | 
| Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6733 | /* Drop unexpected ApplicationData records, | 
|  | 6734 | * except at the beginning of renegotiations */ | 
|  | 6735 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && | 
|  | 6736 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER | 
|  | 6737 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6738 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 6739 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) | 
| Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6740 | #endif | 
| Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6741 | ) | 
|  | 6742 | { | 
|  | 6743 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); | 
|  | 6744 | return( MBEDTLS_ERR_SSL_NON_FATAL ); | 
|  | 6745 | } | 
|  | 6746 |  | 
|  | 6747 | if( ssl->handshake != NULL && | 
|  | 6748 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER  ) | 
|  | 6749 | { | 
|  | 6750 | ssl_handshake_wrapup_free_hs_transform( ssl ); | 
|  | 6751 | } | 
|  | 6752 | } | 
| Hanno Becker | f65ad82 | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 6753 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6754 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6755 | return( 0 ); | 
|  | 6756 | } | 
|  | 6757 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6758 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6759 | { | 
|  | 6760 | int ret; | 
|  | 6761 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6762 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, | 
|  | 6763 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6764 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6765 | { | 
|  | 6766 | return( ret ); | 
|  | 6767 | } | 
|  | 6768 |  | 
|  | 6769 | return( 0 ); | 
|  | 6770 | } | 
|  | 6771 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6772 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, | 
| Hanno Becker | 1facd55 | 2019-07-03 13:57:23 +0100 | [diff] [blame] | 6773 | unsigned char level, | 
|  | 6774 | unsigned char message ) | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6775 | { | 
|  | 6776 | int ret; | 
|  | 6777 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6778 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 6779 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 6780 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6781 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6782 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message )); | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6783 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6784 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6785 | ssl->out_msglen = 2; | 
|  | 6786 | ssl->out_msg[0] = level; | 
|  | 6787 | ssl->out_msg[1] = message; | 
|  | 6788 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 6789 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6790 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6791 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6792 | return( ret ); | 
|  | 6793 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6794 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6795 |  | 
|  | 6796 | return( 0 ); | 
|  | 6797 | } | 
|  | 6798 |  | 
| Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6799 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 6800 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) | 
|  | 6801 | { | 
|  | 6802 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 6803 | if( session->peer_cert != NULL ) | 
|  | 6804 | { | 
|  | 6805 | mbedtls_x509_crt_free( session->peer_cert ); | 
|  | 6806 | mbedtls_free( session->peer_cert ); | 
|  | 6807 | session->peer_cert = NULL; | 
|  | 6808 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6809 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6810 | if( session->peer_cert_digest != NULL ) | 
|  | 6811 | { | 
|  | 6812 | /* Zeroization is not necessary. */ | 
|  | 6813 | mbedtls_free( session->peer_cert_digest ); | 
|  | 6814 | session->peer_cert_digest      = NULL; | 
|  | 6815 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; | 
|  | 6816 | session->peer_cert_digest_len  = 0; | 
|  | 6817 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6818 | #else | 
|  | 6819 | ((void) session); | 
|  | 6820 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ | 
| Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6821 | } | 
|  | 6822 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 6823 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6824 | /* | 
|  | 6825 | * Handshake functions | 
|  | 6826 | */ | 
| Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6827 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6828 | /* No certificate support -> dummy functions */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6829 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6830 | { | 
| Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6831 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = | 
|  | 6832 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6833 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6834 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6835 |  | 
| Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6836 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6837 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6838 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 6839 | if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE ) | 
|  | 6840 | { | 
|  | 6841 | ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE; | 
|  | 6842 | } | 
|  | 6843 | else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE ) | 
|  | 6844 | { | 
|  | 6845 | ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE; | 
|  | 6846 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 6847 | else | 
|  | 6848 | { | 
|  | 6849 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 6850 | } | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6851 | return( 0 ); | 
|  | 6852 | } | 
|  | 6853 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6854 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 6855 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6856 | } | 
|  | 6857 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6858 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6859 | { | 
| Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6860 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = | 
|  | 6861 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6862 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6863 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6864 |  | 
| Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6865 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6866 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6867 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 6868 | if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE ) | 
|  | 6869 | { | 
|  | 6870 | ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE; | 
|  | 6871 | } | 
|  | 6872 | else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE ) | 
|  | 6873 | { | 
|  | 6874 | ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE; | 
|  | 6875 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 6876 | else | 
|  | 6877 | { | 
|  | 6878 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 6879 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6880 | return( 0 ); | 
|  | 6881 | } | 
|  | 6882 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6883 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 6884 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6885 | } | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6886 |  | 
| Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6887 | #else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6888 | /* Some certificate support -> implement write and parse */ | 
|  | 6889 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6890 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6891 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6892 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6893 | size_t i, n; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6894 | const mbedtls_x509_crt *crt; | 
| Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 6895 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = | 
|  | 6896 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6897 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6898 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6899 |  | 
| Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6900 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6901 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6902 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 6903 | if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE ) | 
|  | 6904 | { | 
|  | 6905 | ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE; | 
|  | 6906 | } | 
|  | 6907 | else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE ) | 
|  | 6908 | { | 
|  | 6909 | ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE; | 
|  | 6910 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 6911 | else | 
|  | 6912 | { | 
|  | 6913 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 6914 | } | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6915 | return( 0 ); | 
|  | 6916 | } | 
|  | 6917 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6918 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6919 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 6920 | MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6921 | { | 
|  | 6922 | if( ssl->client_auth == 0 ) | 
|  | 6923 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6924 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 6925 | if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE ) | 
|  | 6926 | { | 
|  | 6927 | ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE; | 
|  | 6928 | } | 
|  | 6929 | else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE ) | 
|  | 6930 | { | 
|  | 6931 | ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE; | 
|  | 6932 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 6933 | else | 
|  | 6934 | { | 
|  | 6935 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 6936 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6937 | return( 0 ); | 
|  | 6938 | } | 
|  | 6939 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6940 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6941 | /* | 
|  | 6942 | * If using SSLv3 and got no cert, send an Alert message | 
|  | 6943 | * (otherwise an empty Certificate message will be sent). | 
|  | 6944 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6945 | if( mbedtls_ssl_own_cert( ssl )  == NULL && | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 6946 | mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6947 | { | 
|  | 6948 | ssl->out_msglen  = 2; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6949 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; | 
|  | 6950 | ssl->out_msg[0]  = MBEDTLS_SSL_ALERT_LEVEL_WARNING; | 
|  | 6951 | ssl->out_msg[1]  = MBEDTLS_SSL_ALERT_MSG_NO_CERT; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6952 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6953 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6954 | goto write_msg; | 
|  | 6955 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6956 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6957 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6958 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 6959 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6960 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6961 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6962 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6963 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6964 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); | 
|  | 6965 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6966 | } | 
|  | 6967 | } | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6968 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6969 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6970 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6971 |  | 
|  | 6972 | /* | 
|  | 6973 | *     0  .  0    handshake type | 
|  | 6974 | *     1  .  3    handshake length | 
|  | 6975 | *     4  .  6    length of all certs | 
|  | 6976 | *     7  .  9    length of cert. 1 | 
|  | 6977 | *    10  . n-1   peer certificate | 
|  | 6978 | *     n  . n+2   length of cert. 2 | 
|  | 6979 | *    n+3 . ...   upper level cert, etc. | 
|  | 6980 | */ | 
|  | 6981 | i = 7; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6982 | crt = mbedtls_ssl_own_cert( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6983 |  | 
| Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6984 | while( crt != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6985 | { | 
|  | 6986 | n = crt->raw.len; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6987 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6988 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6989 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6990 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6991 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6992 | } | 
|  | 6993 |  | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 6994 | (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6995 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 6996 | i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6997 | i += n; crt = crt->next; | 
|  | 6998 | } | 
|  | 6999 |  | 
| Arto Kinnunen | 3d7439e | 2019-09-10 11:30:40 +0300 | [diff] [blame] | 7000 | (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7001 |  | 
|  | 7002 | ssl->out_msglen  = i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7003 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 7004 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7005 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 7006 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7007 | write_msg: | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7008 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7009 |  | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 7010 | if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE ) | 
|  | 7011 | { | 
|  | 7012 | ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE; | 
|  | 7013 | } | 
|  | 7014 | else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE ) | 
|  | 7015 | { | 
|  | 7016 | ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE; | 
|  | 7017 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 7018 | else | 
|  | 7019 | { | 
|  | 7020 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 7021 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7022 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7023 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7024 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7025 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7026 | return( ret ); | 
|  | 7027 | } | 
|  | 7028 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7029 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7030 |  | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 7031 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7032 | } | 
|  | 7033 |  | 
| Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 7034 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 7035 |  | 
|  | 7036 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7037 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, | 
|  | 7038 | unsigned char *crt_buf, | 
|  | 7039 | size_t crt_buf_len ) | 
|  | 7040 | { | 
|  | 7041 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; | 
|  | 7042 |  | 
|  | 7043 | if( peer_crt == NULL ) | 
|  | 7044 | return( -1 ); | 
|  | 7045 |  | 
|  | 7046 | if( peer_crt->raw.len != crt_buf_len ) | 
|  | 7047 | return( -1 ); | 
|  | 7048 |  | 
| Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 7049 | return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) ); | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7050 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7051 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 7052 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, | 
|  | 7053 | unsigned char *crt_buf, | 
|  | 7054 | size_t crt_buf_len ) | 
|  | 7055 | { | 
|  | 7056 | int ret; | 
|  | 7057 | unsigned char const * const peer_cert_digest = | 
|  | 7058 | ssl->session->peer_cert_digest; | 
|  | 7059 | mbedtls_md_type_t const peer_cert_digest_type = | 
|  | 7060 | ssl->session->peer_cert_digest_type; | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 7061 | mbedtls_md_handle_t digest_info = | 
| Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 7062 | mbedtls_md_info_from_type( peer_cert_digest_type ); | 
|  | 7063 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; | 
|  | 7064 | size_t digest_len; | 
|  | 7065 |  | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 7066 | if( peer_cert_digest == NULL || | 
|  | 7067 | digest_info == MBEDTLS_MD_INVALID_HANDLE ) | 
|  | 7068 | { | 
| Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 7069 | return( -1 ); | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 7070 | } | 
| Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 7071 |  | 
|  | 7072 | digest_len = mbedtls_md_get_size( digest_info ); | 
|  | 7073 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) | 
|  | 7074 | return( -1 ); | 
|  | 7075 |  | 
|  | 7076 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); | 
|  | 7077 | if( ret != 0 ) | 
|  | 7078 | return( -1 ); | 
|  | 7079 |  | 
| Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 7080 | return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) ); | 
| Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 7081 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7082 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ | 
| Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 7083 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7084 |  | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7085 | /* | 
|  | 7086 | * Once the certificate message is read, parse it into a cert chain and | 
|  | 7087 | * perform basic checks, but leave actual verification to the caller | 
|  | 7088 | */ | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7089 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, | 
|  | 7090 | mbedtls_x509_crt *chain ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7091 | { | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7092 | int ret; | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7093 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7094 | int crt_cnt=0; | 
|  | 7095 | #endif | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 7096 | size_t i, n; | 
| Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 7097 | uint8_t alert; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7098 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7099 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7100 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7101 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7102 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7103 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7104 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7105 | } | 
|  | 7106 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7107 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || | 
|  | 7108 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7109 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7110 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7111 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7112 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7113 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7114 | } | 
|  | 7115 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7116 | i = mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 7117 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7118 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7119 | * Same message structure as in mbedtls_ssl_write_certificate() | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7120 | */ | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 7121 | n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7122 |  | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 7123 | if( ssl->in_msg[i] != 0 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7124 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7125 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7126 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7127 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7128 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7129 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7130 | } | 
|  | 7131 |  | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7132 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ | 
|  | 7133 | i += 3; | 
|  | 7134 |  | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7135 | /* Iterate through and parse the CRTs in the provided chain. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7136 | while( i < ssl->in_hslen ) | 
|  | 7137 | { | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7138 | /* Check that there's room for the next CRT's length fields. */ | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 7139 | if ( i + 3 > ssl->in_hslen ) { | 
|  | 7140 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7141 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7142 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 7143 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
|  | 7144 | } | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7145 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support | 
|  | 7146 | * anything beyond 2**16 ~ 64K. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7147 | if( ssl->in_msg[i] != 0 ) | 
|  | 7148 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7149 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7150 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7151 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7152 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7153 | } | 
|  | 7154 |  | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7155 | /* Read length of the next CRT in the chain. */ | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 7156 | n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7157 | i += 3; | 
|  | 7158 |  | 
|  | 7159 | if( n < 128 || i + n > ssl->in_hslen ) | 
|  | 7160 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7161 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7162 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7163 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7164 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7165 | } | 
|  | 7166 |  | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7167 | /* Check if we're handling the first CRT in the chain. */ | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7168 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7169 | if( crt_cnt++ == 0 && | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7170 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 7171 | MBEDTLS_SSL_IS_CLIENT && | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7172 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7173 | { | 
| Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 7174 | /* During client-side renegotiation, check that the server's | 
|  | 7175 | * end-CRTs hasn't changed compared to the initial handshake, | 
|  | 7176 | * mitigating the triple handshake attack. On success, reuse | 
|  | 7177 | * the original end-CRT instead of parsing it again. */ | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7178 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); | 
|  | 7179 | if( ssl_check_peer_crt_unchanged( ssl, | 
|  | 7180 | &ssl->in_msg[i], | 
|  | 7181 | n ) != 0 ) | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7182 | { | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7183 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7184 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7185 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7186 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7187 | } | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7188 |  | 
|  | 7189 | /* Now we can safely free the original chain. */ | 
|  | 7190 | ssl_clear_peer_cert( ssl->session ); | 
|  | 7191 | } | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7192 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
|  | 7193 |  | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7194 | /* Parse the next certificate in the chain. */ | 
| Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 7195 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7196 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); | 
| Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 7197 | #else | 
| Hanno Becker | 42de8f8 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 7198 | /* If we don't need to store the CRT chain permanently, parse | 
| Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 7199 | * it in-place from the input buffer instead of making a copy. */ | 
|  | 7200 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); | 
|  | 7201 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7202 | switch( ret ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7203 | { | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7204 | case 0: /*ok*/ | 
|  | 7205 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: | 
|  | 7206 | /* Ignore certificate with an unknown algorithm: maybe a | 
|  | 7207 | prior certificate was already trusted. */ | 
|  | 7208 | break; | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7209 |  | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7210 | case MBEDTLS_ERR_X509_ALLOC_FAILED: | 
|  | 7211 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; | 
|  | 7212 | goto crt_parse_der_failed; | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7213 |  | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7214 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: | 
|  | 7215 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 7216 | goto crt_parse_der_failed; | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7217 |  | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7218 | default: | 
|  | 7219 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 7220 | crt_parse_der_failed: | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7221 | mbedtls_ssl_pend_fatal_alert( ssl,  alert ); | 
| Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 7222 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); | 
|  | 7223 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7224 | } | 
|  | 7225 |  | 
|  | 7226 | i += n; | 
|  | 7227 | } | 
|  | 7228 |  | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7229 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7230 | return( 0 ); | 
|  | 7231 | } | 
|  | 7232 |  | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7233 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 7234 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) | 
|  | 7235 | { | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7236 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7237 | return( -1 ); | 
|  | 7238 |  | 
|  | 7239 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 7240 | /* | 
|  | 7241 | * Check if the client sent an empty certificate | 
|  | 7242 | */ | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 7243 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7244 | { | 
|  | 7245 | if( ssl->in_msglen  == 2                        && | 
|  | 7246 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT            && | 
|  | 7247 | ssl->in_msg[0]  == MBEDTLS_SSL_ALERT_LEVEL_WARNING  && | 
|  | 7248 | ssl->in_msg[1]  == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) | 
|  | 7249 | { | 
|  | 7250 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); | 
|  | 7251 | return( 0 ); | 
|  | 7252 | } | 
|  | 7253 |  | 
|  | 7254 | return( -1 ); | 
|  | 7255 | } | 
|  | 7256 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 7257 |  | 
|  | 7258 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 7259 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 7260 | if( ssl->in_hslen   == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && | 
|  | 7261 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE    && | 
|  | 7262 | ssl->in_msg[0]  == MBEDTLS_SSL_HS_CERTIFICATE   && | 
| Teppo Järvelin | 0efac53 | 2019-10-04 13:21:08 +0300 | [diff] [blame] | 7263 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) // use regular memcmp as comparing public data | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7264 | { | 
|  | 7265 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); | 
|  | 7266 | return( 0 ); | 
|  | 7267 | } | 
|  | 7268 |  | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7269 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 7270 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Hanno Becker | 17daaa5 | 2019-06-18 12:31:45 +0100 | [diff] [blame] | 7271 |  | 
|  | 7272 | return( -1 ); | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7273 | } | 
|  | 7274 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 7275 |  | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7276 | /* Check if a certificate message is expected. | 
|  | 7277 | * Return either | 
|  | 7278 | * - SSL_CERTIFICATE_EXPECTED, or | 
|  | 7279 | * - SSL_CERTIFICATE_SKIP | 
|  | 7280 | * indicating whether a Certificate message is expected or not. | 
|  | 7281 | */ | 
|  | 7282 | #define SSL_CERTIFICATE_EXPECTED 0 | 
| Jarno Lamsa | af60cd7 | 2019-12-19 16:45:23 +0200 | [diff] [blame] | 7283 | #define SSL_CERTIFICATE_SKIP     0xff | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7284 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, | 
|  | 7285 | int authmode ) | 
|  | 7286 | { | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7287 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = | 
| Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 7288 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7289 |  | 
|  | 7290 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) | 
|  | 7291 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 7292 |  | 
|  | 7293 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7294 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7295 | { | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7296 | if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) == | 
|  | 7297 | MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
|  | 7298 | { | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7299 | return( SSL_CERTIFICATE_SKIP ); | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7300 | } | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7301 |  | 
|  | 7302 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
|  | 7303 | { | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7304 | ssl->session_negotiate->verify_result = | 
|  | 7305 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; | 
|  | 7306 | return( SSL_CERTIFICATE_SKIP ); | 
|  | 7307 | } | 
|  | 7308 | } | 
| Hanno Becker | fd5dc8a | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 7309 | #else | 
|  | 7310 | ((void) authmode); | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7311 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 7312 |  | 
|  | 7313 | return( SSL_CERTIFICATE_EXPECTED ); | 
|  | 7314 | } | 
|  | 7315 |  | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7316 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7317 | volatile int authmode, | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7318 | mbedtls_x509_crt *chain, | 
|  | 7319 | void *rs_ctx ) | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7320 | { | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7321 | volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; | 
|  | 7322 | volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 7323 | volatile int flow_counter = 0; | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 7324 | mbedtls_ssl_ciphersuite_handle_t ciphersuite_info = | 
| Hanno Becker | df64596 | 2019-06-26 13:02:22 +0100 | [diff] [blame] | 7325 | mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake ); | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7326 | mbedtls_x509_crt *ca_chain; | 
|  | 7327 | mbedtls_x509_crl *ca_crl; | 
|  | 7328 |  | 
|  | 7329 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 7330 | { | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7331 | return( 0 ); | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 7332 | } | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7333 |  | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 7334 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) ); | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7335 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 7336 | if( ssl->handshake->sni_ca_chain != NULL ) | 
|  | 7337 | { | 
|  | 7338 | ca_chain = ssl->handshake->sni_ca_chain; | 
|  | 7339 | ca_crl   = ssl->handshake->sni_ca_crl; | 
|  | 7340 | } | 
|  | 7341 | else | 
|  | 7342 | #endif | 
|  | 7343 | { | 
|  | 7344 | ca_chain = ssl->conf->ca_chain; | 
|  | 7345 | ca_crl   = ssl->conf->ca_crl; | 
|  | 7346 | } | 
|  | 7347 |  | 
|  | 7348 | /* | 
|  | 7349 | * Main check: verify certificate | 
|  | 7350 | */ | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7351 | verify_ret = mbedtls_x509_crt_verify_restartable( | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7352 | chain, | 
|  | 7353 | ca_chain, ca_crl, | 
|  | 7354 | ssl->conf->cert_profile, | 
| Teppo Järvelin | 4009d8f | 2019-08-19 14:48:09 +0300 | [diff] [blame] | 7355 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7356 | ssl->hostname, | 
| Teppo Järvelin | 4009d8f | 2019-08-19 14:48:09 +0300 | [diff] [blame] | 7357 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */ | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7358 | &ssl->session_negotiate->verify_result, | 
| Hanno Becker | 9ec3fe0 | 2019-07-01 17:36:12 +0100 | [diff] [blame] | 7359 | #if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK) | 
|  | 7360 | ssl->conf->f_vrfy, ssl->conf->p_vrfy, | 
|  | 7361 | #endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */ | 
|  | 7362 | rs_ctx ); | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7363 |  | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7364 | if( verify_ret == 0 ) | 
|  | 7365 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 7366 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7367 | if( verify_ret == 0 ) | 
|  | 7368 | { | 
|  | 7369 | flow_counter++; | 
|  | 7370 | } | 
|  | 7371 | else | 
|  | 7372 | { | 
|  | 7373 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
|  | 7374 | } | 
|  | 7375 | } | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7376 | if( verify_ret != 0 ) | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7377 | { | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7378 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret ); | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7379 | flow_counter++; | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7380 | } | 
|  | 7381 |  | 
|  | 7382 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7383 | if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7384 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); | 
|  | 7385 | #endif | 
|  | 7386 |  | 
|  | 7387 | /* | 
|  | 7388 | * Secondary checks: always done, but change 'ret' only if it was 0 | 
|  | 7389 | */ | 
|  | 7390 |  | 
| Hanno Becker | 7e9c2e0 | 2019-08-21 17:05:20 +0100 | [diff] [blame] | 7391 | #if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT) | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7392 | { | 
| Hanno Becker | 7e9c2e0 | 2019-08-21 17:05:20 +0100 | [diff] [blame] | 7393 | #if defined(MBEDTLS_USE_TINYCRYPT) | 
| Hanno Becker | ee902df | 2019-08-23 13:47:47 +0100 | [diff] [blame] | 7394 | ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 ); | 
| Hanno Becker | 7e9c2e0 | 2019-08-21 17:05:20 +0100 | [diff] [blame] | 7395 | #else /* MBEDTLS_USE_TINYCRYPT */ | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7396 | mbedtls_pk_context *pk; | 
|  | 7397 | ret = mbedtls_x509_crt_pk_acquire( chain, &pk ); | 
|  | 7398 | if( ret != 0 ) | 
| Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 7399 | { | 
|  | 7400 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7401 | return( ret ); | 
| Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 7402 | } | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7403 |  | 
|  | 7404 | /* If certificate uses an EC key, make sure the curve is OK */ | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7405 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) | 
| Hanno Becker | 7e9c2e0 | 2019-08-21 17:05:20 +0100 | [diff] [blame] | 7406 | { | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7407 | ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ); | 
| Hanno Becker | 7e9c2e0 | 2019-08-21 17:05:20 +0100 | [diff] [blame] | 7408 | } | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7409 |  | 
| Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 7410 | mbedtls_x509_crt_pk_release( chain ); | 
| Hanno Becker | 7e9c2e0 | 2019-08-21 17:05:20 +0100 | [diff] [blame] | 7411 | #endif /* MBEDTLS_USE_TINYCRYPT */ | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7412 |  | 
|  | 7413 | if( ret != 0 ) | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7414 | { | 
|  | 7415 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; | 
|  | 7416 |  | 
|  | 7417 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7418 | if( verify_ret == 0 ) | 
|  | 7419 | verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7420 | flow_counter++; | 
|  | 7421 | } | 
|  | 7422 | if( ret == 0 ) | 
|  | 7423 | { | 
|  | 7424 | flow_counter++; | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7425 | } | 
|  | 7426 | } | 
| Hanno Becker | 7e9c2e0 | 2019-08-21 17:05:20 +0100 | [diff] [blame] | 7427 | #endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */ | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7428 |  | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7429 | ret = mbedtls_ssl_check_cert_usage( chain, | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7430 | ciphersuite_info, | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7431 | ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 7432 | MBEDTLS_SSL_IS_CLIENT ), | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7433 | &ssl->session_negotiate->verify_result ); | 
|  | 7434 | if( ret == 0 ) | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7435 | { | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7436 | flow_counter++; | 
|  | 7437 | } | 
|  | 7438 | else | 
|  | 7439 | { | 
|  | 7440 | flow_counter++; | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7441 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7442 | if( verify_ret == 0 ) | 
|  | 7443 | verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7444 | } | 
|  | 7445 |  | 
|  | 7446 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a | 
|  | 7447 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, | 
|  | 7448 | * with details encoded in the verification flags. All other kinds | 
|  | 7449 | * of error codes, including those from the user provided f_vrfy | 
|  | 7450 | * functions, are treated as fatal and lead to a failure of | 
|  | 7451 | * ssl_parse_certificate even if verification was optional. */ | 
|  | 7452 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7453 | ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || | 
|  | 7454 | verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7455 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 7456 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7457 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && | 
|  | 7458 | ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || | 
|  | 7459 | verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) | 
|  | 7460 | { | 
|  | 7461 | verify_ret = 0; | 
|  | 7462 | flow_counter++; | 
|  | 7463 | } | 
|  | 7464 | else | 
|  | 7465 | { | 
|  | 7466 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
|  | 7467 | } | 
|  | 7468 | } else { | 
|  | 7469 | flow_counter++; | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7470 | } | 
|  | 7471 |  | 
|  | 7472 | if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) | 
|  | 7473 | { | 
|  | 7474 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7475 | verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7476 | flow_counter++; | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7477 | } | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 7478 | else | 
|  | 7479 | { | 
|  | 7480 | flow_counter++; | 
|  | 7481 | } | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7482 |  | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7483 | if( verify_ret != 0 ) | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7484 | { | 
|  | 7485 | uint8_t alert; | 
|  | 7486 |  | 
|  | 7487 | /* The certificate may have been rejected for several reasons. | 
|  | 7488 | Pick one and send the corresponding alert. Which alert to send | 
|  | 7489 | may be a subject of debate in some cases. */ | 
|  | 7490 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) | 
|  | 7491 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; | 
|  | 7492 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) | 
|  | 7493 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 7494 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) | 
|  | 7495 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 7496 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) | 
|  | 7497 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 7498 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) | 
|  | 7499 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 7500 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) | 
|  | 7501 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 7502 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) | 
|  | 7503 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 7504 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) | 
|  | 7505 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; | 
|  | 7506 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) | 
|  | 7507 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; | 
|  | 7508 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) | 
|  | 7509 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; | 
|  | 7510 | else | 
|  | 7511 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7512 | mbedtls_ssl_pend_fatal_alert( ssl, alert ); | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7513 | } | 
|  | 7514 |  | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7515 | if( verify_ret == 0 && | 
|  | 7516 | #if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT) | 
|  | 7517 | flow_counter == 5 ) | 
|  | 7518 | #else | 
|  | 7519 | flow_counter == 4 ) | 
|  | 7520 | #endif | 
|  | 7521 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 7522 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7523 | if( verify_ret == 0 && | 
|  | 7524 | #if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT) | 
|  | 7525 | flow_counter == 5 ) | 
|  | 7526 | #else | 
|  | 7527 | flow_counter == 4 ) | 
|  | 7528 | #endif | 
|  | 7529 | { | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 7530 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) ); | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7531 | ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET; | 
|  | 7532 | } | 
|  | 7533 | else | 
|  | 7534 | { | 
|  | 7535 | return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED ); | 
|  | 7536 | } | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 7537 | } else { | 
|  | 7538 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter)); | 
| Jarno Lamsa | ba4730f | 2019-12-19 08:42:03 +0200 | [diff] [blame] | 7539 | } | 
|  | 7540 |  | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7541 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 7542 | if( ssl->session_negotiate->verify_result != 0 ) | 
|  | 7543 | { | 
|  | 7544 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", | 
|  | 7545 | ssl->session_negotiate->verify_result ) ); | 
|  | 7546 | } | 
|  | 7547 | else | 
|  | 7548 | { | 
|  | 7549 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); | 
|  | 7550 | } | 
|  | 7551 | #endif /* MBEDTLS_DEBUG_C */ | 
|  | 7552 |  | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7553 | return( verify_ret ); | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7554 | } | 
|  | 7555 |  | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7556 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7557 |  | 
|  | 7558 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7559 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, | 
|  | 7560 | unsigned char *start, size_t len ) | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7561 | { | 
|  | 7562 | int ret; | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7563 | /* Remember digest of the peer's end-CRT. */ | 
|  | 7564 | ssl->session_negotiate->peer_cert_digest = | 
|  | 7565 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); | 
|  | 7566 | if( ssl->session_negotiate->peer_cert_digest == NULL ) | 
|  | 7567 | { | 
|  | 7568 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
|  | 7569 | sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7570 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7571 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7572 |  | 
|  | 7573 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 7574 | } | 
|  | 7575 |  | 
|  | 7576 | ret = mbedtls_md( mbedtls_md_info_from_type( | 
|  | 7577 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), | 
|  | 7578 | start, len, | 
|  | 7579 | ssl->session_negotiate->peer_cert_digest ); | 
|  | 7580 |  | 
|  | 7581 | ssl->session_negotiate->peer_cert_digest_type = | 
|  | 7582 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; | 
|  | 7583 | ssl->session_negotiate->peer_cert_digest_len = | 
|  | 7584 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; | 
|  | 7585 |  | 
|  | 7586 | return( ret ); | 
|  | 7587 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7588 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7589 |  | 
|  | 7590 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, | 
|  | 7591 | unsigned char *start, size_t len ) | 
|  | 7592 | { | 
|  | 7593 | unsigned char *end = start + len; | 
|  | 7594 | int ret; | 
|  | 7595 |  | 
|  | 7596 | /* Make a copy of the peer's raw public key. */ | 
|  | 7597 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); | 
|  | 7598 | ret = mbedtls_pk_parse_subpubkey( &start, end, | 
|  | 7599 | &ssl->handshake->peer_pubkey ); | 
|  | 7600 | if( ret != 0 ) | 
|  | 7601 | { | 
|  | 7602 | /* We should have parsed the public key before. */ | 
|  | 7603 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 7604 | } | 
|  | 7605 |  | 
| Manuel Pégourié-Gonnard | 2829bbf | 2019-09-19 10:45:14 +0200 | [diff] [blame] | 7606 | ssl->handshake->got_peer_pubkey = 1; | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7607 | return( 0 ); | 
|  | 7608 | } | 
|  | 7609 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7610 |  | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7611 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
|  | 7612 | { | 
|  | 7613 | int ret = 0; | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7614 | int crt_expected; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7615 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 7616 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET | 
|  | 7617 | ? ssl->handshake->sni_authmode | 
| Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 7618 | : mbedtls_ssl_conf_get_authmode( ssl->conf ); | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7619 | #else | 
| Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 7620 | const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf ); | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7621 | #endif | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7622 | void *rs_ctx = NULL; | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7623 | mbedtls_x509_crt *chain = NULL; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7624 |  | 
|  | 7625 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
|  | 7626 |  | 
| Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 7627 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); | 
|  | 7628 | if( crt_expected == SSL_CERTIFICATE_SKIP ) | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7629 | { | 
|  | 7630 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
| Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7631 | goto exit; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7632 | } | 
|  | 7633 |  | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7634 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
|  | 7635 | if( ssl->handshake->ecrs_enabled && | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7636 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7637 | { | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7638 | chain = ssl->handshake->ecrs_peer_cert; | 
|  | 7639 | ssl->handshake->ecrs_peer_cert = NULL; | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7640 | goto crt_verify; | 
|  | 7641 | } | 
|  | 7642 | #endif | 
|  | 7643 |  | 
| Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 7644 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7645 | { | 
|  | 7646 | /* mbedtls_ssl_read_record may have sent an alert already. We | 
|  | 7647 | let it decide whether to alert. */ | 
|  | 7648 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7649 | goto exit; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7650 | } | 
|  | 7651 |  | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7652 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7653 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) | 
|  | 7654 | { | 
|  | 7655 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7656 |  | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7657 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) | 
| Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7658 | ret = 0; | 
|  | 7659 | else | 
|  | 7660 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7661 |  | 
| Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7662 | goto exit; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7663 | } | 
| Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 7664 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 7665 |  | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7666 | /* Clear existing peer CRT structure in case we tried to | 
|  | 7667 | * reuse a session but it failed, and allocate a new one. */ | 
| Hanno Becker | a46c287 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7668 | ssl_clear_peer_cert( ssl->session_negotiate ); | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7669 |  | 
|  | 7670 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | 
|  | 7671 | if( chain == NULL ) | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7672 | { | 
|  | 7673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
|  | 7674 | sizeof( mbedtls_x509_crt ) ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7675 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7676 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Hanno Becker | a46c287 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7677 |  | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7678 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
|  | 7679 | goto exit; | 
|  | 7680 | } | 
|  | 7681 | mbedtls_x509_crt_init( chain ); | 
|  | 7682 |  | 
|  | 7683 | ret = ssl_parse_certificate_chain( ssl, chain ); | 
| Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7684 | if( ret != 0 ) | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7685 | goto exit; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7686 |  | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7687 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
|  | 7688 | if( ssl->handshake->ecrs_enabled) | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7689 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7690 |  | 
|  | 7691 | crt_verify: | 
|  | 7692 | if( ssl->handshake->ecrs_enabled) | 
|  | 7693 | rs_ctx = &ssl->handshake->ecrs_ctx; | 
|  | 7694 | #endif | 
|  | 7695 |  | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7696 | ret = ssl_parse_certificate_verify( ssl, authmode, | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7697 | chain, rs_ctx ); | 
| Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7698 | if( ret != 0 ) | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7699 | goto exit; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7700 |  | 
| Hanno Becker | 3008d28 | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7701 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7702 | { | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7703 | size_t pk_len; | 
|  | 7704 | unsigned char *pk_start; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7705 |  | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7706 | /* We parse the CRT chain without copying, so | 
|  | 7707 | * these pointers point into the input buffer, | 
|  | 7708 | * and are hence still valid after freeing the | 
|  | 7709 | * CRT chain. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7710 |  | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7711 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 7712 | unsigned char *crt_start; | 
|  | 7713 | size_t crt_len; | 
|  | 7714 |  | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7715 | crt_start = chain->raw.p; | 
|  | 7716 | crt_len   = chain->raw.len; | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7717 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7718 |  | 
| Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 7719 | pk_start = chain->cache->pk_raw.p; | 
|  | 7720 | pk_len   = chain->cache->pk_raw.len; | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7721 |  | 
|  | 7722 | /* Free the CRT structures before computing | 
|  | 7723 | * digest and copying the peer's public key. */ | 
|  | 7724 | mbedtls_x509_crt_free( chain ); | 
|  | 7725 | mbedtls_free( chain ); | 
|  | 7726 | chain = NULL; | 
|  | 7727 |  | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7728 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7729 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7730 | if( ret != 0 ) | 
| Hanno Becker | cf291d6 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7731 | goto exit; | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 7732 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7733 |  | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7734 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7735 | if( ret != 0 ) | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7736 | goto exit; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7737 | } | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7738 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 7739 | /* Pass ownership to session structure. */ | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7740 | ssl->session_negotiate->peer_cert = chain; | 
|  | 7741 | chain = NULL; | 
| Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7742 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7743 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7744 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7745 |  | 
| Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7746 | exit: | 
|  | 7747 |  | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7748 | if( ret == 0 ) | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 7749 | { | 
|  | 7750 | if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE ) | 
|  | 7751 | { | 
|  | 7752 | ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE; | 
|  | 7753 | } | 
|  | 7754 | else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE ) | 
|  | 7755 | { | 
|  | 7756 | ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE; | 
|  | 7757 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 7758 | else | 
|  | 7759 | { | 
|  | 7760 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 7761 | } | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 7762 | } | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7763 |  | 
|  | 7764 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
|  | 7765 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) | 
|  | 7766 | { | 
|  | 7767 | ssl->handshake->ecrs_peer_cert = chain; | 
|  | 7768 | chain = NULL; | 
|  | 7769 | } | 
|  | 7770 | #endif | 
|  | 7771 |  | 
|  | 7772 | if( chain != NULL ) | 
|  | 7773 | { | 
|  | 7774 | mbedtls_x509_crt_free( chain ); | 
|  | 7775 | mbedtls_free( chain ); | 
|  | 7776 | } | 
|  | 7777 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7778 | return( ret ); | 
|  | 7779 | } | 
| Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 7780 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7781 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7782 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7783 | { | 
|  | 7784 | int ret; | 
|  | 7785 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7786 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7787 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7788 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7789 | ssl->out_msglen  = 1; | 
|  | 7790 | ssl->out_msg[0]  = 1; | 
|  | 7791 |  | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 7792 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ) | 
|  | 7793 | { | 
|  | 7794 | ssl->state = MBEDTLS_SSL_CLIENT_FINISHED; | 
|  | 7795 | } | 
|  | 7796 | else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) | 
|  | 7797 | { | 
|  | 7798 | ssl->state = MBEDTLS_SSL_SERVER_FINISHED; | 
|  | 7799 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 7800 | else | 
|  | 7801 | { | 
|  | 7802 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 7803 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7804 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7805 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7806 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7807 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7808 | return( ret ); | 
|  | 7809 | } | 
|  | 7810 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7811 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7812 |  | 
|  | 7813 | return( 0 ); | 
|  | 7814 | } | 
|  | 7815 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7816 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7817 | { | 
|  | 7818 | int ret; | 
|  | 7819 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7820 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7821 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7822 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7823 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7824 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7825 | return( ret ); | 
|  | 7826 | } | 
|  | 7827 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7828 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7829 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7830 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7831 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7832 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7833 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7834 | } | 
|  | 7835 |  | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 7836 | /* CCS records are only accepted if they have length 1 and content '1', | 
|  | 7837 | * so we don't need to check this here. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7838 |  | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7839 | /* | 
|  | 7840 | * Switch to our negotiated transform and session parameters for inbound | 
|  | 7841 | * data. | 
|  | 7842 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7843 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) ); | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7844 | ssl->transform_in = ssl->transform_negotiate; | 
|  | 7845 | ssl->session_in = ssl->session_negotiate; | 
|  | 7846 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7847 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7848 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7849 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7850 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7851 | ssl_dtls_replay_reset( ssl ); | 
|  | 7852 | #endif | 
|  | 7853 |  | 
|  | 7854 | /* Increment epoch */ | 
|  | 7855 | if( ++ssl->in_epoch == 0 ) | 
|  | 7856 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7857 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7858 | /* This is highly unlikely to happen for legitimate reasons, so | 
|  | 7859 | treat it as an attack and don't send an alert. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7860 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7861 | } | 
|  | 7862 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7863 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7864 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7865 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 7866 | { | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 7867 | mbedtls_platform_memset( ssl->in_ctr, 0, 8 ); | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7868 | } | 
|  | 7869 | #endif | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7870 |  | 
| Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7871 | ssl_update_in_pointers( ssl ); | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7872 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7873 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 7874 | if( mbedtls_ssl_hw_record_activate != NULL ) | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7875 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7876 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7877 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7878 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 7879 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 7880 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7881 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7882 | } | 
|  | 7883 | } | 
|  | 7884 | #endif | 
|  | 7885 |  | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 7886 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ) | 
|  | 7887 | { | 
|  | 7888 | ssl->state = MBEDTLS_SSL_CLIENT_FINISHED; | 
|  | 7889 | } | 
|  | 7890 | else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) | 
|  | 7891 | { | 
|  | 7892 | ssl->state = MBEDTLS_SSL_SERVER_FINISHED; | 
|  | 7893 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 7894 | else | 
|  | 7895 | { | 
|  | 7896 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 7897 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7898 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7899 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7900 |  | 
|  | 7901 | return( 0 ); | 
|  | 7902 | } | 
|  | 7903 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7904 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7905 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7906 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 7907 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Hanno Becker | 533f5b1 | 2019-08-15 16:56:35 +0100 | [diff] [blame] | 7908 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5  ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7909 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7910 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7911 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 7912 | #if defined(MBEDTLS_SHA256_C) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7913 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7914 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7915 | #if defined(MBEDTLS_SHA512_C) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7916 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7917 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7918 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7919 | } | 
|  | 7920 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7921 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7922 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7923 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7924 |  | 
|  | 7925 | /* | 
|  | 7926 | * Free our handshake params | 
|  | 7927 | */ | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7928 | mbedtls_ssl_handshake_free( ssl ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7929 | mbedtls_free( ssl->handshake ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7930 | ssl->handshake = NULL; | 
|  | 7931 |  | 
|  | 7932 | /* | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7933 | * Free the previous transform and swith in the current one | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7934 | */ | 
|  | 7935 | if( ssl->transform ) | 
|  | 7936 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7937 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 7938 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7939 | } | 
|  | 7940 | ssl->transform = ssl->transform_negotiate; | 
|  | 7941 | ssl->transform_negotiate = NULL; | 
|  | 7942 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7943 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7944 | } | 
|  | 7945 |  | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 7946 | int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7947 | { | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 7948 | volatile int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
| Jarno Lamsa | 489dccd | 2019-12-19 15:11:16 +0200 | [diff] [blame] | 7949 |  | 
|  | 7950 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Jarno Lamsa | 015aa44 | 2019-12-20 12:09:37 +0200 | [diff] [blame] | 7951 | volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET | 
| Jarno Lamsa | 489dccd | 2019-12-19 15:11:16 +0200 | [diff] [blame] | 7952 | ? ssl->handshake->sni_authmode | 
|  | 7953 | : mbedtls_ssl_conf_get_authmode( ssl->conf ); | 
|  | 7954 | #else | 
| Jarno Lamsa | 015aa44 | 2019-12-20 12:09:37 +0200 | [diff] [blame] | 7955 | volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf ); | 
| Jarno Lamsa | 489dccd | 2019-12-19 15:11:16 +0200 | [diff] [blame] | 7956 | #endif | 
| Jarno Lamsa | af60cd7 | 2019-12-19 16:45:23 +0200 | [diff] [blame] | 7957 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 7958 | volatile int crt_expected = SSL_CERTIFICATE_EXPECTED; | 
|  | 7959 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); | 
|  | 7960 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7961 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7962 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7963 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 7964 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7965 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7966 | ssl->renego_status =  MBEDTLS_SSL_RENEGOTIATION_DONE; | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7967 | ssl->renego_records_seen = 0; | 
|  | 7968 | } | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7969 | #endif | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7970 |  | 
|  | 7971 | /* | 
|  | 7972 | * Free the previous session and switch in the current one | 
|  | 7973 | */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7974 | if( ssl->session ) | 
|  | 7975 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7976 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 7977 | /* RFC 7366 3.1: keep the EtM state */ | 
|  | 7978 | ssl->session_negotiate->encrypt_then_mac = | 
|  | 7979 | ssl->session->encrypt_then_mac; | 
|  | 7980 | #endif | 
|  | 7981 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7982 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 7983 | mbedtls_free( ssl->session ); | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7984 | } | 
|  | 7985 | ssl->session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7986 | ssl->session_negotiate = NULL; | 
|  | 7987 |  | 
| Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7988 | #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7989 | /* | 
|  | 7990 | * Add cache entry | 
|  | 7991 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7992 | if( ssl->conf->f_set_cache != NULL && | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 7993 | ssl->session->id_len != 0 && | 
| Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame] | 7994 | ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET ) | 
| Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7995 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7996 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7997 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); | 
| Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7998 | } | 
| Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7999 | #endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8000 |  | 
| Jarno Lamsa | af60cd7 | 2019-12-19 16:45:23 +0200 | [diff] [blame] | 8001 | if( authmode == MBEDTLS_SSL_VERIFY_NONE || | 
|  | 8002 | authmode == MBEDTLS_SSL_VERIFY_OPTIONAL || | 
|  | 8003 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 8004 | crt_expected == SSL_CERTIFICATE_SKIP ) | 
|  | 8005 | #else | 
|  | 8006 | 1 ) | 
|  | 8007 | #endif | 
| Jarno Lamsa | 489dccd | 2019-12-19 15:11:16 +0200 | [diff] [blame] | 8008 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 8009 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | af60cd7 | 2019-12-19 16:45:23 +0200 | [diff] [blame] | 8010 | if( authmode == MBEDTLS_SSL_VERIFY_NONE || | 
|  | 8011 | authmode == MBEDTLS_SSL_VERIFY_OPTIONAL || | 
|  | 8012 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 8013 | crt_expected == SSL_CERTIFICATE_SKIP ) | 
|  | 8014 | #else | 
|  | 8015 | 1 ) | 
|  | 8016 | #endif | 
| Jarno Lamsa | 489dccd | 2019-12-19 15:11:16 +0200 | [diff] [blame] | 8017 | { | 
|  | 8018 | ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET; | 
|  | 8019 | } | 
|  | 8020 | else | 
|  | 8021 | { | 
|  | 8022 | ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; | 
|  | 8023 | goto cleanup; | 
|  | 8024 | } | 
|  | 8025 | } | 
|  | 8026 |  | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8027 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) | 
| Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame] | 8028 | if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET ) | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8029 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 8030 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame] | 8031 | if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET ) | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8032 | { | 
| Jarno Lamsa | 0616405 | 2019-12-19 14:40:36 +0200 | [diff] [blame] | 8033 | /* When doing session resume, no premaster or peer authentication */ | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8034 | ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET; | 
| Jarno Lamsa | 0616405 | 2019-12-19 14:40:36 +0200 | [diff] [blame] | 8035 | ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET; | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8036 | } | 
|  | 8037 | else | 
|  | 8038 | { | 
|  | 8039 | ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; | 
| Jarno Lamsa | 489dccd | 2019-12-19 15:11:16 +0200 | [diff] [blame] | 8040 | goto cleanup; | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8041 | } | 
|  | 8042 | } | 
|  | 8043 | #endif | 
|  | 8044 |  | 
|  | 8045 | if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET ) | 
|  | 8046 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 8047 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8048 | if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET ) | 
|  | 8049 | { | 
|  | 8050 | ret = 0; | 
|  | 8051 | } | 
|  | 8052 | else | 
|  | 8053 | { | 
|  | 8054 | ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; | 
| Jarno Lamsa | 0616405 | 2019-12-19 14:40:36 +0200 | [diff] [blame] | 8055 | goto cleanup; | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8056 | } | 
|  | 8057 | } | 
|  | 8058 | else | 
|  | 8059 | { | 
|  | 8060 | ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED; | 
| Jarno Lamsa | 0616405 | 2019-12-19 14:40:36 +0200 | [diff] [blame] | 8061 | goto cleanup; | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8062 | } | 
|  | 8063 |  | 
| Jarno Lamsa | 0616405 | 2019-12-19 14:40:36 +0200 | [diff] [blame] | 8064 | if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET && | 
|  | 8065 | ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET && | 
|  | 8066 | ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET ) | 
|  | 8067 | { | 
| Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 8068 | mbedtls_platform_random_delay(); | 
| Jarno Lamsa | 0616405 | 2019-12-19 14:40:36 +0200 | [diff] [blame] | 8069 | if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET && | 
|  | 8070 | ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET && | 
|  | 8071 | ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET ) | 
|  | 8072 | { | 
|  | 8073 | ret = 0; | 
|  | 8074 | } | 
|  | 8075 | else | 
|  | 8076 | { | 
|  | 8077 | ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED; | 
|  | 8078 | goto cleanup; | 
|  | 8079 | } | 
|  | 8080 | } | 
|  | 8081 | else | 
|  | 8082 | { | 
|  | 8083 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) ); | 
|  | 8084 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) ); | 
|  | 8085 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) ); | 
|  | 8086 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; | 
|  | 8087 | } | 
|  | 8088 |  | 
|  | 8089 | cleanup: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8090 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8091 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8092 | ssl->handshake->flight != NULL ) | 
|  | 8093 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8094 | /* Cancel handshake timer */ | 
|  | 8095 | ssl_set_timer( ssl, 0 ); | 
|  | 8096 |  | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8097 | /* Keep last flight around in case we need to resend it: | 
|  | 8098 | * we need the handshake and transform structures for that */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8099 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8100 | } | 
|  | 8101 | else | 
|  | 8102 | #endif | 
|  | 8103 | ssl_handshake_wrapup_free_hs_transform( ssl ); | 
|  | 8104 |  | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 8105 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8106 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8107 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); | 
| Jarno Lamsa | e1621d4 | 2019-12-19 08:58:56 +0200 | [diff] [blame] | 8108 | return ret; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8109 | } | 
|  | 8110 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8111 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8112 | { | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8113 | int ret, hash_len; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8114 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8115 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8116 |  | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8117 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); | 
| Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 8118 |  | 
| Hanno Becker | c2fb759 | 2019-08-15 16:31:23 +0100 | [diff] [blame] | 8119 | ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 8120 | mbedtls_ssl_suite_get_mac( | 
|  | 8121 | mbedtls_ssl_ciphersuite_from_id( | 
|  | 8122 | mbedtls_ssl_session_get_ciphersuite( | 
|  | 8123 | ssl->session_negotiate ) ) ), | 
|  | 8124 | ssl, ssl->out_msg + 4, | 
|  | 8125 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8126 |  | 
| Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 8127 | /* | 
|  | 8128 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites | 
|  | 8129 | * may define some other value. Currently (early 2016), no defined | 
|  | 8130 | * ciphersuite does this (and this is unlikely to change as activity has | 
|  | 8131 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. | 
|  | 8132 | */ | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 8133 | hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8134 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8135 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8136 | ssl->verify_data_len = hash_len; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 8137 | mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8138 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8139 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8140 | ssl->out_msglen  = 4 + hash_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8141 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 8142 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_FINISHED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8143 |  | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8144 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8145 | /* | 
|  | 8146 | * In case of session resuming, invert the client and server | 
|  | 8147 | * ChangeCipherSpec messages order. | 
|  | 8148 | */ | 
| Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame] | 8149 | if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8150 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8151 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8152 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 8153 | MBEDTLS_SSL_IS_CLIENT ) | 
|  | 8154 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8155 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8156 | } | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8157 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8158 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8159 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 8160 | MBEDTLS_SSL_IS_SERVER ) | 
|  | 8161 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8162 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8163 | } | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8164 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8165 | } | 
|  | 8166 | else | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8167 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 8168 | { | 
|  | 8169 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ) | 
|  | 8170 | { | 
|  | 8171 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; | 
|  | 8172 | } | 
|  | 8173 | else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) | 
|  | 8174 | { | 
|  | 8175 | ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS; | 
|  | 8176 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 8177 | else | 
|  | 8178 | { | 
|  | 8179 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 8180 | } | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 8181 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8182 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8183 | /* | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8184 | * Switch to our negotiated transform and session parameters for outbound | 
|  | 8185 | * data. | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8186 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8187 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 8188 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8189 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8190 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8191 | { | 
|  | 8192 | unsigned char i; | 
|  | 8193 |  | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8194 | /* Remember current epoch settings for resending */ | 
|  | 8195 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 8196 | mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8197 |  | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8198 | /* Set sequence_number to zero */ | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8199 | memset( ssl->cur_out_ctr + 2, 0, 6 ); | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8200 |  | 
|  | 8201 | /* Increment epoch */ | 
|  | 8202 | for( i = 2; i > 0; i-- ) | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8203 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8204 | break; | 
|  | 8205 |  | 
|  | 8206 | /* The loop goes to its end iff the counter is wrapping */ | 
|  | 8207 | if( i == 0 ) | 
|  | 8208 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8209 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); | 
|  | 8210 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8211 | } | 
|  | 8212 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8213 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8214 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8215 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 8216 | { | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 8217 | mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 ); | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8218 | } | 
|  | 8219 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8220 |  | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8221 | ssl->transform_out = ssl->transform_negotiate; | 
|  | 8222 | ssl->session_out = ssl->session_negotiate; | 
|  | 8223 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8224 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 8225 | if( mbedtls_ssl_hw_record_activate != NULL ) | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8226 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8227 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 ) | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8228 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8229 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); | 
|  | 8230 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 8231 | } | 
|  | 8232 | } | 
|  | 8233 | #endif | 
|  | 8234 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8235 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8236 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8237 | mbedtls_ssl_send_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 8238 | #endif | 
|  | 8239 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8240 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8241 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8242 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8243 | return( ret ); | 
|  | 8244 | } | 
|  | 8245 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 8246 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8247 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 8248 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 8249 | { | 
|  | 8250 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 8251 | return( ret ); | 
|  | 8252 | } | 
|  | 8253 | #endif | 
|  | 8254 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8255 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8256 |  | 
|  | 8257 | return( 0 ); | 
|  | 8258 | } | 
|  | 8259 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8260 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8261 | #define SSL_MAX_HASH_LEN 36 | 
|  | 8262 | #else | 
|  | 8263 | #define SSL_MAX_HASH_LEN 12 | 
|  | 8264 | #endif | 
|  | 8265 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8266 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8267 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 8268 | int ret; | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 8269 | unsigned int hash_len; | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8270 | unsigned char buf[SSL_MAX_HASH_LEN]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8271 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8272 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8273 |  | 
| Hanno Becker | c2fb759 | 2019-08-15 16:31:23 +0100 | [diff] [blame] | 8274 | ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 8275 | mbedtls_ssl_suite_get_mac( | 
|  | 8276 | mbedtls_ssl_ciphersuite_from_id( | 
|  | 8277 | mbedtls_ssl_session_get_ciphersuite( | 
|  | 8278 | ssl->session_negotiate ) ) ), | 
|  | 8279 | ssl, buf, | 
|  | 8280 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8281 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 8282 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8283 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8284 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8285 | return( ret ); | 
|  | 8286 | } | 
|  | 8287 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8288 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8289 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8290 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 8291 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 8292 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8293 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8294 | } | 
|  | 8295 |  | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8296 | /* There is currently no ciphersuite using another length with TLS 1.2 */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8297 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 8298 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 8299 | hash_len = 36; | 
|  | 8300 | else | 
|  | 8301 | #endif | 
|  | 8302 | hash_len = 12; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8303 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8304 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || | 
|  | 8305 | ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8306 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8307 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 8308 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 8309 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8310 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8311 | } | 
|  | 8312 |  | 
| Teppo Järvelin | 707ceb8 | 2019-10-04 07:49:39 +0300 | [diff] [blame] | 8313 | if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), | 
| Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 8314 | buf, hash_len ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8315 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8316 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 8317 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 8318 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8319 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8320 | } | 
|  | 8321 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8322 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8323 | ssl->verify_data_len = hash_len; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 8324 | mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8325 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8326 |  | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8327 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) | 
| Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame] | 8328 | if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8329 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8330 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8331 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8332 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8333 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8334 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8335 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8336 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 8337 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8338 | } | 
|  | 8339 | else | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8340 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 8341 | { | 
|  | 8342 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ) | 
|  | 8343 | { | 
|  | 8344 | ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC; | 
|  | 8345 | } | 
|  | 8346 | else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) | 
|  | 8347 | { | 
|  | 8348 | ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS; | 
|  | 8349 | } | 
| Jarno Lamsa | b018009 | 2019-11-12 15:46:46 +0200 | [diff] [blame] | 8350 | else | 
|  | 8351 | { | 
|  | 8352 | ssl->state = MBEDTLS_SSL_INVALID; | 
|  | 8353 | } | 
| Jarno Lamsa | 2b20516 | 2019-11-12 15:36:21 +0200 | [diff] [blame] | 8354 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8355 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8356 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8357 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8358 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8359 | #endif | 
|  | 8360 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8361 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8362 |  | 
|  | 8363 | return( 0 ); | 
|  | 8364 | } | 
|  | 8365 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8366 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8367 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8368 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8369 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8370 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 8371 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 8372 | mbedtls_md5_init(   &handshake->fin_md5  ); | 
|  | 8373 | mbedtls_sha1_init(   &handshake->fin_sha1 ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8374 | mbedtls_md5_starts_ret( &handshake->fin_md5  ); | 
|  | 8375 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8376 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8377 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 8378 | #if defined(MBEDTLS_SHA256_C) | 
|  | 8379 | mbedtls_sha256_init(   &handshake->fin_sha256    ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8380 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8381 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8382 | #if defined(MBEDTLS_SHA512_C) | 
|  | 8383 | mbedtls_sha512_init(   &handshake->fin_sha512    ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 8384 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8385 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8386 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8387 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 8388 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 8389 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 8390 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); | 
|  | 8391 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8392 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8393 | #if defined(MBEDTLS_DHM_C) | 
|  | 8394 | mbedtls_dhm_init( &handshake->dhm_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8395 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8396 | #if defined(MBEDTLS_ECDH_C) | 
|  | 8397 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8398 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8399 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8400 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 8401 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 8402 | handshake->ecjpake_cache = NULL; | 
|  | 8403 | handshake->ecjpake_cache_len = 0; | 
|  | 8404 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8405 | #endif | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8406 |  | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8407 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 8408 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8409 | #endif | 
|  | 8410 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8411 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 8412 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; | 
|  | 8413 | #endif | 
| Hanno Becker | 3bf8cdf | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 8414 |  | 
|  | 8415 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 8416 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 8417 | mbedtls_pk_init( &handshake->peer_pubkey ); | 
|  | 8418 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8419 | } | 
|  | 8420 |  | 
| Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8421 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8422 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8423 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8424 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8425 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); | 
|  | 8426 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 8427 |  | 
| Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8428 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8429 | mbedtls_md_init( &transform->md_ctx_enc ); | 
|  | 8430 | mbedtls_md_init( &transform->md_ctx_dec ); | 
| Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 8431 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8432 | } | 
|  | 8433 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8434 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8435 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8436 | memset( session, 0, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8437 | } | 
|  | 8438 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8439 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8440 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8441 | /* Clear old handshake information if present */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8442 | if( ssl->transform_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8443 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8444 | if( ssl->session_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8445 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8446 | if( ssl->handshake ) | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8447 | mbedtls_ssl_handshake_free( ssl ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8448 |  | 
|  | 8449 | /* | 
|  | 8450 | * Either the pointers are now NULL or cleared properly and can be freed. | 
|  | 8451 | * Now allocate missing structures. | 
|  | 8452 | */ | 
|  | 8453 | if( ssl->transform_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8454 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8455 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8456 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8457 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8458 | if( ssl->session_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8459 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8460 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8461 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8462 |  | 
| Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 8463 | if( ssl->handshake == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8464 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8465 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8466 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8467 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8468 | /* All pointers should exist and can be directly freed without issue */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8469 | if( ssl->handshake == NULL || | 
|  | 8470 | ssl->transform_negotiate == NULL || | 
|  | 8471 | ssl->session_negotiate == NULL ) | 
|  | 8472 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 8473 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8474 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8475 | mbedtls_free( ssl->handshake ); | 
|  | 8476 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 8477 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8478 |  | 
|  | 8479 | ssl->handshake = NULL; | 
|  | 8480 | ssl->transform_negotiate = NULL; | 
|  | 8481 | ssl->session_negotiate = NULL; | 
|  | 8482 |  | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8483 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8484 | } | 
|  | 8485 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8486 | /* Initialize structures */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8487 | mbedtls_ssl_session_init( ssl->session_negotiate ); | 
| Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8488 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); | 
| Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 8489 | ssl_handshake_params_init( ssl->handshake ); | 
|  | 8490 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8491 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8492 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8493 | { | 
|  | 8494 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8495 |  | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8496 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8497 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | 
|  | 8498 | else | 
|  | 8499 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
|  | 8500 | } | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8501 | #endif | 
|  | 8502 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8503 | return( 0 ); | 
|  | 8504 | } | 
|  | 8505 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8506 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8507 | /* Dummy cookie callbacks for defaults */ | 
|  | 8508 | static int ssl_cookie_write_dummy( void *ctx, | 
|  | 8509 | unsigned char **p, unsigned char *end, | 
|  | 8510 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 8511 | { | 
|  | 8512 | ((void) ctx); | 
|  | 8513 | ((void) p); | 
|  | 8514 | ((void) end); | 
|  | 8515 | ((void) cli_id); | 
|  | 8516 | ((void) cli_id_len); | 
|  | 8517 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8518 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8519 | } | 
|  | 8520 |  | 
|  | 8521 | static int ssl_cookie_check_dummy( void *ctx, | 
|  | 8522 | const unsigned char *cookie, size_t cookie_len, | 
|  | 8523 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 8524 | { | 
|  | 8525 | ((void) ctx); | 
|  | 8526 | ((void) cookie); | 
|  | 8527 | ((void) cookie_len); | 
|  | 8528 | ((void) cli_id); | 
|  | 8529 | ((void) cli_id_len); | 
|  | 8530 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8531 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8532 | } | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8533 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8534 |  | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8535 | /* Once ssl->out_hdr as the address of the beginning of the | 
|  | 8536 | * next outgoing record is set, deduce the other pointers. | 
|  | 8537 | * | 
|  | 8538 | * Note: For TLS, we save the implicit record sequence number | 
|  | 8539 | *       (entering MAC computation) in the 8 bytes before ssl->out_hdr, | 
|  | 8540 | *       and the caller has to make sure there's space for this. | 
|  | 8541 | */ | 
|  | 8542 |  | 
|  | 8543 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, | 
|  | 8544 | mbedtls_ssl_transform *transform ) | 
|  | 8545 | { | 
|  | 8546 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8547 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8548 | { | 
|  | 8549 | ssl->out_ctr = ssl->out_hdr +  3; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8550 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8551 | ssl->out_cid = ssl->out_ctr +  8; | 
|  | 8552 | ssl->out_len = ssl->out_cid; | 
|  | 8553 | if( transform != NULL ) | 
|  | 8554 | ssl->out_len += transform->out_cid_len; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8555 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8556 | ssl->out_len = ssl->out_ctr + 8; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8557 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8558 | ssl->out_iv  = ssl->out_len + 2; | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8559 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8560 | MBEDTLS_SSL_TRANSPORT_ELSE | 
|  | 8561 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 8562 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8563 | { | 
|  | 8564 | ssl->out_ctr = ssl->out_hdr - 8; | 
|  | 8565 | ssl->out_len = ssl->out_hdr + 3; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8566 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8567 | ssl->out_cid = ssl->out_len; | 
|  | 8568 | #endif | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8569 | ssl->out_iv  = ssl->out_hdr + 5; | 
|  | 8570 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8571 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8572 |  | 
|  | 8573 | /* Adjust out_msg to make space for explicit IV, if used. */ | 
|  | 8574 | if( transform != NULL && | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 8575 | mbedtls_ssl_ver_geq( | 
|  | 8576 | mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 8577 | MBEDTLS_SSL_MINOR_VERSION_2 ) ) | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8578 | { | 
|  | 8579 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; | 
|  | 8580 | } | 
|  | 8581 | else | 
|  | 8582 | ssl->out_msg = ssl->out_iv; | 
|  | 8583 | } | 
|  | 8584 |  | 
|  | 8585 | /* Once ssl->in_hdr as the address of the beginning of the | 
|  | 8586 | * next incoming record is set, deduce the other pointers. | 
|  | 8587 | * | 
|  | 8588 | * Note: For TLS, we save the implicit record sequence number | 
|  | 8589 | *       (entering MAC computation) in the 8 bytes before ssl->in_hdr, | 
|  | 8590 | *       and the caller has to make sure there's space for this. | 
|  | 8591 | */ | 
|  | 8592 |  | 
| Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8593 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ) | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8594 | { | 
| Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8595 | /* This function sets the pointers to match the case | 
| Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8596 | * of unprotected TLS/DTLS records, with ssl->in_msg | 
|  | 8597 | * pointing to the beginning of the record content. | 
| Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8598 | * | 
|  | 8599 | * When decrypting a protected record, ssl->in_msg | 
|  | 8600 | * will be shifted to point to the beginning of the | 
|  | 8601 | * record plaintext. | 
|  | 8602 | */ | 
|  | 8603 |  | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8604 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8605 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8606 | { | 
| Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8607 | /* This sets the header pointers to match records | 
|  | 8608 | * without CID. When we receive a record containing | 
|  | 8609 | * a CID, the fields are shifted accordingly in | 
|  | 8610 | * ssl_parse_record_header(). */ | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8611 | ssl->in_ctr = ssl->in_hdr +  3; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8612 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8613 | ssl->in_cid = ssl->in_ctr +  8; | 
|  | 8614 | ssl->in_len = ssl->in_cid; /* Default: no CID */ | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8615 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8616 | ssl->in_len = ssl->in_ctr + 8; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8617 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8618 | ssl->in_msg  = ssl->in_len + 2; | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8619 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8620 | MBEDTLS_SSL_TRANSPORT_ELSE | 
|  | 8621 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 8622 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8623 | { | 
|  | 8624 | ssl->in_ctr = ssl->in_hdr - 8; | 
|  | 8625 | ssl->in_len = ssl->in_hdr + 3; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8626 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8627 | ssl->in_cid = ssl->in_len; | 
|  | 8628 | #endif | 
| Hanno Becker | c360dcc | 2019-07-12 10:00:45 +0100 | [diff] [blame] | 8629 | ssl->in_msg  = ssl->in_hdr + 5; | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8630 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8631 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8632 | } | 
|  | 8633 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8634 | /* | 
|  | 8635 | * Initialize an SSL context | 
|  | 8636 | */ | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 8637 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) | 
|  | 8638 | { | 
|  | 8639 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); | 
|  | 8640 | } | 
|  | 8641 |  | 
|  | 8642 | /* | 
|  | 8643 | * Setup an SSL context | 
|  | 8644 | */ | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8645 |  | 
|  | 8646 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) | 
|  | 8647 | { | 
|  | 8648 | /* Set the incoming and outgoing record pointers. */ | 
|  | 8649 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8650 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8651 | { | 
|  | 8652 | ssl->out_hdr = ssl->out_buf; | 
|  | 8653 | ssl->in_hdr  = ssl->in_buf; | 
|  | 8654 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8655 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8656 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8657 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8658 | { | 
|  | 8659 | ssl->out_hdr = ssl->out_buf + 8; | 
|  | 8660 | ssl->in_hdr  = ssl->in_buf  + 8; | 
|  | 8661 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 8662 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8663 |  | 
|  | 8664 | /* Derive other internal pointers. */ | 
|  | 8665 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); | 
| Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8666 | ssl_update_in_pointers ( ssl ); | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8667 | } | 
|  | 8668 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8669 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 8670 | const mbedtls_ssl_config *conf ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8671 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8672 | int ret; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8673 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8674 | ssl->conf = conf; | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8675 |  | 
| Hanno Becker | ef982d5 | 2019-07-23 15:56:18 +0100 | [diff] [blame] | 8676 | #if defined(MBEDTLS_USE_TINYCRYPT) | 
|  | 8677 | uECC_set_rng( &uecc_rng_wrapper ); | 
|  | 8678 | #endif | 
|  | 8679 |  | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8680 | /* | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 8681 | * Prepare base structures | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8682 | */ | 
| k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 8683 |  | 
|  | 8684 | /* Set to NULL in case of an error condition */ | 
|  | 8685 | ssl->out_buf = NULL; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8686 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8687 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); | 
|  | 8688 | if( ssl->in_buf == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8689 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8690 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) ); | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8691 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8692 | goto error; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8693 | } | 
|  | 8694 |  | 
|  | 8695 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
|  | 8696 | if( ssl->out_buf == NULL ) | 
|  | 8697 | { | 
|  | 8698 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) ); | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8699 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8700 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8701 | } | 
|  | 8702 |  | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8703 | ssl_reset_in_out_pointers( ssl ); | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 8704 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8705 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8706 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8707 |  | 
| Hanno Becker | c8f5299 | 2019-07-25 11:15:08 +0100 | [diff] [blame] | 8708 | ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE; | 
| Hanno Becker | f46e1ce | 2019-07-03 13:56:59 +0100 | [diff] [blame] | 8709 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8710 | return( 0 ); | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8711 |  | 
|  | 8712 | error: | 
|  | 8713 | mbedtls_free( ssl->in_buf ); | 
|  | 8714 | mbedtls_free( ssl->out_buf ); | 
|  | 8715 |  | 
|  | 8716 | ssl->conf = NULL; | 
|  | 8717 |  | 
|  | 8718 | ssl->in_buf = NULL; | 
|  | 8719 | ssl->out_buf = NULL; | 
|  | 8720 |  | 
|  | 8721 | ssl->in_hdr = NULL; | 
|  | 8722 | ssl->in_ctr = NULL; | 
|  | 8723 | ssl->in_len = NULL; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8724 | ssl->in_msg = NULL; | 
|  | 8725 |  | 
|  | 8726 | ssl->out_hdr = NULL; | 
|  | 8727 | ssl->out_ctr = NULL; | 
|  | 8728 | ssl->out_len = NULL; | 
|  | 8729 | ssl->out_iv = NULL; | 
|  | 8730 | ssl->out_msg = NULL; | 
|  | 8731 |  | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8732 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8733 | } | 
|  | 8734 |  | 
|  | 8735 | /* | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8736 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 8737 | * all application-set variables, function pointers and data. | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8738 | * | 
|  | 8739 | * If partial is non-zero, keep data in the input buffer and client ID. | 
|  | 8740 | * (Use when a DTLS client reconnects from the same port.) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8741 | */ | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8742 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8743 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8744 | int ret; | 
|  | 8745 |  | 
| Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 8746 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) ||     \ | 
|  | 8747 | !defined(MBEDTLS_SSL_SRV_C) | 
|  | 8748 | ((void) partial); | 
|  | 8749 | #endif | 
|  | 8750 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8751 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8752 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8753 | /* Cancel any possibly running timer */ | 
|  | 8754 | ssl_set_timer( ssl, 0 ); | 
|  | 8755 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8756 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 8757 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8758 | ssl->renego_records_seen = 0; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8759 |  | 
|  | 8760 | ssl->verify_data_len = 0; | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 8761 | mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); | 
|  | 8762 | mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8763 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8764 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8765 |  | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8766 | ssl->in_offt = NULL; | 
| Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 8767 | ssl_reset_in_out_pointers( ssl ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8768 |  | 
|  | 8769 | ssl->in_msgtype = 0; | 
|  | 8770 | ssl->in_msglen = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8771 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8772 | ssl->next_record_offset = 0; | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 8773 | ssl->in_epoch = 0; | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8774 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8775 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 8776 | ssl_dtls_replay_reset( ssl ); | 
|  | 8777 | #endif | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8778 |  | 
|  | 8779 | ssl->in_hslen = 0; | 
|  | 8780 | ssl->nb_zero = 0; | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 8781 |  | 
|  | 8782 | ssl->keep_current_message = 0; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8783 |  | 
|  | 8784 | ssl->out_msgtype = 0; | 
|  | 8785 | ssl->out_msglen = 0; | 
|  | 8786 | ssl->out_left = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8787 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
|  | 8788 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8789 | ssl->split_done = 0; | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8790 | #endif | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8791 |  | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8792 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | 
|  | 8793 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8794 | ssl->transform_in = NULL; | 
|  | 8795 | ssl->transform_out = NULL; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8796 |  | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8797 | ssl->session_in = NULL; | 
|  | 8798 | ssl->session_out = NULL; | 
|  | 8799 |  | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 8800 | mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8801 |  | 
|  | 8802 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8803 | if( partial == 0 ) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8804 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ | 
|  | 8805 | { | 
|  | 8806 | ssl->in_left = 0; | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 8807 | mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8808 | } | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8809 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8810 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 8811 | if( mbedtls_ssl_hw_record_reset != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8812 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8813 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); | 
|  | 8814 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8815 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8816 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); | 
|  | 8817 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8818 | } | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8819 | } | 
|  | 8820 | #endif | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8821 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8822 | if( ssl->transform ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8823 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8824 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 8825 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8826 | ssl->transform = NULL; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8827 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8828 |  | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8829 | if( ssl->session ) | 
|  | 8830 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8831 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 8832 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8833 | ssl->session = NULL; | 
|  | 8834 | } | 
|  | 8835 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8836 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8837 | ssl->alpn_chosen = NULL; | 
|  | 8838 | #endif | 
|  | 8839 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8840 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8841 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8842 | if( partial == 0 ) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8843 | #endif | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8844 | { | 
|  | 8845 | mbedtls_free( ssl->cli_id ); | 
|  | 8846 | ssl->cli_id = NULL; | 
|  | 8847 | ssl->cli_id_len = 0; | 
|  | 8848 | } | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8849 | #endif | 
|  | 8850 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8851 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 8852 | return( ret ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8853 |  | 
|  | 8854 | return( 0 ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8855 | } | 
|  | 8856 |  | 
| Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 8857 | /* | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8858 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 8859 | * all application-set variables, function pointers and data. | 
|  | 8860 | */ | 
|  | 8861 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) | 
|  | 8862 | { | 
|  | 8863 | return( ssl_session_reset_int( ssl, 0 ) ); | 
|  | 8864 | } | 
|  | 8865 |  | 
|  | 8866 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8867 | * SSL set accessors | 
|  | 8868 | */ | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8869 | #if !defined(MBEDTLS_SSL_CONF_ENDPOINT) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8870 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8871 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8872 | conf->endpoint   = endpoint; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8873 | } | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8874 | #endif /* MBEDTLS_SSL_CONF_ENDPOINT */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8875 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8876 | void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport ) | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8877 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8878 | conf->transport = transport; | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8879 | } | 
|  | 8880 |  | 
| Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8881 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \ | 
|  | 8882 | !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8883 | void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode ) | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8884 | { | 
| Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8885 | conf->anti_replay   = mode; | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8886 | } | 
| Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8887 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */ | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8888 |  | 
| Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 8889 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \ | 
|  | 8890 | !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT) | 
|  | 8891 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, | 
|  | 8892 | unsigned limit ) | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8893 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8894 | conf->badmac_limit = limit; | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8895 | } | 
| Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 8896 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */ | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8897 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8898 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8899 |  | 
| Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 8900 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, | 
|  | 8901 | unsigned allow_packing ) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8902 | { | 
|  | 8903 | ssl->disable_datagram_packing = !allow_packing; | 
|  | 8904 | } | 
|  | 8905 |  | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8906 | #if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) &&      \ | 
|  | 8907 | defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) ) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8908 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, | 
|  | 8909 | uint32_t min, uint32_t max ) | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8910 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8911 | conf->hs_timeout_min = min; | 
|  | 8912 | conf->hs_timeout_max = max; | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8913 | } | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8914 | #else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN && | 
|  | 8915 | MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */ | 
|  | 8916 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, | 
|  | 8917 | uint32_t min, uint32_t max ) | 
|  | 8918 | { | 
|  | 8919 | ((void) conf); | 
|  | 8920 | ((void) min); | 
|  | 8921 | ((void) max); | 
|  | 8922 | } | 
|  | 8923 | #endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN && | 
|  | 8924 | MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */ | 
|  | 8925 |  | 
|  | 8926 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8927 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8928 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8929 | { | 
| Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 8930 | #if !defined(MBEDTLS_SSL_CONF_AUTHMODE) | 
|  | 8931 | conf->authmode = authmode; | 
|  | 8932 | #else | 
|  | 8933 | ((void) conf); | 
|  | 8934 | ((void) authmode); | 
|  | 8935 | #endif /* MBEDTLS_SSL_CONF_AUTHMODE */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8936 | } | 
|  | 8937 |  | 
| Hanno Becker | 9ec3fe0 | 2019-07-01 17:36:12 +0100 | [diff] [blame] | 8938 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 8939 | !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8940 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8941 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8942 | void *p_vrfy ) | 
|  | 8943 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8944 | conf->f_vrfy      = f_vrfy; | 
|  | 8945 | conf->p_vrfy      = p_vrfy; | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8946 | } | 
| Hanno Becker | 9ec3fe0 | 2019-07-01 17:36:12 +0100 | [diff] [blame] | 8947 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */ | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8948 |  | 
| Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 8949 | #if !defined(MBEDTLS_SSL_CONF_RNG) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8950 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 8951 | int (*f_rng)(void *, unsigned char *, size_t), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8952 | void *p_rng ) | 
|  | 8953 | { | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 8954 | conf->f_rng      = f_rng; | 
|  | 8955 | conf->p_rng      = p_rng; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8956 | } | 
| Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame] | 8957 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8958 |  | 
| Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 8959 | #if defined(MBEDTLS_DEBUG_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8960 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 8961 | void (*f_dbg)(void *, int, const char *, int, const char *), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8962 | void  *p_dbg ) | 
|  | 8963 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8964 | conf->f_dbg      = f_dbg; | 
|  | 8965 | conf->p_dbg      = p_dbg; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8966 | } | 
| Hanno Becker | 14a4a44 | 2019-07-02 17:00:34 +0100 | [diff] [blame] | 8967 | #endif /* MBEDTLS_DEBUG_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8968 |  | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8969 | #if !defined(MBEDTLS_SSL_CONF_RECV) && \ | 
|  | 8970 | !defined(MBEDTLS_SSL_CONF_SEND) && \ | 
|  | 8971 | !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8972 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8973 | void *p_bio, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8974 | mbedtls_ssl_send_t *f_send, | 
|  | 8975 | mbedtls_ssl_recv_t *f_recv, | 
|  | 8976 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8977 | { | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8978 | ssl->p_bio = p_bio; | 
|  | 8979 | ssl->f_send = f_send; | 
|  | 8980 | ssl->f_recv = f_recv; | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8981 | ssl->f_recv_timeout = f_recv_timeout; | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8982 | } | 
| Hanno Becker | a58a896 | 2019-06-13 16:11:15 +0100 | [diff] [blame] | 8983 | #else | 
|  | 8984 | void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl, | 
|  | 8985 | void *p_bio ) | 
|  | 8986 | { | 
|  | 8987 | ssl->p_bio = p_bio; | 
|  | 8988 | } | 
|  | 8989 | #endif | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8990 |  | 
| Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 8991 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 8992 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) | 
|  | 8993 | { | 
|  | 8994 | ssl->mtu = mtu; | 
|  | 8995 | } | 
|  | 8996 | #endif | 
|  | 8997 |  | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8998 | #if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8999 | void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout ) | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 9000 | { | 
|  | 9001 | conf->read_timeout   = timeout; | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 9002 | } | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 9003 | #endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */ | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 9004 |  | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 9005 | #if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \ | 
|  | 9006 | !defined(MBEDTLS_SSL_CONF_GET_TIMER) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 9007 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, | 
|  | 9008 | void *p_timer, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 9009 | mbedtls_ssl_set_timer_t *f_set_timer, | 
|  | 9010 | mbedtls_ssl_get_timer_t *f_get_timer ) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 9011 | { | 
|  | 9012 | ssl->p_timer        = p_timer; | 
|  | 9013 | ssl->f_set_timer    = f_set_timer; | 
|  | 9014 | ssl->f_get_timer    = f_get_timer; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 9015 | /* Make sure we start with no timer running */ | 
|  | 9016 | ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 9017 | } | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 9018 | #else | 
|  | 9019 | void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl, | 
|  | 9020 | void *p_timer ) | 
|  | 9021 | { | 
|  | 9022 | ssl->p_timer        = p_timer; | 
|  | 9023 | /* Make sure we start with no timer running */ | 
|  | 9024 | ssl_set_timer( ssl, 0 ); | 
|  | 9025 | } | 
|  | 9026 | #endif | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 9027 |  | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 9028 | #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9029 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 9030 | void *p_cache, | 
|  | 9031 | int (*f_get_cache)(void *, mbedtls_ssl_session *), | 
|  | 9032 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9033 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 9034 | conf->p_cache = p_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9035 | conf->f_get_cache = f_get_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9036 | conf->f_set_cache = f_set_cache; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9037 | } | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 9038 | #endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9039 |  | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 9040 | #if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9041 | int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9042 | { | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 9043 | int ret; | 
|  | 9044 |  | 
|  | 9045 | if( ssl == NULL || | 
|  | 9046 | session == NULL || | 
|  | 9047 | ssl->session_negotiate == NULL || | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9048 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 9049 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9050 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 9051 | } | 
|  | 9052 |  | 
| Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9053 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, | 
|  | 9054 | session ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 9055 | return( ret ); | 
|  | 9056 |  | 
| Jarno Lamsa | 8d09e57 | 2019-12-19 15:20:19 +0200 | [diff] [blame] | 9057 | ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 9058 |  | 
|  | 9059 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9060 | } | 
| Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 9061 | #endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9062 |  | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 9063 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9064 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9065 | const int *ciphersuites ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9066 | { | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 9067 | conf->ciphersuite_list[0] = ciphersuites; | 
|  | 9068 | conf->ciphersuite_list[1] = ciphersuites; | 
|  | 9069 | conf->ciphersuite_list[2] = ciphersuites; | 
|  | 9070 | conf->ciphersuite_list[3] = ciphersuites; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 9071 | } | 
|  | 9072 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9073 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 9074 | const int *ciphersuites, | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 9075 | int major, int minor ) | 
|  | 9076 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9077 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 9078 | return; | 
|  | 9079 |  | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 9080 | if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) || | 
|  | 9081 | mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) ) | 
|  | 9082 | { | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 9083 | return; | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 9084 | } | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 9085 |  | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 9086 | conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] = | 
|  | 9087 | ciphersuites; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9088 | } | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 9089 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9090 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9091 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 9092 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, | 
| Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 9093 | const mbedtls_x509_crt_profile *profile ) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 9094 | { | 
|  | 9095 | conf->cert_profile = profile; | 
|  | 9096 | } | 
|  | 9097 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 9098 | /* Append a new keycert entry to a (possibly empty) list */ | 
|  | 9099 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, | 
|  | 9100 | mbedtls_x509_crt *cert, | 
|  | 9101 | mbedtls_pk_context *key ) | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 9102 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 9103 | mbedtls_ssl_key_cert *new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 9104 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 9105 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); | 
|  | 9106 | if( new_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9107 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 9108 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 9109 | new_cert->cert = cert; | 
|  | 9110 | new_cert->key  = key; | 
|  | 9111 | new_cert->next = NULL; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 9112 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 9113 | /* Update head is the list was null, else add to the end */ | 
|  | 9114 | if( *head == NULL ) | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 9115 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 9116 | *head = new_cert; | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 9117 | } | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 9118 | else | 
|  | 9119 | { | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 9120 | mbedtls_ssl_key_cert *cur = *head; | 
|  | 9121 | while( cur->next != NULL ) | 
|  | 9122 | cur = cur->next; | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 9123 | cur->next = new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 9124 | } | 
|  | 9125 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 9126 | return( 0 ); | 
|  | 9127 | } | 
|  | 9128 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9129 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 9130 | mbedtls_x509_crt *own_cert, | 
|  | 9131 | mbedtls_pk_context *pk_key ) | 
|  | 9132 | { | 
| Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 9133 | return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) ); | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 9134 | } | 
|  | 9135 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9136 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9137 | mbedtls_x509_crt *ca_chain, | 
|  | 9138 | mbedtls_x509_crl *ca_crl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9139 | { | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9140 | conf->ca_chain   = ca_chain; | 
|  | 9141 | conf->ca_crl     = ca_crl; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9142 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9143 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 9144 |  | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 9145 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 9146 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, | 
|  | 9147 | mbedtls_x509_crt *own_cert, | 
|  | 9148 | mbedtls_pk_context *pk_key ) | 
|  | 9149 | { | 
|  | 9150 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, | 
|  | 9151 | own_cert, pk_key ) ); | 
|  | 9152 | } | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 9153 |  | 
|  | 9154 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, | 
|  | 9155 | mbedtls_x509_crt *ca_chain, | 
|  | 9156 | mbedtls_x509_crl *ca_crl ) | 
|  | 9157 | { | 
|  | 9158 | ssl->handshake->sni_ca_chain   = ca_chain; | 
|  | 9159 | ssl->handshake->sni_ca_crl     = ca_crl; | 
|  | 9160 | } | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 9161 |  | 
|  | 9162 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, | 
|  | 9163 | int authmode ) | 
|  | 9164 | { | 
|  | 9165 | ssl->handshake->sni_authmode = authmode; | 
|  | 9166 | } | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 9167 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
|  | 9168 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 9169 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9170 | /* | 
|  | 9171 | * Set EC J-PAKE password for current handshake | 
|  | 9172 | */ | 
|  | 9173 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, | 
|  | 9174 | const unsigned char *pw, | 
|  | 9175 | size_t pw_len ) | 
|  | 9176 | { | 
|  | 9177 | mbedtls_ecjpake_role role; | 
|  | 9178 |  | 
| Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 9179 | if( ssl->handshake == NULL || ssl->conf == NULL ) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9180 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 9181 |  | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9182 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9183 | role = MBEDTLS_ECJPAKE_SERVER; | 
|  | 9184 | else | 
|  | 9185 | role = MBEDTLS_ECJPAKE_CLIENT; | 
|  | 9186 |  | 
|  | 9187 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, | 
|  | 9188 | role, | 
|  | 9189 | MBEDTLS_MD_SHA256, | 
|  | 9190 | MBEDTLS_ECP_DP_SECP256R1, | 
|  | 9191 | pw, pw_len ) ); | 
|  | 9192 | } | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 9193 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 9194 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9195 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9196 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9197 | const unsigned char *psk, size_t psk_len, | 
|  | 9198 | const unsigned char *psk_identity, size_t psk_identity_len ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 9199 | { | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9200 | if( psk == NULL || psk_identity == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9201 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9202 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9203 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 9204 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 9205 |  | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 9206 | /* Identity len will be encoded on two bytes */ | 
|  | 9207 | if( ( psk_identity_len >> 16 ) != 0 || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9208 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 9209 | { | 
|  | 9210 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 9211 | } | 
|  | 9212 |  | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 9213 | if( conf->psk != NULL ) | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9214 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9215 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 9216 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9217 | mbedtls_free( conf->psk ); | 
| Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 9218 | conf->psk = NULL; | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 9219 | conf->psk_len = 0; | 
|  | 9220 | } | 
|  | 9221 | if( conf->psk_identity != NULL ) | 
|  | 9222 | { | 
|  | 9223 | mbedtls_free( conf->psk_identity ); | 
| Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 9224 | conf->psk_identity = NULL; | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 9225 | conf->psk_identity_len = 0; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9226 | } | 
|  | 9227 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 9228 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL || | 
|  | 9229 | ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL ) | 
| Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 9230 | { | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9231 | mbedtls_free( conf->psk ); | 
| Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 9232 | mbedtls_free( conf->psk_identity ); | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9233 | conf->psk = NULL; | 
| Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 9234 | conf->psk_identity = NULL; | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9235 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 9236 | } | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9237 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 9238 | conf->psk_len = psk_len; | 
|  | 9239 | conf->psk_identity_len = psk_identity_len; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9240 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 9241 | mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len ); | 
|  | 9242 | mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9243 |  | 
|  | 9244 | return( 0 ); | 
|  | 9245 | } | 
|  | 9246 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9247 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, | 
|  | 9248 | const unsigned char *psk, size_t psk_len ) | 
|  | 9249 | { | 
|  | 9250 | if( psk == NULL || ssl->handshake == NULL ) | 
|  | 9251 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 9252 |  | 
|  | 9253 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 9254 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 9255 |  | 
|  | 9256 | if( ssl->handshake->psk != NULL ) | 
| Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 9257 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9258 | mbedtls_platform_zeroize( ssl->handshake->psk, | 
|  | 9259 | ssl->handshake->psk_len ); | 
| Simon Butcher | 5b8d1d6 | 2015-10-04 22:06:51 +0100 | [diff] [blame] | 9260 | mbedtls_free( ssl->handshake->psk ); | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 9261 | ssl->handshake->psk_len = 0; | 
| Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 9262 | } | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9263 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 9264 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 9265 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9266 |  | 
|  | 9267 | ssl->handshake->psk_len = psk_len; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 9268 | mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 9269 |  | 
|  | 9270 | return( 0 ); | 
|  | 9271 | } | 
|  | 9272 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9273 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9274 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 9275 | size_t), | 
|  | 9276 | void *p_psk ) | 
|  | 9277 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9278 | conf->f_psk = f_psk; | 
|  | 9279 | conf->p_psk = p_psk; | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 9280 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9281 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ | 
| Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 9282 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 9283 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 9284 |  | 
|  | 9285 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9286 | int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9287 | { | 
|  | 9288 | int ret; | 
|  | 9289 |  | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9290 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || | 
|  | 9291 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) | 
|  | 9292 | { | 
|  | 9293 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 9294 | mbedtls_mpi_free( &conf->dhm_G ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9295 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9296 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9297 |  | 
|  | 9298 | return( 0 ); | 
|  | 9299 | } | 
| Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 9300 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9301 |  | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 9302 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, | 
|  | 9303 | const unsigned char *dhm_P, size_t P_len, | 
|  | 9304 | const unsigned char *dhm_G, size_t G_len ) | 
|  | 9305 | { | 
|  | 9306 | int ret; | 
|  | 9307 |  | 
|  | 9308 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || | 
|  | 9309 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) | 
|  | 9310 | { | 
|  | 9311 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 9312 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 9313 | return( ret ); | 
|  | 9314 | } | 
|  | 9315 |  | 
|  | 9316 | return( 0 ); | 
|  | 9317 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9318 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9319 | int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx ) | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9320 | { | 
|  | 9321 | int ret; | 
|  | 9322 |  | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9323 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || | 
|  | 9324 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) | 
|  | 9325 | { | 
|  | 9326 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 9327 | mbedtls_mpi_free( &conf->dhm_G ); | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9328 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 9329 | } | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9330 |  | 
|  | 9331 | return( 0 ); | 
|  | 9332 | } | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 9333 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 9334 |  | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 9335 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 9336 | /* | 
|  | 9337 | * Set the minimum length for Diffie-Hellman parameters | 
|  | 9338 | */ | 
|  | 9339 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, | 
|  | 9340 | unsigned int bitlen ) | 
|  | 9341 | { | 
|  | 9342 | conf->dhm_min_bitlen = bitlen; | 
|  | 9343 | } | 
|  | 9344 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ | 
|  | 9345 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9346 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9347 | /* | 
|  | 9348 | * Set allowed/preferred hashes for handshake signatures | 
|  | 9349 | */ | 
|  | 9350 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, | 
|  | 9351 | const int *hashes ) | 
|  | 9352 | { | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 9353 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9354 | conf->sig_hashes = hashes; | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 9355 | #else | 
|  | 9356 | ((void) conf); | 
|  | 9357 | ((void) hashes); | 
|  | 9358 | #endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */ | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9359 | } | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9360 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 9361 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 9362 | #if defined(MBEDTLS_ECP_C) | 
| Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 9363 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9364 | /* | 
|  | 9365 | * Set the allowed elliptic curves | 
|  | 9366 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9367 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9368 | const mbedtls_ecp_group_id *curve_list ) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9369 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9370 | conf->curve_list = curve_list; | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9371 | } | 
| Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 9372 | #endif /* MBEDTLS_SSL_CONF_SINGLE_EC */ | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9373 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 9374 |  | 
| Teppo Järvelin | 4009d8f | 2019-08-19 14:48:09 +0300 | [diff] [blame] | 9375 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9376 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9377 | { | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9378 | /* Initialize to suppress unnecessary compiler warning */ | 
|  | 9379 | size_t hostname_len = 0; | 
|  | 9380 |  | 
|  | 9381 | /* Check if new hostname is valid before | 
|  | 9382 | * making any change to current one */ | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9383 | if( hostname != NULL ) | 
|  | 9384 | { | 
|  | 9385 | hostname_len = strlen( hostname ); | 
|  | 9386 |  | 
|  | 9387 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) | 
|  | 9388 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 9389 | } | 
|  | 9390 |  | 
|  | 9391 | /* Now it's clear that we will overwrite the old hostname, | 
|  | 9392 | * so we can free it safely */ | 
|  | 9393 |  | 
|  | 9394 | if( ssl->hostname != NULL ) | 
|  | 9395 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9396 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9397 | mbedtls_free( ssl->hostname ); | 
|  | 9398 | } | 
|  | 9399 |  | 
|  | 9400 | /* Passing NULL as hostname shall clear the old one */ | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 9401 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9402 | if( hostname == NULL ) | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9403 | { | 
|  | 9404 | ssl->hostname = NULL; | 
|  | 9405 | } | 
|  | 9406 | else | 
|  | 9407 | { | 
|  | 9408 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9409 | if( ssl->hostname == NULL ) | 
|  | 9410 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9411 |  | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 9412 | /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */ | 
|  | 9413 | memcpy( ssl->hostname, hostname, hostname_len ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 9414 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 9415 | ssl->hostname[hostname_len] = '\0'; | 
|  | 9416 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9417 |  | 
|  | 9418 | return( 0 ); | 
|  | 9419 | } | 
| Teppo Järvelin | 4009d8f | 2019-08-19 14:48:09 +0300 | [diff] [blame] | 9420 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9421 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9422 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9423 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9424 | int (*f_sni)(void *, mbedtls_ssl_context *, | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9425 | const unsigned char *, size_t), | 
|  | 9426 | void *p_sni ) | 
|  | 9427 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9428 | conf->f_sni = f_sni; | 
|  | 9429 | conf->p_sni = p_sni; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9430 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9431 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9432 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9433 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9434 | int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos ) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9435 | { | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9436 | size_t cur_len, tot_len; | 
|  | 9437 | const char **p; | 
|  | 9438 |  | 
|  | 9439 | /* | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9440 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings | 
|  | 9441 | * MUST NOT be truncated." | 
|  | 9442 | * We check lengths now rather than later. | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9443 | */ | 
|  | 9444 | tot_len = 0; | 
|  | 9445 | for( p = protos; *p != NULL; p++ ) | 
|  | 9446 | { | 
|  | 9447 | cur_len = strlen( *p ); | 
|  | 9448 | tot_len += cur_len; | 
|  | 9449 |  | 
|  | 9450 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9451 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9452 | } | 
|  | 9453 |  | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9454 | conf->alpn_list = protos; | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9455 |  | 
|  | 9456 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9457 | } | 
|  | 9458 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9459 | const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9460 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9461 | return( ssl->alpn_chosen ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9462 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9463 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9464 |  | 
| Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9465 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \ | 
|  | 9466 | !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) | 
|  | 9467 | void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, | 
|  | 9468 | int major, int minor ) | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9469 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9470 | conf->max_major_ver = major; | 
|  | 9471 | conf->max_minor_ver = minor; | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9472 | } | 
| Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9473 | #endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER || | 
|  | 9474 | MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9475 |  | 
| Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9476 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \ | 
|  | 9477 | !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) | 
|  | 9478 | void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, | 
|  | 9479 | int major, int minor ) | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9480 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9481 | conf->min_major_ver = major; | 
|  | 9482 | conf->min_minor_ver = minor; | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9483 | } | 
| Hanno Becker | 33b9b25 | 2019-07-05 11:23:25 +0100 | [diff] [blame] | 9484 | #endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER || | 
|  | 9485 | MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9486 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9487 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9488 | void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback ) | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9489 | { | 
| Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 9490 | conf->fallback = fallback; | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9491 | } | 
|  | 9492 | #endif | 
|  | 9493 |  | 
| Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 9494 | #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST) | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 9495 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, | 
|  | 9496 | char cert_req_ca_list ) | 
|  | 9497 | { | 
|  | 9498 | conf->cert_req_ca_list = cert_req_ca_list; | 
|  | 9499 | } | 
|  | 9500 | #endif | 
|  | 9501 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9502 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9503 | void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm ) | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9504 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9505 | conf->encrypt_then_mac = etm; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9506 | } | 
|  | 9507 | #endif | 
|  | 9508 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9509 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9510 | #if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9511 | void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems ) | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9512 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9513 | conf->extended_ms = ems; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9514 | } | 
| Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9515 | #endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */ | 
|  | 9516 | #if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET) | 
| Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 9517 | void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf, | 
| Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 9518 | char ems_enf ) | 
| Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 9519 | { | 
|  | 9520 | conf->enforce_extended_master_secret = ems_enf; | 
|  | 9521 | } | 
| Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 9522 | #endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ | 
| Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 9523 | #endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9524 |  | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9525 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9526 | void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 ) | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9527 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9528 | conf->arc4_disabled = arc4; | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9529 | } | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9530 | #endif | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9531 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9532 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9533 | int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code ) | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9534 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9535 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9536 | ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9537 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9538 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9539 | } | 
|  | 9540 |  | 
| Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 9541 | conf->mfl_code = mfl_code; | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9542 |  | 
|  | 9543 | return( 0 ); | 
|  | 9544 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9545 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9546 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9547 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9548 | void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate ) | 
| Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9549 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9550 | conf->trunc_hmac = truncate; | 
| Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9551 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9552 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9553 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9554 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9555 | void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split ) | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9556 | { | 
| Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9557 | conf->cbc_record_splitting = split; | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9558 | } | 
|  | 9559 | #endif | 
|  | 9560 |  | 
| Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 9561 | #if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9562 | void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9563 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9564 | conf->allow_legacy_renegotiation = allow_legacy; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9565 | } | 
| Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 9566 | #endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9567 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9568 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9569 | void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation ) | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9570 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9571 | conf->disable_renegotiation = renegotiation; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9572 | } | 
|  | 9573 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9574 | void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records ) | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9575 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9576 | conf->renego_max_records = max_records; | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9577 | } | 
|  | 9578 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9579 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9580 | const unsigned char period[8] ) | 
|  | 9581 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 9582 | mbedtls_platform_memcpy( conf->renego_period, period, 8 ); | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9583 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9584 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9585 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9586 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9587 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 9588 | void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets ) | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9589 | { | 
| Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 9590 | conf->session_tickets = use_tickets; | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9591 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9592 | #endif | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9593 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9594 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9595 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, | 
|  | 9596 | mbedtls_ssl_ticket_write_t *f_ticket_write, | 
|  | 9597 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, | 
|  | 9598 | void *p_ticket ) | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9599 | { | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9600 | conf->f_ticket_write = f_ticket_write; | 
|  | 9601 | conf->f_ticket_parse = f_ticket_parse; | 
|  | 9602 | conf->p_ticket       = p_ticket; | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9603 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9604 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9605 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9606 |  | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9607 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) | 
|  | 9608 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, | 
|  | 9609 | mbedtls_ssl_export_keys_t *f_export_keys, | 
|  | 9610 | void *p_export_keys ) | 
|  | 9611 | { | 
|  | 9612 | conf->f_export_keys = f_export_keys; | 
|  | 9613 | conf->p_export_keys = p_export_keys; | 
|  | 9614 | } | 
|  | 9615 | #endif | 
|  | 9616 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9617 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9618 | void mbedtls_ssl_conf_async_private_cb( | 
|  | 9619 | mbedtls_ssl_config *conf, | 
|  | 9620 | mbedtls_ssl_async_sign_t *f_async_sign, | 
|  | 9621 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, | 
|  | 9622 | mbedtls_ssl_async_resume_t *f_async_resume, | 
|  | 9623 | mbedtls_ssl_async_cancel_t *f_async_cancel, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9624 | void *async_config_data ) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9625 | { | 
|  | 9626 | conf->f_async_sign_start = f_async_sign; | 
|  | 9627 | conf->f_async_decrypt_start = f_async_decrypt; | 
|  | 9628 | conf->f_async_resume = f_async_resume; | 
|  | 9629 | conf->f_async_cancel = f_async_cancel; | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9630 | conf->p_async_config_data = async_config_data; | 
|  | 9631 | } | 
|  | 9632 |  | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 9633 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) | 
|  | 9634 | { | 
|  | 9635 | return( conf->p_async_config_data ); | 
|  | 9636 | } | 
|  | 9637 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9638 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9639 | { | 
|  | 9640 | if( ssl->handshake == NULL ) | 
|  | 9641 | return( NULL ); | 
|  | 9642 | else | 
|  | 9643 | return( ssl->handshake->user_async_ctx ); | 
|  | 9644 | } | 
|  | 9645 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9646 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9647 | void *ctx ) | 
|  | 9648 | { | 
|  | 9649 | if( ssl->handshake != NULL ) | 
|  | 9650 | ssl->handshake->user_async_ctx = ctx; | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9651 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9652 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9653 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9654 | /* | 
|  | 9655 | * SSL get accessors | 
|  | 9656 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9657 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9658 | { | 
|  | 9659 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); | 
|  | 9660 | } | 
|  | 9661 |  | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9662 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) | 
|  | 9663 | { | 
|  | 9664 | /* | 
|  | 9665 | * Case A: We're currently holding back | 
|  | 9666 | * a message for further processing. | 
|  | 9667 | */ | 
|  | 9668 |  | 
|  | 9669 | if( ssl->keep_current_message == 1 ) | 
|  | 9670 | { | 
| Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9671 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) ); | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9672 | return( 1 ); | 
|  | 9673 | } | 
|  | 9674 |  | 
|  | 9675 | /* | 
|  | 9676 | * Case B: Further records are pending in the current datagram. | 
|  | 9677 | */ | 
|  | 9678 |  | 
|  | 9679 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9680 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9681 | ssl->in_left > ssl->next_record_offset ) | 
|  | 9682 | { | 
| Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9683 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) ); | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9684 | return( 1 ); | 
|  | 9685 | } | 
|  | 9686 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 9687 |  | 
|  | 9688 | /* | 
|  | 9689 | * Case C: A handshake message is being processed. | 
|  | 9690 | */ | 
|  | 9691 |  | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9692 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) | 
|  | 9693 | { | 
| Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9694 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) ); | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9695 | return( 1 ); | 
|  | 9696 | } | 
|  | 9697 |  | 
|  | 9698 | /* | 
|  | 9699 | * Case D: An application data message is being processed | 
|  | 9700 | */ | 
|  | 9701 | if( ssl->in_offt != NULL ) | 
|  | 9702 | { | 
| Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9703 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) ); | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9704 | return( 1 ); | 
|  | 9705 | } | 
|  | 9706 |  | 
|  | 9707 | /* | 
|  | 9708 | * In all other cases, the rest of the message can be dropped. | 
| Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 9709 | * As in ssl_get_next_record, this needs to be adapted if | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9710 | * we implement support for multiple alerts in single records. | 
|  | 9711 | */ | 
|  | 9712 |  | 
|  | 9713 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); | 
|  | 9714 | return( 0 ); | 
|  | 9715 | } | 
|  | 9716 |  | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9717 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9718 | { | 
| Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 9719 | if( ssl->session != NULL ) | 
|  | 9720 | return( ssl->session->verify_result ); | 
|  | 9721 |  | 
|  | 9722 | if( ssl->session_negotiate != NULL ) | 
|  | 9723 | return( ssl->session_negotiate->verify_result ); | 
|  | 9724 |  | 
| Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 9725 | return( 0xFFFFFFFF ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9726 | } | 
|  | 9727 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9728 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9729 | { | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9730 | int suite; | 
|  | 9731 |  | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9732 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9733 | return( NULL ); | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9734 |  | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 9735 | suite = mbedtls_ssl_session_get_ciphersuite( ssl->session ); | 
|  | 9736 | return( mbedtls_ssl_get_ciphersuite_name( suite ) ); | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9737 | } | 
|  | 9738 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9739 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9740 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9741 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9742 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9743 | { | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 9744 | switch( mbedtls_ssl_get_minor_ver( ssl ) ) | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9745 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9746 | case MBEDTLS_SSL_MINOR_VERSION_2: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9747 | return( "DTLSv1.0" ); | 
|  | 9748 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9749 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9750 | return( "DTLSv1.2" ); | 
|  | 9751 |  | 
|  | 9752 | default: | 
|  | 9753 | return( "unknown (DTLS)" ); | 
|  | 9754 | } | 
|  | 9755 | } | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9756 | MBEDTLS_SSL_TRANSPORT_ELSE | 
|  | 9757 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 9758 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9759 | { | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 9760 | switch( mbedtls_ssl_get_minor_ver( ssl ) ) | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9761 | { | 
|  | 9762 | case MBEDTLS_SSL_MINOR_VERSION_0: | 
|  | 9763 | return( "SSLv3.0" ); | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9764 |  | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9765 | case MBEDTLS_SSL_MINOR_VERSION_1: | 
|  | 9766 | return( "TLSv1.0" ); | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9767 |  | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9768 | case MBEDTLS_SSL_MINOR_VERSION_2: | 
|  | 9769 | return( "TLSv1.1" ); | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9770 |  | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9771 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
|  | 9772 | return( "TLSv1.2" ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 9773 |  | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9774 | default: | 
|  | 9775 | return( "unknown" ); | 
|  | 9776 | } | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9777 | } | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9778 | #endif /* MBEDTLS_SSL_PROTO_TLS */ | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9779 | } | 
|  | 9780 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9781 | int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9782 | { | 
| Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9783 | size_t transform_expansion = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9784 | const mbedtls_ssl_transform *transform = ssl->transform_out; | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9785 |  | 
| Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9786 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); | 
|  | 9787 |  | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9788 | if( transform == NULL ) | 
| Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9789 | return( (int) out_hdr_len ); | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9790 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9791 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 9792 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) | 
|  | 9793 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9794 | #endif | 
|  | 9795 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9796 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9797 | { | 
| Hanno Becker | a9d5c45 | 2019-07-25 16:47:12 +0100 | [diff] [blame] | 9798 | #if defined(MBEDTLS_GCM_C)        || \ | 
|  | 9799 | defined(MBEDTLS_CCM_C)        || \ | 
|  | 9800 | defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 9801 | #if defined(MBEDTLS_GCM_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9802 | case MBEDTLS_MODE_GCM: | 
| Hanno Becker | a9d5c45 | 2019-07-25 16:47:12 +0100 | [diff] [blame] | 9803 | #endif | 
|  | 9804 | #if defined(MBEDTLS_CCM_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9805 | case MBEDTLS_MODE_CCM: | 
| Hanno Becker | a9d5c45 | 2019-07-25 16:47:12 +0100 | [diff] [blame] | 9806 | #endif | 
|  | 9807 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9808 | case MBEDTLS_MODE_CHACHAPOLY: | 
| Hanno Becker | a9d5c45 | 2019-07-25 16:47:12 +0100 | [diff] [blame] | 9809 | #endif | 
|  | 9810 | transform_expansion = | 
|  | 9811 | transform->ivlen - transform->fixed_ivlen + transform->taglen; | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9812 | break; | 
|  | 9813 |  | 
| Hanno Becker | a9d5c45 | 2019-07-25 16:47:12 +0100 | [diff] [blame] | 9814 | #endif /* MBEDTLS_GCM_C        || MBEDTLS_CCM_C || | 
|  | 9815 | MBEDTLS_CHACHAPOLY_C */ | 
|  | 9816 |  | 
|  | 9817 | #if defined(MBEDTLS_CIPHER_MODE_STREAM) | 
|  | 9818 | case MBEDTLS_MODE_STREAM: | 
|  | 9819 | transform_expansion = transform->maclen; | 
|  | 9820 | break; | 
|  | 9821 | #endif /* MBEDTLS_CIPHER_MODE_STREAM */ | 
|  | 9822 |  | 
|  | 9823 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9824 | case MBEDTLS_MODE_CBC: | 
| Hanno Becker | a9d5c45 | 2019-07-25 16:47:12 +0100 | [diff] [blame] | 9825 | { | 
|  | 9826 | size_t block_size; | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9827 |  | 
|  | 9828 | block_size = mbedtls_cipher_get_block_size( | 
|  | 9829 | &transform->cipher_ctx_enc ); | 
|  | 9830 |  | 
| Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9831 | /* Expansion due to the addition of the MAC. */ | 
|  | 9832 | transform_expansion += transform->maclen; | 
|  | 9833 |  | 
|  | 9834 | /* Expansion due to the addition of CBC padding; | 
|  | 9835 | * Theoretically up to 256 bytes, but we never use | 
|  | 9836 | * more than the block size of the underlying cipher. */ | 
|  | 9837 | transform_expansion += block_size; | 
|  | 9838 |  | 
|  | 9839 | /* For TLS 1.1 or higher, an explicit IV is added | 
|  | 9840 | * after the record header. */ | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9841 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 9842 | if( mbedtls_ssl_ver_geq( | 
|  | 9843 | mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 9844 | MBEDTLS_SSL_MINOR_VERSION_2 ) ) | 
|  | 9845 | { | 
| Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9846 | transform_expansion += block_size; | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 9847 | } | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9848 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9849 |  | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9850 | break; | 
| Hanno Becker | a9d5c45 | 2019-07-25 16:47:12 +0100 | [diff] [blame] | 9851 | } | 
|  | 9852 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9853 |  | 
|  | 9854 | default: | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9855 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9856 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9857 | } | 
|  | 9858 |  | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9859 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
| Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9860 | if( transform->out_cid_len != 0 ) | 
|  | 9861 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; | 
| Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9862 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
| Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9863 |  | 
| Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9864 | return( (int)( out_hdr_len + transform_expansion ) ); | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9865 | } | 
|  | 9866 |  | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9867 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 9868 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) | 
|  | 9869 | { | 
|  | 9870 | size_t max_len; | 
|  | 9871 |  | 
|  | 9872 | /* | 
|  | 9873 | * Assume mfl_code is correct since it was checked when set | 
|  | 9874 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9875 | max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code ); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9876 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9877 | /* Check if a smaller max length was negotiated */ | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9878 | if( ssl->session_out != NULL && | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9879 | ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len ) | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9880 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9881 | max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9882 | } | 
|  | 9883 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9884 | /* During a handshake, use the value being negotiated */ | 
|  | 9885 | if( ssl->session_negotiate != NULL && | 
|  | 9886 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) | 
|  | 9887 | { | 
|  | 9888 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | 
|  | 9889 | } | 
|  | 9890 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9891 | return( max_len ); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9892 | } | 
|  | 9893 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 9894 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9895 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 9896 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) | 
|  | 9897 | { | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9898 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9899 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT && | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9900 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || | 
|  | 9901 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 9902 | return( 0 ); | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9903 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9904 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) | 
|  | 9905 | return( ssl->mtu ); | 
|  | 9906 |  | 
|  | 9907 | if( ssl->mtu == 0 ) | 
|  | 9908 | return( ssl->handshake->mtu ); | 
|  | 9909 |  | 
|  | 9910 | return( ssl->mtu < ssl->handshake->mtu ? | 
|  | 9911 | ssl->mtu : ssl->handshake->mtu ); | 
|  | 9912 | } | 
|  | 9913 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 9914 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9915 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) | 
|  | 9916 | { | 
|  | 9917 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 9918 |  | 
| Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 9919 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ | 
|  | 9920 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 9921 | (void) ssl; | 
|  | 9922 | #endif | 
|  | 9923 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9924 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 9925 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); | 
|  | 9926 |  | 
|  | 9927 | if( max_len > mfl ) | 
|  | 9928 | max_len = mfl; | 
|  | 9929 | #endif | 
|  | 9930 |  | 
|  | 9931 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9932 | if( ssl_get_current_mtu( ssl ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9933 | { | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9934 | const size_t mtu = ssl_get_current_mtu( ssl ); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9935 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); | 
|  | 9936 | const size_t overhead = (size_t) ret; | 
|  | 9937 |  | 
|  | 9938 | if( ret < 0 ) | 
|  | 9939 | return( ret ); | 
|  | 9940 |  | 
|  | 9941 | if( mtu <= overhead ) | 
|  | 9942 | { | 
|  | 9943 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); | 
|  | 9944 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 9945 | } | 
|  | 9946 |  | 
|  | 9947 | if( max_len > mtu - overhead ) | 
|  | 9948 | max_len = mtu - overhead; | 
|  | 9949 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9950 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9951 |  | 
| Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 9952 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) &&        \ | 
|  | 9953 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 9954 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9955 | #endif | 
|  | 9956 |  | 
|  | 9957 | return( (int) max_len ); | 
|  | 9958 | } | 
|  | 9959 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9960 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 9961 | const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9962 | { | 
|  | 9963 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9964 | return( NULL ); | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9965 |  | 
| Hanno Becker | bfab9df | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9966 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9967 | return( ssl->session->peer_cert ); | 
| Hanno Becker | bfab9df | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9968 | #else | 
|  | 9969 | return( NULL ); | 
|  | 9970 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9971 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9972 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9973 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9974 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | 933b9fc | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 9975 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, | 
|  | 9976 | mbedtls_ssl_session *dst ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9977 | { | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9978 | if( ssl == NULL || | 
|  | 9979 | dst == NULL || | 
|  | 9980 | ssl->session == NULL || | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9981 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9982 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9983 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9984 | } | 
|  | 9985 |  | 
| Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9986 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9987 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9988 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9989 |  | 
| Manuel Pégourié-Gonnard | 37a5324 | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 9990 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) | 
|  | 9991 | { | 
|  | 9992 | if( ssl == NULL ) | 
|  | 9993 | return( NULL ); | 
|  | 9994 |  | 
|  | 9995 | return( ssl->session ); | 
|  | 9996 | } | 
|  | 9997 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9998 | /* | 
| Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9999 | * Define ticket header determining Mbed TLS version | 
|  | 10000 | * and structure of the ticket. | 
|  | 10001 | */ | 
|  | 10002 |  | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10003 | /* | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10004 | * Define bitflag determining compile-time settings influencing | 
|  | 10005 | * structure of serialized SSL sessions. | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10006 | */ | 
|  | 10007 |  | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10008 | #if defined(MBEDTLS_HAVE_TIME) | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10009 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10010 | #else | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10011 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10012 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 10013 |  | 
|  | 10014 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10015 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10016 | #else | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10017 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10018 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 10019 |  | 
|  | 10020 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10021 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10022 | #else | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10023 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10024 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 10025 |  | 
|  | 10026 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10027 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10028 | #else | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10029 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10030 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 10031 |  | 
|  | 10032 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10033 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10034 | #else | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10035 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10036 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
|  | 10037 |  | 
|  | 10038 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10039 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10040 | #else | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10041 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10042 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
|  | 10043 |  | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10044 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 10045 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 | 
|  | 10046 | #else | 
|  | 10047 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 | 
|  | 10048 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
|  | 10049 |  | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10050 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 10051 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1 | 
|  | 10052 | #else | 
|  | 10053 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0 | 
|  | 10054 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 10055 |  | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10056 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 10057 | #define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1 | 
|  | 10058 | #else | 
|  | 10059 | #define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0 | 
|  | 10060 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 10061 |  | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10062 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          0 | 
|  | 10063 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           1 | 
|  | 10064 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 | 
|  | 10065 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           3 | 
|  | 10066 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT    4 | 
|  | 10067 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           5 | 
|  | 10068 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        6 | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10069 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT      7 | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10070 | #define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT   8 | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10071 |  | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10072 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG                           \ | 
| Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 10073 | ( (uint16_t) (                                                      \ | 
|  | 10074 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME          << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT          ) | \ | 
|  | 10075 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT           << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT           ) | \ | 
|  | 10076 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ | 
|  | 10077 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL           << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT           ) | \ | 
|  | 10078 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC    << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT    ) | \ | 
|  | 10079 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM           << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT           ) | \ | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10080 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET        << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT        ) | \ | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10081 | ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION   << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT   ) | \ | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10082 | ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT      << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT      ) ) ) | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10083 |  | 
| Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 10084 | static unsigned char ssl_serialized_session_header[] = { | 
| Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 10085 | MBEDTLS_VERSION_MAJOR, | 
|  | 10086 | MBEDTLS_VERSION_MINOR, | 
|  | 10087 | MBEDTLS_VERSION_PATCH, | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10088 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, | 
|  | 10089 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, | 
| Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 10090 | }; | 
| Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10091 |  | 
|  | 10092 | /* | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10093 | * Serialize a session in the following format: | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10094 | * (in the presentation language of TLS, RFC 8446 section 3) | 
|  | 10095 | * | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10096 | *  opaque mbedtls_version[3];   // major, minor, patch | 
|  | 10097 | *  opaque session_format[2];    // version-specific 16-bit field determining | 
|  | 10098 | *                               // the format of the remaining | 
|  | 10099 | *                               // serialized data. | 
| Hanno Becker | b36db4f | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 10100 | * | 
|  | 10101 | *  Note: When updating the format, remember to keep | 
|  | 10102 | *        these version+format bytes. | 
|  | 10103 | * | 
| Hanno Becker | 7bf7710 | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 10104 | *                               // In this version, `session_format` determines | 
|  | 10105 | *                               // the setting of those compile-time | 
|  | 10106 | *                               // configuration options which influence | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10107 | *                               // the structure of mbedtls_ssl_session. | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10108 | *  uint64 start_time; | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10109 | *  uint8 ciphersuite[2];        // defined by the standard | 
|  | 10110 | *  uint8 compression;           // 0 or 1 | 
|  | 10111 | *  uint8 session_id_len;        // at most 32 | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10112 | *  opaque session_id[32]; | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10113 | *  opaque master[48];           // fixed length in the standard | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10114 | *  uint32 verify_result; | 
| Hanno Becker | 0528f82 | 2019-06-18 12:45:31 +0100 | [diff] [blame] | 10115 | *  select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) { | 
|  | 10116 | *      case enabled:  opaque peer_cert<0..2^24-1>; // length 0 means no cert | 
|  | 10117 | *      case disabled: uint8_t peer_cert_digest_type; | 
|  | 10118 | *                     opaque peer_cert_digest<0..2^8-1>; | 
|  | 10119 | *  } | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10120 | *  opaque ticket<0..2^24-1>;    // length 0 means no ticket | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10121 | *  uint32 ticket_lifetime; | 
| Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 10122 | *  uint8 mfl_code;              // up to 255 according to standard | 
|  | 10123 | *  uint8 trunc_hmac;            // 0 or 1 | 
|  | 10124 | *  uint8 encrypt_then_mac;      // 0 or 1 | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10125 | * | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10126 | * The order is the same as in the definition of the structure, except | 
|  | 10127 | * verify_result is put before peer_cert so that all mandatory fields come | 
|  | 10128 | * together in one block. | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10129 | */ | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10130 | static int ssl_session_save( const mbedtls_ssl_session *session, | 
|  | 10131 | unsigned char omit_header, | 
|  | 10132 | unsigned char *buf, | 
|  | 10133 | size_t buf_len, | 
|  | 10134 | size_t *olen ) | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10135 | { | 
|  | 10136 | unsigned char *p = buf; | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10137 | size_t used = 0; | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10138 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 10139 | uint64_t start; | 
|  | 10140 | #endif | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10141 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10142 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10143 | size_t cert_len; | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10144 | #endif | 
| Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10145 | #endif | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10146 |  | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10147 | if( !omit_header ) | 
| Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10148 | { | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10149 | /* | 
|  | 10150 | * Add version identifier | 
|  | 10151 | */ | 
|  | 10152 |  | 
|  | 10153 | used += sizeof( ssl_serialized_session_header ); | 
|  | 10154 |  | 
|  | 10155 | if( used <= buf_len ) | 
|  | 10156 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 10157 | mbedtls_platform_memcpy( p, ssl_serialized_session_header, | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10158 | sizeof( ssl_serialized_session_header ) ); | 
|  | 10159 | p += sizeof( ssl_serialized_session_header ); | 
|  | 10160 | } | 
| Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10161 | } | 
|  | 10162 |  | 
|  | 10163 | /* | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10164 | * Time | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10165 | */ | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10166 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 10167 | used += 8; | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10168 |  | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10169 | if( used <= buf_len ) | 
|  | 10170 | { | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10171 | start = (uint64_t) session->start; | 
|  | 10172 |  | 
|  | 10173 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); | 
|  | 10174 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); | 
|  | 10175 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); | 
|  | 10176 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); | 
|  | 10177 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); | 
|  | 10178 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); | 
|  | 10179 | *p++ = (unsigned char)( ( start >>  8 ) & 0xFF ); | 
|  | 10180 | *p++ = (unsigned char)( ( start       ) & 0xFF ); | 
|  | 10181 | } | 
|  | 10182 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 10183 |  | 
|  | 10184 | /* | 
|  | 10185 | * Basic mandatory fields | 
|  | 10186 | */ | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10187 | { | 
|  | 10188 | size_t const ciphersuite_len = 2; | 
|  | 10189 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 10190 | size_t const compression_len = 1; | 
|  | 10191 | #else | 
|  | 10192 | size_t const compression_len = 0; | 
|  | 10193 | #endif | 
|  | 10194 | size_t const id_len_len = 1; | 
|  | 10195 | size_t const id_len = 32; | 
|  | 10196 | size_t const master_len = 48; | 
|  | 10197 | size_t const verif_result_len = 4; | 
|  | 10198 |  | 
|  | 10199 | size_t const basic_len = | 
|  | 10200 | ciphersuite_len + | 
|  | 10201 | compression_len + | 
|  | 10202 | id_len_len      + | 
|  | 10203 | id_len          + | 
|  | 10204 | master_len      + | 
|  | 10205 | verif_result_len; | 
|  | 10206 |  | 
|  | 10207 | used += basic_len; | 
|  | 10208 | } | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10209 |  | 
|  | 10210 | if( used <= buf_len ) | 
|  | 10211 | { | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 10212 | const int ciphersuite = | 
|  | 10213 | mbedtls_ssl_session_get_ciphersuite( session ); | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 10214 | p = mbedtls_platform_put_uint16_be( p, ciphersuite ); | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10215 |  | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10216 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 10217 | *p++ = (unsigned char)( | 
|  | 10218 | mbedtls_ssl_session_get_compression( session ) ); | 
|  | 10219 | #endif | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10220 |  | 
|  | 10221 | *p++ = (unsigned char)( session->id_len & 0xFF ); | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 10222 | /* Not using more secure mbedtls_platform_memcpy as session id is public */ | 
|  | 10223 | memcpy( p, session->id, 32 ); | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10224 | p += 32; | 
|  | 10225 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 10226 | mbedtls_platform_memcpy( p, session->master, 48 ); | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10227 | p += 48; | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 10228 | p = mbedtls_platform_put_uint32_be( p, session->verify_result ); | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10229 | } | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10230 |  | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10231 | /* | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10232 | * Peer's end-entity certificate | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10233 | */ | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10234 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10235 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10236 | if( session->peer_cert == NULL ) | 
|  | 10237 | cert_len = 0; | 
|  | 10238 | else | 
|  | 10239 | cert_len = session->peer_cert->raw.len; | 
|  | 10240 |  | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10241 | used += 3 + cert_len; | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10242 |  | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10243 | if( used <= buf_len ) | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10244 | { | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 10245 | p = mbedtls_platform_put_uint24_be( p, cert_len ); | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10246 |  | 
|  | 10247 | if( session->peer_cert != NULL ) | 
|  | 10248 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 10249 | mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len ); | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10250 | p += cert_len; | 
|  | 10251 | } | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10252 | } | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10253 |  | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10254 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10255 | /* Digest of peer certificate */ | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10256 | if( session->peer_cert_digest != NULL ) | 
|  | 10257 | { | 
|  | 10258 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; | 
|  | 10259 | if( used <= buf_len ) | 
|  | 10260 | { | 
|  | 10261 | *p++ = (unsigned char) session->peer_cert_digest_type; | 
|  | 10262 | *p++ = (unsigned char) session->peer_cert_digest_len; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 10263 | mbedtls_platform_memcpy( p, session->peer_cert_digest, | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10264 | session->peer_cert_digest_len ); | 
|  | 10265 | p += session->peer_cert_digest_len; | 
|  | 10266 | } | 
|  | 10267 | } | 
|  | 10268 | else | 
|  | 10269 | { | 
|  | 10270 | used += 2; | 
|  | 10271 | if( used <= buf_len ) | 
|  | 10272 | { | 
|  | 10273 | *p++ = (unsigned char) MBEDTLS_MD_NONE; | 
|  | 10274 | *p++ = 0; | 
|  | 10275 | } | 
|  | 10276 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10277 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10278 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 10279 |  | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10280 | /* | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10281 | * Session ticket if any, plus associated data | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10282 | */ | 
|  | 10283 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10284 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10285 |  | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10286 | if( used <= buf_len ) | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10287 | { | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 10288 | p = mbedtls_platform_put_uint24_be( p, session->ticket_len ); | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10289 |  | 
|  | 10290 | if( session->ticket != NULL ) | 
|  | 10291 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 10292 | mbedtls_platform_memcpy( p, session->ticket, session->ticket_len ); | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10293 | p += session->ticket_len; | 
|  | 10294 | } | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10295 |  | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 10296 | p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime ); | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10297 | } | 
|  | 10298 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 10299 |  | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10300 | /* | 
|  | 10301 | * Misc extension-related info | 
|  | 10302 | */ | 
|  | 10303 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 10304 | used += 1; | 
|  | 10305 |  | 
|  | 10306 | if( used <= buf_len ) | 
|  | 10307 | *p++ = session->mfl_code; | 
|  | 10308 | #endif | 
|  | 10309 |  | 
|  | 10310 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 10311 | used += 1; | 
|  | 10312 |  | 
|  | 10313 | if( used <= buf_len ) | 
|  | 10314 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); | 
|  | 10315 | #endif | 
|  | 10316 |  | 
|  | 10317 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 10318 | used += 1; | 
|  | 10319 |  | 
|  | 10320 | if( used <= buf_len ) | 
|  | 10321 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); | 
|  | 10322 | #endif | 
|  | 10323 |  | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10324 | /* Done */ | 
| Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 10325 | *olen = used; | 
|  | 10326 |  | 
|  | 10327 | if( used > buf_len ) | 
|  | 10328 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10329 |  | 
|  | 10330 | return( 0 ); | 
|  | 10331 | } | 
|  | 10332 |  | 
|  | 10333 | /* | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10334 | * Public wrapper for ssl_session_save() | 
|  | 10335 | */ | 
|  | 10336 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, | 
|  | 10337 | unsigned char *buf, | 
|  | 10338 | size_t buf_len, | 
|  | 10339 | size_t *olen ) | 
|  | 10340 | { | 
|  | 10341 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); | 
|  | 10342 | } | 
|  | 10343 |  | 
|  | 10344 | /* | 
| Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 10345 | * Deserialize session, see mbedtls_ssl_session_save() for format. | 
| Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10346 | * | 
|  | 10347 | * This internal version is wrapped by a public function that cleans up in | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10348 | * case of error, and has an extra option omit_header. | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10349 | */ | 
| Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10350 | static int ssl_session_load( mbedtls_ssl_session *session, | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10351 | unsigned char omit_header, | 
| Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10352 | const unsigned char *buf, | 
|  | 10353 | size_t len ) | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10354 | { | 
|  | 10355 | const unsigned char *p = buf; | 
|  | 10356 | const unsigned char * const end = buf + len; | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 10357 | int ciphersuite; | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10358 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 10359 | uint64_t start; | 
|  | 10360 | #endif | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10361 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10362 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10363 | size_t cert_len; | 
| Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10364 | #endif | 
|  | 10365 | #endif | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10366 |  | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10367 | if( !omit_header ) | 
| Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10368 | { | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10369 | /* | 
|  | 10370 | * Check version identifier | 
|  | 10371 | */ | 
|  | 10372 |  | 
|  | 10373 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) | 
|  | 10374 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10375 |  | 
| Teppo Järvelin | 0efac53 | 2019-10-04 13:21:08 +0300 | [diff] [blame] | 10376 | // use regular memcmp as session header is public data | 
| Teppo Järvelin | 650343c | 2019-10-03 15:36:59 +0300 | [diff] [blame] | 10377 | if( memcmp( p, ssl_serialized_session_header, | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10378 | sizeof( ssl_serialized_session_header ) ) != 0 ) | 
|  | 10379 | { | 
|  | 10380 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 10381 | } | 
|  | 10382 | p += sizeof( ssl_serialized_session_header ); | 
| Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10383 | } | 
| Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 10384 |  | 
|  | 10385 | /* | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10386 | * Time | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10387 | */ | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10388 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 10389 | if( 8 > (size_t)( end - p ) ) | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10390 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10391 |  | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10392 | start = ( (uint64_t) p[0] << 56 ) | | 
|  | 10393 | ( (uint64_t) p[1] << 48 ) | | 
|  | 10394 | ( (uint64_t) p[2] << 40 ) | | 
|  | 10395 | ( (uint64_t) p[3] << 32 ) | | 
|  | 10396 | ( (uint64_t) p[4] << 24 ) | | 
|  | 10397 | ( (uint64_t) p[5] << 16 ) | | 
|  | 10398 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 10399 | ( (uint64_t) p[7]       ); | 
|  | 10400 | p += 8; | 
|  | 10401 |  | 
|  | 10402 | session->start = (time_t) start; | 
|  | 10403 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 10404 |  | 
|  | 10405 | /* | 
|  | 10406 | * Basic mandatory fields | 
|  | 10407 | */ | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10408 | { | 
|  | 10409 | size_t const ciphersuite_len = 2; | 
|  | 10410 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 10411 | size_t const compression_len = 1; | 
|  | 10412 | #else | 
|  | 10413 | size_t const compression_len = 0; | 
|  | 10414 | #endif | 
|  | 10415 | size_t const id_len_len = 1; | 
|  | 10416 | size_t const id_len = 32; | 
|  | 10417 | size_t const master_len = 48; | 
|  | 10418 | size_t const verif_result_len = 4; | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 10419 |  | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10420 | size_t const basic_len = | 
|  | 10421 | ciphersuite_len + | 
|  | 10422 | compression_len + | 
|  | 10423 | id_len_len      + | 
|  | 10424 | id_len          + | 
|  | 10425 | master_len      + | 
|  | 10426 | verif_result_len; | 
|  | 10427 |  | 
|  | 10428 | if( basic_len > (size_t)( end - p ) ) | 
|  | 10429 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10430 | } | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10431 |  | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 10432 | ciphersuite = (int)mbedtls_platform_get_uint16_be( p ); | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10433 | p += 2; | 
|  | 10434 |  | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 10435 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 10436 | session->ciphersuite = ciphersuite; | 
|  | 10437 | #else | 
|  | 10438 | if( ciphersuite != | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 10439 | MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) ) | 
| Hanno Becker | e02758c | 2019-06-26 15:31:31 +0100 | [diff] [blame] | 10440 | { | 
|  | 10441 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 10442 | } | 
|  | 10443 | #endif | 
|  | 10444 |  | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10445 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10446 | session->compression = *p++; | 
| Hanno Becker | 8844055 | 2019-07-03 14:16:13 +0100 | [diff] [blame] | 10447 | #endif | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10448 |  | 
|  | 10449 | session->id_len = *p++; | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 10450 | /* Not using more secure mbedtls_platform_memcpy as session id is public */ | 
|  | 10451 | memcpy( session->id, p, 32 ); | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10452 | p += 32; | 
|  | 10453 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 10454 | mbedtls_platform_memcpy( session->master, p, 48 ); | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10455 | p += 48; | 
|  | 10456 |  | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 10457 | session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p ); | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10458 | p += 4; | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10459 |  | 
| Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10460 | /* Immediately clear invalid pointer values that have been read, in case | 
|  | 10461 | * we exit early before we replaced them with valid ones. */ | 
|  | 10462 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10463 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10464 | session->peer_cert = NULL; | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10465 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10466 | session->peer_cert_digest = NULL; | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10467 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10468 | #endif | 
|  | 10469 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 10470 | session->ticket = NULL; | 
|  | 10471 | #endif | 
|  | 10472 |  | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10473 | /* | 
|  | 10474 | * Peer certificate | 
|  | 10475 | */ | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10476 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 10477 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10478 | if( 3 > (size_t)( end - p ) ) | 
|  | 10479 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10480 |  | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 10481 | cert_len = mbedtls_platform_get_uint24_be( &p[0] ); | 
|  | 10482 |  | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10483 | p += 3; | 
|  | 10484 |  | 
|  | 10485 | if( cert_len == 0 ) | 
|  | 10486 | { | 
|  | 10487 | session->peer_cert = NULL; | 
|  | 10488 | } | 
|  | 10489 | else | 
|  | 10490 | { | 
|  | 10491 | int ret; | 
|  | 10492 |  | 
|  | 10493 | if( cert_len > (size_t)( end - p ) ) | 
|  | 10494 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10495 |  | 
|  | 10496 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); | 
|  | 10497 |  | 
|  | 10498 | if( session->peer_cert == NULL ) | 
|  | 10499 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 10500 |  | 
|  | 10501 | mbedtls_x509_crt_init( session->peer_cert ); | 
|  | 10502 |  | 
|  | 10503 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, | 
|  | 10504 | p, cert_len ) ) != 0 ) | 
|  | 10505 | { | 
|  | 10506 | mbedtls_x509_crt_free( session->peer_cert ); | 
|  | 10507 | mbedtls_free( session->peer_cert ); | 
|  | 10508 | session->peer_cert = NULL; | 
|  | 10509 | return( ret ); | 
|  | 10510 | } | 
|  | 10511 |  | 
|  | 10512 | p += cert_len; | 
|  | 10513 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10514 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10515 | /* Deserialize CRT digest from the end of the ticket. */ | 
|  | 10516 | if( 2 > (size_t)( end - p ) ) | 
|  | 10517 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10518 |  | 
|  | 10519 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; | 
|  | 10520 | session->peer_cert_digest_len  = (size_t) *p++; | 
|  | 10521 |  | 
|  | 10522 | if( session->peer_cert_digest_len != 0 ) | 
|  | 10523 | { | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 10524 | mbedtls_md_handle_t md_info = | 
| Hanno Becker | 2326d20 | 2019-06-06 14:54:55 +0100 | [diff] [blame] | 10525 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 10526 | if( md_info == MBEDTLS_MD_INVALID_HANDLE ) | 
| Hanno Becker | 2326d20 | 2019-06-06 14:54:55 +0100 | [diff] [blame] | 10527 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10528 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) | 
|  | 10529 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10530 |  | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10531 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) | 
|  | 10532 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10533 |  | 
|  | 10534 | session->peer_cert_digest = | 
|  | 10535 | mbedtls_calloc( 1, session->peer_cert_digest_len ); | 
|  | 10536 | if( session->peer_cert_digest == NULL ) | 
|  | 10537 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 10538 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 10539 | mbedtls_platform_memcpy( session->peer_cert_digest, p, | 
| Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 10540 | session->peer_cert_digest_len ); | 
|  | 10541 | p += session->peer_cert_digest_len; | 
|  | 10542 | } | 
| Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 10543 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10544 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
|  | 10545 |  | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10546 | /* | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10547 | * Session ticket and associated data | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10548 | */ | 
|  | 10549 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 10550 | if( 3 > (size_t)( end - p ) ) | 
|  | 10551 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10552 |  | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 10553 | session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] ); | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10554 | p += 3; | 
|  | 10555 |  | 
|  | 10556 | if( session->ticket_len != 0 ) | 
|  | 10557 | { | 
|  | 10558 | if( session->ticket_len > (size_t)( end - p ) ) | 
|  | 10559 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10560 |  | 
|  | 10561 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); | 
|  | 10562 | if( session->ticket == NULL ) | 
|  | 10563 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
|  | 10564 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 10565 | mbedtls_platform_memcpy( session->ticket, p, session->ticket_len ); | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10566 | p += session->ticket_len; | 
|  | 10567 | } | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10568 |  | 
|  | 10569 | if( 4 > (size_t)( end - p ) ) | 
|  | 10570 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10571 |  | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 10572 | session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p ); | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10573 | p += 4; | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10574 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
|  | 10575 |  | 
| Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 10576 | /* | 
|  | 10577 | * Misc extension-related info | 
|  | 10578 | */ | 
|  | 10579 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 10580 | if( 1 > (size_t)( end - p ) ) | 
|  | 10581 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10582 |  | 
|  | 10583 | session->mfl_code = *p++; | 
|  | 10584 | #endif | 
|  | 10585 |  | 
|  | 10586 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 10587 | if( 1 > (size_t)( end - p ) ) | 
|  | 10588 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10589 |  | 
|  | 10590 | session->trunc_hmac = *p++; | 
|  | 10591 | #endif | 
|  | 10592 |  | 
|  | 10593 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 10594 | if( 1 > (size_t)( end - p ) ) | 
|  | 10595 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10596 |  | 
|  | 10597 | session->encrypt_then_mac = *p++; | 
|  | 10598 | #endif | 
|  | 10599 |  | 
| Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 10600 | /* Done, should have consumed entire buffer */ | 
| Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 10601 | if( p != end ) | 
|  | 10602 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10603 |  | 
|  | 10604 | return( 0 ); | 
|  | 10605 | } | 
|  | 10606 |  | 
|  | 10607 | /* | 
| Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 10608 | * Deserialize session: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10609 | */ | 
|  | 10610 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, | 
|  | 10611 | const unsigned char *buf, | 
|  | 10612 | size_t len ) | 
|  | 10613 | { | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 10614 | int ret = ssl_session_load( session, 0, buf, len ); | 
| Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 10615 |  | 
|  | 10616 | if( ret != 0 ) | 
|  | 10617 | mbedtls_ssl_session_free( session ); | 
|  | 10618 |  | 
|  | 10619 | return( ret ); | 
|  | 10620 | } | 
|  | 10621 |  | 
|  | 10622 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10623 | * Perform a single step of the SSL handshake | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10624 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10625 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10626 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10627 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10628 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10629 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 10630 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10631 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10632 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10633 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10634 | ret = mbedtls_ssl_handshake_client_step( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10635 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10636 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10637 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10638 | ret = mbedtls_ssl_handshake_server_step( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10639 | #endif | 
|  | 10640 |  | 
| Hanno Becker | b82350b | 2019-07-26 07:24:05 +0100 | [diff] [blame] | 10641 | ssl_send_pending_fatal_alert( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10642 | return( ret ); | 
|  | 10643 | } | 
|  | 10644 |  | 
|  | 10645 | /* | 
|  | 10646 | * Perform the SSL handshake | 
|  | 10647 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10648 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10649 | { | 
|  | 10650 | int ret = 0; | 
|  | 10651 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10652 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 10653 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10654 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10656 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10657 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10658 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10659 | ret = mbedtls_ssl_handshake_step( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 10660 |  | 
|  | 10661 | if( ret != 0 ) | 
|  | 10662 | break; | 
|  | 10663 | } | 
|  | 10664 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10665 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10666 |  | 
|  | 10667 | return( ret ); | 
|  | 10668 | } | 
|  | 10669 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10670 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 10671 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10672 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10673 | * Write HelloRequest to request renegotiation on server | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10674 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10675 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10676 | { | 
|  | 10677 | int ret; | 
|  | 10678 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10679 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10680 |  | 
|  | 10681 | ssl->out_msglen  = 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10682 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 10683 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10684 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10685 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10686 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 10687 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10688 | return( ret ); | 
|  | 10689 | } | 
|  | 10690 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10691 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10692 |  | 
|  | 10693 | return( 0 ); | 
|  | 10694 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10695 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10696 |  | 
|  | 10697 | /* | 
|  | 10698 | * Actually renegotiate current connection, triggered by either: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10699 | * - any side: calling mbedtls_ssl_renegotiate(), | 
|  | 10700 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), | 
|  | 10701 | * - server: receiving any handshake message on server during mbedtls_ssl_read() after | 
| Manuel Pégourié-Gonnard | 55e4ff2 | 2014-08-19 11:16:35 +0200 | [diff] [blame] | 10702 | *   the initial handshake is completed. | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10703 | * If the handshake doesn't complete due to waiting for I/O, it will continue | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10704 | * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively. | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10705 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10706 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10707 | { | 
|  | 10708 | int ret; | 
|  | 10709 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10710 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10711 |  | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10712 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 10713 | return( ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10714 |  | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10715 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and | 
|  | 10716 | * the ServerHello will have message_seq = 1" */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10717 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10718 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10719 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10720 | { | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10721 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 10722 | MBEDTLS_SSL_IS_SERVER ) | 
|  | 10723 | { | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10724 | ssl->handshake->out_msg_seq = 1; | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10725 | } | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10726 | else | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10727 | { | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 10728 | ssl->handshake->in_msg_seq = 1; | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10729 | } | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 10730 | } | 
|  | 10731 | #endif | 
|  | 10732 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10733 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 10734 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10735 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10736 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10737 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10738 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10739 | return( ret ); | 
|  | 10740 | } | 
|  | 10741 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10742 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10743 |  | 
|  | 10744 | return( 0 ); | 
|  | 10745 | } | 
|  | 10746 |  | 
|  | 10747 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10748 | * Renegotiate current connection on client, | 
|  | 10749 | * or request renegotiation on server | 
|  | 10750 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10751 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10752 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10753 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10754 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10755 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 10756 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10757 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10758 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10759 | /* On server, just send the request */ | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10760 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10761 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10762 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 10763 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10764 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10765 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10766 |  | 
|  | 10767 | /* Did we already try/start sending HelloRequest? */ | 
|  | 10768 | if( ssl->out_left != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10769 | return( mbedtls_ssl_flush_output( ssl ) ); | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 10770 |  | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10771 | return( ssl_write_hello_request( ssl ) ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10772 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10773 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10774 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10775 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10776 | /* | 
|  | 10777 | * On client, either start the renegotiation process or, | 
|  | 10778 | * if already in progress, continue the handshake | 
|  | 10779 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10780 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10781 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10782 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 10783 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10784 |  | 
|  | 10785 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) | 
|  | 10786 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10787 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10788 | return( ret ); | 
|  | 10789 | } | 
|  | 10790 | } | 
|  | 10791 | else | 
|  | 10792 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10793 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10794 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10795 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10796 | return( ret ); | 
|  | 10797 | } | 
|  | 10798 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10799 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 10800 |  | 
| Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 10801 | return( ret ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 10802 | } | 
|  | 10803 |  | 
|  | 10804 | /* | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10805 | * Check record counters and renegotiate if they're above the limit. | 
|  | 10806 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10807 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10808 | { | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10809 | size_t ep_len = ssl_ep_len( ssl ); | 
|  | 10810 | int in_ctr_cmp; | 
|  | 10811 | int out_ctr_cmp; | 
|  | 10812 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10813 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || | 
|  | 10814 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || | 
| Manuel Pégourié-Gonnard | 18332c5 | 2019-07-29 12:17:52 +0200 | [diff] [blame] | 10815 | ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10816 | { | 
|  | 10817 | return( 0 ); | 
|  | 10818 | } | 
|  | 10819 |  | 
| Teppo Järvelin | 0efac53 | 2019-10-04 13:21:08 +0300 | [diff] [blame] | 10820 | // use regular memcmp as counters are public data | 
| Teppo Järvelin | 650343c | 2019-10-03 15:36:59 +0300 | [diff] [blame] | 10821 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10822 | ssl->conf->renego_period + ep_len, 8 - ep_len ); | 
| Teppo Järvelin | 650343c | 2019-10-03 15:36:59 +0300 | [diff] [blame] | 10823 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10824 | ssl->conf->renego_period + ep_len, 8 - ep_len ); | 
|  | 10825 |  | 
|  | 10826 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10827 | { | 
|  | 10828 | return( 0 ); | 
|  | 10829 | } | 
|  | 10830 |  | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 10831 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10832 | return( mbedtls_ssl_renegotiate( ssl ) ); | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10833 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10834 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10835 |  | 
|  | 10836 | /* | 
|  | 10837 | * Receive application data decrypted from the SSL layer | 
|  | 10838 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10839 | int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10840 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10841 | int ret; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10842 | size_t n; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10843 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10844 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 10845 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 10846 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10847 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10848 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10849 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10850 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10851 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10852 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10853 | return( ret ); | 
|  | 10854 |  | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10855 | if( ssl->handshake != NULL && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10856 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10857 | { | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 10858 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10859 | return( ret ); | 
|  | 10860 | } | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 10861 | } | 
|  | 10862 | #endif | 
|  | 10863 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10864 | /* | 
|  | 10865 | * Check if renegotiation is necessary and/or handshake is | 
|  | 10866 | * in process. If yes, perform/continue, and fall through | 
|  | 10867 | * if an unexpected packet is received while the client | 
|  | 10868 | * is waiting for the ServerHello. | 
|  | 10869 | * | 
|  | 10870 | * (There is no equivalent to the last condition on | 
|  | 10871 | *  the server-side as it is not treated as within | 
|  | 10872 | *  a handshake while waiting for the ClientHello | 
|  | 10873 | *  after a renegotiation request.) | 
|  | 10874 | */ | 
|  | 10875 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10876 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10877 | ret = ssl_check_ctr_renegotiate( ssl ); | 
|  | 10878 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && | 
|  | 10879 | ret != 0 ) | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10880 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10881 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 10882 | return( ret ); | 
|  | 10883 | } | 
|  | 10884 | #endif | 
|  | 10885 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10886 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10887 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10888 | ret = mbedtls_ssl_handshake( ssl ); | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10889 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && | 
|  | 10890 | ret != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10891 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10892 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10893 | return( ret ); | 
|  | 10894 | } | 
|  | 10895 | } | 
|  | 10896 |  | 
| Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 10897 | /* Loop as long as no application data record is available */ | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10898 | while( ssl->in_offt == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10899 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10900 | /* Start timer if not already running */ | 
| Hanno Becker | 0ae6b24 | 2019-06-13 16:45:36 +0100 | [diff] [blame] | 10901 | if( mbedtls_ssl_get_get_timer( ssl ) != NULL && | 
|  | 10902 | mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 ) | 
| Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10903 | { | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 10904 | ssl_set_timer( ssl, | 
|  | 10905 | mbedtls_ssl_conf_get_read_timeout( ssl->conf ) ); | 
| Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10906 | } | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10907 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10908 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10909 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10910 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) | 
|  | 10911 | return( 0 ); | 
| Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10912 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10913 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 10914 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10915 | } | 
|  | 10916 |  | 
|  | 10917 | if( ssl->in_msglen  == 0 && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10918 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10919 | { | 
|  | 10920 | /* | 
|  | 10921 | * OpenSSL sends empty messages to randomize the IV | 
|  | 10922 | */ | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10923 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10924 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10925 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) | 
| Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10926 | return( 0 ); | 
|  | 10927 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10928 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10929 | return( ret ); | 
|  | 10930 | } | 
|  | 10931 | } | 
|  | 10932 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10933 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10934 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10935 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10936 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10937 | /* | 
|  | 10938 | * - For client-side, expect SERVER_HELLO_REQUEST. | 
|  | 10939 | * - For server-side, expect CLIENT_HELLO. | 
|  | 10940 | * - Fail (TLS) or silently drop record (DTLS) in other cases. | 
|  | 10941 | */ | 
|  | 10942 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10943 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10944 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 10945 | MBEDTLS_SSL_IS_CLIENT && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10946 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10947 | ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) ) ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10948 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10949 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10950 |  | 
|  | 10951 | /* With DTLS, drop the packet (probably from last handshake) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10952 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10953 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10954 | { | 
|  | 10955 | continue; | 
|  | 10956 | } | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10957 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10958 | #endif | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10959 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 10960 | { | 
|  | 10961 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 10962 | } | 
|  | 10963 | #endif | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10964 | } | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10965 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10966 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10967 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10968 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 10969 | MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10970 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10971 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10972 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10973 |  | 
|  | 10974 | /* With DTLS, drop the packet (probably from last handshake) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10975 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10976 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10977 | { | 
|  | 10978 | continue; | 
|  | 10979 | } | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10980 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10981 | #endif | 
| Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10982 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 10983 | { | 
|  | 10984 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
|  | 10985 | } | 
|  | 10986 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10987 | } | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10988 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 10989 |  | 
| Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10990 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10991 | /* Determine whether renegotiation attempt should be accepted */ | 
| Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10992 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || | 
|  | 10993 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
| Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 10994 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == | 
| Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10995 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) | 
|  | 10996 | { | 
|  | 10997 | /* | 
|  | 10998 | * Accept renegotiation request | 
|  | 10999 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11000 |  | 
| Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 11001 | /* DTLS clients need to know renego is server-initiated */ | 
|  | 11002 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 11003 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 11004 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 11005 | MBEDTLS_SSL_IS_CLIENT ) | 
| Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 11006 | { | 
|  | 11007 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; | 
|  | 11008 | } | 
|  | 11009 | #endif | 
|  | 11010 | ret = ssl_start_renegotiation( ssl ); | 
|  | 11011 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && | 
|  | 11012 | ret != 0 ) | 
|  | 11013 | { | 
|  | 11014 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); | 
|  | 11015 | return( ret ); | 
|  | 11016 | } | 
|  | 11017 | } | 
|  | 11018 | else | 
| Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 11019 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11020 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 11021 | /* | 
|  | 11022 | * Refuse renegotiation | 
|  | 11023 | */ | 
|  | 11024 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11025 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11026 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11027 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Hanno Becker | 2881d80 | 2019-05-22 14:44:53 +0100 | [diff] [blame] | 11028 | if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11029 | { | 
| Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 11030 | /* SSLv3 does not have a "no_renegotiation" warning, so | 
|  | 11031 | we send a fatal alert and abort the connection. */ | 
|  | 11032 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 11033 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 11034 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 11035 | } | 
|  | 11036 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11037 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 11038 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 11039 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 11040 | if( mbedtls_ssl_ver_geq( | 
|  | 11041 | mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 11042 | MBEDTLS_SSL_MINOR_VERSION_1 ) ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 11043 | { | 
| Hanno Becker | 2e8d133 | 2019-07-25 10:27:36 +0100 | [diff] [blame] | 11044 | ret = mbedtls_ssl_send_alert_message( ssl, | 
|  | 11045 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, | 
|  | 11046 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ); | 
|  | 11047 | if( ret != 0 ) | 
|  | 11048 | return( ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11049 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 11050 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11051 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || | 
|  | 11052 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 11053 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11054 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 11055 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 11056 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11057 | } | 
| Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 11058 |  | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 11059 | /* At this point, we don't know whether the renegotiation has been | 
|  | 11060 | * completed or not. The cases to consider are the following: | 
|  | 11061 | * 1) The renegotiation is complete. In this case, no new record | 
|  | 11062 | *    has been read yet. | 
|  | 11063 | * 2) The renegotiation is incomplete because the client received | 
|  | 11064 | *    an application data record while awaiting the ServerHello. | 
|  | 11065 | * 3) The renegotiation is incomplete because the client received | 
|  | 11066 | *    a non-handshake, non-application data message while awaiting | 
|  | 11067 | *    the ServerHello. | 
|  | 11068 | * In each of these case, looping will be the proper action: | 
|  | 11069 | * - For 1), the next iteration will read a new record and check | 
|  | 11070 | *   if it's application data. | 
|  | 11071 | * - For 2), the loop condition isn't satisfied as application data | 
|  | 11072 | *   is present, hence continue is the same as break | 
|  | 11073 | * - For 3), the loop condition is satisfied and read_record | 
|  | 11074 | *   will re-deliver the message that was held back by the client | 
|  | 11075 | *   when expecting the ServerHello. | 
|  | 11076 | */ | 
|  | 11077 | continue; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11078 | } | 
| Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 11079 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11080 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 11081 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 11082 | if( ssl->conf->renego_max_records >= 0 ) | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 11083 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 11084 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 11085 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11086 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 11087 | "but not honored by client" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11088 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 11089 | } | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 11090 | } | 
| Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 11091 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11092 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 11093 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11094 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ | 
|  | 11095 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) | 
| Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 11096 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11097 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 11098 | return( MBEDTLS_ERR_SSL_WANT_READ ); | 
| Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 11099 | } | 
|  | 11100 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11101 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11102 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); | 
|  | 11104 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11105 | } | 
|  | 11106 |  | 
|  | 11107 | ssl->in_offt = ssl->in_msg; | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 11108 |  | 
| Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 11109 | /* We're going to return something now, cancel timer, | 
|  | 11110 | * except if handshake (renegotiation) is in progress */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11111 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 11112 | ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 11113 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 11114 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 11115 | /* If we requested renego but received AppData, resend HelloRequest. | 
|  | 11116 | * Do it now, after setting in_offt, to avoid taking this branch | 
|  | 11117 | * again if ssl_write_hello_request() returns WANT_WRITE */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11118 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 11119 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == | 
|  | 11120 | MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11121 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 11122 | { | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 11123 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 11124 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11125 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 11126 | return( ret ); | 
|  | 11127 | } | 
|  | 11128 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11129 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 11130 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11131 | } | 
|  | 11132 |  | 
|  | 11133 | n = ( len < ssl->in_msglen ) | 
|  | 11134 | ? len : ssl->in_msglen; | 
|  | 11135 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 11136 | mbedtls_platform_memcpy( buf, ssl->in_offt, n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11137 | ssl->in_msglen -= n; | 
|  | 11138 |  | 
| Teppo Järvelin | cafb6c9 | 2020-01-08 09:19:07 +0200 | [diff] [blame] | 11139 | // clear incoming data after it's copied to buffer | 
|  | 11140 | mbedtls_platform_memset(ssl->in_offt, 0, n); | 
|  | 11141 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11142 | if( ssl->in_msglen == 0 ) | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 11143 | { | 
|  | 11144 | /* all bytes consumed */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11145 | ssl->in_offt = NULL; | 
| Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 11146 | ssl->keep_current_message = 0; | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 11147 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11148 | else | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 11149 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11150 | /* more data available */ | 
|  | 11151 | ssl->in_offt += n; | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 11152 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11153 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11154 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11155 |  | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 11156 | return( (int) n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11157 | } | 
|  | 11158 |  | 
|  | 11159 | /* | 
| Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 11160 | * Send application data to be encrypted by the SSL layer, taking care of max | 
|  | 11161 | * fragment length and buffer size. | 
|  | 11162 | * | 
|  | 11163 | * According to RFC 5246 Section 6.2.1: | 
|  | 11164 | * | 
|  | 11165 | *      Zero-length fragments of Application data MAY be sent as they are | 
|  | 11166 | *      potentially useful as a traffic analysis countermeasure. | 
|  | 11167 | * | 
|  | 11168 | * Therefore, it is possible that the input message length is 0 and the | 
|  | 11169 | * corresponding return code is 0 on success. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11170 | */ | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11171 | static int ssl_write_real( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11172 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11173 | { | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 11174 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); | 
|  | 11175 | const size_t max_len = (size_t) ret; | 
|  | 11176 |  | 
|  | 11177 | if( ret < 0 ) | 
|  | 11178 | { | 
|  | 11179 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); | 
|  | 11180 | return( ret ); | 
|  | 11181 | } | 
|  | 11182 |  | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11183 | if( len > max_len ) | 
|  | 11184 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11185 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11186 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11187 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11188 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11189 | "maximum fragment length: %d > %d", | 
|  | 11190 | len, max_len ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11191 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11192 | } | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11193 | MBEDTLS_SSL_TRANSPORT_ELSE | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11194 | #endif | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11195 | #if defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 11196 | { | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11197 | len = max_len; | 
| Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11198 | } | 
|  | 11199 | #endif | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11200 | } | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11201 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11202 | if( ssl->out_left != 0 ) | 
|  | 11203 | { | 
| Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 11204 | /* | 
|  | 11205 | * The user has previously tried to send the data and | 
|  | 11206 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially | 
|  | 11207 | * written. In this case, we expect the high-level write function | 
|  | 11208 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters | 
|  | 11209 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11210 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11211 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11212 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11213 | return( ret ); | 
|  | 11214 | } | 
|  | 11215 | } | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11216 | else | 
| Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 11217 | { | 
| Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 11218 | /* | 
|  | 11219 | * The user is trying to send a message the first time, so we need to | 
|  | 11220 | * copy the data into the internal buffers and setup the data structure | 
|  | 11221 | * to keep track of partial writes | 
|  | 11222 | */ | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11223 | ssl->out_msglen  = len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11224 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 11225 | mbedtls_platform_memcpy( ssl->out_msg, buf, len ); | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11226 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 11227 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11228 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11229 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 11230 | return( ret ); | 
|  | 11231 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11232 | } | 
|  | 11233 |  | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 11234 | return( (int) len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11235 | } | 
|  | 11236 |  | 
|  | 11237 | /* | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11238 | * Write application data, doing 1/n-1 splitting if necessary. | 
|  | 11239 | * | 
|  | 11240 | * With non-blocking I/O, ssl_write_real() may return WANT_WRITE, | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 11241 | * then the caller will call us again with the same arguments, so | 
| Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 11242 | * remember whether we already did the split or not. | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11243 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11244 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11245 | static int ssl_write_split( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11246 | const unsigned char *buf, size_t len ) | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11247 | { | 
|  | 11248 | int ret; | 
|  | 11249 |  | 
| Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 11250 | if( ssl->conf->cbc_record_splitting == | 
|  | 11251 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 11252 | len <= 1 || | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 11253 | mbedtls_ssl_ver_gt( | 
|  | 11254 | mbedtls_ssl_get_minor_ver( ssl ), | 
|  | 11255 | MBEDTLS_SSL_MINOR_VERSION_1 ) || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11256 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) | 
|  | 11257 | != MBEDTLS_MODE_CBC ) | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11258 | { | 
|  | 11259 | return( ssl_write_real( ssl, buf, len ) ); | 
|  | 11260 | } | 
|  | 11261 |  | 
|  | 11262 | if( ssl->split_done == 0 ) | 
|  | 11263 | { | 
| Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11264 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11265 | return( ret ); | 
| Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11266 | ssl->split_done = 1; | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11267 | } | 
|  | 11268 |  | 
| Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 11269 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) | 
|  | 11270 | return( ret ); | 
|  | 11271 | ssl->split_done = 0; | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11272 |  | 
|  | 11273 | return( ret + 1 ); | 
|  | 11274 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11275 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 11276 |  | 
|  | 11277 | /* | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11278 | * Write application data (public-facing wrapper) | 
|  | 11279 | */ | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11280 | int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len ) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11281 | { | 
|  | 11282 | int ret; | 
|  | 11283 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11284 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11285 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 11286 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 11287 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11288 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11289 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11290 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) | 
|  | 11291 | { | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11292 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11293 | return( ret ); | 
|  | 11294 | } | 
|  | 11295 | #endif | 
|  | 11296 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11297 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11298 | { | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11299 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11300 | { | 
| Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 11301 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11302 | return( ret ); | 
|  | 11303 | } | 
|  | 11304 | } | 
|  | 11305 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11306 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11307 | ret = ssl_write_split( ssl, buf, len ); | 
|  | 11308 | #else | 
|  | 11309 | ret = ssl_write_real( ssl, buf, len ); | 
|  | 11310 | #endif | 
|  | 11311 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 11312 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 11313 |  | 
|  | 11314 | return( ret ); | 
|  | 11315 | } | 
|  | 11316 |  | 
|  | 11317 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11318 | * Notify the peer that the connection is being closed | 
|  | 11319 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11320 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11321 | { | 
|  | 11322 | int ret; | 
|  | 11323 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 11324 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 11325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11326 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11327 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11328 |  | 
| Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 11329 | if( ssl->out_left != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11330 | return( mbedtls_ssl_flush_output( ssl ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11331 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11332 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11333 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11334 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, | 
|  | 11335 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, | 
|  | 11336 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11337 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11338 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11339 | return( ret ); | 
|  | 11340 | } | 
|  | 11341 | } | 
|  | 11342 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11343 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11344 |  | 
| Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 11345 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11346 | } | 
|  | 11347 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11348 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11349 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11350 | if( transform == NULL ) | 
|  | 11351 | return; | 
|  | 11352 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11353 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11354 | deflateEnd( &transform->ctx_deflate ); | 
|  | 11355 | inflateEnd( &transform->ctx_inflate ); | 
|  | 11356 | #endif | 
|  | 11357 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11358 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); | 
|  | 11359 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); | 
| Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 11360 |  | 
| Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 11361 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11362 | mbedtls_md_free( &transform->md_ctx_enc ); | 
|  | 11363 | mbedtls_md_free( &transform->md_ctx_dec ); | 
| Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 11364 | #endif | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11365 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11366 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11367 | } | 
|  | 11368 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11369 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 11370 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11371 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11372 | mbedtls_ssl_key_cert *cur = key_cert, *next; | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11373 |  | 
|  | 11374 | while( cur != NULL ) | 
|  | 11375 | { | 
|  | 11376 | next = cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11377 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11378 | cur = next; | 
|  | 11379 | } | 
|  | 11380 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11381 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11382 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11383 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 11384 |  | 
|  | 11385 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) | 
|  | 11386 | { | 
|  | 11387 | unsigned offset; | 
|  | 11388 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 11389 |  | 
|  | 11390 | if( hs == NULL ) | 
|  | 11391 | return; | 
|  | 11392 |  | 
| Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 11393 | ssl_free_buffered_record( ssl ); | 
|  | 11394 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11395 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11396 | ssl_buffering_free_slot( ssl, offset ); | 
|  | 11397 | } | 
|  | 11398 |  | 
|  | 11399 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, | 
|  | 11400 | uint8_t slot ) | 
|  | 11401 | { | 
|  | 11402 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 11403 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; | 
| Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 11404 |  | 
|  | 11405 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) | 
|  | 11406 | return; | 
|  | 11407 |  | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11408 | if( hs_buf->is_valid == 1 ) | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11409 | { | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11410 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; | 
| Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 11411 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 11412 | mbedtls_free( hs_buf->data ); | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 11413 | mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11414 | } | 
|  | 11415 | } | 
|  | 11416 |  | 
|  | 11417 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 11418 |  | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11419 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11420 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 11421 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 11422 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11423 | if( handshake == NULL ) | 
|  | 11424 | return; | 
|  | 11425 |  | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 11426 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
|  | 11427 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) | 
|  | 11428 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 11429 | ssl->conf->f_async_cancel( ssl ); | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 11430 | handshake->async_in_progress = 0; | 
|  | 11431 | } | 
|  | 11432 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
|  | 11433 |  | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 11434 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 11435 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 11436 | mbedtls_md5_free(    &handshake->fin_md5  ); | 
|  | 11437 | mbedtls_sha1_free(   &handshake->fin_sha1 ); | 
|  | 11438 | #endif | 
|  | 11439 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 11440 | #if defined(MBEDTLS_SHA256_C) | 
|  | 11441 | mbedtls_sha256_free(   &handshake->fin_sha256    ); | 
|  | 11442 | #endif | 
|  | 11443 | #if defined(MBEDTLS_SHA512_C) | 
|  | 11444 | mbedtls_sha512_free(   &handshake->fin_sha512    ); | 
|  | 11445 | #endif | 
|  | 11446 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 11447 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11448 | #if defined(MBEDTLS_DHM_C) | 
|  | 11449 | mbedtls_dhm_free( &handshake->dhm_ctx ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11450 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11451 | #if defined(MBEDTLS_ECDH_C) | 
|  | 11452 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11453 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 11454 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 11455 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 11456 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 11457 | mbedtls_free( handshake->ecjpake_cache ); | 
|  | 11458 | handshake->ecjpake_cache = NULL; | 
|  | 11459 | handshake->ecjpake_cache_len = 0; | 
|  | 11460 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 11461 | #endif | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 11462 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 11463 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 11464 | if( handshake->psk != NULL ) | 
|  | 11465 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11466 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 11467 | mbedtls_free( handshake->psk ); | 
|  | 11468 | } | 
|  | 11469 | #endif | 
|  | 11470 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11471 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 11472 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11473 | /* | 
|  | 11474 | * Free only the linked list wrapper, not the keys themselves | 
|  | 11475 | * since the belong to the SNI callback | 
|  | 11476 | */ | 
|  | 11477 | if( handshake->sni_key_cert != NULL ) | 
|  | 11478 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11479 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11480 |  | 
|  | 11481 | while( cur != NULL ) | 
|  | 11482 | { | 
|  | 11483 | next = cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11484 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 11485 | cur = next; | 
|  | 11486 | } | 
|  | 11487 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11488 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 11489 |  | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 11490 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 11491 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); | 
| Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 11492 | if( handshake->ecrs_peer_cert != NULL ) | 
|  | 11493 | { | 
|  | 11494 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); | 
|  | 11495 | mbedtls_free( handshake->ecrs_peer_cert ); | 
|  | 11496 | } | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 11497 | #endif | 
|  | 11498 |  | 
| Hanno Becker | 3bf8cdf | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 11499 | #if defined(MBEDTLS_X509_CRT_PARSE_C) &&        \ | 
|  | 11500 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) | 
|  | 11501 | mbedtls_pk_free( &handshake->peer_pubkey ); | 
|  | 11502 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ | 
|  | 11503 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11504 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 11505 | mbedtls_free( handshake->verify_cookie ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 11506 | ssl_flight_free( handshake->flight ); | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 11507 | ssl_buffering_free( ssl ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 11508 | #endif | 
|  | 11509 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11510 | mbedtls_platform_zeroize( handshake, | 
|  | 11511 | sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11512 | } | 
|  | 11513 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11514 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11515 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 11516 | if( session == NULL ) | 
|  | 11517 | return; | 
|  | 11518 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11519 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Hanno Becker | 2214159 | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 11520 | ssl_clear_peer_cert( session ); | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 11521 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 11522 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 11523 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11524 | mbedtls_free( session->ticket ); | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 11525 | #endif | 
| Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 11526 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11527 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 11528 | } | 
|  | 11529 |  | 
| Manuel Pégourié-Gonnard | 4c1d06e | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 11530 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) | 
| Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11531 |  | 
|  | 11532 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 11533 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u | 
|  | 11534 | #else | 
|  | 11535 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u | 
|  | 11536 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 11537 |  | 
|  | 11538 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
|  | 11539 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u | 
|  | 11540 | #else | 
|  | 11541 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u | 
|  | 11542 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ | 
|  | 11543 |  | 
|  | 11544 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 11545 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u | 
|  | 11546 | #else | 
|  | 11547 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u | 
|  | 11548 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 11549 |  | 
|  | 11550 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 11551 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u | 
|  | 11552 | #else | 
|  | 11553 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u | 
|  | 11554 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 11555 |  | 
|  | 11556 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT    0 | 
|  | 11557 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT     1 | 
|  | 11558 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT      2 | 
|  | 11559 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                  3 | 
|  | 11560 |  | 
|  | 11561 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG   \ | 
|  | 11562 | ( (uint32_t) (                              \ | 
|  | 11563 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID     << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT     ) | \ | 
|  | 11564 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT      << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT      ) | \ | 
|  | 11565 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY       << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT       ) | \ | 
|  | 11566 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN                   << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT                   ) | \ | 
|  | 11567 | 0u ) ) | 
|  | 11568 |  | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11569 | static unsigned char ssl_serialized_context_header[] = { | 
|  | 11570 | MBEDTLS_VERSION_MAJOR, | 
|  | 11571 | MBEDTLS_VERSION_MINOR, | 
|  | 11572 | MBEDTLS_VERSION_PATCH, | 
|  | 11573 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, | 
|  | 11574 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, | 
| Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11575 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF, | 
|  | 11576 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >>  8 ) & 0xFF, | 
|  | 11577 | ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >>  0 ) & 0xFF, | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11578 | }; | 
|  | 11579 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11580 | /* | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11581 | * Serialize a full SSL context | 
| Manuel Pégourié-Gonnard | b6163ef | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11582 | * | 
|  | 11583 | * The format of the serialized data is: | 
|  | 11584 | * (in the presentation language of TLS, RFC 8446 section 3) | 
|  | 11585 | * | 
|  | 11586 | *  // header | 
|  | 11587 | *  opaque mbedtls_version[3];   // major, minor, patch | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11588 | *  opaque context_format[5];    // version-specific field determining | 
| Manuel Pégourié-Gonnard | b6163ef | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11589 | *                               // the format of the remaining | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11590 | *                               // serialized data. | 
| Manuel Pégourié-Gonnard | a7cd483 | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 11591 | *  Note: When updating the format, remember to keep these | 
|  | 11592 | *        version+format bytes. (We may make their size part of the API.) | 
| Manuel Pégourié-Gonnard | b6163ef | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 11593 | * | 
|  | 11594 | *  // session sub-structure | 
|  | 11595 | *  opaque session<1..2^32-1>;  // see mbedtls_ssl_session_save() | 
|  | 11596 | *  // transform sub-structure | 
|  | 11597 | *  uint8 random[64];           // ServerHello.random+ClientHello.random | 
|  | 11598 | *  uint8 in_cid<0..2^8-1>      // Connection ID: expected incoming value | 
|  | 11599 | *  uint8 out_cid<0..2^8-1>     // Connection ID: outgoing value to use | 
|  | 11600 | *  // fields from ssl_context | 
|  | 11601 | *  uint32 badmac_seen;         // DTLS: number of records with failing MAC | 
|  | 11602 | *  uint64 in_window_top;       // DTLS: last validated record seq_num | 
|  | 11603 | *  uint64 in_window;           // DTLS: bitmask for replay protection | 
|  | 11604 | *  uint8 disable_datagram_packing; // DTLS: only one record per datagram | 
|  | 11605 | *  uint64 cur_out_ctr;         // Record layer: outgoing sequence number | 
|  | 11606 | *  uint16 mtu;                 // DTLS: path mtu (max outgoing fragment size) | 
|  | 11607 | *  uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol | 
|  | 11608 | * | 
|  | 11609 | * Note that many fields of the ssl_context or sub-structures are not | 
|  | 11610 | * serialized, as they fall in one of the following categories: | 
|  | 11611 | * | 
|  | 11612 | *  1. forced value (eg in_left must be 0) | 
|  | 11613 | *  2. pointer to dynamically-allocated memory (eg session, transform) | 
|  | 11614 | *  3. value can be re-derived from other data (eg session keys from MS) | 
|  | 11615 | *  4. value was temporary (eg content of input buffer) | 
|  | 11616 | *  5. value will be provided by the user again (eg I/O callbacks and context) | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11617 | */ | 
|  | 11618 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, | 
|  | 11619 | unsigned char *buf, | 
|  | 11620 | size_t buf_len, | 
|  | 11621 | size_t *olen ) | 
|  | 11622 | { | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11623 | unsigned char *p = buf; | 
|  | 11624 | size_t used = 0; | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11625 | size_t session_len; | 
|  | 11626 | int ret = 0; | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11627 |  | 
| Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11628 | /* | 
| Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11629 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in | 
|  | 11630 | * this function's documentation. | 
|  | 11631 | * | 
|  | 11632 | * These are due to assumptions/limitations in the implementation. Some of | 
|  | 11633 | * them are likely to stay (no handshake in progress) some might go away | 
|  | 11634 | * (only DTLS) but are currently used to simplify the implementation. | 
| Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11635 | */ | 
| Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11636 | /* The initial handshake must be over */ | 
|  | 11637 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | 569ed6b | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 11638 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 69a3e41 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 11639 | if( ssl->handshake != NULL ) | 
|  | 11640 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11641 | /* Double-check that sub-structures are indeed ready */ | 
|  | 11642 | if( ssl->transform == NULL || ssl->session == NULL ) | 
|  | 11643 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11644 | /* There must be no pending incoming or outgoing data */ | 
|  | 11645 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) | 
|  | 11646 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11647 | if( ssl->out_left != 0 ) | 
|  | 11648 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11649 | /* Protocol must be DLTS, not TLS */ | 
|  | 11650 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) | 
|  | 11651 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11652 | /* Version must be 1.2 */ | 
|  | 11653 | if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
|  | 11654 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11655 | if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 11656 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11657 | /* We must be using an AEAD ciphersuite */ | 
|  | 11658 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) | 
|  | 11659 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11660 | /* Renegotiation must not be enabled */ | 
|  | 11661 | if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) | 
|  | 11662 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11663 |  | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11664 | /* | 
|  | 11665 | * Version and format identifier | 
|  | 11666 | */ | 
|  | 11667 | used += sizeof( ssl_serialized_context_header ); | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11668 |  | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11669 | if( used <= buf_len ) | 
|  | 11670 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 11671 | mbedtls_platform_memcpy( p, ssl_serialized_context_header, | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11672 | sizeof( ssl_serialized_context_header ) ); | 
|  | 11673 | p += sizeof( ssl_serialized_context_header ); | 
|  | 11674 | } | 
|  | 11675 |  | 
|  | 11676 | /* | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11677 | * Session (length + data) | 
|  | 11678 | */ | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11679 | ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len ); | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11680 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) | 
|  | 11681 | return( ret ); | 
|  | 11682 |  | 
|  | 11683 | used += 4 + session_len; | 
|  | 11684 | if( used <= buf_len ) | 
|  | 11685 | { | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 11686 | p = mbedtls_platform_put_uint32_be( p, session_len ); | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11687 |  | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11688 | ret = ssl_session_save( ssl->session, 1, | 
|  | 11689 | p, session_len, &session_len ); | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11690 | if( ret != 0 ) | 
|  | 11691 | return( ret ); | 
|  | 11692 |  | 
|  | 11693 | p += session_len; | 
|  | 11694 | } | 
|  | 11695 |  | 
|  | 11696 | /* | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11697 | * Transform | 
|  | 11698 | */ | 
|  | 11699 | used += sizeof( ssl->transform->randbytes ); | 
|  | 11700 | if( used <= buf_len ) | 
|  | 11701 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 11702 | mbedtls_platform_memcpy( p, ssl->transform->randbytes, | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11703 | sizeof( ssl->transform->randbytes ) ); | 
|  | 11704 | p += sizeof( ssl->transform->randbytes ); | 
|  | 11705 | } | 
|  | 11706 |  | 
|  | 11707 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 11708 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; | 
|  | 11709 | if( used <= buf_len ) | 
|  | 11710 | { | 
|  | 11711 | *p++ = ssl->transform->in_cid_len; | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 11712 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 11713 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11714 | p += ssl->transform->in_cid_len; | 
|  | 11715 |  | 
|  | 11716 | *p++ = ssl->transform->out_cid_len; | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 11717 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 11718 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11719 | p += ssl->transform->out_cid_len; | 
|  | 11720 | } | 
|  | 11721 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 11722 |  | 
|  | 11723 | /* | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11724 | * Saved fields from top-level ssl_context structure | 
|  | 11725 | */ | 
|  | 11726 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
|  | 11727 | used += 4; | 
|  | 11728 | if( used <= buf_len ) | 
|  | 11729 | { | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 11730 | p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen ); | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11731 | } | 
|  | 11732 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ | 
|  | 11733 |  | 
|  | 11734 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 11735 | used += 16; | 
|  | 11736 | if( used <= buf_len ) | 
|  | 11737 | { | 
|  | 11738 | *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF ); | 
|  | 11739 | *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF ); | 
|  | 11740 | *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF ); | 
|  | 11741 | *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF ); | 
|  | 11742 | *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF ); | 
|  | 11743 | *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF ); | 
|  | 11744 | *p++ = (unsigned char)( ( ssl->in_window_top >>  8 ) & 0xFF ); | 
|  | 11745 | *p++ = (unsigned char)( ( ssl->in_window_top       ) & 0xFF ); | 
|  | 11746 |  | 
|  | 11747 | *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF ); | 
|  | 11748 | *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF ); | 
|  | 11749 | *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF ); | 
|  | 11750 | *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF ); | 
|  | 11751 | *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF ); | 
|  | 11752 | *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF ); | 
|  | 11753 | *p++ = (unsigned char)( ( ssl->in_window >>  8 ) & 0xFF ); | 
|  | 11754 | *p++ = (unsigned char)( ( ssl->in_window       ) & 0xFF ); | 
|  | 11755 | } | 
|  | 11756 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 11757 |  | 
|  | 11758 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 11759 | used += 1; | 
|  | 11760 | if( used <= buf_len ) | 
|  | 11761 | { | 
|  | 11762 | *p++ = ssl->disable_datagram_packing; | 
|  | 11763 | } | 
|  | 11764 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 11765 |  | 
|  | 11766 | used += 8; | 
|  | 11767 | if( used <= buf_len ) | 
|  | 11768 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 11769 | mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 ); | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11770 | p += 8; | 
|  | 11771 | } | 
|  | 11772 |  | 
|  | 11773 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 11774 | used += 2; | 
|  | 11775 | if( used <= buf_len ) | 
|  | 11776 | { | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 11777 | p = mbedtls_platform_put_uint16_be( p, ssl->mtu ); | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11778 | } | 
|  | 11779 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 11780 |  | 
|  | 11781 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 11782 | { | 
|  | 11783 | const uint8_t alpn_len = ssl->alpn_chosen | 
| Manuel Pégourié-Gonnard | 7af7375 | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 11784 | ? (uint8_t) strlen( ssl->alpn_chosen ) | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11785 | : 0; | 
|  | 11786 |  | 
|  | 11787 | used += 1 + alpn_len; | 
|  | 11788 | if( used <= buf_len ) | 
|  | 11789 | { | 
|  | 11790 | *p++ = alpn_len; | 
|  | 11791 |  | 
|  | 11792 | if( ssl->alpn_chosen != NULL ) | 
|  | 11793 | { | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 11794 | mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len ); | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11795 | p += alpn_len; | 
|  | 11796 | } | 
|  | 11797 | } | 
|  | 11798 | } | 
|  | 11799 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 11800 |  | 
|  | 11801 | /* | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11802 | * Done | 
|  | 11803 | */ | 
|  | 11804 | *olen = used; | 
|  | 11805 |  | 
|  | 11806 | if( used > buf_len ) | 
|  | 11807 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11808 |  | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11809 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); | 
|  | 11810 |  | 
| Manuel Pégourié-Gonnard | bc847ca | 2019-07-23 14:51:09 +0200 | [diff] [blame] | 11811 | return( ssl_session_reset_int( ssl, 0 ) ); | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11812 | } | 
|  | 11813 |  | 
|  | 11814 | /* | 
| Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 11815 | * Deserialize context, see mbedtls_ssl_context_save() for format. | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11816 | * | 
|  | 11817 | * This internal version is wrapped by a public function that cleans up in | 
|  | 11818 | * case of error. | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11819 | */ | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11820 | static int ssl_context_load( mbedtls_ssl_context *ssl, | 
|  | 11821 | const unsigned char *buf, | 
|  | 11822 | size_t len ) | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 11823 | { | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11824 | const unsigned char *p = buf; | 
|  | 11825 | const unsigned char * const end = buf + len; | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11826 | size_t session_len; | 
|  | 11827 | int ret; | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11828 |  | 
| Manuel Pégourié-Gonnard | 5e534ba | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11829 | /* | 
|  | 11830 | * The context should have been freshly setup or reset. | 
|  | 11831 | * Give the user an error in case of obvious misuse. | 
| Manuel Pégourié-Gonnard | 14e2a8a | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 11832 | * (Checking session is useful because it won't be NULL if we're | 
| Manuel Pégourié-Gonnard | 5e534ba | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11833 | * renegotiating, or if the user mistakenly loaded a session first.) | 
|  | 11834 | */ | 
|  | 11835 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || | 
|  | 11836 | ssl->session != NULL ) | 
|  | 11837 | { | 
|  | 11838 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11839 | } | 
|  | 11840 |  | 
|  | 11841 | /* | 
|  | 11842 | * We can't check that the config matches the initial one, but we can at | 
|  | 11843 | * least check it matches the requirements for serializing. | 
|  | 11844 | */ | 
|  | 11845 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 11846 | mbedtls_ssl_ver_lt( | 
|  | 11847 | mbedtls_ssl_conf_get_max_major_ver( ssl->conf ), | 
|  | 11848 | MBEDTLS_SSL_MAJOR_VERSION_3 ) || | 
|  | 11849 | mbedtls_ssl_ver_gt( | 
|  | 11850 | mbedtls_ssl_conf_get_min_major_ver( ssl->conf ), | 
|  | 11851 | MBEDTLS_SSL_MAJOR_VERSION_3 ) || | 
|  | 11852 | mbedtls_ssl_ver_lt( | 
|  | 11853 | mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ), | 
|  | 11854 | MBEDTLS_SSL_MINOR_VERSION_3 ) || | 
|  | 11855 | mbedtls_ssl_ver_gt( | 
|  | 11856 | mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ), | 
|  | 11857 | MBEDTLS_SSL_MINOR_VERSION_3 ) || | 
| Manuel Pégourié-Gonnard | 18332c5 | 2019-07-29 12:17:52 +0200 | [diff] [blame] | 11858 | mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) ) | 
| Manuel Pégourié-Gonnard | 5e534ba | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 11859 | { | 
|  | 11860 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11861 | } | 
|  | 11862 |  | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11863 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); | 
|  | 11864 |  | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11865 | /* | 
|  | 11866 | * Check version identifier | 
|  | 11867 | */ | 
|  | 11868 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) | 
|  | 11869 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11870 |  | 
| Teppo Järvelin | 650343c | 2019-10-03 15:36:59 +0300 | [diff] [blame] | 11871 | // use regular memcmp as header is not that critical | 
|  | 11872 | if( memcmp( p, ssl_serialized_context_header, | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 11873 | sizeof( ssl_serialized_context_header ) ) != 0 ) | 
|  | 11874 | { | 
|  | 11875 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); | 
|  | 11876 | } | 
|  | 11877 | p += sizeof( ssl_serialized_context_header ); | 
|  | 11878 |  | 
|  | 11879 | /* | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11880 | * Session | 
|  | 11881 | */ | 
|  | 11882 | if( (size_t)( end - p ) < 4 ) | 
|  | 11883 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11884 |  | 
| Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 11885 | session_len = mbedtls_platform_get_uint32_be( p ); | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11886 | p += 4; | 
|  | 11887 |  | 
| Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11888 | /* This has been allocated by ssl_handshake_init(), called by | 
|  | 11889 | * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */ | 
|  | 11890 | ssl->session = ssl->session_negotiate; | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11891 | ssl->session_in = ssl->session; | 
|  | 11892 | ssl->session_out = ssl->session; | 
| Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11893 | ssl->session_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11894 |  | 
|  | 11895 | if( (size_t)( end - p ) < session_len ) | 
|  | 11896 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11897 |  | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11898 | ret = ssl_session_load( ssl->session, 1, p, session_len ); | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11899 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11900 | { | 
|  | 11901 | mbedtls_ssl_session_free( ssl->session ); | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11902 | return( ret ); | 
| Manuel Pégourié-Gonnard | 7ce9446 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 11903 | } | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 11904 |  | 
|  | 11905 | p += session_len; | 
|  | 11906 |  | 
|  | 11907 | /* | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11908 | * Transform | 
|  | 11909 | */ | 
|  | 11910 |  | 
| Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11911 | /* This has been allocated by ssl_handshake_init(), called by | 
|  | 11912 | * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */ | 
|  | 11913 | ssl->transform = ssl->transform_negotiate; | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11914 | ssl->transform_in = ssl->transform; | 
|  | 11915 | ssl->transform_out = ssl->transform; | 
| Manuel Pégourié-Gonnard | ff22200 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 11916 | ssl->transform_negotiate = NULL; | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11917 |  | 
|  | 11918 | /* Read random bytes and populate structure */ | 
|  | 11919 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) | 
|  | 11920 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11921 |  | 
|  | 11922 | ret = ssl_populate_transform( ssl->transform, | 
|  | 11923 | mbedtls_ssl_session_get_ciphersuite( ssl->session ), | 
|  | 11924 | ssl->session->master, | 
|  | 11925 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) | 
|  | 11926 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 11927 | ssl->session->encrypt_then_mac, | 
|  | 11928 | #endif | 
|  | 11929 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
|  | 11930 | ssl->session->trunc_hmac, | 
|  | 11931 | #endif | 
|  | 11932 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ | 
|  | 11933 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 11934 | ssl->session->compression, | 
|  | 11935 | #endif | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11936 | p, /* currently pointing to randbytes */ | 
|  | 11937 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ | 
|  | 11938 | mbedtls_ssl_conf_get_endpoint( ssl->conf ), | 
|  | 11939 | ssl ); | 
|  | 11940 | if( ret != 0 ) | 
|  | 11941 | return( ret ); | 
|  | 11942 |  | 
|  | 11943 | p += sizeof( ssl->transform->randbytes ); | 
|  | 11944 |  | 
|  | 11945 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) | 
|  | 11946 | /* Read connection IDs and store them */ | 
|  | 11947 | if( (size_t)( end - p ) < 1 ) | 
|  | 11948 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11949 |  | 
|  | 11950 | ssl->transform->in_cid_len = *p++; | 
|  | 11951 |  | 
| Manuel Pégourié-Gonnard | 2f3fa62 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 11952 | if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u ) | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11953 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11954 |  | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 11955 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 11956 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11957 | p += ssl->transform->in_cid_len; | 
|  | 11958 |  | 
|  | 11959 | ssl->transform->out_cid_len = *p++; | 
|  | 11960 |  | 
|  | 11961 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) | 
|  | 11962 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11963 |  | 
| Teppo Järvelin | 6f4e030 | 2019-10-04 13:53:53 +0300 | [diff] [blame] | 11964 | /* Not using more secure mbedtls_platform_memcpy as cid is public */ | 
|  | 11965 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); | 
| Manuel Pégourié-Gonnard | 322f3c7 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 11966 | p += ssl->transform->out_cid_len; | 
|  | 11967 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ | 
|  | 11968 |  | 
|  | 11969 | /* | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11970 | * Saved fields from top-level ssl_context structure | 
|  | 11971 | */ | 
|  | 11972 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
|  | 11973 | if( (size_t)( end - p ) < 4 ) | 
|  | 11974 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11975 |  | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 11976 | ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p ); | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 11977 | p += 4; | 
|  | 11978 | #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */ | 
|  | 11979 |  | 
|  | 11980 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 11981 | if( (size_t)( end - p ) < 16 ) | 
|  | 11982 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 11983 |  | 
|  | 11984 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | | 
|  | 11985 | ( (uint64_t) p[1] << 48 ) | | 
|  | 11986 | ( (uint64_t) p[2] << 40 ) | | 
|  | 11987 | ( (uint64_t) p[3] << 32 ) | | 
|  | 11988 | ( (uint64_t) p[4] << 24 ) | | 
|  | 11989 | ( (uint64_t) p[5] << 16 ) | | 
|  | 11990 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 11991 | ( (uint64_t) p[7]       ); | 
|  | 11992 | p += 8; | 
|  | 11993 |  | 
|  | 11994 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | | 
|  | 11995 | ( (uint64_t) p[1] << 48 ) | | 
|  | 11996 | ( (uint64_t) p[2] << 40 ) | | 
|  | 11997 | ( (uint64_t) p[3] << 32 ) | | 
|  | 11998 | ( (uint64_t) p[4] << 24 ) | | 
|  | 11999 | ( (uint64_t) p[5] << 16 ) | | 
|  | 12000 | ( (uint64_t) p[6] <<  8 ) | | 
|  | 12001 | ( (uint64_t) p[7]       ); | 
|  | 12002 | p += 8; | 
|  | 12003 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
|  | 12004 |  | 
|  | 12005 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 12006 | if( (size_t)( end - p ) < 1 ) | 
|  | 12007 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 12008 |  | 
|  | 12009 | ssl->disable_datagram_packing = *p++; | 
|  | 12010 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 12011 |  | 
|  | 12012 | if( (size_t)( end - p ) < 8 ) | 
|  | 12013 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 12014 |  | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 12015 | mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 ); | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 12016 | p += 8; | 
|  | 12017 |  | 
|  | 12018 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 12019 | if( (size_t)( end - p ) < 2 ) | 
|  | 12020 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 12021 | ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p ); | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 12022 | p += 2; | 
|  | 12023 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 12024 |  | 
|  | 12025 | #if defined(MBEDTLS_SSL_ALPN) | 
|  | 12026 | { | 
|  | 12027 | uint8_t alpn_len; | 
|  | 12028 | const char **cur; | 
|  | 12029 |  | 
|  | 12030 | if( (size_t)( end - p ) < 1 ) | 
|  | 12031 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 12032 |  | 
|  | 12033 | alpn_len = *p++; | 
|  | 12034 |  | 
|  | 12035 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) | 
|  | 12036 | { | 
|  | 12037 | /* alpn_chosen should point to an item in the configured list */ | 
|  | 12038 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) | 
|  | 12039 | { | 
|  | 12040 | if( strlen( *cur ) == alpn_len && | 
| Teppo Järvelin | 61f412e | 2019-10-03 12:25:22 +0300 | [diff] [blame] | 12041 | mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 ) | 
| Manuel Pégourié-Gonnard | 16d1485 | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 12042 | { | 
|  | 12043 | ssl->alpn_chosen = *cur; | 
|  | 12044 | break; | 
|  | 12045 | } | 
|  | 12046 | } | 
|  | 12047 | } | 
|  | 12048 |  | 
|  | 12049 | /* can only happen on conf mismatch */ | 
|  | 12050 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) | 
|  | 12051 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 12052 |  | 
|  | 12053 | p += alpn_len; | 
|  | 12054 | } | 
|  | 12055 | #endif /* MBEDTLS_SSL_ALPN */ | 
|  | 12056 |  | 
|  | 12057 | /* | 
| Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 12058 | * Forced fields from top-level ssl_context structure | 
|  | 12059 | * | 
|  | 12060 | * Most of them already set to the correct value by mbedtls_ssl_init() and | 
|  | 12061 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. | 
|  | 12062 | */ | 
|  | 12063 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; | 
|  | 12064 |  | 
| Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 12065 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER) | 
| Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 12066 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
| Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 12067 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */ | 
|  | 12068 | #if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER) | 
| Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 12069 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; | 
| Manuel Pégourié-Gonnard | 2cc9223 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 12070 | #endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */ | 
| Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 12071 |  | 
| Hanno Becker | 8398582 | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 12072 | /* Adjust pointers for header fields of outgoing records to | 
|  | 12073 | * the given transform, accounting for explicit IV and CID. */ | 
|  | 12074 | ssl_update_out_pointers( ssl, ssl->transform ); | 
|  | 12075 |  | 
| Manuel Pégourié-Gonnard | 138079d | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 12076 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 12077 | ssl->in_epoch = 1; | 
|  | 12078 | #endif | 
|  | 12079 |  | 
|  | 12080 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, | 
|  | 12081 | * which we don't want - otherwise we'd end up freeing the wrong transform | 
|  | 12082 | * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */ | 
|  | 12083 | if( ssl->handshake != NULL ) | 
|  | 12084 | { | 
|  | 12085 | mbedtls_ssl_handshake_free( ssl ); | 
|  | 12086 | mbedtls_free( ssl->handshake ); | 
|  | 12087 | ssl->handshake = NULL; | 
|  | 12088 | } | 
|  | 12089 |  | 
|  | 12090 | /* | 
| Manuel Pégourié-Gonnard | d0dd104 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 12091 | * Done - should have consumed entire buffer | 
|  | 12092 | */ | 
|  | 12093 | if( p != end ) | 
|  | 12094 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 12095 |  | 
|  | 12096 | return( 0 ); | 
|  | 12097 | } | 
|  | 12098 |  | 
|  | 12099 | /* | 
| Manuel Pégourié-Gonnard | 8175816 | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 12100 | * Deserialize context: public wrapper for error cleaning | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 12101 | */ | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 12102 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 12103 | const unsigned char *buf, | 
|  | 12104 | size_t len ) | 
|  | 12105 | { | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 12106 | int ret = ssl_context_load( context, buf, len ); | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 12107 |  | 
| Manuel Pégourié-Gonnard | f1f3e52 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 12108 | if( ret != 0 ) | 
|  | 12109 | mbedtls_ssl_free( context ); | 
|  | 12110 |  | 
|  | 12111 | return( ret ); | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 12112 | } | 
| Manuel Pégourié-Gonnard | 4c1d06e | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 12113 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ | 
| Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 12114 |  | 
|  | 12115 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12116 | * Free an SSL context | 
|  | 12117 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12118 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12119 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 12120 | if( ssl == NULL ) | 
|  | 12121 | return; | 
|  | 12122 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12123 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12124 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 12125 | if( ssl->out_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12126 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 12127 | mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12128 | mbedtls_free( ssl->out_buf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12129 | } | 
|  | 12130 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 12131 | if( ssl->in_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12132 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 12133 | mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12134 | mbedtls_free( ssl->in_buf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12135 | } | 
|  | 12136 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12137 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 12138 | if( ssl->compress_buf != NULL ) | 
|  | 12139 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 12140 | mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12141 | mbedtls_free( ssl->compress_buf ); | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 12142 | } | 
|  | 12143 | #endif | 
|  | 12144 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 12145 | if( ssl->transform ) | 
|  | 12146 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12147 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 12148 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 12149 | } | 
|  | 12150 |  | 
|  | 12151 | if( ssl->handshake ) | 
|  | 12152 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 12153 | mbedtls_ssl_handshake_free( ssl ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12154 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
|  | 12155 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 12156 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12157 | mbedtls_free( ssl->handshake ); | 
|  | 12158 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 12159 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 12160 | } | 
|  | 12161 |  | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 12162 | if( ssl->session ) | 
|  | 12163 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12164 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 12165 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 12166 | } | 
|  | 12167 |  | 
| Teppo Järvelin | 4009d8f | 2019-08-19 14:48:09 +0300 | [diff] [blame] | 12168 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 12169 | if( ssl->hostname != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12170 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12171 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12172 | mbedtls_free( ssl->hostname ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12173 | } | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 12174 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12175 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12176 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 12177 | if( mbedtls_ssl_hw_record_finish != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 12178 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12179 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); | 
|  | 12180 | mbedtls_ssl_hw_record_finish( ssl ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 12181 | } | 
|  | 12182 | #endif | 
|  | 12183 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 12184 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12185 | mbedtls_free( ssl->cli_id ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 12186 | #endif | 
|  | 12187 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12188 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); | 
| Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 12189 |  | 
| Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 12190 | /* Actually clear after last debug message */ | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12191 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 12192 | } | 
|  | 12193 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12194 | /* | 
|  | 12195 | * Initialze mbedtls_ssl_config | 
|  | 12196 | */ | 
|  | 12197 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) | 
|  | 12198 | { | 
|  | 12199 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); | 
| Manuel Pégourié-Gonnard | e744eab | 2019-03-18 10:51:18 +0100 | [diff] [blame] | 12200 |  | 
|  | 12201 | #if !defined(MBEDTLS_SSL_PROTO_TLS) | 
|  | 12202 | conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; | 
|  | 12203 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12204 | } | 
|  | 12205 |  | 
| Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 12206 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12207 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 12208 | static int ssl_preset_default_hashes[] = { | 
|  | 12209 | #if defined(MBEDTLS_SHA512_C) | 
|  | 12210 | MBEDTLS_MD_SHA512, | 
|  | 12211 | MBEDTLS_MD_SHA384, | 
|  | 12212 | #endif | 
|  | 12213 | #if defined(MBEDTLS_SHA256_C) | 
|  | 12214 | MBEDTLS_MD_SHA256, | 
|  | 12215 | MBEDTLS_MD_SHA224, | 
|  | 12216 | #endif | 
| Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 12217 | #if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 12218 | MBEDTLS_MD_SHA1, | 
|  | 12219 | #endif | 
|  | 12220 | MBEDTLS_MD_NONE | 
|  | 12221 | }; | 
| Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 12222 | #endif | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12223 | #endif | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 12224 |  | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 12225 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12226 | static int ssl_preset_suiteb_ciphersuites[] = { | 
|  | 12227 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 
|  | 12228 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | 
|  | 12229 | 0 | 
|  | 12230 | }; | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 12231 | #endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12232 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12233 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12234 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12235 | static int ssl_preset_suiteb_hashes[] = { | 
|  | 12236 | MBEDTLS_MD_SHA256, | 
|  | 12237 | MBEDTLS_MD_SHA384, | 
|  | 12238 | MBEDTLS_MD_NONE | 
|  | 12239 | }; | 
|  | 12240 | #endif | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12241 | #endif | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12242 |  | 
| Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 12243 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12244 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { | 
| Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 12245 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12246 | MBEDTLS_ECP_DP_SECP256R1, | 
| Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 12247 | #endif | 
|  | 12248 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12249 | MBEDTLS_ECP_DP_SECP384R1, | 
| Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 12250 | #endif | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12251 | MBEDTLS_ECP_DP_NONE | 
|  | 12252 | }; | 
|  | 12253 | #endif | 
|  | 12254 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12255 | /* | 
| Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 12256 | * Load default in mbedtls_ssl_config | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12257 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 12258 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12259 | int endpoint, int transport, int preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12260 | { | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 12261 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12262 | int ret; | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 12263 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12264 |  | 
| Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 12265 | /* Use the functions here so that they are covered in tests, | 
|  | 12266 | * but otherwise access member directly for efficiency */ | 
|  | 12267 | mbedtls_ssl_conf_endpoint( conf, endpoint ); | 
|  | 12268 | mbedtls_ssl_conf_transport( conf, transport ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12269 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12270 | /* | 
|  | 12271 | * Things that are common to all presets | 
|  | 12272 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 12273 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 12274 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 12275 | { | 
| Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 12276 | #if !defined(MBEDTLS_SSL_CONF_AUTHMODE) | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 12277 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; | 
| Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 12278 | #endif /* !MBEDTLS_SSL_CONF_AUTHMODE */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 12279 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 12280 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; | 
|  | 12281 | #endif | 
|  | 12282 | } | 
|  | 12283 | #endif | 
|  | 12284 |  | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 12285 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12286 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 12287 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12288 |  | 
|  | 12289 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 12290 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
|  | 12291 | #endif | 
|  | 12292 |  | 
|  | 12293 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 12294 | #if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12295 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
| Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 12296 | #endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */ | 
|  | 12297 | #if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET) | 
| Jarno Lamsa | d9382f8 | 2019-06-10 10:27:14 +0300 | [diff] [blame] | 12298 | conf->enforce_extended_master_secret = | 
| Jarno Lamsa | 18b9a49 | 2019-06-10 15:23:29 +0300 | [diff] [blame] | 12299 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED; | 
| Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 12300 | #endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12301 | #endif | 
|  | 12302 |  | 
| Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 12303 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
|  | 12304 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; | 
|  | 12305 | #endif | 
|  | 12306 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 12307 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12308 | conf->f_cookie_write = ssl_cookie_write_dummy; | 
|  | 12309 | conf->f_cookie_check = ssl_cookie_check_dummy; | 
|  | 12310 | #endif | 
|  | 12311 |  | 
| Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 12312 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \ | 
|  | 12313 | !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12314 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; | 
|  | 12315 | #endif | 
|  | 12316 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 12317 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 12318 | #if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST) | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 12319 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; | 
| Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 12320 | #endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */ | 
|  | 12321 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 12322 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12323 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 12324 | #if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12325 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 12326 | #endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */ | 
|  | 12327 | #if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12328 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; | 
| Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 12329 | #endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */ | 
|  | 12330 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12331 |  | 
|  | 12332 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 12333 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 12334 | mbedtls_platform_memset( conf->renego_period,     0x00, 2 ); | 
|  | 12335 | mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12336 | #endif | 
|  | 12337 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12338 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
|  | 12339 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 12340 | { | 
| Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 12341 | const unsigned char dhm_p[] = | 
|  | 12342 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; | 
|  | 12343 | const unsigned char dhm_g[] = | 
|  | 12344 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; | 
|  | 12345 |  | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 12346 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, | 
|  | 12347 | dhm_p, sizeof( dhm_p ), | 
|  | 12348 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12349 | { | 
|  | 12350 | return( ret ); | 
|  | 12351 | } | 
|  | 12352 | } | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 12353 | #endif | 
|  | 12354 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12355 | /* | 
|  | 12356 | * Preset-specific defaults | 
|  | 12357 | */ | 
|  | 12358 | switch( preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12359 | { | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12360 | /* | 
|  | 12361 | * NSA Suite B | 
|  | 12362 | */ | 
|  | 12363 | case MBEDTLS_SSL_PRESET_SUITEB: | 
| Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 12364 | #if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12365 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
| Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 12366 | #endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ | 
|  | 12367 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12368 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ | 
| Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 12369 | #endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */ | 
|  | 12370 | #if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12371 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
| Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 12372 | #endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ | 
|  | 12373 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12374 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
| Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 12375 | #endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12376 |  | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 12377 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 12378 | conf->ciphersuite_list[0] = | 
|  | 12379 | conf->ciphersuite_list[1] = | 
|  | 12380 | conf->ciphersuite_list[2] = | 
|  | 12381 | conf->ciphersuite_list[3] = | 
|  | 12382 | ssl_preset_suiteb_ciphersuites; | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 12383 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12384 |  | 
|  | 12385 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 12386 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12387 | #endif | 
|  | 12388 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12389 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12390 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12391 | conf->sig_hashes = ssl_preset_suiteb_hashes; | 
|  | 12392 | #endif | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12393 | #endif | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12394 |  | 
|  | 12395 | #if defined(MBEDTLS_ECP_C) | 
| Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 12396 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12397 | conf->curve_list = ssl_preset_suiteb_curves; | 
|  | 12398 | #endif | 
| Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 12399 | #endif | 
| Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 12400 | break; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12401 |  | 
|  | 12402 | /* | 
|  | 12403 | * Default | 
|  | 12404 | */ | 
|  | 12405 | default: | 
| Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 12406 | #if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER) | 
| Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 12407 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > | 
|  | 12408 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? | 
|  | 12409 | MBEDTLS_SSL_MIN_MAJOR_VERSION : | 
|  | 12410 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; | 
| Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 12411 | #endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */ | 
|  | 12412 | #if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) | 
| Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 12413 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > | 
|  | 12414 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? | 
|  | 12415 | MBEDTLS_SSL_MIN_MINOR_VERSION : | 
|  | 12416 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12417 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 12418 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12419 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; | 
|  | 12420 | #endif | 
| Hanno Becker | e965bd3 | 2019-06-12 14:04:34 +0100 | [diff] [blame] | 12421 | #endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */ | 
|  | 12422 | #if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER) | 
|  | 12423 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
|  | 12424 | #endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */ | 
|  | 12425 | #if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) | 
|  | 12426 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
|  | 12427 | #endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12428 |  | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 12429 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE) | 
| Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 12430 | conf->ciphersuite_list[0] = | 
|  | 12431 | conf->ciphersuite_list[1] = | 
|  | 12432 | conf->ciphersuite_list[2] = | 
|  | 12433 | conf->ciphersuite_list[3] = | 
|  | 12434 | mbedtls_ssl_list_ciphersuites(); | 
| Hanno Becker | 73f4cb1 | 2019-06-27 13:51:07 +0100 | [diff] [blame] | 12435 | #endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */ | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12436 |  | 
|  | 12437 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 12438 | conf->cert_profile = &mbedtls_x509_crt_profile_default; | 
|  | 12439 | #endif | 
|  | 12440 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12441 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12442 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 12443 | conf->sig_hashes = ssl_preset_default_hashes; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12444 | #endif | 
| Hanno Becker | 56595f4 | 2019-06-19 16:31:38 +0100 | [diff] [blame] | 12445 | #endif | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12446 |  | 
|  | 12447 | #if defined(MBEDTLS_ECP_C) | 
| Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 12448 | #if !defined(MBEDTLS_SSL_CONF_SINGLE_EC) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12449 | conf->curve_list = mbedtls_ecp_grp_id_list(); | 
|  | 12450 | #endif | 
| Hanno Becker | c1096e7 | 2019-06-19 12:30:41 +0100 | [diff] [blame] | 12451 | #endif | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 12452 |  | 
|  | 12453 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 12454 | conf->dhm_min_bitlen = 1024; | 
|  | 12455 | #endif | 
|  | 12456 | } | 
|  | 12457 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12458 | return( 0 ); | 
|  | 12459 | } | 
|  | 12460 |  | 
|  | 12461 | /* | 
|  | 12462 | * Free mbedtls_ssl_config | 
|  | 12463 | */ | 
|  | 12464 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) | 
|  | 12465 | { | 
|  | 12466 | #if defined(MBEDTLS_DHM_C) | 
|  | 12467 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 12468 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 12469 | #endif | 
|  | 12470 |  | 
|  | 12471 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 12472 | if( conf->psk != NULL ) | 
|  | 12473 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12474 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12475 | mbedtls_free( conf->psk ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 12476 | conf->psk = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12477 | conf->psk_len = 0; | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 12478 | } | 
|  | 12479 |  | 
|  | 12480 | if( conf->psk_identity != NULL ) | 
|  | 12481 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12482 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 12483 | mbedtls_free( conf->psk_identity ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 12484 | conf->psk_identity = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12485 | conf->psk_identity_len = 0; | 
|  | 12486 | } | 
|  | 12487 | #endif | 
|  | 12488 |  | 
|  | 12489 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 12490 | ssl_key_cert_free( conf->key_cert ); | 
|  | 12491 | #endif | 
|  | 12492 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 12493 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 12494 | } | 
|  | 12495 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 12496 | #if defined(MBEDTLS_PK_C) && \ | 
| Jarno Lamsa | 7cb5c11 | 2019-04-23 15:54:56 +0300 | [diff] [blame] | 12497 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \ | 
|  | 12498 | ( defined(MBEDTLS_USE_TINYCRYPT) ) | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12499 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12500 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12501 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12502 | unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk ) | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12503 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12504 | #if defined(MBEDTLS_RSA_C) | 
|  | 12505 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) | 
|  | 12506 | return( MBEDTLS_SSL_SIG_RSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12507 | #endif | 
| Jarno Lamsa | 7cb5c11 | 2019-04-23 15:54:56 +0300 | [diff] [blame] | 12508 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12509 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) | 
|  | 12510 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12511 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12512 | return( MBEDTLS_SSL_SIG_ANON ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 12513 | } | 
|  | 12514 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 12515 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) | 
|  | 12516 | { | 
|  | 12517 | switch( type ) { | 
|  | 12518 | case MBEDTLS_PK_RSA: | 
|  | 12519 | return( MBEDTLS_SSL_SIG_RSA ); | 
|  | 12520 | case MBEDTLS_PK_ECDSA: | 
|  | 12521 | case MBEDTLS_PK_ECKEY: | 
|  | 12522 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
|  | 12523 | default: | 
|  | 12524 | return( MBEDTLS_SSL_SIG_ANON ); | 
|  | 12525 | } | 
|  | 12526 | } | 
|  | 12527 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12528 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig ) | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12529 | { | 
|  | 12530 | switch( sig ) | 
|  | 12531 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12532 | #if defined(MBEDTLS_RSA_C) | 
|  | 12533 | case MBEDTLS_SSL_SIG_RSA: | 
|  | 12534 | return( MBEDTLS_PK_RSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12535 | #endif | 
| Jarno Lamsa | 7cb5c11 | 2019-04-23 15:54:56 +0300 | [diff] [blame] | 12536 | #if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12537 | case MBEDTLS_SSL_SIG_ECDSA: | 
|  | 12538 | return( MBEDTLS_PK_ECDSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12539 | #endif | 
|  | 12540 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12541 | return( MBEDTLS_PK_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12542 | } | 
|  | 12543 | } | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 12544 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12545 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 12546 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 12547 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 12548 |  | 
|  | 12549 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ | 
|  | 12550 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 12551 | mbedtls_pk_type_t sig_alg ) | 
|  | 12552 | { | 
|  | 12553 | switch( sig_alg ) | 
|  | 12554 | { | 
|  | 12555 | case MBEDTLS_PK_RSA: | 
|  | 12556 | return( set->rsa ); | 
|  | 12557 | case MBEDTLS_PK_ECDSA: | 
|  | 12558 | return( set->ecdsa ); | 
|  | 12559 | default: | 
|  | 12560 | return( MBEDTLS_MD_NONE ); | 
|  | 12561 | } | 
|  | 12562 | } | 
|  | 12563 |  | 
|  | 12564 | /* Add a signature-hash-pair to a signature-hash set */ | 
|  | 12565 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 12566 | mbedtls_pk_type_t sig_alg, | 
|  | 12567 | mbedtls_md_type_t md_alg ) | 
|  | 12568 | { | 
|  | 12569 | switch( sig_alg ) | 
|  | 12570 | { | 
|  | 12571 | case MBEDTLS_PK_RSA: | 
|  | 12572 | if( set->rsa == MBEDTLS_MD_NONE ) | 
|  | 12573 | set->rsa = md_alg; | 
|  | 12574 | break; | 
|  | 12575 |  | 
|  | 12576 | case MBEDTLS_PK_ECDSA: | 
|  | 12577 | if( set->ecdsa == MBEDTLS_MD_NONE ) | 
|  | 12578 | set->ecdsa = md_alg; | 
|  | 12579 | break; | 
|  | 12580 |  | 
|  | 12581 | default: | 
|  | 12582 | break; | 
|  | 12583 | } | 
|  | 12584 | } | 
|  | 12585 |  | 
|  | 12586 | /* Allow exactly one hash algorithm for each signature. */ | 
|  | 12587 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 12588 | mbedtls_md_type_t md_alg ) | 
|  | 12589 | { | 
|  | 12590 | set->rsa   = md_alg; | 
|  | 12591 | set->ecdsa = md_alg; | 
|  | 12592 | } | 
|  | 12593 |  | 
|  | 12594 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && | 
|  | 12595 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
|  | 12596 |  | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 12597 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12598 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 12599 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12600 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash ) | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12601 | { | 
|  | 12602 | switch( hash ) | 
|  | 12603 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12604 | #if defined(MBEDTLS_MD5_C) | 
|  | 12605 | case MBEDTLS_SSL_HASH_MD5: | 
|  | 12606 | return( MBEDTLS_MD_MD5 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12607 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12608 | #if defined(MBEDTLS_SHA1_C) | 
|  | 12609 | case MBEDTLS_SSL_HASH_SHA1: | 
|  | 12610 | return( MBEDTLS_MD_SHA1 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12611 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12612 | #if defined(MBEDTLS_SHA256_C) | 
|  | 12613 | case MBEDTLS_SSL_HASH_SHA224: | 
|  | 12614 | return( MBEDTLS_MD_SHA224 ); | 
|  | 12615 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 12616 | return( MBEDTLS_MD_SHA256 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12617 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12618 | #if defined(MBEDTLS_SHA512_C) | 
|  | 12619 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 12620 | return( MBEDTLS_MD_SHA384 ); | 
|  | 12621 | case MBEDTLS_SSL_HASH_SHA512: | 
|  | 12622 | return( MBEDTLS_MD_SHA512 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12623 | #endif | 
|  | 12624 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12625 | return( MBEDTLS_MD_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 12626 | } | 
|  | 12627 | } | 
|  | 12628 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12629 | /* | 
|  | 12630 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX | 
|  | 12631 | */ | 
|  | 12632 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) | 
|  | 12633 | { | 
|  | 12634 | switch( md ) | 
|  | 12635 | { | 
|  | 12636 | #if defined(MBEDTLS_MD5_C) | 
|  | 12637 | case MBEDTLS_MD_MD5: | 
|  | 12638 | return( MBEDTLS_SSL_HASH_MD5 ); | 
|  | 12639 | #endif | 
|  | 12640 | #if defined(MBEDTLS_SHA1_C) | 
|  | 12641 | case MBEDTLS_MD_SHA1: | 
|  | 12642 | return( MBEDTLS_SSL_HASH_SHA1 ); | 
|  | 12643 | #endif | 
|  | 12644 | #if defined(MBEDTLS_SHA256_C) | 
|  | 12645 | case MBEDTLS_MD_SHA224: | 
|  | 12646 | return( MBEDTLS_SSL_HASH_SHA224 ); | 
|  | 12647 | case MBEDTLS_MD_SHA256: | 
|  | 12648 | return( MBEDTLS_SSL_HASH_SHA256 ); | 
|  | 12649 | #endif | 
|  | 12650 | #if defined(MBEDTLS_SHA512_C) | 
|  | 12651 | case MBEDTLS_MD_SHA384: | 
|  | 12652 | return( MBEDTLS_SSL_HASH_SHA384 ); | 
|  | 12653 | case MBEDTLS_MD_SHA512: | 
|  | 12654 | return( MBEDTLS_SSL_HASH_SHA512 ); | 
|  | 12655 | #endif | 
|  | 12656 | default: | 
|  | 12657 | return( MBEDTLS_SSL_HASH_NONE ); | 
|  | 12658 | } | 
|  | 12659 | } | 
|  | 12660 |  | 
| Hanno Becker | ee902df | 2019-08-23 13:47:47 +0100 | [diff] [blame] | 12661 | #if defined(MBEDTLS_USE_TINYCRYPT) | 
|  | 12662 | /* | 
|  | 12663 | * Check if a curve proposed by the peer is in our list. | 
|  | 12664 | * Return 0 if we're willing to use it, -1 otherwise. | 
|  | 12665 | */ | 
|  | 12666 | int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl, | 
|  | 12667 | mbedtls_uecc_group_id grp_id ) | 
|  | 12668 | { | 
|  | 12669 | MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id ) | 
|  | 12670 | if( own_ec_id == grp_id ) | 
|  | 12671 | return( 0 ); | 
|  | 12672 | MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID | 
|  | 12673 |  | 
|  | 12674 | return( -1 ); | 
|  | 12675 | } | 
|  | 12676 | #endif /* MBEDTLS_USE_TINYCRYPT */ | 
|  | 12677 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 12678 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12679 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12680 | * Check if a curve proposed by the peer is in our list. | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12681 | * Return 0 if we're willing to use it, -1 otherwise. | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12682 | */ | 
| Hanno Becker | ee902df | 2019-08-23 13:47:47 +0100 | [diff] [blame] | 12683 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, | 
|  | 12684 | mbedtls_ecp_group_id grp_id ) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12685 | { | 
| Hanno Becker | a4a9c69 | 2019-06-18 16:55:47 +0100 | [diff] [blame] | 12686 | MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id ) | 
|  | 12687 | if( own_ec_id == grp_id ) | 
|  | 12688 | return( 0 ); | 
|  | 12689 | MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12690 |  | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 12691 | return( -1 ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 12692 | } | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 12693 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12694 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12695 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12696 | /* | 
|  | 12697 | * Check if a hash proposed by the peer is in our list. | 
|  | 12698 | * Return 0 if we're willing to use it, -1 otherwise. | 
|  | 12699 | */ | 
|  | 12700 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, | 
|  | 12701 | mbedtls_md_type_t md ) | 
|  | 12702 | { | 
| Hanno Becker | f1bc9e1 | 2019-06-19 16:23:21 +0100 | [diff] [blame] | 12703 | MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg ) | 
|  | 12704 | if( md_alg == md ) | 
|  | 12705 | return( 0 ); | 
|  | 12706 | MBEDTLS_SSL_END_FOR_EACH_SIG_HASH | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12707 |  | 
|  | 12708 | return( -1 ); | 
|  | 12709 | } | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 12710 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 12711 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12712 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 12713 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 12714 | mbedtls_ssl_ciphersuite_handle_t ciphersuite, | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12715 | int cert_endpoint, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 12716 | uint32_t *flags ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12717 | { | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12718 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12719 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12720 | int usage = 0; | 
|  | 12721 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12722 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12723 | const char *ext_oid; | 
|  | 12724 | size_t ext_len; | 
|  | 12725 | #endif | 
|  | 12726 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12727 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) &&          \ | 
|  | 12728 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12729 | ((void) cert); | 
|  | 12730 | ((void) cert_endpoint); | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12731 | ((void) flags); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12732 | #endif | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12733 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12734 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) | 
|  | 12735 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12736 | { | 
|  | 12737 | /* Server part of the key exchange */ | 
| Hanno Becker | 473f98f | 2019-06-26 10:27:32 +0100 | [diff] [blame] | 12738 | switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12739 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12740 | case MBEDTLS_KEY_EXCHANGE_RSA: | 
|  | 12741 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12742 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12743 | break; | 
|  | 12744 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12745 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: | 
|  | 12746 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: | 
|  | 12747 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: | 
|  | 12748 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12749 | break; | 
|  | 12750 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12751 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: | 
|  | 12752 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12753 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12754 | break; | 
|  | 12755 |  | 
|  | 12756 | /* Don't use default: we want warnings when adding new values */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12757 | case MBEDTLS_KEY_EXCHANGE_NONE: | 
|  | 12758 | case MBEDTLS_KEY_EXCHANGE_PSK: | 
|  | 12759 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: | 
|  | 12760 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | 
| Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 12761 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12762 | usage = 0; | 
|  | 12763 | } | 
|  | 12764 | } | 
|  | 12765 | else | 
|  | 12766 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12767 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ | 
|  | 12768 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12769 | } | 
|  | 12770 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12771 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12772 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12773 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12774 | ret = -1; | 
|  | 12775 | } | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12776 | #else | 
|  | 12777 | ((void) ciphersuite); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12778 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12779 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12780 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
|  | 12781 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12782 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12783 | ext_oid = MBEDTLS_OID_SERVER_AUTH; | 
|  | 12784 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12785 | } | 
|  | 12786 | else | 
|  | 12787 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12788 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; | 
|  | 12789 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12790 | } | 
|  | 12791 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12792 | if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 ) | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12793 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 12794 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12795 | ret = -1; | 
|  | 12796 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12797 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 12798 |  | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 12799 | return( ret ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 12800 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12801 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 12802 |  | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12803 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 12804 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 12805 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, | 
|  | 12806 | unsigned char *output, | 
|  | 12807 | unsigned char *data, size_t data_len ) | 
|  | 12808 | { | 
|  | 12809 | int ret = 0; | 
|  | 12810 | mbedtls_md5_context mbedtls_md5; | 
|  | 12811 | mbedtls_sha1_context mbedtls_sha1; | 
|  | 12812 |  | 
|  | 12813 | mbedtls_md5_init( &mbedtls_md5 ); | 
|  | 12814 | mbedtls_sha1_init( &mbedtls_sha1 ); | 
|  | 12815 |  | 
|  | 12816 | /* | 
|  | 12817 | * digitally-signed struct { | 
|  | 12818 | *     opaque md5_hash[16]; | 
|  | 12819 | *     opaque sha_hash[20]; | 
|  | 12820 | * }; | 
|  | 12821 | * | 
|  | 12822 | * md5_hash | 
|  | 12823 | *     MD5(ClientHello.random + ServerHello.random | 
|  | 12824 | *                            + ServerParams); | 
|  | 12825 | * sha_hash | 
|  | 12826 | *     SHA(ClientHello.random + ServerHello.random | 
|  | 12827 | *                            + ServerParams); | 
|  | 12828 | */ | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12829 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12830 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12831 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12832 | goto exit; | 
|  | 12833 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12834 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12835 | ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 12836 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12837 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12838 | goto exit; | 
|  | 12839 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12840 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12841 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12842 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12843 | goto exit; | 
|  | 12844 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12845 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12846 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12847 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12848 | goto exit; | 
|  | 12849 | } | 
|  | 12850 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12851 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12852 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12853 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12854 | goto exit; | 
|  | 12855 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12856 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12857 | ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 12858 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12859 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12860 | goto exit; | 
|  | 12861 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12862 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12863 | data_len ) ) != 0 ) | 
|  | 12864 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12865 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12866 | goto exit; | 
|  | 12867 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12868 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12869 | output + 16 ) ) != 0 ) | 
|  | 12870 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 12871 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12872 | goto exit; | 
|  | 12873 | } | 
|  | 12874 |  | 
|  | 12875 | exit: | 
|  | 12876 | mbedtls_md5_free( &mbedtls_md5 ); | 
|  | 12877 | mbedtls_sha1_free( &mbedtls_sha1 ); | 
|  | 12878 |  | 
|  | 12879 | if( ret != 0 ) | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 12880 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 12881 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12882 |  | 
|  | 12883 | return( ret ); | 
|  | 12884 |  | 
|  | 12885 | } | 
|  | 12886 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 12887 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 12888 |  | 
|  | 12889 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 12890 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 12891 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12892 | unsigned char *hash, size_t *hashlen, | 
|  | 12893 | unsigned char *data, size_t data_len, | 
|  | 12894 | mbedtls_md_type_t md_alg ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12895 | { | 
|  | 12896 | int ret = 0; | 
|  | 12897 | mbedtls_md_context_t ctx; | 
| Hanno Becker | a5cedbc | 2019-07-17 11:21:02 +0100 | [diff] [blame] | 12898 | mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg ); | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12899 | *hashlen = mbedtls_md_get_size( md_info ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12900 |  | 
|  | 12901 | mbedtls_md_init( &ctx ); | 
|  | 12902 |  | 
|  | 12903 | /* | 
|  | 12904 | * digitally-signed struct { | 
|  | 12905 | *     opaque client_random[32]; | 
|  | 12906 | *     opaque server_random[32]; | 
|  | 12907 | *     ServerDHParams params; | 
|  | 12908 | * }; | 
|  | 12909 | */ | 
|  | 12910 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) | 
|  | 12911 | { | 
|  | 12912 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
|  | 12913 | goto exit; | 
|  | 12914 | } | 
|  | 12915 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) | 
|  | 12916 | { | 
|  | 12917 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); | 
|  | 12918 | goto exit; | 
|  | 12919 | } | 
|  | 12920 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 12921 | { | 
|  | 12922 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 12923 | goto exit; | 
|  | 12924 | } | 
|  | 12925 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) | 
|  | 12926 | { | 
|  | 12927 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 12928 | goto exit; | 
|  | 12929 | } | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 12930 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12931 | { | 
|  | 12932 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); | 
|  | 12933 | goto exit; | 
|  | 12934 | } | 
|  | 12935 |  | 
|  | 12936 | exit: | 
|  | 12937 | mbedtls_md_free( &ctx ); | 
|  | 12938 |  | 
|  | 12939 | if( ret != 0 ) | 
| Hanno Becker | de62da9 | 2019-07-24 13:23:50 +0100 | [diff] [blame] | 12940 | mbedtls_ssl_pend_fatal_alert( ssl, | 
|  | 12941 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 12942 |  | 
|  | 12943 | return( ret ); | 
|  | 12944 | } | 
|  | 12945 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 12946 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 12947 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12948 | #endif /* MBEDTLS_SSL_TLS_C */ |