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" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 51 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 52 | #include <string.h> |
| 53 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 54 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 56 | #endif |
| 57 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 58 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 59 | 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] | 60 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 61 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | 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] | 63 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 64 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 65 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 66 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 67 | |
| 68 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 69 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 70 | return( 2 ); |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 71 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 72 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 73 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 74 | return( 0 ); |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 75 | #endif |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Start a timer. |
| 80 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 81 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | 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] | 83 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 84 | if( ssl->f_set_timer == NULL ) |
| 85 | return; |
| 86 | |
| 87 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 88 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Return -1 is timer is expired, 0 if it isn't. |
| 93 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 95 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 96 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 97 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 98 | |
| 99 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 100 | { |
| 101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 102 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 103 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 104 | |
| 105 | return( 0 ); |
| 106 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 107 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 108 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 109 | mbedtls_ssl_transform *transform ); |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 110 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 111 | |
| 112 | #define SSL_DONT_FORCE_FLUSH 0 |
| 113 | #define SSL_FORCE_FLUSH 1 |
| 114 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 115 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 116 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 117 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 118 | /* Top-level Connection ID API */ |
| 119 | |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 120 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \ |
| 121 | !defined(MBEDTLS_SSL_CONF_CID_LEN) && \ |
| 122 | !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 123 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 124 | size_t len, |
| 125 | int ignore_other_cid ) |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 126 | { |
| 127 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 128 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 129 | |
Hanno Becker | 791ec6b | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 130 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 131 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 132 | { |
| 133 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 134 | } |
| 135 | |
| 136 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 137 | conf->cid_len = len; |
| 138 | return( 0 ); |
| 139 | } |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 140 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID && |
| 141 | !MBEDTLS_SSL_CONF_CID_LEN && |
| 142 | !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ |
| 143 | |
| 144 | #if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 145 | #error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN" |
| 146 | #endif |
| 147 | #if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \ |
| 148 | MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL |
| 149 | #error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID" |
| 150 | #endif |
| 151 | |
| 152 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID && |
| 153 | !MBEDTLS_SSL_CONF_CID_LEN && |
| 154 | !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */ |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 155 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 156 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 157 | int enable, |
| 158 | unsigned char const *own_cid, |
| 159 | size_t own_cid_len ) |
| 160 | { |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 161 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 162 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 163 | |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 164 | ssl->negotiate_cid = enable; |
| 165 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 166 | { |
| 167 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 168 | return( 0 ); |
| 169 | } |
| 170 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 171 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 172 | |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 173 | if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 174 | { |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 175 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 176 | (unsigned) own_cid_len, |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 177 | (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) ); |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 178 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 179 | } |
| 180 | |
| 181 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b4a5606 | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 182 | /* Truncation is not an issue here because |
| 183 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 184 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 185 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 186 | return( 0 ); |
| 187 | } |
| 188 | |
| 189 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 190 | int *enabled, |
| 191 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 192 | size_t *peer_cid_len ) |
| 193 | { |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 194 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 195 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 196 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 197 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 198 | { |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 199 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 200 | } |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 201 | |
Hanno Becker | cb063f5 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 202 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 203 | * were used, but client and server requested the empty CID. |
| 204 | * This is indistinguishable from not using the CID extension |
| 205 | * in the first place. */ |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 206 | if( ssl->transform_in->in_cid_len == 0 && |
| 207 | ssl->transform_in->out_cid_len == 0 ) |
| 208 | { |
| 209 | return( 0 ); |
| 210 | } |
| 211 | |
Hanno Becker | 633d604 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 212 | if( peer_cid_len != NULL ) |
| 213 | { |
| 214 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 215 | if( peer_cid != NULL ) |
| 216 | { |
| 217 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 218 | ssl->transform_in->out_cid_len ); |
| 219 | } |
| 220 | } |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 221 | |
| 222 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 223 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 224 | return( 0 ); |
| 225 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 226 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 227 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 228 | /* Forward declarations for functions related to message buffering. */ |
| 229 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 230 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 231 | uint8_t slot ); |
| 232 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 233 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 234 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 235 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
| 236 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 237 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 238 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 239 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 240 | 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] | 241 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 242 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 243 | |
| 244 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 245 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 246 | |
| 247 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 248 | } |
| 249 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 250 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 251 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 252 | size_t const bytes_written = ssl->out_left; |
| 253 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 254 | |
| 255 | /* Double-check that the write-index hasn't gone |
| 256 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 257 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 258 | { |
| 259 | /* Should never happen... */ |
| 260 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 261 | } |
| 262 | |
| 263 | return( (int) ( mtu - bytes_written ) ); |
| 264 | } |
| 265 | |
| 266 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 267 | { |
| 268 | int ret; |
| 269 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 270 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 271 | |
| 272 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 273 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 274 | |
| 275 | if( max_len > mfl ) |
| 276 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 277 | |
| 278 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 279 | * only limits the maximum record payload size, so in theory |
| 280 | * we would be allowed to pack multiple records of payload size |
| 281 | * MFL into a single datagram. However, this would mean that there's |
| 282 | * no way to explicitly communicate MTU restrictions to the peer. |
| 283 | * |
| 284 | * The following reduction of max_len makes sure that we never |
| 285 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 286 | */ |
| 287 | if( max_len <= ssl->out_left ) |
| 288 | return( 0 ); |
| 289 | |
| 290 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 291 | #endif |
| 292 | |
| 293 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 294 | if( ret < 0 ) |
| 295 | return( ret ); |
| 296 | remaining = (size_t) ret; |
| 297 | |
| 298 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 299 | if( ret < 0 ) |
| 300 | return( ret ); |
| 301 | expansion = (size_t) ret; |
| 302 | |
| 303 | if( remaining <= expansion ) |
| 304 | return( 0 ); |
| 305 | |
| 306 | remaining -= expansion; |
| 307 | if( remaining >= max_len ) |
| 308 | remaining = max_len; |
| 309 | |
| 310 | return( (int) remaining ); |
| 311 | } |
| 312 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 313 | /* |
| 314 | * Double the retransmit timeout value, within the allowed range, |
| 315 | * returning -1 if the maximum value has already been reached. |
| 316 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 318 | { |
| 319 | uint32_t new_timeout; |
| 320 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 321 | if( ssl->handshake->retransmit_timeout >= |
| 322 | mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) ) |
| 323 | { |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 324 | return( -1 ); |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 325 | } |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 326 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 327 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 328 | * in the following way: after the initial transmission and a first |
| 329 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 330 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 331 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 332 | * on most non-IP stacks too. */ |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 333 | if( ssl->handshake->retransmit_timeout != |
| 334 | mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 335 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 336 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 337 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 338 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 339 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 340 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 341 | |
| 342 | /* Avoid arithmetic overflow and range overflow */ |
| 343 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 344 | 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] | 345 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 346 | 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] | 347 | } |
| 348 | |
| 349 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | 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] | 351 | ssl->handshake->retransmit_timeout ) ); |
| 352 | |
| 353 | return( 0 ); |
| 354 | } |
| 355 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 356 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 357 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 358 | 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] | 359 | 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] | 360 | ssl->handshake->retransmit_timeout ) ); |
| 361 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 364 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 365 | /* |
| 366 | * Convert max_fragment_length codes to length. |
| 367 | * RFC 6066 says: |
| 368 | * enum{ |
| 369 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 370 | * } MaxFragmentLength; |
| 371 | * and we add 0 -> extension unused |
| 372 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 373 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 374 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 375 | switch( mfl ) |
| 376 | { |
| 377 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 378 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 379 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 380 | return 512; |
| 381 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 382 | return 1024; |
| 383 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 384 | return 2048; |
| 385 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 386 | return 4096; |
| 387 | default: |
| 388 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 389 | } |
| 390 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 392 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 393 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 394 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 395 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | mbedtls_ssl_session_free( dst ); |
| 397 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | d5258fa | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 400 | |
| 401 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 402 | if( src->peer_cert != NULL ) |
| 403 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 404 | int ret; |
| 405 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 406 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 407 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 408 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | 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] | 413 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 414 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 416 | dst->peer_cert = NULL; |
| 417 | return( ret ); |
| 418 | } |
| 419 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 420 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 421 | if( src->peer_cert_digest != NULL ) |
| 422 | { |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 423 | dst->peer_cert_digest = |
Hanno Becker | 9d64b78 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 424 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 425 | if( dst->peer_cert_digest == NULL ) |
| 426 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 427 | |
| 428 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 429 | src->peer_cert_digest_len ); |
| 430 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | 9d64b78 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 431 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 432 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 433 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 9fb6e2e | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 435 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 436 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 437 | #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] | 438 | if( src->ticket != NULL ) |
| 439 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 440 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 441 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 442 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 443 | |
| 444 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 445 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 446 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 447 | |
| 448 | return( 0 ); |
| 449 | } |
| 450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 451 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 452 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 453 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 454 | size_t keylen, |
| 455 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 456 | size_t ivlen, |
| 457 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 458 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 460 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 461 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 462 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 463 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 464 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 465 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 466 | /* |
| 467 | * Key material generation |
| 468 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 469 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 470 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 471 | const char *label, |
| 472 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 473 | unsigned char *dstbuf, size_t dlen ) |
| 474 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 475 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 476 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 477 | mbedtls_md5_context md5; |
| 478 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 479 | unsigned char padding[16]; |
| 480 | unsigned char sha1sum[20]; |
| 481 | ((void)label); |
| 482 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 483 | mbedtls_md5_init( &md5 ); |
| 484 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 485 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 486 | /* |
| 487 | * SSLv3: |
| 488 | * block = |
| 489 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 490 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 491 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 492 | * ... |
| 493 | */ |
| 494 | for( i = 0; i < dlen / 16; i++ ) |
| 495 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 496 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 497 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 498 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 499 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 500 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 501 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 502 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 503 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 504 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 505 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 506 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 507 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 508 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 509 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 510 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 511 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 512 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 513 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 514 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 515 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 516 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 519 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 520 | mbedtls_md5_free( &md5 ); |
| 521 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 522 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 523 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 524 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 525 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 526 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 527 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 531 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 532 | const char *label, |
| 533 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 534 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 535 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 536 | size_t nb, hs; |
| 537 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 538 | const unsigned char *S1, *S2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 539 | unsigned char tmp[128]; |
| 540 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 541 | const mbedtls_md_info_t *md_info; |
| 542 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 543 | int ret; |
| 544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 546 | |
| 547 | if( sizeof( tmp ) < 20 + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 548 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 549 | |
| 550 | hs = ( slen + 1 ) / 2; |
| 551 | S1 = secret; |
| 552 | S2 = secret + slen - hs; |
| 553 | |
| 554 | nb = strlen( label ); |
| 555 | memcpy( tmp + 20, label, nb ); |
| 556 | memcpy( tmp + 20 + nb, random, rlen ); |
| 557 | nb += rlen; |
| 558 | |
| 559 | /* |
| 560 | * First compute P_md5(secret,label+random)[0..dlen] |
| 561 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
| 563 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | 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] | 566 | return( ret ); |
| 567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 569 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 570 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 571 | |
| 572 | for( i = 0; i < dlen; i += 16 ) |
| 573 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 575 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 576 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 578 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 579 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 580 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 581 | |
| 582 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 583 | |
| 584 | for( j = 0; j < k; j++ ) |
| 585 | dstbuf[i + j] = h_i[j]; |
| 586 | } |
| 587 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 589 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 590 | /* |
| 591 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 592 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
| 594 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 595 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | 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] | 597 | return( ret ); |
| 598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 600 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 601 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 602 | |
| 603 | for( i = 0; i < dlen; i += 20 ) |
| 604 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 606 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 607 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 608 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 609 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 610 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 611 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 612 | |
| 613 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 614 | |
| 615 | for( j = 0; j < k; j++ ) |
| 616 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 617 | } |
| 618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 619 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 620 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 621 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 622 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 623 | |
| 624 | return( 0 ); |
| 625 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 629 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 630 | const unsigned char *secret, size_t slen, |
| 631 | const char *label, |
| 632 | const unsigned char *random, size_t rlen, |
| 633 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 634 | { |
| 635 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 636 | size_t i, j, k, md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 637 | unsigned char tmp[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 639 | const mbedtls_md_info_t *md_info; |
| 640 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 641 | int ret; |
| 642 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 646 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 647 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 648 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 649 | |
| 650 | if( sizeof( tmp ) < md_len + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 652 | |
| 653 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 654 | memcpy( tmp + md_len, label, nb ); |
| 655 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 656 | nb += rlen; |
| 657 | |
| 658 | /* |
| 659 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 660 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | 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] | 662 | return( ret ); |
| 663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 665 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 666 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 667 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 668 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 669 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 670 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 671 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 672 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 675 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 676 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 677 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 678 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 679 | |
| 680 | for( j = 0; j < k; j++ ) |
| 681 | dstbuf[i + j] = h_i[j]; |
| 682 | } |
| 683 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 685 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 686 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 687 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 688 | |
| 689 | return( 0 ); |
| 690 | } |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 691 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 692 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 693 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 694 | const char *label, |
| 695 | const unsigned char *random, size_t rlen, |
| 696 | unsigned char *dstbuf, size_t dlen ) |
| 697 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 699 | label, random, rlen, dstbuf, dlen ) ); |
| 700 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 704 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 705 | const char *label, |
| 706 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 707 | unsigned char *dstbuf, size_t dlen ) |
| 708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 710 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 711 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 712 | #endif /* MBEDTLS_SHA512_C */ |
| 713 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 718 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 719 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 720 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 721 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 722 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 723 | static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 725 | #endif |
| 726 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 728 | static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 729 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 730 | #endif |
| 731 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 733 | #if defined(MBEDTLS_SHA256_C) |
| 734 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 735 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 737 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 738 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 739 | #if defined(MBEDTLS_SHA512_C) |
| 740 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 741 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 742 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 743 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 745 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 746 | /* Type for the TLS PRF */ |
| 747 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 748 | const unsigned char *, size_t, |
| 749 | unsigned char *, size_t); |
| 750 | |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 751 | /* |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 752 | * Populate a transform structure with session keys and all the other |
| 753 | * necessary information. |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 754 | * |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 755 | * Parameters: |
| 756 | * - [in/out]: transform: structure to populate |
| 757 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 758 | * [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] | 759 | * - [in] ciphersuite |
| 760 | * - [in] master |
| 761 | * - [in] encrypt_then_mac |
| 762 | * - [in] trunc_hmac |
| 763 | * - [in] compression |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 764 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 765 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 766 | * - [in] minor_ver: SSL/TLS minor version |
| 767 | * - [in] endpoint: client or server |
| 768 | * - [in] ssl: optionally used for: |
| 769 | * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context |
| 770 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 771 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 772 | */ |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 773 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 774 | int ciphersuite, |
| 775 | const unsigned char master[48], |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 776 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 777 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 778 | int encrypt_then_mac, |
| 779 | #endif |
| 780 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 781 | int trunc_hmac, |
| 782 | #endif |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 783 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 784 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 785 | int compression, |
| 786 | #endif |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 787 | ssl_tls_prf_t tls_prf, |
| 788 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 789 | int minor_ver, |
| 790 | unsigned endpoint, |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 791 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 792 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 793 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 794 | unsigned char keyblk[256]; |
| 795 | unsigned char *key1; |
| 796 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 797 | unsigned char *mac_enc; |
| 798 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 799 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 800 | size_t iv_copy_len; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 801 | unsigned keylen; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 802 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | const mbedtls_cipher_info_t *cipher_info; |
| 804 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 805 | |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 806 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ |
| 807 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ |
| 808 | !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 809 | 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] | 810 | (void) ssl; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 811 | #endif |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 812 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 813 | /* Copy info about negotiated version and extensions */ |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 814 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 815 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 816 | transform->encrypt_then_mac = encrypt_then_mac; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 817 | #endif |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 818 | transform->minor_ver = minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 819 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 820 | /* |
| 821 | * Get various info structures |
| 822 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 823 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 824 | if( ciphersuite_info == NULL ) |
| 825 | { |
| 826 | 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] | 827 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 828 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 829 | } |
| 830 | |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 831 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 832 | if( cipher_info == NULL ) |
| 833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 835 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 837 | } |
| 838 | |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 839 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 840 | if( md_info == NULL ) |
| 841 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 843 | ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 844 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 847 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 848 | /* Copy own and peer's CID if the use of the CID |
| 849 | * extension has been negotiated. */ |
| 850 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 851 | { |
| 852 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | d91dc37 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 853 | |
Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 854 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 855 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 8013b27 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 856 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 857 | transform->in_cid_len ); |
Hanno Becker | e582d12 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 858 | |
| 859 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 860 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 861 | ssl->handshake->peer_cid_len ); |
| 862 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 863 | transform->out_cid_len ); |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 864 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 865 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 866 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 867 | /* |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 868 | * Compute key block using the PRF |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 869 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 870 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 871 | if( ret != 0 ) |
| 872 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 873 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 874 | return( ret ); |
| 875 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 876 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 877 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 878 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 879 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 880 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 881 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 882 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 883 | /* |
| 884 | * Determine the appropriate key, IV and MAC length. |
| 885 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 886 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 887 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 888 | |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 889 | #if defined(MBEDTLS_GCM_C) || \ |
| 890 | defined(MBEDTLS_CCM_C) || \ |
| 891 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 893 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 894 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 895 | { |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 896 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 897 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 898 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 899 | mac_key_len = 0; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 900 | transform->taglen = |
| 901 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 902 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 903 | /* All modes haves 96-bit IVs; |
| 904 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 905 | * ChachaPoly has all 12 bytes implicit |
| 906 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 907 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 908 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 909 | transform->fixed_ivlen = 12; |
| 910 | else |
| 911 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 912 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 913 | /* Minimum length of encrypted record */ |
| 914 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 915 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 916 | } |
| 917 | else |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 918 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 919 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 920 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 921 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 922 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 923 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 924 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 925 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 926 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 928 | return( ret ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 929 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 930 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 931 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 932 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 933 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 934 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 935 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 936 | /* |
| 937 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 938 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 939 | * so we only need to adjust the length here. |
| 940 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 941 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 942 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 944 | |
| 945 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 946 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 947 | * HMAC implementation which also truncates the key |
| 948 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 949 | mac_key_len = transform->maclen; |
| 950 | #endif |
| 951 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 952 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 953 | |
| 954 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 955 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 956 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 957 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 958 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 959 | transform->minlen = transform->maclen; |
| 960 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 961 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 962 | /* |
| 963 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 964 | * 1. if EtM is in use: one block plus MAC |
| 965 | * otherwise: * first multiple of blocklen greater than maclen |
| 966 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 967 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 968 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 969 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 970 | { |
| 971 | transform->minlen = transform->maclen |
| 972 | + cipher_info->block_size; |
| 973 | } |
| 974 | else |
| 975 | #endif |
| 976 | { |
| 977 | transform->minlen = transform->maclen |
| 978 | + cipher_info->block_size |
| 979 | - transform->maclen % cipher_info->block_size; |
| 980 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 982 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 983 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 984 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 985 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 986 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 987 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 988 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 989 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 990 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 991 | { |
| 992 | transform->minlen += transform->ivlen; |
| 993 | } |
| 994 | else |
| 995 | #endif |
| 996 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 997 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 998 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 999 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1000 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1001 | } |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1002 | else |
| 1003 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1004 | { |
| 1005 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1006 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1007 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1008 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1009 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1010 | (unsigned) keylen, |
| 1011 | (unsigned) transform->minlen, |
| 1012 | (unsigned) transform->ivlen, |
| 1013 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1014 | |
| 1015 | /* |
| 1016 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1017 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1018 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1019 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1020 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1021 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1022 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1023 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1024 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1025 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1026 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1027 | /* |
| 1028 | * This is not used in TLS v1.1. |
| 1029 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1030 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1031 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1032 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1033 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1034 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1035 | } |
| 1036 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1037 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1038 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1039 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1040 | { |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1041 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1042 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1043 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1044 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1045 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1046 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1047 | /* |
| 1048 | * This is not used in TLS v1.1. |
| 1049 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1050 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1051 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1052 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1053 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1054 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1055 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1056 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1058 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1060 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1061 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1062 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1063 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1064 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1065 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1066 | { |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1067 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1068 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1069 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1070 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1071 | } |
| 1072 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1073 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1074 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1075 | } |
| 1076 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1078 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1079 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1080 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1081 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1082 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1083 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1084 | if( mac_key_len != 0 ) |
| 1085 | { |
| 1086 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1087 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1088 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1089 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1090 | else |
| 1091 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1092 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1093 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1094 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1095 | } |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1096 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1097 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1099 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1100 | { |
| 1101 | int ret = 0; |
| 1102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1103 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1104 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1105 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1106 | transform->iv_enc, transform->iv_dec, |
| 1107 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1108 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1109 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1110 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1111 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
| 1112 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1115 | #else |
| 1116 | ((void) mac_dec); |
| 1117 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1118 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1119 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1120 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1121 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1122 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1123 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1124 | master, keyblk, |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1125 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1126 | iv_copy_len ); |
| 1127 | } |
| 1128 | #endif |
| 1129 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1130 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1131 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1132 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1133 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1134 | return( ret ); |
| 1135 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1136 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1137 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1138 | cipher_info ) ) != 0 ) |
| 1139 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1140 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1141 | return( ret ); |
| 1142 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1144 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1145 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1146 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1147 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1148 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1149 | return( ret ); |
| 1150 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1152 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1153 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1154 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1156 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1157 | return( ret ); |
| 1158 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1160 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1161 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1162 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1163 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1164 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1165 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1166 | 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] | 1167 | return( ret ); |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1168 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1170 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1171 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1172 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1173 | 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] | 1174 | return( ret ); |
| 1175 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1176 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1177 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1178 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1179 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1180 | |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1181 | /* Initialize Zlib contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1182 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1183 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1184 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1185 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1186 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1187 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1188 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1189 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1190 | if( deflateInit( &transform->ctx_deflate, |
| 1191 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1192 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1193 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1194 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
| 1195 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1196 | } |
| 1197 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1199 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1200 | return( 0 ); |
| 1201 | } |
| 1202 | |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1203 | /* |
Manuel Pégourié-Gonnard | 42c814f | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1204 | * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1205 | * |
| 1206 | * Inputs: |
| 1207 | * - SSL/TLS minor version |
| 1208 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1209 | * |
Manuel Pégourié-Gonnard | cf31216 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1210 | * Outputs: |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1211 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1212 | */ |
| 1213 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1214 | int minor_ver, |
| 1215 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1216 | { |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1217 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) |
| 1218 | (void) hash; |
| 1219 | #endif |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1220 | |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1221 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1222 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1223 | { |
| 1224 | handshake->tls_prf = ssl3_prf; |
| 1225 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1226 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1227 | } |
| 1228 | else |
| 1229 | #endif |
| 1230 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1231 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1232 | { |
| 1233 | handshake->tls_prf = tls1_prf; |
| 1234 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1235 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1236 | } |
| 1237 | else |
| 1238 | #endif |
| 1239 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1240 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1241 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1242 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1243 | { |
| 1244 | handshake->tls_prf = tls_prf_sha384; |
| 1245 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1246 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1247 | } |
| 1248 | else |
| 1249 | #endif |
| 1250 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1251 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1252 | { |
| 1253 | handshake->tls_prf = tls_prf_sha256; |
| 1254 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1255 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1256 | } |
| 1257 | else |
| 1258 | #endif |
| 1259 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1260 | { |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1261 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1262 | } |
| 1263 | |
| 1264 | return( 0 ); |
| 1265 | } |
| 1266 | |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1267 | /* |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1268 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1269 | * |
| 1270 | * Parameters: |
| 1271 | * [in/out] handshake |
| 1272 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 1273 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1274 | * [out] master |
| 1275 | * [in] ssl: optionally used for debugging and calc_verify |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1276 | */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1277 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1278 | unsigned char *master, |
Manuel Pégourié-Gonnard | ed3b7a9 | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1279 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1280 | { |
| 1281 | int ret; |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1282 | |
| 1283 | #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1284 | ssl = NULL; /* make sure we don't use it except for debug and EMS */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1285 | (void) ssl; |
| 1286 | #endif |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1287 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 1288 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1289 | if( handshake->resume != 0 ) |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1290 | { |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1291 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 1292 | return( 0 ); |
| 1293 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 1294 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1295 | |
| 1296 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, |
| 1297 | handshake->pmslen ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1298 | |
| 1299 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | a49ec56 | 2019-06-11 14:47:55 +0100 | [diff] [blame] | 1300 | if( mbedtls_ssl_hs_get_extended_ms( handshake ) |
| 1301 | == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1302 | { |
| 1303 | unsigned char session_hash[48]; |
| 1304 | size_t hash_len; |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1305 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1306 | handshake->calc_verify( ssl, session_hash, &hash_len ); |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1307 | |
Manuel Pégourié-Gonnard | 9c5bcc9 | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1308 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1309 | session_hash, hash_len ); |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1310 | |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1311 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1312 | "extended master secret", |
| 1313 | session_hash, hash_len, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1314 | master, 48 ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1315 | } |
| 1316 | else |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1317 | #endif |
Manuel Pégourié-Gonnard | bcf258e | 2019-05-03 11:46:27 +0200 | [diff] [blame] | 1318 | { |
| 1319 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1320 | "master secret", |
| 1321 | handshake->randbytes, 64, |
| 1322 | master, 48 ); |
| 1323 | } |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1324 | if( ret != 0 ) |
| 1325 | { |
| 1326 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1327 | return( ret ); |
| 1328 | } |
| 1329 | |
| 1330 | mbedtls_platform_zeroize( handshake->premaster, |
| 1331 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1332 | |
| 1333 | return( 0 ); |
| 1334 | } |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1335 | |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1336 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1337 | { |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1338 | int ret; |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1339 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1340 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1341 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1342 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1343 | |
| 1344 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1345 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1346 | ssl->minor_ver, |
| 1347 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1348 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1349 | { |
| 1350 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1351 | return( ret ); |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1352 | } |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1353 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1354 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1355 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1356 | ssl->session_negotiate->master, |
| 1357 | ssl ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1358 | if( ret != 0 ) |
| 1359 | { |
| 1360 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1361 | return( ret ); |
| 1362 | } |
| 1363 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1364 | /* Swap the client and server random values: |
| 1365 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1366 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1367 | { |
| 1368 | unsigned char tmp[64]; |
| 1369 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1370 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1371 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1372 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1373 | } |
| 1374 | |
| 1375 | /* Populate transform structure */ |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1376 | ret = ssl_populate_transform( ssl->transform_negotiate, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1377 | ssl->session_negotiate->ciphersuite, |
| 1378 | ssl->session_negotiate->master, |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1379 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1380 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1381 | ssl->session_negotiate->encrypt_then_mac, |
| 1382 | #endif |
| 1383 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1384 | ssl->session_negotiate->trunc_hmac, |
| 1385 | #endif |
Jaeden Amero | 2eaf2c7 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1386 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1387 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1388 | ssl->session_negotiate->compression, |
| 1389 | #endif |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1390 | ssl->handshake->tls_prf, |
| 1391 | ssl->handshake->randbytes, |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1392 | ssl->minor_ver, |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 1393 | mbedtls_ssl_conf_get_endpoint( ssl->conf ), |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1394 | ssl ); |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1395 | if( ret != 0 ) |
| 1396 | { |
| 1397 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1398 | return( ret ); |
| 1399 | } |
| 1400 | |
| 1401 | /* We no longer need Server/ClientHello.random values */ |
| 1402 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1403 | sizeof( ssl->handshake->randbytes ) ); |
| 1404 | |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1405 | /* Allocate compression buffer */ |
| 1406 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1407 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && |
| 1408 | ssl->compress_buf == NULL ) |
| 1409 | { |
| 1410 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
| 1411 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
| 1412 | if( ssl->compress_buf == NULL ) |
| 1413 | { |
| 1414 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1415 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1416 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1417 | } |
| 1418 | } |
| 1419 | #endif |
| 1420 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1421 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1422 | |
| 1423 | return( 0 ); |
| 1424 | } |
| 1425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1426 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1427 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
| 1428 | unsigned char hash[36], |
| 1429 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1430 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1431 | mbedtls_md5_context md5; |
| 1432 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1433 | unsigned char pad_1[48]; |
| 1434 | unsigned char pad_2[48]; |
| 1435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1436 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1437 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1438 | mbedtls_md5_init( &md5 ); |
| 1439 | mbedtls_sha1_init( &sha1 ); |
| 1440 | |
| 1441 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1442 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1443 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1444 | memset( pad_1, 0x36, 48 ); |
| 1445 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1446 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1447 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1448 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1449 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1450 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1451 | mbedtls_md5_starts_ret( &md5 ); |
| 1452 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1453 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1454 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1455 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1456 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1457 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1458 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1459 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1460 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1461 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1462 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1463 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1464 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1465 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1466 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1467 | *hlen = 36; |
| 1468 | |
| 1469 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1470 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1471 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1472 | mbedtls_md5_free( &md5 ); |
| 1473 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1474 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1475 | return; |
| 1476 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1477 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1479 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1480 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
| 1481 | unsigned char hash[36], |
| 1482 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1483 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1484 | mbedtls_md5_context md5; |
| 1485 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1486 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1487 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1488 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1489 | mbedtls_md5_init( &md5 ); |
| 1490 | mbedtls_sha1_init( &sha1 ); |
| 1491 | |
| 1492 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1493 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1494 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1495 | mbedtls_md5_finish_ret( &md5, hash ); |
| 1496 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1497 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1498 | *hlen = 36; |
| 1499 | |
| 1500 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1501 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1502 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1503 | mbedtls_md5_free( &md5 ); |
| 1504 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1505 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1506 | return; |
| 1507 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1508 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1509 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1510 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1511 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1512 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 1513 | unsigned char hash[32], |
| 1514 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1515 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1516 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1517 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1518 | mbedtls_sha256_init( &sha256 ); |
| 1519 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1520 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1521 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1522 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1523 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1524 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1525 | *hlen = 32; |
| 1526 | |
| 1527 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1528 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1529 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1530 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1531 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1532 | return; |
| 1533 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1534 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1537 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 1538 | unsigned char hash[48], |
| 1539 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1540 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1541 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1542 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1543 | mbedtls_sha512_init( &sha512 ); |
| 1544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1545 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1546 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1547 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1548 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1549 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1550 | *hlen = 48; |
| 1551 | |
| 1552 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1554 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1555 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1556 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1557 | return; |
| 1558 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | #endif /* MBEDTLS_SHA512_C */ |
| 1560 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1562 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1563 | 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] | 1564 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1565 | unsigned char *p = ssl->handshake->premaster; |
| 1566 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1567 | const unsigned char *psk = ssl->conf->psk; |
| 1568 | size_t psk_len = ssl->conf->psk_len; |
| 1569 | |
| 1570 | /* If the psk callback was called, use its result */ |
| 1571 | if( ssl->handshake->psk != NULL ) |
| 1572 | { |
| 1573 | psk = ssl->handshake->psk; |
| 1574 | psk_len = ssl->handshake->psk_len; |
| 1575 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1576 | |
| 1577 | /* |
| 1578 | * PMS = struct { |
| 1579 | * opaque other_secret<0..2^16-1>; |
| 1580 | * opaque psk<0..2^16-1>; |
| 1581 | * }; |
| 1582 | * with "other_secret" depending on the particular key exchange |
| 1583 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1584 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1585 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1586 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1587 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1588 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1589 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1590 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1591 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1592 | |
| 1593 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1594 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1595 | |
| 1596 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1597 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1598 | } |
| 1599 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1600 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1601 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1602 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1603 | { |
| 1604 | /* |
| 1605 | * other_secret already set by the ClientKeyExchange message, |
| 1606 | * and is 48 bytes long |
| 1607 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1608 | if( end - p < 2 ) |
| 1609 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1610 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1611 | *p++ = 0; |
| 1612 | *p++ = 48; |
| 1613 | p += 48; |
| 1614 | } |
| 1615 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1617 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1618 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1619 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1620 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1621 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1622 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1623 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1624 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1625 | p + 2, end - ( p + 2 ), &len, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame^] | 1626 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
| 1627 | ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1629 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1630 | return( ret ); |
| 1631 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1632 | *(p++) = (unsigned char)( len >> 8 ); |
| 1633 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1634 | p += len; |
| 1635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | 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] | 1637 | } |
| 1638 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1639 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1640 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1641 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1642 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1643 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1644 | size_t zlen; |
| 1645 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1646 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1647 | p + 2, end - ( p + 2 ), |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame^] | 1648 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
| 1649 | ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1651 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1652 | return( ret ); |
| 1653 | } |
| 1654 | |
| 1655 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 1656 | *(p++) = (unsigned char)( zlen ); |
| 1657 | p += zlen; |
| 1658 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 1659 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1660 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1661 | } |
| 1662 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1663 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1664 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1665 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1666 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1670 | if( end - p < 2 ) |
| 1671 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1672 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1673 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1674 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1675 | |
| 1676 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1677 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1678 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1679 | memcpy( p, psk, psk_len ); |
| 1680 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1681 | |
| 1682 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1683 | |
| 1684 | return( 0 ); |
| 1685 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1686 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1688 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1689 | /* |
| 1690 | * SSLv3.0 MAC functions |
| 1691 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1692 | #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] | 1693 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 1694 | const unsigned char *secret, |
| 1695 | const unsigned char *buf, size_t len, |
| 1696 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1697 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1698 | { |
| 1699 | unsigned char header[11]; |
| 1700 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1701 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1702 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 1703 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1704 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1705 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1706 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1707 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1708 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1709 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1710 | |
| 1711 | memcpy( header, ctr, 8 ); |
| 1712 | header[ 8] = (unsigned char) type; |
| 1713 | header[ 9] = (unsigned char)( len >> 8 ); |
| 1714 | header[10] = (unsigned char)( len ); |
| 1715 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1716 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1717 | mbedtls_md_starts( md_ctx ); |
| 1718 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1719 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 1720 | mbedtls_md_update( md_ctx, header, 11 ); |
| 1721 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1722 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1723 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1724 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1725 | mbedtls_md_starts( md_ctx ); |
| 1726 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1727 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1728 | mbedtls_md_update( md_ctx, out, md_size ); |
| 1729 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1730 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1731 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1732 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1733 | /* 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] | 1734 | * 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] | 1735 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1736 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 1737 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1738 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 1739 | /* This function makes sure every byte in the memory region is accessed |
| 1740 | * (in ascending addresses order) */ |
| 1741 | static void ssl_read_memory( unsigned char *p, size_t len ) |
| 1742 | { |
| 1743 | unsigned char acc = 0; |
| 1744 | volatile unsigned char force; |
| 1745 | |
| 1746 | for( ; len != 0; p++, len-- ) |
| 1747 | acc ^= *p; |
| 1748 | |
| 1749 | force = acc; |
| 1750 | (void) force; |
| 1751 | } |
| 1752 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 1753 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1754 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1755 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 1756 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1757 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1758 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8969369 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 1759 | /* This functions transforms a DTLS plaintext fragment and a record content |
| 1760 | * type into an instance of the DTLSInnerPlaintext structure: |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1761 | * |
| 1762 | * struct { |
| 1763 | * opaque content[DTLSPlaintext.length]; |
| 1764 | * ContentType real_type; |
| 1765 | * uint8 zeros[length_of_padding]; |
| 1766 | * } DTLSInnerPlaintext; |
| 1767 | * |
| 1768 | * Input: |
| 1769 | * - `content`: The beginning of the buffer holding the |
| 1770 | * plaintext to be wrapped. |
| 1771 | * - `*content_size`: The length of the plaintext in Bytes. |
| 1772 | * - `max_len`: The number of Bytes available starting from |
| 1773 | * `content`. This must be `>= *content_size`. |
| 1774 | * - `rec_type`: The desired record content type. |
| 1775 | * |
| 1776 | * Output: |
| 1777 | * - `content`: The beginning of the resulting DTLSInnerPlaintext structure. |
| 1778 | * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. |
| 1779 | * |
| 1780 | * Returns: |
| 1781 | * - `0` on success. |
| 1782 | * - A negative error code if `max_len` didn't offer enough space |
| 1783 | * for the expansion. |
| 1784 | */ |
| 1785 | static int ssl_cid_build_inner_plaintext( unsigned char *content, |
| 1786 | size_t *content_size, |
| 1787 | size_t remaining, |
| 1788 | uint8_t rec_type ) |
| 1789 | { |
| 1790 | size_t len = *content_size; |
Hanno Becker | 7842609 | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 1791 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - |
| 1792 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % |
| 1793 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* Write real content type */ |
| 1796 | if( remaining == 0 ) |
| 1797 | return( -1 ); |
| 1798 | content[ len ] = rec_type; |
| 1799 | len++; |
| 1800 | remaining--; |
| 1801 | |
| 1802 | if( remaining < pad ) |
| 1803 | return( -1 ); |
| 1804 | memset( content + len, 0, pad ); |
| 1805 | len += pad; |
| 1806 | remaining -= pad; |
| 1807 | |
| 1808 | *content_size = len; |
| 1809 | return( 0 ); |
| 1810 | } |
| 1811 | |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 1812 | /* This function parses a DTLSInnerPlaintext structure. |
| 1813 | * See ssl_cid_build_inner_plaintext() for details. */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1814 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, |
| 1815 | size_t *content_size, |
| 1816 | uint8_t *rec_type ) |
| 1817 | { |
| 1818 | size_t remaining = *content_size; |
| 1819 | |
| 1820 | /* Determine length of padding by skipping zeroes from the back. */ |
| 1821 | do |
| 1822 | { |
| 1823 | if( remaining == 0 ) |
| 1824 | return( -1 ); |
| 1825 | remaining--; |
| 1826 | } while( content[ remaining ] == 0 ); |
| 1827 | |
| 1828 | *content_size = remaining; |
| 1829 | *rec_type = content[ remaining ]; |
| 1830 | |
| 1831 | return( 0 ); |
| 1832 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1833 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1834 | |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 1835 | /* `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] | 1836 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1837 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1838 | size_t *add_data_len, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1839 | mbedtls_record *rec ) |
| 1840 | { |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 1841 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1842 | * |
| 1843 | * additional_data = seq_num + TLSCompressed.type + |
| 1844 | * TLSCompressed.version + TLSCompressed.length; |
| 1845 | * |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 1846 | * For the CID extension, this is extended as follows |
| 1847 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 1848 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1849 | * |
| 1850 | * additional_data = seq_num + DTLSPlaintext.type + |
| 1851 | * DTLSPlaintext.version + |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 1852 | * cid + |
| 1853 | * cid_length + |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1854 | * length_of_DTLSInnerPlaintext; |
| 1855 | */ |
| 1856 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1857 | memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); |
| 1858 | add_data[8] = rec->type; |
Hanno Becker | 24ce1eb | 2019-05-20 15:01:46 +0100 | [diff] [blame] | 1859 | memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1860 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1861 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 1862 | if( rec->cid_len != 0 ) |
| 1863 | { |
| 1864 | memcpy( add_data + 11, rec->cid, rec->cid_len ); |
| 1865 | add_data[11 + rec->cid_len + 0] = rec->cid_len; |
| 1866 | add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF; |
| 1867 | add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF; |
| 1868 | *add_data_len = 13 + 1 + rec->cid_len; |
| 1869 | } |
| 1870 | else |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1871 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 1872 | { |
| 1873 | add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF; |
| 1874 | add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF; |
| 1875 | *add_data_len = 13; |
| 1876 | } |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1879 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 1880 | mbedtls_ssl_transform *transform, |
| 1881 | mbedtls_record *rec, |
| 1882 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1883 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1884 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1885 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1886 | int auth_done = 0; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1887 | unsigned char * data; |
Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 1888 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1889 | size_t add_data_len; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1890 | size_t post_avail; |
| 1891 | |
| 1892 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1893 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1894 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1895 | ((void) ssl); |
| 1896 | #endif |
| 1897 | |
| 1898 | /* The PRNG is used for dynamic IV generation that's used |
| 1899 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
| 1900 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 1901 | ( defined(MBEDTLS_AES_C) || \ |
| 1902 | defined(MBEDTLS_ARIA_C) || \ |
| 1903 | defined(MBEDTLS_CAMELLIA_C) ) && \ |
| 1904 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) |
| 1905 | ((void) f_rng); |
| 1906 | ((void) p_rng); |
| 1907 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1908 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1909 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1910 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1911 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1912 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1913 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 1914 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1915 | } |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1916 | if( rec == NULL |
| 1917 | || rec->buf == NULL |
| 1918 | || rec->buf_len < rec->data_offset |
| 1919 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1920 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1921 | || rec->cid_len != 0 |
| 1922 | #endif |
| 1923 | ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1924 | { |
| 1925 | 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] | 1926 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1929 | data = rec->buf + rec->data_offset; |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1930 | 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] | 1931 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1932 | data, rec->data_len ); |
| 1933 | |
| 1934 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 1935 | |
| 1936 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 1937 | { |
| 1938 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", |
| 1939 | (unsigned) rec->data_len, |
| 1940 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 1941 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1942 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 1943 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1944 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1945 | /* |
| 1946 | * Add CID information |
| 1947 | */ |
| 1948 | rec->cid_len = transform->out_cid_len; |
| 1949 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 1950 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1951 | |
| 1952 | if( rec->cid_len != 0 ) |
| 1953 | { |
| 1954 | /* |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 1955 | * Wrap plaintext into DTLSInnerPlaintext structure. |
| 1956 | * See ssl_cid_build_inner_plaintext() for more information. |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1957 | * |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 1958 | * Note that this changes `rec->data_len`, and hence |
| 1959 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1960 | */ |
| 1961 | if( ssl_cid_build_inner_plaintext( data, |
| 1962 | &rec->data_len, |
| 1963 | post_avail, |
| 1964 | rec->type ) != 0 ) |
| 1965 | { |
| 1966 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 1967 | } |
| 1968 | |
| 1969 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 1970 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1971 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1972 | |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1973 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 1974 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1975 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1976 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1977 | */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1978 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1979 | if( mode == MBEDTLS_MODE_STREAM || |
| 1980 | ( mode == MBEDTLS_MODE_CBC |
| 1981 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1982 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1983 | #endif |
| 1984 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1985 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1986 | if( post_avail < transform->maclen ) |
| 1987 | { |
| 1988 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 1989 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 1990 | } |
| 1991 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1992 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1993 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1994 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1995 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1996 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, |
| 1997 | data, rec->data_len, rec->ctr, rec->type, mac ); |
| 1998 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1999 | } |
| 2000 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2001 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2002 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2003 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2004 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2005 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2006 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2007 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2008 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2009 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2010 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2011 | add_data_len ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2012 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2013 | data, rec->data_len ); |
| 2014 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2015 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
| 2016 | |
| 2017 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2018 | } |
| 2019 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2020 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2021 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2022 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2023 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2024 | } |
| 2025 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2026 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 2027 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2028 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2029 | rec->data_len += transform->maclen; |
| 2030 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2031 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2032 | } |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2033 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2034 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2035 | /* |
| 2036 | * Encrypt |
| 2037 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2038 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2039 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2040 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2041 | int ret; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2042 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2043 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2044 | "including %d bytes of padding", |
| 2045 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2046 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2047 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2048 | transform->iv_enc, transform->ivlen, |
| 2049 | data, rec->data_len, |
| 2050 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2052 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2053 | return( ret ); |
| 2054 | } |
| 2055 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2056 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2057 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2058 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2059 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2060 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2061 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2062 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2063 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2064 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2065 | #if defined(MBEDTLS_GCM_C) || \ |
| 2066 | defined(MBEDTLS_CCM_C) || \ |
| 2067 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2069 | mode == MBEDTLS_MODE_CCM || |
| 2070 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2071 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2072 | int ret; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2073 | unsigned char iv[12]; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2074 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2075 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2076 | /* Check that there's space for both the authentication tag |
| 2077 | * and the explicit IV before and after the record content. */ |
| 2078 | if( post_avail < transform->taglen || |
| 2079 | rec->data_offset < explicit_iv_len ) |
| 2080 | { |
| 2081 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2082 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2083 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2084 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2085 | /* |
| 2086 | * Generate IV |
| 2087 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2088 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2089 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2090 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2091 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2092 | memcpy( iv + transform->fixed_ivlen, rec->ctr, |
| 2093 | explicit_iv_len ); |
| 2094 | /* Prefix record content with explicit IV. */ |
| 2095 | memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2096 | } |
| 2097 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2098 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2099 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2100 | unsigned char i; |
| 2101 | |
| 2102 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 2103 | |
| 2104 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2105 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2106 | } |
| 2107 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2108 | { |
| 2109 | /* 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] | 2110 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2111 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2112 | } |
| 2113 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2114 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 0888581 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2115 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2116 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 2117 | iv, transform->ivlen ); |
| 2118 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2119 | data - explicit_iv_len, explicit_iv_len ); |
| 2120 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2121 | add_data, add_data_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2122 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2123 | "including 0 bytes of padding", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2124 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2125 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2126 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2127 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2128 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2129 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2130 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2131 | iv, transform->ivlen, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2132 | add_data, add_data_len, /* add data */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2133 | data, rec->data_len, /* source */ |
| 2134 | data, &rec->data_len, /* destination */ |
| 2135 | data + rec->data_len, transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2136 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2137 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2138 | return( ret ); |
| 2139 | } |
| 2140 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2141 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
| 2142 | data + rec->data_len, transform->taglen ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2143 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2144 | rec->data_len += transform->taglen + explicit_iv_len; |
| 2145 | rec->data_offset -= explicit_iv_len; |
| 2146 | post_avail -= transform->taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2147 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2148 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2149 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2150 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2151 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2152 | ( 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] | 2153 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2154 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2155 | int ret; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2156 | size_t padlen, i; |
| 2157 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2158 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2159 | /* Currently we're always using minimal padding |
| 2160 | * (up to 255 bytes would be allowed). */ |
| 2161 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 2162 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2163 | padlen = 0; |
| 2164 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2165 | /* Check there's enough space in the buffer for the padding. */ |
| 2166 | if( post_avail < padlen + 1 ) |
| 2167 | { |
| 2168 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2169 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2170 | } |
| 2171 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2172 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2173 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2174 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2175 | rec->data_len += padlen + 1; |
| 2176 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2177 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2178 | #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] | 2179 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2180 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 2181 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2182 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2183 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2184 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2185 | if( f_rng == NULL ) |
| 2186 | { |
| 2187 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 2188 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2189 | } |
| 2190 | |
| 2191 | if( rec->data_offset < transform->ivlen ) |
| 2192 | { |
| 2193 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2194 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2195 | } |
| 2196 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2197 | /* |
| 2198 | * Generate IV |
| 2199 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2200 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2201 | if( ret != 0 ) |
| 2202 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2203 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2204 | memcpy( data - transform->ivlen, transform->iv_enc, |
| 2205 | transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2206 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2207 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2208 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2210 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2211 | "including %d bytes of IV and %d bytes of padding", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2212 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2213 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2214 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2215 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2216 | transform->iv_enc, |
| 2217 | transform->ivlen, |
| 2218 | data, rec->data_len, |
| 2219 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2220 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2221 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2222 | return( ret ); |
| 2223 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2224 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2225 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2228 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2229 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2231 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2232 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2233 | { |
| 2234 | /* |
| 2235 | * Save IV in SSL3 and TLS1 |
| 2236 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2237 | memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 2238 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2239 | } |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2240 | else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2241 | #endif |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2242 | { |
| 2243 | data -= transform->ivlen; |
| 2244 | rec->data_offset -= transform->ivlen; |
| 2245 | rec->data_len += transform->ivlen; |
| 2246 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2248 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2249 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2250 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2251 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2252 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2253 | /* |
| 2254 | * MAC(MAC_write_key, seq_num + |
| 2255 | * TLSCipherText.type + |
| 2256 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2257 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2258 | * IV + // except for TLS 1.0 |
| 2259 | * ENC(content + padding + padding_length)); |
| 2260 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2261 | |
| 2262 | if( post_avail < transform->maclen) |
| 2263 | { |
| 2264 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2265 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2266 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2267 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2268 | 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] | 2269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2270 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2271 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2272 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2273 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2274 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2275 | add_data_len ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2276 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2277 | data, rec->data_len ); |
| 2278 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2279 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2280 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2281 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2282 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2283 | rec->data_len += transform->maclen; |
| 2284 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2285 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2286 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2287 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2288 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2289 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2290 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2291 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2293 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2294 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2295 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2296 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2297 | /* Make extra sure authentication was performed, exactly once */ |
| 2298 | if( auth_done != 1 ) |
| 2299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2301 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2302 | } |
| 2303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2304 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2305 | |
| 2306 | return( 0 ); |
| 2307 | } |
| 2308 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2309 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl, |
| 2310 | mbedtls_ssl_transform *transform, |
| 2311 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2312 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2313 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2314 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2315 | int ret, auth_done = 0; |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2316 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2317 | size_t padlen = 0, correct = 1; |
| 2318 | #endif |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2319 | unsigned char* data; |
Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2320 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2321 | size_t add_data_len; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2322 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2323 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2324 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2325 | ((void) ssl); |
| 2326 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2328 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2329 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2330 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2331 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) ); |
| 2332 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2333 | } |
| 2334 | if( rec == NULL || |
| 2335 | rec->buf == NULL || |
| 2336 | rec->buf_len < rec->data_offset || |
| 2337 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 2338 | { |
| 2339 | 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] | 2340 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2341 | } |
| 2342 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2343 | data = rec->buf + rec->data_offset; |
| 2344 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2345 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2346 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2347 | /* |
| 2348 | * Match record's CID with incoming CID. |
| 2349 | */ |
Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2350 | if( rec->cid_len != transform->in_cid_len || |
| 2351 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 2352 | { |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2353 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2354 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2355 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2357 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2358 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2359 | { |
| 2360 | padlen = 0; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2361 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2362 | transform->iv_dec, |
| 2363 | transform->ivlen, |
| 2364 | data, rec->data_len, |
| 2365 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2367 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2368 | return( ret ); |
| 2369 | } |
| 2370 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2371 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2372 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2373 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2374 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2375 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2376 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2377 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2378 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2379 | #if defined(MBEDTLS_GCM_C) || \ |
| 2380 | defined(MBEDTLS_CCM_C) || \ |
| 2381 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2382 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2383 | mode == MBEDTLS_MODE_CCM || |
| 2384 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2385 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2386 | unsigned char iv[12]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2387 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2388 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2389 | /* |
| 2390 | * Compute and update sizes |
| 2391 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2392 | if( rec->data_len < explicit_iv_len + transform->taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2393 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2394 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2395 | "+ taglen (%d)", rec->data_len, |
| 2396 | explicit_iv_len, transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2397 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2398 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2399 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2400 | /* |
| 2401 | * Prepare IV |
| 2402 | */ |
| 2403 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2404 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2405 | /* GCM and CCM: fixed || explicit (transmitted) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2406 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2407 | memcpy( iv + transform->fixed_ivlen, data, 8 ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2408 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2409 | } |
| 2410 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2411 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2412 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2413 | unsigned char i; |
| 2414 | |
| 2415 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2416 | |
| 2417 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2418 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2419 | } |
| 2420 | else |
| 2421 | { |
| 2422 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2423 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2424 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2425 | } |
| 2426 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2427 | data += explicit_iv_len; |
| 2428 | rec->data_offset += explicit_iv_len; |
| 2429 | rec->data_len -= explicit_iv_len + transform->taglen; |
| 2430 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2431 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2432 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2433 | add_data, add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2434 | |
| 2435 | memcpy( transform->iv_dec + transform->fixed_ivlen, |
| 2436 | data - explicit_iv_len, explicit_iv_len ); |
| 2437 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2438 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2439 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2440 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2441 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2442 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2443 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2444 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2445 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2446 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, |
| 2447 | iv, transform->ivlen, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2448 | add_data, add_data_len, |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2449 | data, rec->data_len, |
| 2450 | data, &olen, |
| 2451 | data + rec->data_len, |
| 2452 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2453 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2454 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2455 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2456 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2457 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2458 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2459 | return( ret ); |
| 2460 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2461 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2462 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2463 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2464 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2465 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2466 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2467 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2468 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2469 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2470 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2471 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2472 | ( 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] | 2473 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2474 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2475 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2476 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2477 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2478 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2479 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2480 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2481 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 2482 | { |
| 2483 | /* The ciphertext is prefixed with the CBC IV. */ |
| 2484 | minlen += transform->ivlen; |
| 2485 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2486 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2487 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2488 | /* Size considerations: |
| 2489 | * |
| 2490 | * - The CBC cipher text must not be empty and hence |
| 2491 | * at least of size transform->ivlen. |
| 2492 | * |
| 2493 | * Together with the potential IV-prefix, this explains |
| 2494 | * the first of the two checks below. |
| 2495 | * |
| 2496 | * - The record must contain a MAC, either in plain or |
| 2497 | * encrypted, depending on whether Encrypt-then-MAC |
| 2498 | * is used or not. |
| 2499 | * - If it is, the message contains the IV-prefix, |
| 2500 | * the CBC ciphertext, and the MAC. |
| 2501 | * - If it is not, the padded plaintext, and hence |
| 2502 | * the CBC ciphertext, has at least length maclen + 1 |
| 2503 | * because there is at least the padding length byte. |
| 2504 | * |
| 2505 | * As the CBC ciphertext is not empty, both cases give the |
| 2506 | * lower bound minlen + maclen + 1 on the record size, which |
| 2507 | * we test for in the second check below. |
| 2508 | */ |
| 2509 | if( rec->data_len < minlen + transform->ivlen || |
| 2510 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2511 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2512 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2513 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 2514 | transform->ivlen, |
| 2515 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2516 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2517 | } |
| 2518 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2519 | /* |
| 2520 | * Authenticate before decrypt if enabled |
| 2521 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2522 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2523 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2524 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2525 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2527 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2528 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2529 | /* Safe due to the check data_len >= minlen + maclen + 1 above. */ |
| 2530 | rec->data_len -= transform->maclen; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2531 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2532 | 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] | 2533 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2534 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 2535 | add_data_len ); |
| 2536 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 2537 | add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2538 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 2539 | data, rec->data_len ); |
| 2540 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 2541 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2542 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2543 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 2544 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2545 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2546 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2547 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2548 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 2549 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2550 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2551 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2552 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2553 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2554 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2555 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2556 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2557 | |
| 2558 | /* |
| 2559 | * Check length sanity |
| 2560 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2561 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2562 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2563 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2564 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2565 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2566 | } |
| 2567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2568 | #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] | 2569 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2570 | * 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] | 2571 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2572 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2573 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2574 | /* This is safe because data_len >= minlen + maclen + 1 initially, |
| 2575 | * and at this point we have at most subtracted maclen (note that |
| 2576 | * minlen == transform->ivlen here). */ |
| 2577 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2578 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2579 | data += transform->ivlen; |
| 2580 | rec->data_offset += transform->ivlen; |
| 2581 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2582 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2583 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2584 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2585 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2586 | transform->iv_dec, transform->ivlen, |
| 2587 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2588 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2589 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2590 | return( ret ); |
| 2591 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2592 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2593 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2594 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2595 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2596 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2597 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2599 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2600 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2601 | { |
| 2602 | /* |
| 2603 | * Save IV in SSL3 and TLS1 |
| 2604 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2605 | memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 2606 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2607 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2608 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2609 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2610 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 2611 | * subtracted at most minlen and maclen up to this point, |
| 2612 | * data_len > 0. */ |
| 2613 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2614 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2615 | if( auth_done == 1 ) |
| 2616 | { |
| 2617 | correct *= ( rec->data_len >= padlen + 1 ); |
| 2618 | padlen *= ( rec->data_len >= padlen + 1 ); |
| 2619 | } |
| 2620 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2621 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2622 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2623 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 2624 | { |
| 2625 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
| 2626 | rec->data_len, |
| 2627 | transform->maclen, |
| 2628 | padlen + 1 ) ); |
| 2629 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2630 | #endif |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2631 | |
| 2632 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
| 2633 | padlen *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2634 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2635 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2636 | padlen++; |
| 2637 | |
| 2638 | /* Regardless of the validity of the padding, |
| 2639 | * we have data_len >= padlen here. */ |
| 2640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2641 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2642 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2643 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2644 | if( padlen > transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2645 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2647 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2648 | "should be no more than %d", |
| 2649 | padlen, transform->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2650 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2651 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2655 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2656 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2657 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2658 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2659 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2660 | /* The padding check involves a series of up to 256 |
| 2661 | * consecutive memory reads at the end of the record |
| 2662 | * plaintext buffer. In order to hide the length and |
| 2663 | * validity of the padding, always perform exactly |
| 2664 | * `min(256,plaintext_len)` reads (but take into account |
| 2665 | * only the last `padlen` bytes for the padding check). */ |
| 2666 | size_t pad_count = 0; |
| 2667 | size_t real_count = 0; |
| 2668 | volatile unsigned char* const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2669 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2670 | /* Index of first padding byte; it has been ensured above |
| 2671 | * that the subtraction is safe. */ |
| 2672 | size_t const padding_idx = rec->data_len - padlen; |
| 2673 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 2674 | size_t const start_idx = rec->data_len - num_checks; |
| 2675 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2676 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2677 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2678 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2679 | real_count |= ( idx >= padding_idx ); |
| 2680 | pad_count += real_count * ( check[idx] == padlen - 1 ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2681 | } |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2682 | correct &= ( pad_count == padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2683 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2684 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2685 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2686 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2687 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2688 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2689 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2690 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2691 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2692 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2694 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2695 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2696 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2697 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2698 | /* If the padding was found to be invalid, padlen == 0 |
| 2699 | * and the subtraction is safe. If the padding was found valid, |
| 2700 | * padlen hasn't been changed and the previous assertion |
| 2701 | * data_len >= padlen still holds. */ |
| 2702 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2703 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2704 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2705 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2706 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2707 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2708 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2709 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2710 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2711 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2712 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2713 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2714 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2715 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2716 | |
| 2717 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2718 | * Authenticate if not done yet. |
| 2719 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2720 | */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2721 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2722 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2723 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2724 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2725 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2726 | /* If the initial value of padlen was such that |
| 2727 | * data_len < maclen + padlen + 1, then padlen |
| 2728 | * got reset to 1, and the initial check |
| 2729 | * data_len >= minlen + maclen + 1 |
| 2730 | * guarantees that at this point we still |
| 2731 | * have at least data_len >= maclen. |
| 2732 | * |
| 2733 | * If the initial value of padlen was such that |
| 2734 | * data_len >= maclen + padlen + 1, then we have |
| 2735 | * subtracted either padlen + 1 (if the padding was correct) |
| 2736 | * or 0 (if the padding was incorrect) since then, |
| 2737 | * hence data_len >= maclen in any case. |
| 2738 | */ |
| 2739 | rec->data_len -= transform->maclen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2740 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2741 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2743 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2744 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2745 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2746 | ssl_mac( &transform->md_ctx_dec, |
| 2747 | transform->mac_dec, |
| 2748 | data, rec->data_len, |
| 2749 | rec->ctr, rec->type, |
| 2750 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2751 | } |
| 2752 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2753 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2754 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2755 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2756 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2757 | { |
| 2758 | /* |
| 2759 | * Process MAC and always update for padlen afterwards to make |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2760 | * total time independent of padlen. |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2761 | * |
| 2762 | * Known timing attacks: |
| 2763 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 2764 | * |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2765 | * To compensate for different timings for the MAC calculation |
| 2766 | * depending on how much padding was removed (which is determined |
| 2767 | * by padlen), process extra_run more blocks through the hash |
| 2768 | * function. |
| 2769 | * |
| 2770 | * The formula in the paper is |
| 2771 | * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) |
| 2772 | * where L1 is the size of the header plus the decrypted message |
| 2773 | * plus CBC padding and L2 is the size of the header plus the |
| 2774 | * decrypted message. This is for an underlying hash function |
| 2775 | * with 64-byte blocks. |
| 2776 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values |
| 2777 | * correctly. We round down instead of up, so -56 is the correct |
| 2778 | * value for our calculations instead of -55. |
| 2779 | * |
Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 2780 | * Repeat the formula rather than defining a block_size variable. |
| 2781 | * This avoids requiring division by a variable at runtime |
| 2782 | * (which would be marginally less efficient and would require |
| 2783 | * linking an extra division function in some builds). |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2784 | */ |
| 2785 | size_t j, extra_run = 0; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2786 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2787 | |
| 2788 | /* |
| 2789 | * The next two sizes are the minimum and maximum values of |
| 2790 | * in_msglen over all padlen values. |
| 2791 | * |
| 2792 | * They're independent of padlen, since we previously did |
| 2793 | * in_msglen -= padlen. |
| 2794 | * |
| 2795 | * Note that max_len + maclen is never more than the buffer |
| 2796 | * length, as we previously did in_msglen -= maclen too. |
| 2797 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2798 | const size_t max_len = rec->data_len + padlen; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2799 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 2800 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2801 | memset( tmp, 0, sizeof( tmp ) ); |
| 2802 | |
| 2803 | switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2804 | { |
Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 2805 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ |
| 2806 | defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2807 | case MBEDTLS_MD_MD5: |
| 2808 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2809 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2810 | /* 8 bytes of message size, 64-byte compression blocks */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2811 | extra_run = |
| 2812 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - |
| 2813 | ( add_data_len + rec->data_len + 8 ) / 64; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2814 | break; |
| 2815 | #endif |
Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 2816 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2817 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2818 | /* 16 bytes of message size, 128-byte compression blocks */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2819 | extra_run = |
| 2820 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - |
| 2821 | ( add_data_len + rec->data_len + 16 ) / 128; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2822 | break; |
| 2823 | #endif |
| 2824 | default: |
Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 2825 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2826 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2827 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2828 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2829 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2830 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2831 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 2832 | add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2833 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, |
| 2834 | rec->data_len ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2835 | /* Make sure we access everything even when padlen > 0. This |
| 2836 | * makes the synchronisation requirements for just-in-time |
| 2837 | * Prime+Probe attacks much tighter and hopefully impractical. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2838 | ssl_read_memory( data + rec->data_len, padlen ); |
| 2839 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2840 | |
| 2841 | /* Call mbedtls_md_process at least once due to cache attacks |
| 2842 | * that observe whether md_process() was called of not */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 2843 | for( j = 0; j < extra_run + 1; j++ ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2844 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2845 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2846 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2847 | |
| 2848 | /* Make sure we access all the memory that could contain the MAC, |
| 2849 | * before we check it in the next code block. This makes the |
| 2850 | * synchronisation requirements for just-in-time Prime+Probe |
| 2851 | * attacks much tighter and hopefully impractical. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2852 | ssl_read_memory( data + min_len, |
| 2853 | max_len - min_len + transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2854 | } |
| 2855 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2856 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2857 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2858 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2859 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2860 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2861 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2862 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2863 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2864 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
| 2865 | 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] | 2866 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2867 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2868 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 2869 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2870 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2871 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2872 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2873 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2874 | correct = 0; |
| 2875 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2876 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2877 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 2878 | |
| 2879 | /* |
| 2880 | * Finally check the correct flag |
| 2881 | */ |
| 2882 | if( correct == 0 ) |
| 2883 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2884 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2885 | |
| 2886 | /* Make extra sure authentication was performed, exactly once */ |
| 2887 | if( auth_done != 1 ) |
| 2888 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2889 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2890 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2891 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2892 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2893 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2894 | if( rec->cid_len != 0 ) |
| 2895 | { |
| 2896 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, |
| 2897 | &rec->type ); |
| 2898 | if( ret != 0 ) |
| 2899 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 2900 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2901 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2903 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2904 | |
| 2905 | return( 0 ); |
| 2906 | } |
| 2907 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2908 | #undef MAC_NONE |
| 2909 | #undef MAC_PLAINTEXT |
| 2910 | #undef MAC_CIPHERTEXT |
| 2911 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2912 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2913 | /* |
| 2914 | * Compression/decompression functions |
| 2915 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2916 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2917 | { |
| 2918 | int ret; |
| 2919 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 2920 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2921 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2922 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2923 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2924 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2925 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2926 | if( len_pre == 0 ) |
| 2927 | return( 0 ); |
| 2928 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2929 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 2930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2931 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2932 | ssl->out_msglen ) ); |
| 2933 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2934 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2935 | ssl->out_msg, ssl->out_msglen ); |
| 2936 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2937 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 2938 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 2939 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2940 | 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] | 2941 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2942 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2943 | if( ret != Z_OK ) |
| 2944 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2945 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 2946 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2949 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 2950 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2953 | ssl->out_msglen ) ); |
| 2954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2955 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2956 | ssl->out_msg, ssl->out_msglen ); |
| 2957 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2958 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2959 | |
| 2960 | return( 0 ); |
| 2961 | } |
| 2962 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2963 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2964 | { |
| 2965 | int ret; |
| 2966 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2967 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2968 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2969 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2970 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2971 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2972 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2973 | if( len_pre == 0 ) |
| 2974 | return( 0 ); |
| 2975 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2976 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 2977 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2978 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2979 | ssl->in_msglen ) ); |
| 2980 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2981 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2982 | ssl->in_msg, ssl->in_msglen ); |
| 2983 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2984 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 2985 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 2986 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2987 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2988 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2989 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2990 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2991 | if( ret != Z_OK ) |
| 2992 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2993 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 2994 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2997 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2998 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2999 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3000 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3001 | ssl->in_msglen ) ); |
| 3002 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3003 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3004 | ssl->in_msg, ssl->in_msglen ); |
| 3005 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3006 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3007 | |
| 3008 | return( 0 ); |
| 3009 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3013 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3014 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3015 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3016 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3017 | { |
| 3018 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 3019 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3020 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3021 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3022 | uint32_t ratio = |
| 3023 | mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) / |
| 3024 | mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3025 | unsigned char doublings = 1; |
| 3026 | |
| 3027 | while( ratio != 0 ) |
| 3028 | { |
| 3029 | ++doublings; |
| 3030 | ratio >>= 1; |
| 3031 | } |
| 3032 | |
| 3033 | if( ++ssl->renego_records_seen > doublings ) |
| 3034 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 3035 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3036 | return( 0 ); |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | return( ssl_write_hello_request( ssl ) ); |
| 3041 | } |
| 3042 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3043 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3044 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3045 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3046 | * Fill the input message buffer by appending data to it. |
| 3047 | * 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] | 3048 | * |
| 3049 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 3050 | * available (from this read and/or a previous one). Otherwise, an error code |
| 3051 | * is returned (possibly EOF or WANT_READ). |
| 3052 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3053 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 3054 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 3055 | * since we always read a whole datagram at once. |
| 3056 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3057 | * 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] | 3058 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3059 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3060 | 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] | 3061 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3062 | int ret; |
| 3063 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3064 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3065 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3066 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3067 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 3068 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3069 | 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] | 3070 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3071 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3072 | } |
| 3073 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3074 | 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] | 3075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3076 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 3077 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3078 | } |
| 3079 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3080 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3081 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3082 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3083 | uint32_t timeout; |
| 3084 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3085 | /* Just to be sure */ |
| 3086 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) |
| 3087 | { |
| 3088 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3089 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3090 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3091 | } |
| 3092 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3093 | /* |
| 3094 | * The point is, we need to always read a full datagram at once, so we |
| 3095 | * sometimes read more then requested, and handle the additional data. |
| 3096 | * It could be the rest of the current record (while fetching the |
| 3097 | * header) and/or some other records in the same datagram. |
| 3098 | */ |
| 3099 | |
| 3100 | /* |
| 3101 | * Move to the next record in the already read datagram if applicable |
| 3102 | */ |
| 3103 | if( ssl->next_record_offset != 0 ) |
| 3104 | { |
| 3105 | if( ssl->in_left < ssl->next_record_offset ) |
| 3106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3108 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | ssl->in_left -= ssl->next_record_offset; |
| 3112 | |
| 3113 | if( ssl->in_left != 0 ) |
| 3114 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3115 | 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] | 3116 | ssl->next_record_offset ) ); |
| 3117 | memmove( ssl->in_hdr, |
| 3118 | ssl->in_hdr + ssl->next_record_offset, |
| 3119 | ssl->in_left ); |
| 3120 | } |
| 3121 | |
| 3122 | ssl->next_record_offset = 0; |
| 3123 | } |
| 3124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3125 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3126 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3127 | |
| 3128 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3129 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3130 | */ |
| 3131 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3133 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3134 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3135 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3136 | |
| 3137 | /* |
Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 3138 | * 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] | 3139 | * are not at the beginning of a new record, the caller did something |
| 3140 | * wrong. |
| 3141 | */ |
| 3142 | if( ssl->in_left != 0 ) |
| 3143 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3144 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3145 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3146 | } |
| 3147 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3148 | /* |
| 3149 | * Don't even try to read if time's out already. |
| 3150 | * This avoids by-passing the timer when repeatedly receiving messages |
| 3151 | * that will end up being dropped. |
| 3152 | */ |
| 3153 | if( ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3154 | { |
| 3155 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3156 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3157 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3158 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3159 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3160 | 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] | 3161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3162 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3163 | timeout = ssl->handshake->retransmit_timeout; |
| 3164 | else |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3165 | timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3167 | 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] | 3168 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3169 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3170 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 3171 | timeout ); |
| 3172 | else |
| 3173 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 3174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3175 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3176 | |
| 3177 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3178 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3179 | } |
| 3180 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3181 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3183 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3184 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3185 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3186 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3187 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3188 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 3189 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3190 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3191 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3192 | } |
| 3193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3194 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3195 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3196 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3197 | return( ret ); |
| 3198 | } |
| 3199 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3200 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3201 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3202 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 3203 | else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 3204 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3205 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3206 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3207 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3208 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3209 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3210 | return( ret ); |
| 3211 | } |
| 3212 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3213 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3214 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3215 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3216 | } |
| 3217 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3218 | if( ret < 0 ) |
| 3219 | return( ret ); |
| 3220 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3221 | ssl->in_left = ret; |
| 3222 | } |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3223 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 3224 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3225 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3227 | 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] | 3228 | ssl->in_left, nb_want ) ); |
| 3229 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3230 | while( ssl->in_left < nb_want ) |
| 3231 | { |
| 3232 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3233 | |
| 3234 | if( ssl_check_timer( ssl ) != 0 ) |
| 3235 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 3236 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3237 | { |
| 3238 | if( ssl->f_recv_timeout != NULL ) |
| 3239 | { |
| 3240 | ret = ssl->f_recv_timeout( ssl->p_bio, |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 3241 | ssl->in_hdr + ssl->in_left, len, |
| 3242 | mbedtls_ssl_conf_get_read_timeout( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3243 | } |
| 3244 | else |
| 3245 | { |
| 3246 | ret = ssl->f_recv( ssl->p_bio, |
| 3247 | ssl->in_hdr + ssl->in_left, len ); |
| 3248 | } |
| 3249 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3251 | 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] | 3252 | ssl->in_left, nb_want ) ); |
| 3253 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3254 | |
| 3255 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3256 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3257 | |
| 3258 | if( ret < 0 ) |
| 3259 | return( ret ); |
| 3260 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3261 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3262 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3263 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3264 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3265 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3266 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3267 | } |
| 3268 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3269 | ssl->in_left += ret; |
| 3270 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3271 | } |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3272 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3274 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3275 | |
| 3276 | return( 0 ); |
| 3277 | } |
| 3278 | |
| 3279 | /* |
| 3280 | * Flush any data not yet written |
| 3281 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3282 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3283 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3284 | int ret; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3285 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3287 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3288 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3289 | if( ssl->f_send == NULL ) |
| 3290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3291 | 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] | 3292 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3293 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3294 | } |
| 3295 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3296 | /* Avoid incrementing counter if data is flushed */ |
| 3297 | if( ssl->out_left == 0 ) |
| 3298 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3299 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3300 | return( 0 ); |
| 3301 | } |
| 3302 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3303 | while( ssl->out_left > 0 ) |
| 3304 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3305 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3306 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3307 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3308 | buf = ssl->out_hdr - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3309 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 3310 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3311 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3312 | |
| 3313 | if( ret <= 0 ) |
| 3314 | return( ret ); |
| 3315 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3316 | 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] | 3317 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3318 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3319 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3320 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3321 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3322 | } |
| 3323 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3324 | ssl->out_left -= ret; |
| 3325 | } |
| 3326 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3327 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3328 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3329 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3330 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3331 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3332 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3333 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3334 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3335 | { |
| 3336 | ssl->out_hdr = ssl->out_buf + 8; |
| 3337 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3338 | #endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3339 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3341 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3342 | |
| 3343 | return( 0 ); |
| 3344 | } |
| 3345 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3346 | /* |
| 3347 | * Functions to handle the DTLS retransmission state machine |
| 3348 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3349 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3350 | /* |
| 3351 | * Append current handshake message to current outgoing flight |
| 3352 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3353 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3354 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3355 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3356 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 3357 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 3358 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3359 | |
| 3360 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3361 | 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] | 3362 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3363 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3364 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3365 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3366 | } |
| 3367 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3368 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3369 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3370 | 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] | 3371 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3372 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | /* Copy current handshake message with headers */ |
| 3376 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 3377 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3378 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3379 | msg->next = NULL; |
| 3380 | |
| 3381 | /* Append to the current flight */ |
| 3382 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3383 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3384 | else |
| 3385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3386 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3387 | while( cur->next != NULL ) |
| 3388 | cur = cur->next; |
| 3389 | cur->next = msg; |
| 3390 | } |
| 3391 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3392 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3393 | return( 0 ); |
| 3394 | } |
| 3395 | |
| 3396 | /* |
| 3397 | * Free the current flight of handshake messages |
| 3398 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3399 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3400 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3401 | mbedtls_ssl_flight_item *cur = flight; |
| 3402 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3403 | |
| 3404 | while( cur != NULL ) |
| 3405 | { |
| 3406 | next = cur->next; |
| 3407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3408 | mbedtls_free( cur->p ); |
| 3409 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3410 | |
| 3411 | cur = next; |
| 3412 | } |
| 3413 | } |
| 3414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3415 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3416 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3417 | #endif |
| 3418 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3419 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3420 | * Swap transform_out and out_ctr with the alternative ones |
| 3421 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3422 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3423 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3424 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3425 | unsigned char tmp_out_ctr[8]; |
| 3426 | |
| 3427 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 3428 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3429 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3430 | return; |
| 3431 | } |
| 3432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3433 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3434 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3435 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3436 | tmp_transform = ssl->transform_out; |
| 3437 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 3438 | ssl->handshake->alt_transform_out = tmp_transform; |
| 3439 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3440 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3441 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3442 | memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3443 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3444 | |
| 3445 | /* Adjust to the newly activated transform */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 3446 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3447 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3448 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3449 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3450 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3451 | 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] | 3452 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3453 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3454 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3455 | } |
| 3456 | } |
| 3457 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | /* |
| 3461 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3462 | */ |
| 3463 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3464 | { |
| 3465 | int ret = 0; |
| 3466 | |
| 3467 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3468 | |
| 3469 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3470 | |
| 3471 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3472 | |
| 3473 | return( ret ); |
| 3474 | } |
| 3475 | |
| 3476 | /* |
| 3477 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3478 | * |
| 3479 | * Need to remember the current message in case flush_output returns |
| 3480 | * 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] | 3481 | * 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] | 3482 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3483 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3484 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3485 | int ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3486 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3488 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3489 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3490 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3491 | |
| 3492 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3493 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3494 | ssl_swap_epochs( ssl ); |
| 3495 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3496 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3497 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3498 | |
| 3499 | while( ssl->handshake->cur_msg != NULL ) |
| 3500 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3501 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3502 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3503 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3504 | int const is_finished = |
| 3505 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3506 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 3507 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3508 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 3509 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 3510 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3511 | /* Swap epochs before sending Finished: we can't do it after |
| 3512 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 3513 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3514 | 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] | 3515 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3516 | 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] | 3517 | ssl_swap_epochs( ssl ); |
| 3518 | } |
| 3519 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3520 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3521 | if( ret < 0 ) |
| 3522 | return( ret ); |
| 3523 | max_frag_len = (size_t) ret; |
| 3524 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3525 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 3526 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3527 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3528 | if( max_frag_len == 0 ) |
| 3529 | { |
| 3530 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3531 | return( ret ); |
| 3532 | |
| 3533 | continue; |
| 3534 | } |
| 3535 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3536 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3537 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3538 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3539 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3540 | /* Update position inside current message */ |
| 3541 | ssl->handshake->cur_msg_p += cur->len; |
| 3542 | } |
| 3543 | else |
| 3544 | { |
| 3545 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 3546 | const size_t hs_len = cur->len - 12; |
| 3547 | const size_t frag_off = p - ( cur->p + 12 ); |
| 3548 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3549 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3550 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3551 | 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] | 3552 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3553 | if( is_finished ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3554 | ssl_swap_epochs( ssl ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3555 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3556 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3557 | return( ret ); |
| 3558 | |
| 3559 | continue; |
| 3560 | } |
| 3561 | max_hs_frag_len = max_frag_len - 12; |
| 3562 | |
| 3563 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 3564 | max_hs_frag_len : rem_len; |
| 3565 | |
| 3566 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3567 | { |
| 3568 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3569 | (unsigned) cur_hs_frag_len, |
| 3570 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3571 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 3572 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3573 | /* Messages are stored with handshake headers as if not fragmented, |
| 3574 | * copy beginning of headers then fill fragmentation fields. |
| 3575 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 3576 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3577 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3578 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 3579 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 3580 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 3581 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3582 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 3583 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 3584 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3585 | |
| 3586 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 3587 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 3588 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3589 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 3590 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3591 | ssl->out_msgtype = cur->type; |
| 3592 | |
| 3593 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3594 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | /* If done with the current message move to the next one if any */ |
| 3598 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 3599 | { |
| 3600 | if( cur->next != NULL ) |
| 3601 | { |
| 3602 | ssl->handshake->cur_msg = cur->next; |
| 3603 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 3604 | } |
| 3605 | else |
| 3606 | { |
| 3607 | ssl->handshake->cur_msg = NULL; |
| 3608 | ssl->handshake->cur_msg_p = NULL; |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3613 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3614 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3615 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3616 | return( ret ); |
| 3617 | } |
| 3618 | } |
| 3619 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3620 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3621 | return( ret ); |
| 3622 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3623 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3624 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 3625 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 3626 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3628 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3629 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 3630 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3631 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3632 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3633 | |
| 3634 | return( 0 ); |
| 3635 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3636 | |
| 3637 | /* |
| 3638 | * To be called when the last message of an incoming flight is received. |
| 3639 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3640 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3641 | { |
| 3642 | /* We won't need to resend that one any more */ |
| 3643 | ssl_flight_free( ssl->handshake->flight ); |
| 3644 | ssl->handshake->flight = NULL; |
| 3645 | ssl->handshake->cur_msg = NULL; |
| 3646 | |
| 3647 | /* The next incoming flight will start with this msg_seq */ |
| 3648 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 3649 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3650 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3651 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3652 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3653 | /* Clear future message buffering structure. */ |
| 3654 | ssl_buffering_free( ssl ); |
| 3655 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3656 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3657 | ssl_set_timer( ssl, 0 ); |
| 3658 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3659 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3660 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3661 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3662 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3663 | } |
| 3664 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3665 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3666 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3667 | |
| 3668 | /* |
| 3669 | * To be called when the last message of an outgoing flight is send. |
| 3670 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3671 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3672 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3673 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3674 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3675 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3676 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3677 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3679 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3680 | } |
| 3681 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3682 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3683 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3684 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3685 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3686 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3687 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3688 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3689 | |
| 3690 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3691 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3692 | * |
| 3693 | * - fill in handshake headers |
| 3694 | * - update handshake checksum |
| 3695 | * - DTLS: save message for resending |
| 3696 | * - then pass to the record layer |
| 3697 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3698 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 3699 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3700 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3701 | * Inputs: |
| 3702 | * - ssl->out_msglen: 4 + actual handshake message len |
| 3703 | * (4 is the size of handshake headers for TLS) |
| 3704 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 3705 | * - ssl->out_msg + 4: the handshake message body |
| 3706 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 3707 | * 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] | 3708 | * - ssl->out_msglen: the length of the record contents |
| 3709 | * (including handshake headers but excluding record headers) |
| 3710 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3711 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3712 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3713 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3714 | int ret; |
| 3715 | const size_t hs_len = ssl->out_msglen - 4; |
| 3716 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3717 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3718 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 3719 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3720 | /* |
| 3721 | * Sanity checks |
| 3722 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3723 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3724 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3725 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3726 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 3727 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
| 3728 | if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 3729 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 3730 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 3731 | MBEDTLS_SSL_IS_CLIENT ) ) |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3732 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 3733 | { |
| 3734 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3735 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3736 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3737 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3738 | |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3739 | /* Whenever we send anything different from a |
| 3740 | * HelloRequest we should be in a handshake - double check. */ |
| 3741 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3742 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3743 | ssl->handshake == NULL ) |
| 3744 | { |
| 3745 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3746 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3747 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3748 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3749 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3750 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3751 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3752 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3753 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3754 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3755 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3756 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3757 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3758 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 3759 | /* Double-check that we did not exceed the bounds |
| 3760 | * of the outgoing record buffer. |
| 3761 | * This should never fail as the various message |
| 3762 | * writing functions must obey the bounds of the |
| 3763 | * outgoing record buffer, but better be safe. |
| 3764 | * |
| 3765 | * Note: We deliberately do not check for the MTU or MFL here. |
| 3766 | */ |
| 3767 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 3768 | { |
| 3769 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 3770 | "size %u, maximum %u", |
| 3771 | (unsigned) ssl->out_msglen, |
| 3772 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 3773 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3774 | } |
| 3775 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3776 | /* |
| 3777 | * Fill handshake headers |
| 3778 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3779 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3780 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3781 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 3782 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 3783 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3784 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3785 | /* |
| 3786 | * DTLS has additional fields in the Handshake layer, |
| 3787 | * between the length field and the actual payload: |
| 3788 | * uint16 message_seq; |
| 3789 | * uint24 fragment_offset; |
| 3790 | * uint24 fragment_length; |
| 3791 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3792 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3793 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3794 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3795 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3796 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 3797 | { |
| 3798 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 3799 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3800 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3801 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 3802 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3803 | } |
| 3804 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3805 | 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] | 3806 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3807 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3808 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3809 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3810 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 3811 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 3812 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 3813 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3814 | } |
| 3815 | else |
| 3816 | { |
| 3817 | ssl->out_msg[4] = 0; |
| 3818 | ssl->out_msg[5] = 0; |
| 3819 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3820 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3821 | /* Handshake hashes are computed without fragmentation, |
| 3822 | * so set frag_offset = 0 and frag_len = hs_len for now */ |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3823 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 3824 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3825 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3826 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3827 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 3828 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3829 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 3830 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3833 | /* 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] | 3834 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3835 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3836 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3837 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3838 | { |
| 3839 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 3840 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3841 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3842 | return( ret ); |
| 3843 | } |
| 3844 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3845 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3846 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3847 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3848 | 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] | 3849 | { |
| 3850 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 3851 | return( ret ); |
| 3852 | } |
| 3853 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3854 | |
| 3855 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 3856 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3857 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | /* |
| 3861 | * Record layer functions |
| 3862 | */ |
| 3863 | |
| 3864 | /* |
| 3865 | * Write current record. |
| 3866 | * |
| 3867 | * Uses: |
| 3868 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 3869 | * - ssl->out_msglen: length of the record content (excl headers) |
| 3870 | * - ssl->out_msg: record content |
| 3871 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3872 | 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] | 3873 | { |
| 3874 | int ret, done = 0; |
| 3875 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3876 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3877 | |
| 3878 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3880 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3881 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3882 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3883 | { |
| 3884 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 3885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3886 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3887 | return( ret ); |
| 3888 | } |
| 3889 | |
| 3890 | len = ssl->out_msglen; |
| 3891 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3892 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3893 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3894 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3895 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3896 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3897 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3898 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3899 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 3900 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3901 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3902 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 3903 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3904 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3905 | |
| 3906 | if( ret == 0 ) |
| 3907 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3908 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3909 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3910 | if( !done ) |
| 3911 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3912 | unsigned i; |
| 3913 | size_t protected_record_size; |
| 3914 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3915 | /* Skip writing the record content type to after the encryption, |
| 3916 | * as it may change when using the CID extension. */ |
| 3917 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3918 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3919 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3920 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3921 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3922 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 3923 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3924 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3925 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3926 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 3927 | mbedtls_record rec; |
| 3928 | |
| 3929 | rec.buf = ssl->out_iv; |
| 3930 | rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - |
| 3931 | ( ssl->out_iv - ssl->out_buf ); |
| 3932 | rec.data_len = ssl->out_msglen; |
| 3933 | rec.data_offset = ssl->out_msg - rec.buf; |
| 3934 | |
| 3935 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
| 3936 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 3937 | ssl->conf->transport, rec.ver ); |
| 3938 | rec.type = ssl->out_msgtype; |
| 3939 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3940 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 3941 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3942 | rec.cid_len = 0; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3943 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3944 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3945 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame^] | 3946 | mbedtls_ssl_conf_get_frng( ssl->conf ), |
| 3947 | ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3949 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3950 | return( ret ); |
| 3951 | } |
| 3952 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 3953 | if( rec.data_offset != 0 ) |
| 3954 | { |
| 3955 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3956 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3957 | } |
| 3958 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3959 | /* Update the record content type and CID. */ |
| 3960 | ssl->out_msgtype = rec.type; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3961 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 3962 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3963 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c5aee96 | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 3964 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 3965 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 3966 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3969 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3970 | |
| 3971 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3972 | /* In case of DTLS, double-check that we don't exceed |
| 3973 | * the remaining space in the datagram. */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3974 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3975 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 3976 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3977 | if( ret < 0 ) |
| 3978 | return( ret ); |
| 3979 | |
| 3980 | if( protected_record_size > (size_t) ret ) |
| 3981 | { |
| 3982 | /* Should never happen */ |
| 3983 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3984 | } |
| 3985 | } |
| 3986 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3987 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3988 | /* Now write the potentially updated record content type. */ |
| 3989 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 3990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3991 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 3992 | "version = [%d:%d], msglen = %d", |
| 3993 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 3994 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3995 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3996 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 3997 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3998 | |
| 3999 | ssl->out_left += protected_record_size; |
| 4000 | ssl->out_hdr += protected_record_size; |
| 4001 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
| 4002 | |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4003 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 4004 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 4005 | break; |
| 4006 | |
| 4007 | /* The loop goes to its end iff the counter is wrapping */ |
| 4008 | if( i == ssl_ep_len( ssl ) ) |
| 4009 | { |
| 4010 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 4011 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4012 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4015 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4016 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 4017 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4018 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 4019 | size_t remaining; |
| 4020 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 4021 | if( ret < 0 ) |
| 4022 | { |
| 4023 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 4024 | ret ); |
| 4025 | return( ret ); |
| 4026 | } |
| 4027 | |
| 4028 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4029 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4030 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4031 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4032 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4033 | else |
| 4034 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 4035 | 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] | 4036 | } |
| 4037 | } |
| 4038 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4039 | |
| 4040 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 4041 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4042 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4043 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4044 | return( ret ); |
| 4045 | } |
| 4046 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4047 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4048 | |
| 4049 | return( 0 ); |
| 4050 | } |
| 4051 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4052 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4053 | |
| 4054 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 4055 | { |
| 4056 | if( ssl->in_msglen < ssl->in_hslen || |
| 4057 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 4058 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 4059 | { |
| 4060 | return( 1 ); |
| 4061 | } |
| 4062 | return( 0 ); |
| 4063 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4064 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4065 | 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] | 4066 | { |
| 4067 | return( ( ssl->in_msg[9] << 16 ) | |
| 4068 | ( ssl->in_msg[10] << 8 ) | |
| 4069 | ssl->in_msg[11] ); |
| 4070 | } |
| 4071 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4072 | 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] | 4073 | { |
| 4074 | return( ( ssl->in_msg[6] << 16 ) | |
| 4075 | ( ssl->in_msg[7] << 8 ) | |
| 4076 | ssl->in_msg[8] ); |
| 4077 | } |
| 4078 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4079 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4080 | { |
| 4081 | uint32_t msg_len, frag_off, frag_len; |
| 4082 | |
| 4083 | msg_len = ssl_get_hs_total_len( ssl ); |
| 4084 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4085 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4086 | |
| 4087 | if( frag_off > msg_len ) |
| 4088 | return( -1 ); |
| 4089 | |
| 4090 | if( frag_len > msg_len - frag_off ) |
| 4091 | return( -1 ); |
| 4092 | |
| 4093 | if( frag_len + 12 > ssl->in_msglen ) |
| 4094 | return( -1 ); |
| 4095 | |
| 4096 | return( 0 ); |
| 4097 | } |
| 4098 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4099 | /* |
| 4100 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 4101 | */ |
| 4102 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 4103 | { |
| 4104 | unsigned int start_bits, end_bits; |
| 4105 | |
| 4106 | start_bits = 8 - ( offset % 8 ); |
| 4107 | if( start_bits != 8 ) |
| 4108 | { |
| 4109 | size_t first_byte_idx = offset / 8; |
| 4110 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4111 | /* Special case */ |
| 4112 | if( len <= start_bits ) |
| 4113 | { |
| 4114 | for( ; len != 0; len-- ) |
| 4115 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 4116 | |
| 4117 | /* Avoid potential issues with offset or len becoming invalid */ |
| 4118 | return; |
| 4119 | } |
| 4120 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4121 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 4122 | len -= start_bits; |
| 4123 | |
| 4124 | for( ; start_bits != 0; start_bits-- ) |
| 4125 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 4126 | } |
| 4127 | |
| 4128 | end_bits = len % 8; |
| 4129 | if( end_bits != 0 ) |
| 4130 | { |
| 4131 | size_t last_byte_idx = ( offset + len ) / 8; |
| 4132 | |
| 4133 | len -= end_bits; /* Now len % 8 == 0 */ |
| 4134 | |
| 4135 | for( ; end_bits != 0; end_bits-- ) |
| 4136 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 4137 | } |
| 4138 | |
| 4139 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 4140 | } |
| 4141 | |
| 4142 | /* |
| 4143 | * Check that bitmask is full |
| 4144 | */ |
| 4145 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 4146 | { |
| 4147 | size_t i; |
| 4148 | |
| 4149 | for( i = 0; i < len / 8; i++ ) |
| 4150 | if( mask[i] != 0xFF ) |
| 4151 | return( -1 ); |
| 4152 | |
| 4153 | for( i = 0; i < len % 8; i++ ) |
| 4154 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 4155 | return( -1 ); |
| 4156 | |
| 4157 | return( 0 ); |
| 4158 | } |
| 4159 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4160 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4161 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4162 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4163 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4164 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4165 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4166 | alloc_len = 12; /* Handshake header */ |
| 4167 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4168 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4169 | if( add_bitmap ) |
| 4170 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4171 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4172 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4173 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4175 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4176 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4177 | 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] | 4178 | { |
| 4179 | return( ( ssl->in_msg[1] << 16 ) | |
| 4180 | ( ssl->in_msg[2] << 8 ) | |
| 4181 | ssl->in_msg[3] ); |
| 4182 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4183 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4184 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4186 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4188 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4189 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4190 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4191 | } |
| 4192 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4193 | 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] | 4194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4195 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4196 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4197 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4199 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4200 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4201 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4202 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4203 | unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4204 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4205 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 4206 | { |
| 4207 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 4208 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4209 | } |
| 4210 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4211 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4212 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4213 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 4214 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4215 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4216 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4217 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 4218 | { |
| 4219 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 4220 | recv_msg_seq, |
| 4221 | ssl->handshake->in_msg_seq ) ); |
| 4222 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4223 | } |
| 4224 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4225 | /* Retransmit only on last message from previous flight, to avoid |
| 4226 | * too many retransmissions. |
| 4227 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 4228 | 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] | 4229 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4230 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4231 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4232 | "message_seq = %d, start_of_flight = %d", |
| 4233 | recv_msg_seq, |
| 4234 | ssl->handshake->in_flight_start_seq ) ); |
| 4235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4236 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4237 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4238 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4239 | return( ret ); |
| 4240 | } |
| 4241 | } |
| 4242 | else |
| 4243 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4244 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4245 | "message_seq = %d, expected = %d", |
| 4246 | recv_msg_seq, |
| 4247 | ssl->handshake->in_msg_seq ) ); |
| 4248 | } |
| 4249 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4250 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4251 | } |
| 4252 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4253 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4254 | /* Message reassembly is handled alongside buffering of future |
| 4255 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4256 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4257 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4258 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4259 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4260 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4261 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4262 | } |
| 4263 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4264 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4265 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4266 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4267 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4268 | /* With TLS we don't handle fragmentation (for now) */ |
| 4269 | if( ssl->in_msglen < ssl->in_hslen ) |
| 4270 | { |
| 4271 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 4272 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4273 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4274 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 4275 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4276 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4277 | return( 0 ); |
| 4278 | } |
| 4279 | |
| 4280 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 4281 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4282 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4283 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4284 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4285 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4286 | ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen ); |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4287 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4288 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4289 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4290 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4291 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4292 | ssl->handshake != NULL ) |
| 4293 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4294 | unsigned offset; |
| 4295 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4296 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4297 | /* Increment handshake sequence number */ |
| 4298 | hs->in_msg_seq++; |
| 4299 | |
| 4300 | /* |
| 4301 | * Clear up handshake buffering and reassembly structure. |
| 4302 | */ |
| 4303 | |
| 4304 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4305 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4306 | |
| 4307 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4308 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 4309 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4310 | offset++, hs_buf++ ) |
| 4311 | { |
| 4312 | *hs_buf = *(hs_buf + 1); |
| 4313 | } |
| 4314 | |
| 4315 | /* Create a fresh last entry */ |
| 4316 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4317 | } |
| 4318 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4319 | } |
| 4320 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4321 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4322 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 4323 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4324 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 4325 | * Bit n is set iff record number in_window_top - n has been seen. |
| 4326 | * |
| 4327 | * Usually, in_window_top is the last record number seen and the lsb of |
| 4328 | * in_window is set. The only exception is the initial state (record number 0 |
| 4329 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4330 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4331 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4332 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4333 | { |
| 4334 | ssl->in_window_top = 0; |
| 4335 | ssl->in_window = 0; |
| 4336 | } |
| 4337 | |
| 4338 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 4339 | { |
| 4340 | return( ( (uint64_t) buf[0] << 40 ) | |
| 4341 | ( (uint64_t) buf[1] << 32 ) | |
| 4342 | ( (uint64_t) buf[2] << 24 ) | |
| 4343 | ( (uint64_t) buf[3] << 16 ) | |
| 4344 | ( (uint64_t) buf[4] << 8 ) | |
| 4345 | ( (uint64_t) buf[5] ) ); |
| 4346 | } |
| 4347 | |
| 4348 | /* |
| 4349 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 4350 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4351 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4352 | { |
| 4353 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4354 | uint64_t bit; |
| 4355 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4356 | if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) == |
| 4357 | MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
| 4358 | { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4359 | return( 0 ); |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4360 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4361 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4362 | if( rec_seqnum > ssl->in_window_top ) |
| 4363 | return( 0 ); |
| 4364 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4365 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4366 | |
| 4367 | if( bit >= 64 ) |
| 4368 | return( -1 ); |
| 4369 | |
| 4370 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 4371 | return( -1 ); |
| 4372 | |
| 4373 | return( 0 ); |
| 4374 | } |
| 4375 | |
| 4376 | /* |
| 4377 | * Update replay window on new validated record |
| 4378 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4379 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4380 | { |
| 4381 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4382 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4383 | if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) == |
| 4384 | MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
| 4385 | { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4386 | return; |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 4387 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4388 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4389 | if( rec_seqnum > ssl->in_window_top ) |
| 4390 | { |
| 4391 | /* Update window_top and the contents of the window */ |
| 4392 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 4393 | |
| 4394 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4395 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4396 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4397 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4398 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4399 | ssl->in_window |= 1; |
| 4400 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4401 | |
| 4402 | ssl->in_window_top = rec_seqnum; |
| 4403 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4404 | else |
| 4405 | { |
| 4406 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4407 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4408 | |
| 4409 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 4410 | ssl->in_window |= (uint64_t) 1 << bit; |
| 4411 | } |
| 4412 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4413 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4414 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4415 | #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] | 4416 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4417 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); |
| 4418 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4419 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4420 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 4421 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 4422 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4423 | * |
| 4424 | * - if cookie is valid, return 0 |
| 4425 | * - if ClientHello looks superficially valid but cookie is not, |
| 4426 | * fill obuf and set olen, then |
| 4427 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 4428 | * - otherwise return a specific error code |
| 4429 | */ |
| 4430 | static int ssl_check_dtls_clihlo_cookie( |
| 4431 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 4432 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 4433 | void *p_cookie, |
| 4434 | const unsigned char *cli_id, size_t cli_id_len, |
| 4435 | const unsigned char *in, size_t in_len, |
| 4436 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 4437 | { |
| 4438 | size_t sid_len, cookie_len; |
| 4439 | unsigned char *p; |
| 4440 | |
| 4441 | if( f_cookie_write == NULL || f_cookie_check == NULL ) |
| 4442 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4443 | |
| 4444 | /* |
| 4445 | * Structure of ClientHello with record and handshake headers, |
| 4446 | * and expected values. We don't need to check a lot, more checks will be |
| 4447 | * done when actually parsing the ClientHello - skipping those checks |
| 4448 | * avoids code duplication and does not make cookie forging any easier. |
| 4449 | * |
| 4450 | * 0-0 ContentType type; copied, must be handshake |
| 4451 | * 1-2 ProtocolVersion version; copied |
| 4452 | * 3-4 uint16 epoch; copied, must be 0 |
| 4453 | * 5-10 uint48 sequence_number; copied |
| 4454 | * 11-12 uint16 length; (ignored) |
| 4455 | * |
| 4456 | * 13-13 HandshakeType msg_type; (ignored) |
| 4457 | * 14-16 uint24 length; (ignored) |
| 4458 | * 17-18 uint16 message_seq; copied |
| 4459 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 4460 | * 22-24 uint24 fragment_length; (ignored) |
| 4461 | * |
| 4462 | * 25-26 ProtocolVersion client_version; (ignored) |
| 4463 | * 27-58 Random random; (ignored) |
| 4464 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 4465 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 4466 | * ... |
| 4467 | * |
| 4468 | * Minimum length is 61 bytes. |
| 4469 | */ |
| 4470 | if( in_len < 61 || |
| 4471 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4472 | in[3] != 0 || in[4] != 0 || |
| 4473 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 4474 | { |
| 4475 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4476 | } |
| 4477 | |
| 4478 | sid_len = in[59]; |
| 4479 | if( sid_len > in_len - 61 ) |
| 4480 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4481 | |
| 4482 | cookie_len = in[60 + sid_len]; |
| 4483 | if( cookie_len > in_len - 60 ) |
| 4484 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4485 | |
| 4486 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 4487 | cli_id, cli_id_len ) == 0 ) |
| 4488 | { |
| 4489 | /* Valid cookie */ |
| 4490 | return( 0 ); |
| 4491 | } |
| 4492 | |
| 4493 | /* |
| 4494 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 4495 | * |
| 4496 | * 0-0 ContentType type; copied |
| 4497 | * 1-2 ProtocolVersion version; copied |
| 4498 | * 3-4 uint16 epoch; copied |
| 4499 | * 5-10 uint48 sequence_number; copied |
| 4500 | * 11-12 uint16 length; olen - 13 |
| 4501 | * |
| 4502 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 4503 | * 14-16 uint24 length; olen - 25 |
| 4504 | * 17-18 uint16 message_seq; copied |
| 4505 | * 19-21 uint24 fragment_offset; copied |
| 4506 | * 22-24 uint24 fragment_length; olen - 25 |
| 4507 | * |
| 4508 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 4509 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 4510 | * |
| 4511 | * Minimum length is 28. |
| 4512 | */ |
| 4513 | if( buf_len < 28 ) |
| 4514 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4515 | |
| 4516 | /* Copy most fields and adapt others */ |
| 4517 | memcpy( obuf, in, 25 ); |
| 4518 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 4519 | obuf[25] = 0xfe; |
| 4520 | obuf[26] = 0xff; |
| 4521 | |
| 4522 | /* Generate and write actual cookie */ |
| 4523 | p = obuf + 28; |
| 4524 | if( f_cookie_write( p_cookie, |
| 4525 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 4526 | { |
| 4527 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4528 | } |
| 4529 | |
| 4530 | *olen = p - obuf; |
| 4531 | |
| 4532 | /* Go back and fill length fields */ |
| 4533 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 4534 | |
| 4535 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 4536 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 4537 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 4538 | |
| 4539 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 4540 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 4541 | |
| 4542 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 4543 | } |
| 4544 | |
| 4545 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4546 | * Handle possible client reconnect with the same UDP quadruplet |
| 4547 | * (RFC 6347 Section 4.2.8). |
| 4548 | * |
| 4549 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 4550 | * that looks like a ClientHello. |
| 4551 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4552 | * - if the input looks like a ClientHello without cookies, |
| 4553 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4554 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4555 | * - if the input looks like a ClientHello with a valid cookie, |
| 4556 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 4557 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4558 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4559 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4560 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 4561 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 4562 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4563 | */ |
| 4564 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 4565 | { |
| 4566 | int ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4567 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4568 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4569 | ret = ssl_check_dtls_clihlo_cookie( |
| 4570 | ssl->conf->f_cookie_write, |
| 4571 | ssl->conf->f_cookie_check, |
| 4572 | ssl->conf->p_cookie, |
| 4573 | ssl->cli_id, ssl->cli_id_len, |
| 4574 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4575 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4576 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4577 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 4578 | |
| 4579 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4580 | { |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4581 | /* 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] | 4582 | * If the error is permanent we'll catch it later, |
| 4583 | * if it's not, then hopefully it'll work next time. */ |
| 4584 | (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len ); |
| 4585 | |
| 4586 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4587 | } |
| 4588 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4589 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4590 | { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4591 | /* Got a valid cookie, partially reset context */ |
| 4592 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
| 4593 | { |
| 4594 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 4595 | return( ret ); |
| 4596 | } |
| 4597 | |
| 4598 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4599 | } |
| 4600 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4601 | return( ret ); |
| 4602 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4603 | #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] | 4604 | |
Hanno Becker | 46483f1 | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4605 | static int ssl_check_record_type( uint8_t record_type ) |
| 4606 | { |
| 4607 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4608 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 4609 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 4610 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 4611 | { |
| 4612 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4613 | } |
| 4614 | |
| 4615 | return( 0 ); |
| 4616 | } |
| 4617 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4618 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4619 | * ContentType type; |
| 4620 | * ProtocolVersion version; |
| 4621 | * uint16 epoch; // DTLS only |
| 4622 | * uint48 sequence_number; // DTLS only |
| 4623 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4624 | * |
| 4625 | * 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] | 4626 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4627 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 4628 | * |
| 4629 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 4630 | * 1. proceed with the record if this function returns 0 |
| 4631 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 4632 | * 3. return CLIENT_RECONNECT if this function return that value |
| 4633 | * 4. drop the whole datagram if this function returns anything else. |
| 4634 | * Point 2 is needed when the peer is resending, and we have already received |
| 4635 | * 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] | 4636 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4637 | static int ssl_parse_record_header( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4638 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4639 | int major_ver, minor_ver; |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4640 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4641 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4642 | /* Parse and validate record content type and version */ |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 4643 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4644 | ssl->in_msgtype = ssl->in_hdr[0]; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4645 | mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4646 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4647 | /* Check record type */ |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4648 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4649 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4650 | ssl->in_msgtype == MBEDTLS_SSL_MSG_CID && |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 4651 | mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 ) |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4652 | { |
| 4653 | /* Shift pointers to account for record header including CID |
| 4654 | * struct { |
| 4655 | * ContentType special_type = tls12_cid; |
| 4656 | * ProtocolVersion version; |
| 4657 | * uint16 epoch; |
| 4658 | * uint48 sequence_number; |
Hanno Becker | 3b2bf5b | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 4659 | * opaque cid[cid_length]; // Additional field compared to |
| 4660 | * // default DTLS record format |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4661 | * uint16 length; |
| 4662 | * opaque enc_content[DTLSCiphertext.length]; |
| 4663 | * } DTLSCiphertext; |
| 4664 | */ |
| 4665 | |
| 4666 | /* So far, we only support static CID lengths |
| 4667 | * fixed in the configuration. */ |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 4668 | ssl->in_len = ssl->in_cid + mbedtls_ssl_conf_get_cid_len( ssl->conf ); |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4669 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 4670 | } |
| 4671 | else |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4672 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 46483f1 | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4673 | if( ssl_check_record_type( ssl->in_msgtype ) ) |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4675 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4676 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4677 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Andres Amaya Garcia | 0169253 | 2017-06-28 09:26:46 +0100 | [diff] [blame] | 4678 | /* Silently ignore invalid DTLS records as recommended by RFC 6347 |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4679 | * Section 4.1.2.7, that is, send alert only with TLS */ |
| 4680 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
| 4681 | { |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4682 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4683 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4684 | } |
| 4685 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4687 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | /* Check version */ |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4691 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4692 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4693 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 4694 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4697 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4698 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4699 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 4700 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4703 | /* Now that the total length of the record header is known, ensure |
| 4704 | * that the current datagram is large enough to hold it. |
| 4705 | * This would fail, for example, if we received a datagram of |
| 4706 | * size 13 + n Bytes where n is less than the size of incoming CIDs. */ |
| 4707 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 4708 | if( ret != 0 ) |
| 4709 | { |
| 4710 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 4711 | return( ret ); |
| 4712 | } |
| 4713 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 4714 | |
| 4715 | /* Parse and validate record length |
| 4716 | * This must happen after the CID parsing because |
| 4717 | * its position in the record header depends on |
| 4718 | * the presence of a CID. */ |
| 4719 | |
| 4720 | ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4721 | if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4722 | - (size_t)( ssl->in_msg - ssl->in_buf ) ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4724 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4725 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4726 | } |
| 4727 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4728 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Hanno Becker | d8f7c4a | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 4729 | "version = [%d:%d], msglen = %d", |
| 4730 | ssl->in_msgtype, |
| 4731 | major_ver, minor_ver, ssl->in_msglen ) ); |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4732 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4733 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4734 | * DTLS-related tests. |
| 4735 | * Check epoch before checking length constraint because |
| 4736 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 4737 | * message gets duplicated before the corresponding Finished message, |
| 4738 | * the second ChangeCipherSpec should be discarded because it belongs |
| 4739 | * to an old epoch, but not because its length is shorter than |
| 4740 | * the minimum record length for packets using the new record transform. |
| 4741 | * Note that these two kinds of failures are handled differently, |
| 4742 | * as an unexpected record is silently skipped but an invalid |
| 4743 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4744 | */ |
| 4745 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4746 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4747 | { |
| 4748 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 4749 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4750 | /* Check epoch (and sequence number) with DTLS */ |
| 4751 | if( rec_epoch != ssl->in_epoch ) |
| 4752 | { |
| 4753 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 4754 | "expected %d, received %d", |
| 4755 | ssl->in_epoch, rec_epoch ) ); |
| 4756 | |
| 4757 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 4758 | /* |
| 4759 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 4760 | * access the first byte of record content (handshake type), as we |
| 4761 | * have an active transform (possibly iv_len != 0), so use the |
| 4762 | * fact that the record header len is 13 instead. |
| 4763 | */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 4764 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 4765 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4766 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4767 | rec_epoch == 0 && |
| 4768 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4769 | ssl->in_left > 13 && |
| 4770 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 4771 | { |
| 4772 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 4773 | "from the same port" ) ); |
| 4774 | return( ssl_handle_possible_reconnect( ssl ) ); |
| 4775 | } |
| 4776 | else |
| 4777 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4778 | { |
| 4779 | /* Consider buffering the record. */ |
| 4780 | if( rec_epoch == (unsigned int) ssl->in_epoch + 1 ) |
| 4781 | { |
| 4782 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 4783 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4784 | } |
| 4785 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4786 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4787 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4791 | /* Replay detection only works for the current epoch */ |
| 4792 | if( rec_epoch == ssl->in_epoch && |
| 4793 | mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
| 4794 | { |
| 4795 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 4796 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 4797 | } |
| 4798 | #endif |
| 4799 | } |
| 4800 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4801 | |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4802 | |
| 4803 | /* Check length against bounds of the current transform and version */ |
| 4804 | if( ssl->transform_in == NULL ) |
| 4805 | { |
| 4806 | if( ssl->in_msglen < 1 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4807 | ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4808 | { |
| 4809 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4810 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4811 | } |
| 4812 | } |
| 4813 | else |
| 4814 | { |
| 4815 | if( ssl->in_msglen < ssl->transform_in->minlen ) |
| 4816 | { |
| 4817 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4818 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4819 | } |
| 4820 | |
| 4821 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 4822 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4823 | ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4824 | { |
| 4825 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4826 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4827 | } |
| 4828 | #endif |
| 4829 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 4830 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4831 | /* |
| 4832 | * TLS encrypted messages can have up to 256 bytes of padding |
| 4833 | */ |
| 4834 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 && |
| 4835 | ssl->in_msglen > ssl->transform_in->minlen + |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4836 | MBEDTLS_SSL_IN_CONTENT_LEN + 256 ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4837 | { |
| 4838 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4839 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4840 | } |
| 4841 | #endif |
| 4842 | } |
| 4843 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4844 | return( 0 ); |
| 4845 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4846 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4847 | /* |
| 4848 | * If applicable, decrypt (and decompress) record content |
| 4849 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4850 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4851 | { |
| 4852 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 4853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4854 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4855 | ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4857 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4858 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4859 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4860 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4861 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4862 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 4863 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4864 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4865 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 4866 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4867 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4868 | |
| 4869 | if( ret == 0 ) |
| 4870 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4871 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4872 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4873 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4874 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4875 | mbedtls_record rec; |
| 4876 | |
| 4877 | rec.buf = ssl->in_iv; |
| 4878 | rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN |
| 4879 | - ( ssl->in_iv - ssl->in_buf ); |
| 4880 | rec.data_len = ssl->in_msglen; |
| 4881 | rec.data_offset = 0; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4882 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | 7ba3568 | 2019-05-09 15:54:28 +0100 | [diff] [blame] | 4883 | rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid ); |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4884 | memcpy( rec.cid, ssl->in_cid, rec.cid_len ); |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4885 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4886 | |
| 4887 | memcpy( &rec.ctr[0], ssl->in_ctr, 8 ); |
| 4888 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 4889 | ssl->conf->transport, rec.ver ); |
| 4890 | rec.type = ssl->in_msgtype; |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4891 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
| 4892 | &rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4893 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4894 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4895 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4896 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4897 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
Hanno Becker | e0200da | 2019-06-13 09:23:43 +0100 | [diff] [blame] | 4898 | mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf ) |
| 4899 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4900 | { |
Hanno Becker | 675c4d6 | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 4901 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 4902 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4903 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4904 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 4905 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4906 | return( ret ); |
| 4907 | } |
| 4908 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4909 | if( ssl->in_msgtype != rec.type ) |
Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 4910 | { |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4911 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
| 4912 | ssl->in_msgtype, rec.type ) ); |
Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 4913 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4914 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4915 | /* The record content type may change during decryption, |
| 4916 | * so re-read it. */ |
| 4917 | ssl->in_msgtype = rec.type; |
| 4918 | /* Also update the input buffer, because unfortunately |
| 4919 | * the server-side ssl_parse_client_hello() reparses the |
| 4920 | * record header when receiving a ClientHello initiating |
| 4921 | * a renegotiation. */ |
| 4922 | ssl->in_hdr[0] = rec.type; |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4923 | ssl->in_msg = rec.buf + rec.data_offset; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4924 | ssl->in_msglen = rec.data_len; |
| 4925 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4926 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 4927 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4928 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
| 4929 | ssl->in_msg, ssl->in_msglen ); |
| 4930 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4931 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4932 | /* We have already checked the record content type |
| 4933 | * in ssl_parse_record_header(), failing or silently |
| 4934 | * dropping the record in the case of an unknown type. |
| 4935 | * |
| 4936 | * Since with the use of CIDs, the record content type |
| 4937 | * might change during decryption, re-check the record |
| 4938 | * content type, but treat a failure as fatal this time. */ |
| 4939 | if( ssl_check_record_type( ssl->in_msgtype ) ) |
| 4940 | { |
| 4941 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 4942 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4943 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4944 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4945 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4946 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4947 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4948 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4949 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4950 | } |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4951 | else if( ssl->in_msglen == 0 ) |
| 4952 | { |
| 4953 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4954 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
| 4955 | && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 4956 | { |
| 4957 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 4958 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 4959 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4960 | } |
| 4961 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4962 | |
| 4963 | ssl->nb_zero++; |
| 4964 | |
| 4965 | /* |
| 4966 | * Three or more empty messages may be a DoS attack |
| 4967 | * (excessive CPU consumption). |
| 4968 | */ |
| 4969 | if( ssl->nb_zero > 3 ) |
| 4970 | { |
| 4971 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 70463db | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 4972 | "messages, possible DoS attack" ) ); |
| 4973 | /* Treat the records as if they were not properly authenticated, |
| 4974 | * thereby failing the connection if we see more than allowed |
| 4975 | * by the configured bad MAC threshold. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4976 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 4977 | } |
| 4978 | } |
| 4979 | else |
| 4980 | ssl->nb_zero = 0; |
| 4981 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4982 | /* Only needed for TLS, as with DTLS in_ctr is read from the header */ |
| 4983 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 4984 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4985 | { |
| 4986 | unsigned i; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4987 | for( i = 8; i > 0; i-- ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4988 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 4989 | break; |
| 4990 | |
Manuel Pégourié-Gonnard | 8794a42 | 2019-06-11 10:04:57 +0200 | [diff] [blame] | 4991 | /* 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] | 4992 | if( i == 0 ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4993 | { |
| 4994 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 4995 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4996 | } |
| 4997 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4998 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4999 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5002 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5003 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5004 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5005 | { |
| 5006 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 5007 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5008 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5009 | return( ret ); |
| 5010 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5011 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5012 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5013 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5014 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5015 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5016 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5017 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5018 | } |
| 5019 | #endif |
| 5020 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5021 | return( 0 ); |
| 5022 | } |
| 5023 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5024 | 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] | 5025 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5026 | /* |
| 5027 | * Read a record. |
| 5028 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5029 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 5030 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 5031 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5032 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5033 | |
| 5034 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 5035 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5036 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 5037 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 5038 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5039 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5040 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5041 | { |
| 5042 | int ret; |
| 5043 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5044 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5045 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5046 | if( ssl->keep_current_message == 0 ) |
| 5047 | { |
| 5048 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5049 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5050 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5051 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5052 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5053 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5054 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5055 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5056 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5057 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5058 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5059 | /* We only check for buffered messages if the |
| 5060 | * current datagram is fully consumed. */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5061 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5062 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5063 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5064 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 5065 | have_buffered = 1; |
| 5066 | } |
| 5067 | |
| 5068 | if( have_buffered == 0 ) |
| 5069 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5070 | { |
| 5071 | ret = ssl_get_next_record( ssl ); |
| 5072 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 5073 | continue; |
| 5074 | |
| 5075 | if( ret != 0 ) |
| 5076 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5077 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5078 | return( ret ); |
| 5079 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5080 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 5084 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5085 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5086 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5087 | { |
| 5088 | /* Buffer future message */ |
| 5089 | ret = ssl_buffer_message( ssl ); |
| 5090 | if( ret != 0 ) |
| 5091 | return( ret ); |
| 5092 | |
| 5093 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 5094 | } |
| 5095 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5096 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5097 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 5098 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5099 | |
| 5100 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5101 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5102 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5103 | return( ret ); |
| 5104 | } |
| 5105 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5106 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5107 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5108 | { |
| 5109 | mbedtls_ssl_update_handshake_status( ssl ); |
| 5110 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5111 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5112 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5113 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5114 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5115 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 5119 | |
| 5120 | return( 0 ); |
| 5121 | } |
| 5122 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5123 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5124 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5125 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5126 | if( ssl->in_left > ssl->next_record_offset ) |
| 5127 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5128 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5129 | return( 0 ); |
| 5130 | } |
| 5131 | |
| 5132 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 5133 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5134 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5135 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5136 | int ret = 0; |
| 5137 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5138 | if( hs == NULL ) |
| 5139 | return( -1 ); |
| 5140 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5141 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 5142 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5143 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 5144 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5145 | { |
| 5146 | /* Check if we have seen a ChangeCipherSpec before. |
| 5147 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5148 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5149 | { |
| 5150 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 5151 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5152 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5153 | } |
| 5154 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5155 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5156 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 5157 | ssl->in_msglen = 1; |
| 5158 | ssl->in_msg[0] = 1; |
| 5159 | |
| 5160 | /* As long as they are equal, the exact value doesn't matter. */ |
| 5161 | ssl->in_left = 0; |
| 5162 | ssl->next_record_offset = 0; |
| 5163 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5164 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5165 | goto exit; |
| 5166 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5167 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5168 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5169 | /* Debug only */ |
| 5170 | { |
| 5171 | unsigned offset; |
| 5172 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 5173 | { |
| 5174 | hs_buf = &hs->buffering.hs[offset]; |
| 5175 | if( hs_buf->is_valid == 1 ) |
| 5176 | { |
| 5177 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 5178 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5179 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5180 | } |
| 5181 | } |
| 5182 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5183 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5184 | |
| 5185 | /* Check if we have buffered and/or fully reassembled the |
| 5186 | * next handshake message. */ |
| 5187 | hs_buf = &hs->buffering.hs[0]; |
| 5188 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 5189 | { |
| 5190 | /* Synthesize a record containing the buffered HS message. */ |
| 5191 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 5192 | ( hs_buf->data[2] << 8 ) | |
| 5193 | hs_buf->data[3]; |
| 5194 | |
| 5195 | /* Double-check that we haven't accidentally buffered |
| 5196 | * a message that doesn't fit into the input buffer. */ |
| 5197 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5198 | { |
| 5199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5200 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5201 | } |
| 5202 | |
| 5203 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 5204 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 5205 | hs_buf->data, msg_len + 12 ); |
| 5206 | |
| 5207 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5208 | ssl->in_hslen = msg_len + 12; |
| 5209 | ssl->in_msglen = msg_len + 12; |
| 5210 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 5211 | |
| 5212 | ret = 0; |
| 5213 | goto exit; |
| 5214 | } |
| 5215 | else |
| 5216 | { |
| 5217 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 5218 | hs->in_msg_seq ) ); |
| 5219 | } |
| 5220 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5221 | ret = -1; |
| 5222 | |
| 5223 | exit: |
| 5224 | |
| 5225 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 5226 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5227 | } |
| 5228 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5229 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 5230 | size_t desired ) |
| 5231 | { |
| 5232 | int offset; |
| 5233 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5234 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 5235 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5236 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5237 | /* Get rid of future records epoch first, if such exist. */ |
| 5238 | ssl_free_buffered_record( ssl ); |
| 5239 | |
| 5240 | /* Check if we have enough space available now. */ |
| 5241 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5242 | hs->buffering.total_bytes_buffered ) ) |
| 5243 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5244 | 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] | 5245 | return( 0 ); |
| 5246 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5247 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5248 | /* We don't have enough space to buffer the next expected handshake |
| 5249 | * message. Remove buffers used for future messages to gain space, |
| 5250 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5251 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 5252 | offset >= 0; offset-- ) |
| 5253 | { |
| 5254 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 5255 | offset ) ); |
| 5256 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5257 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5258 | |
| 5259 | /* Check if we have enough space available now. */ |
| 5260 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5261 | hs->buffering.total_bytes_buffered ) ) |
| 5262 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5263 | 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] | 5264 | return( 0 ); |
| 5265 | } |
| 5266 | } |
| 5267 | |
| 5268 | return( -1 ); |
| 5269 | } |
| 5270 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5271 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 5272 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5273 | int ret = 0; |
| 5274 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5275 | |
| 5276 | if( hs == NULL ) |
| 5277 | return( 0 ); |
| 5278 | |
| 5279 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 5280 | |
| 5281 | switch( ssl->in_msgtype ) |
| 5282 | { |
| 5283 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 5284 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5285 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5286 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5287 | break; |
| 5288 | |
| 5289 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5290 | { |
| 5291 | unsigned recv_msg_seq_offset; |
| 5292 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 5293 | mbedtls_ssl_hs_buffer *hs_buf; |
| 5294 | size_t msg_len = ssl->in_hslen - 12; |
| 5295 | |
| 5296 | /* We should never receive an old handshake |
| 5297 | * message - double-check nonetheless. */ |
| 5298 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 5299 | { |
| 5300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5301 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5302 | } |
| 5303 | |
| 5304 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 5305 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5306 | { |
| 5307 | /* Silently ignore -- message too far in the future */ |
| 5308 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 5309 | ( "Ignore future HS message with sequence number %u, " |
| 5310 | "buffering window %u - %u", |
| 5311 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 5312 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 5313 | |
| 5314 | goto exit; |
| 5315 | } |
| 5316 | |
| 5317 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 5318 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 5319 | |
| 5320 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 5321 | |
| 5322 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5323 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5324 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5325 | size_t reassembly_buf_sz; |
| 5326 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5327 | hs_buf->is_fragmented = |
| 5328 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 5329 | |
| 5330 | /* We copy the message back into the input buffer |
| 5331 | * after reassembly, so check that it's not too large. |
| 5332 | * This is an implementation-specific limitation |
| 5333 | * and not one from the standard, hence it is not |
| 5334 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 5335 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5336 | { |
| 5337 | /* Ignore message */ |
| 5338 | goto exit; |
| 5339 | } |
| 5340 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5341 | /* Check if we have enough space to buffer the message. */ |
| 5342 | if( hs->buffering.total_bytes_buffered > |
| 5343 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 5344 | { |
| 5345 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5346 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5347 | } |
| 5348 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5349 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 5350 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5351 | |
| 5352 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5353 | hs->buffering.total_bytes_buffered ) ) |
| 5354 | { |
| 5355 | if( recv_msg_seq_offset > 0 ) |
| 5356 | { |
| 5357 | /* If we can't buffer a future message because |
| 5358 | * of space limitations -- ignore. */ |
| 5359 | 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", |
| 5360 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5361 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5362 | goto exit; |
| 5363 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 5364 | else |
| 5365 | { |
| 5366 | 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", |
| 5367 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5368 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5369 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5370 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5371 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5372 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5373 | 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", |
| 5374 | (unsigned) msg_len, |
| 5375 | (unsigned) reassembly_buf_sz, |
| 5376 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5377 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5378 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5379 | goto exit; |
| 5380 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 5384 | msg_len ) ); |
| 5385 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5386 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 5387 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5388 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5389 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5390 | goto exit; |
| 5391 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5392 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5393 | |
| 5394 | /* Prepare final header: copy msg_type, length and message_seq, |
| 5395 | * then add standardised fragment_offset and fragment_length */ |
| 5396 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 5397 | memset( hs_buf->data + 6, 0, 3 ); |
| 5398 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 5399 | |
| 5400 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5401 | |
| 5402 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5403 | } |
| 5404 | else |
| 5405 | { |
| 5406 | /* Make sure msg_type and length are consistent */ |
| 5407 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 5408 | { |
| 5409 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 5410 | /* Ignore */ |
| 5411 | goto exit; |
| 5412 | } |
| 5413 | } |
| 5414 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5415 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5416 | { |
| 5417 | size_t frag_len, frag_off; |
| 5418 | unsigned char * const msg = hs_buf->data + 12; |
| 5419 | |
| 5420 | /* |
| 5421 | * Check and copy current fragment |
| 5422 | */ |
| 5423 | |
| 5424 | /* Validation of header fields already done in |
| 5425 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 5426 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 5427 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 5428 | |
| 5429 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 5430 | frag_off, frag_len ) ); |
| 5431 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 5432 | |
| 5433 | if( hs_buf->is_fragmented ) |
| 5434 | { |
| 5435 | unsigned char * const bitmask = msg + msg_len; |
| 5436 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 5437 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 5438 | msg_len ) == 0 ); |
| 5439 | } |
| 5440 | else |
| 5441 | { |
| 5442 | hs_buf->is_complete = 1; |
| 5443 | } |
| 5444 | |
| 5445 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 5446 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 5447 | } |
| 5448 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5449 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5450 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5451 | |
| 5452 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 5453 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5454 | break; |
| 5455 | } |
| 5456 | |
| 5457 | exit: |
| 5458 | |
| 5459 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 5460 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5461 | } |
| 5462 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5463 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5464 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5465 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5466 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5467 | * Consume last content-layer message and potentially |
| 5468 | * update in_msglen which keeps track of the contents' |
| 5469 | * consumption state. |
| 5470 | * |
| 5471 | * (1) Handshake messages: |
| 5472 | * Remove last handshake message, move content |
| 5473 | * and adapt in_msglen. |
| 5474 | * |
| 5475 | * (2) Alert messages: |
| 5476 | * Consume whole record content, in_msglen = 0. |
| 5477 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5478 | * (3) Change cipher spec: |
| 5479 | * Consume whole record content, in_msglen = 0. |
| 5480 | * |
| 5481 | * (4) Application data: |
| 5482 | * Don't do anything - the record layer provides |
| 5483 | * the application data as a stream transport |
| 5484 | * and consumes through mbedtls_ssl_read only. |
| 5485 | * |
| 5486 | */ |
| 5487 | |
| 5488 | /* Case (1): Handshake messages */ |
| 5489 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5490 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5491 | /* Hard assertion to be sure that no application data |
| 5492 | * is in flight, as corrupting ssl->in_msglen during |
| 5493 | * ssl->in_offt != NULL is fatal. */ |
| 5494 | if( ssl->in_offt != NULL ) |
| 5495 | { |
| 5496 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5497 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5498 | } |
| 5499 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5500 | /* |
| 5501 | * Get next Handshake message in the current record |
| 5502 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5503 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5504 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5505 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5506 | * current handshake content: If DTLS handshake |
| 5507 | * fragmentation is used, that's the fragment |
| 5508 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5509 | * size here is faulty and should be changed at |
| 5510 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5511 | * (2) While it doesn't seem to cause problems, one |
| 5512 | * has to be very careful not to assume that in_hslen |
| 5513 | * is always <= in_msglen in a sensible communication. |
| 5514 | * Again, it's wrong for DTLS handshake fragmentation. |
| 5515 | * The following check is therefore mandatory, and |
| 5516 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5517 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 5518 | * bounds after handling a DTLS message with an unexpected |
| 5519 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5520 | */ |
| 5521 | if( ssl->in_hslen < ssl->in_msglen ) |
| 5522 | { |
| 5523 | ssl->in_msglen -= ssl->in_hslen; |
| 5524 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 5525 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5526 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5527 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 5528 | ssl->in_msg, ssl->in_msglen ); |
| 5529 | } |
| 5530 | else |
| 5531 | { |
| 5532 | ssl->in_msglen = 0; |
| 5533 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 5534 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5535 | ssl->in_hslen = 0; |
| 5536 | } |
| 5537 | /* Case (4): Application data */ |
| 5538 | else if( ssl->in_offt != NULL ) |
| 5539 | { |
| 5540 | return( 0 ); |
| 5541 | } |
| 5542 | /* Everything else (CCS & Alerts) */ |
| 5543 | else |
| 5544 | { |
| 5545 | ssl->in_msglen = 0; |
| 5546 | } |
| 5547 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5548 | return( 0 ); |
| 5549 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5550 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5551 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 5552 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5553 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5554 | return( 1 ); |
| 5555 | |
| 5556 | return( 0 ); |
| 5557 | } |
| 5558 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5559 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5560 | |
| 5561 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 5562 | { |
| 5563 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5564 | if( hs == NULL ) |
| 5565 | return; |
| 5566 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5567 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5568 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5569 | hs->buffering.total_bytes_buffered -= |
| 5570 | hs->buffering.future_record.len; |
| 5571 | |
| 5572 | mbedtls_free( hs->buffering.future_record.data ); |
| 5573 | hs->buffering.future_record.data = NULL; |
| 5574 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 5578 | { |
| 5579 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5580 | unsigned char * rec; |
| 5581 | size_t rec_len; |
| 5582 | unsigned rec_epoch; |
| 5583 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5584 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5585 | return( 0 ); |
| 5586 | |
| 5587 | if( hs == NULL ) |
| 5588 | return( 0 ); |
| 5589 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5590 | rec = hs->buffering.future_record.data; |
| 5591 | rec_len = hs->buffering.future_record.len; |
| 5592 | rec_epoch = hs->buffering.future_record.epoch; |
| 5593 | |
| 5594 | if( rec == NULL ) |
| 5595 | return( 0 ); |
| 5596 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5597 | /* Only consider loading future records if the |
| 5598 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5599 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5600 | return( 0 ); |
| 5601 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5602 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 5603 | |
| 5604 | if( rec_epoch != ssl->in_epoch ) |
| 5605 | { |
| 5606 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 5607 | goto exit; |
| 5608 | } |
| 5609 | |
| 5610 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 5611 | |
| 5612 | /* Double-check that the record is not too large */ |
| 5613 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 5614 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 5615 | { |
| 5616 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5617 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5618 | } |
| 5619 | |
| 5620 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 5621 | ssl->in_left = rec_len; |
| 5622 | ssl->next_record_offset = 0; |
| 5623 | |
| 5624 | ssl_free_buffered_record( ssl ); |
| 5625 | |
| 5626 | exit: |
| 5627 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 5628 | return( 0 ); |
| 5629 | } |
| 5630 | |
| 5631 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ) |
| 5632 | { |
| 5633 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5634 | size_t const rec_hdr_len = 13; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5635 | size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5636 | |
| 5637 | /* Don't buffer future records outside handshakes. */ |
| 5638 | if( hs == NULL ) |
| 5639 | return( 0 ); |
| 5640 | |
| 5641 | /* Only buffer handshake records (we are only interested |
| 5642 | * in Finished messages). */ |
| 5643 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 5644 | return( 0 ); |
| 5645 | |
| 5646 | /* Don't buffer more than one future epoch record. */ |
| 5647 | if( hs->buffering.future_record.data != NULL ) |
| 5648 | return( 0 ); |
| 5649 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5650 | /* Don't buffer record if there's not enough buffering space remaining. */ |
| 5651 | if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5652 | hs->buffering.total_bytes_buffered ) ) |
| 5653 | { |
| 5654 | 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", |
| 5655 | (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5656 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5657 | return( 0 ); |
| 5658 | } |
| 5659 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5660 | /* Buffer record */ |
| 5661 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 5662 | ssl->in_epoch + 1 ) ); |
| 5663 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr, |
| 5664 | rec_hdr_len + ssl->in_msglen ); |
| 5665 | |
| 5666 | /* ssl_parse_record_header() only considers records |
| 5667 | * of the next epoch as candidates for buffering. */ |
| 5668 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5669 | hs->buffering.future_record.len = total_buf_sz; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5670 | |
| 5671 | hs->buffering.future_record.data = |
| 5672 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 5673 | if( hs->buffering.future_record.data == NULL ) |
| 5674 | { |
| 5675 | /* If we run out of RAM trying to buffer a |
| 5676 | * record from the next epoch, just ignore. */ |
| 5677 | return( 0 ); |
| 5678 | } |
| 5679 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5680 | memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5681 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5682 | hs->buffering.total_bytes_buffered += total_buf_sz; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5683 | return( 0 ); |
| 5684 | } |
| 5685 | |
| 5686 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5687 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5688 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5689 | { |
| 5690 | int ret; |
| 5691 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5692 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5693 | /* We might have buffered a future record; if so, |
| 5694 | * and if the epoch matches now, load it. |
| 5695 | * On success, this call will set ssl->in_left to |
| 5696 | * the length of the buffered record, so that |
| 5697 | * the calls to ssl_fetch_input() below will |
| 5698 | * essentially be no-ops. */ |
| 5699 | ret = ssl_load_buffered_record( ssl ); |
| 5700 | if( ret != 0 ) |
| 5701 | return( ret ); |
| 5702 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5703 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5704 | /* Reset in pointers to default state for TLS/DTLS records, |
| 5705 | * assuming no CID and no offset between record content and |
| 5706 | * record plaintext. */ |
| 5707 | ssl_update_in_pointers( ssl ); |
| 5708 | |
| 5709 | /* Ensure that we have enough space available for the default form |
| 5710 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 5711 | * with no space for CIDs counted in). */ |
| 5712 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 5713 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5714 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5715 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5716 | return( ret ); |
| 5717 | } |
| 5718 | |
| 5719 | if( ( ret = ssl_parse_record_header( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5720 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5721 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5722 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 5723 | ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5724 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5725 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5726 | { |
| 5727 | ret = ssl_buffer_future_record( ssl ); |
| 5728 | if( ret != 0 ) |
| 5729 | return( ret ); |
| 5730 | |
| 5731 | /* Fall through to handling of unexpected records */ |
| 5732 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 5733 | } |
| 5734 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5735 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 5736 | { |
| 5737 | /* Skip unexpected record (but not whole datagram) */ |
| 5738 | ssl->next_record_offset = ssl->in_msglen |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5739 | + mbedtls_ssl_in_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5740 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5741 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 5742 | "(header)" ) ); |
| 5743 | } |
| 5744 | else |
| 5745 | { |
| 5746 | /* Skip invalid record and the rest of the datagram */ |
| 5747 | ssl->next_record_offset = 0; |
| 5748 | ssl->in_left = 0; |
| 5749 | |
| 5750 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 5751 | "(header)" ) ); |
| 5752 | } |
| 5753 | |
| 5754 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5755 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5756 | } |
| 5757 | #endif |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5758 | return( ret ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5759 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5760 | |
| 5761 | /* |
| 5762 | * Read and optionally decrypt the message contents |
| 5763 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5764 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5765 | mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5766 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5767 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5768 | return( ret ); |
| 5769 | } |
| 5770 | |
| 5771 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5772 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5773 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5774 | { |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5775 | ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl ); |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5776 | if( ssl->next_record_offset < ssl->in_left ) |
| 5777 | { |
| 5778 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 5779 | } |
| 5780 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5781 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5782 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5783 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 5784 | { |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5785 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5786 | } |
| 5787 | #endif |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5788 | |
| 5789 | if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5790 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5791 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5792 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5793 | { |
| 5794 | /* Silently discard invalid records */ |
Hanno Becker | 16e9ae2 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 5795 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5796 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 5797 | /* Except when waiting for Finished as a bad mac here |
| 5798 | * probably means something went wrong in the handshake |
| 5799 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 5800 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 5801 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 5802 | { |
| 5803 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 5804 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 5805 | { |
| 5806 | mbedtls_ssl_send_alert_message( ssl, |
| 5807 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5808 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 5809 | } |
| 5810 | #endif |
| 5811 | return( ret ); |
| 5812 | } |
| 5813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5814 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 5815 | if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 && |
| 5816 | ++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] | 5817 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5818 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 5819 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5820 | } |
| 5821 | #endif |
| 5822 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5823 | /* As above, invalid records cause |
| 5824 | * dismissal of the whole datagram. */ |
| 5825 | |
| 5826 | ssl->next_record_offset = 0; |
| 5827 | ssl->in_left = 0; |
| 5828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5829 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5830 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | return( ret ); |
| 5834 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5835 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 5836 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5837 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5838 | { |
| 5839 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5840 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 5841 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5842 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5843 | mbedtls_ssl_send_alert_message( ssl, |
| 5844 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5845 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5846 | } |
| 5847 | #endif |
| 5848 | return( ret ); |
| 5849 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5850 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5851 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5852 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5853 | return( 0 ); |
| 5854 | } |
| 5855 | |
| 5856 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 5857 | { |
| 5858 | int ret; |
| 5859 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5860 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5861 | * Handle particular types of records |
| 5862 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5863 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5864 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5865 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 5866 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 5867 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5868 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5869 | } |
| 5870 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5871 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5872 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5873 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5874 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5875 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 5876 | ssl->in_msglen ) ); |
| 5877 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5878 | } |
| 5879 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5880 | if( ssl->in_msg[0] != 1 ) |
| 5881 | { |
| 5882 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 5883 | ssl->in_msg[0] ) ); |
| 5884 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5885 | } |
| 5886 | |
| 5887 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5888 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5889 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 5890 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5891 | { |
| 5892 | if( ssl->handshake == NULL ) |
| 5893 | { |
| 5894 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 5895 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5896 | } |
| 5897 | |
| 5898 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 5899 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 5900 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5901 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5902 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5903 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5904 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5905 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 5906 | if( ssl->in_msglen != 2 ) |
| 5907 | { |
| 5908 | /* Note: Standard allows for more than one 2 byte alert |
| 5909 | to be packed in a single message, but Mbed TLS doesn't |
| 5910 | currently support this. */ |
| 5911 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 5912 | ssl->in_msglen ) ); |
| 5913 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5914 | } |
| 5915 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5916 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5917 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 5918 | |
| 5919 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 5920 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5921 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5922 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5923 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5924 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5925 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5926 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5927 | } |
| 5928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5929 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5930 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5931 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5932 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 5933 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5934 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5935 | |
| 5936 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 5937 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5938 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 5939 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5940 | 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] | 5941 | /* Will be handled when trying to parse ServerHello */ |
| 5942 | return( 0 ); |
| 5943 | } |
| 5944 | #endif |
| 5945 | |
| 5946 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
| 5947 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 5948 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 5949 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5950 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5951 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 5952 | { |
| 5953 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 5954 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 5955 | return( 0 ); |
| 5956 | } |
| 5957 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 5958 | |
| 5959 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5960 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5963 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5964 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5965 | { |
Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 5966 | /* Drop unexpected ApplicationData records, |
| 5967 | * except at the beginning of renegotiations */ |
| 5968 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 5969 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 5970 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5971 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 5972 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5973 | #endif |
Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 5974 | ) |
| 5975 | { |
| 5976 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 5977 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 5978 | } |
| 5979 | |
| 5980 | if( ssl->handshake != NULL && |
| 5981 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5982 | { |
| 5983 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 5984 | } |
| 5985 | } |
Hanno Becker | f65ad82 | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 5986 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5987 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5988 | return( 0 ); |
| 5989 | } |
| 5990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5991 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5992 | { |
| 5993 | int ret; |
| 5994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5995 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 5996 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5997 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5998 | { |
| 5999 | return( ret ); |
| 6000 | } |
| 6001 | |
| 6002 | return( 0 ); |
| 6003 | } |
| 6004 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6005 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6006 | unsigned char level, |
| 6007 | unsigned char message ) |
| 6008 | { |
| 6009 | int ret; |
| 6010 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6011 | if( ssl == NULL || ssl->conf == NULL ) |
| 6012 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6013 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6014 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6015 | 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] | 6016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6017 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6018 | ssl->out_msglen = 2; |
| 6019 | ssl->out_msg[0] = level; |
| 6020 | ssl->out_msg[1] = message; |
| 6021 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 6022 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6023 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6024 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6025 | return( ret ); |
| 6026 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6027 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6028 | |
| 6029 | return( 0 ); |
| 6030 | } |
| 6031 | |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6032 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6033 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 6034 | { |
| 6035 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6036 | if( session->peer_cert != NULL ) |
| 6037 | { |
| 6038 | mbedtls_x509_crt_free( session->peer_cert ); |
| 6039 | mbedtls_free( session->peer_cert ); |
| 6040 | session->peer_cert = NULL; |
| 6041 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6042 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6043 | if( session->peer_cert_digest != NULL ) |
| 6044 | { |
| 6045 | /* Zeroization is not necessary. */ |
| 6046 | mbedtls_free( session->peer_cert_digest ); |
| 6047 | session->peer_cert_digest = NULL; |
| 6048 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 6049 | session->peer_cert_digest_len = 0; |
| 6050 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6051 | #else |
| 6052 | ((void) session); |
| 6053 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 1757247 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6054 | } |
| 6055 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 6056 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6057 | /* |
| 6058 | * Handshake functions |
| 6059 | */ |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6060 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6061 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6062 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6063 | { |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6064 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6065 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6066 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6067 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6068 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6069 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6070 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6071 | ssl->state++; |
| 6072 | return( 0 ); |
| 6073 | } |
| 6074 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6075 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6076 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6077 | } |
| 6078 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6079 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6080 | { |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6081 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6082 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6083 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6084 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6085 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6086 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6087 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6088 | ssl->state++; |
| 6089 | return( 0 ); |
| 6090 | } |
| 6091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6092 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6093 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6094 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6095 | |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6096 | #else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6097 | /* Some certificate support -> implement write and parse */ |
| 6098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6099 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6100 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6101 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6102 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6103 | const mbedtls_x509_crt *crt; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6104 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6106 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6107 | |
Hanno Becker | 5097cba | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6108 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6109 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6110 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6111 | ssl->state++; |
| 6112 | return( 0 ); |
| 6113 | } |
| 6114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6115 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6116 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6117 | MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6118 | { |
| 6119 | if( ssl->client_auth == 0 ) |
| 6120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6121 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6122 | ssl->state++; |
| 6123 | return( 0 ); |
| 6124 | } |
| 6125 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6126 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6127 | /* |
| 6128 | * If using SSLv3 and got no cert, send an Alert message |
| 6129 | * (otherwise an empty Certificate message will be sent). |
| 6130 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6131 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 6132 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6133 | { |
| 6134 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6135 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 6136 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 6137 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6139 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6140 | goto write_msg; |
| 6141 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6142 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6143 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6144 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6145 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6146 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6147 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6148 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6149 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6150 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 6151 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6152 | } |
| 6153 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6154 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6156 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6157 | |
| 6158 | /* |
| 6159 | * 0 . 0 handshake type |
| 6160 | * 1 . 3 handshake length |
| 6161 | * 4 . 6 length of all certs |
| 6162 | * 7 . 9 length of cert. 1 |
| 6163 | * 10 . n-1 peer certificate |
| 6164 | * n . n+2 length of cert. 2 |
| 6165 | * n+3 . ... upper level cert, etc. |
| 6166 | */ |
| 6167 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6168 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6169 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6170 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6171 | { |
| 6172 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6173 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6174 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6175 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6176 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6177 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 6181 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 6182 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 6183 | |
| 6184 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6185 | i += n; crt = crt->next; |
| 6186 | } |
| 6187 | |
| 6188 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 6189 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 6190 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 6191 | |
| 6192 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6193 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6194 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6195 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 6196 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6197 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6198 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6199 | |
| 6200 | ssl->state++; |
| 6201 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6202 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6203 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6204 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6205 | return( ret ); |
| 6206 | } |
| 6207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6208 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6209 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6210 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6213 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6214 | |
| 6215 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6216 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6217 | unsigned char *crt_buf, |
| 6218 | size_t crt_buf_len ) |
| 6219 | { |
| 6220 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6221 | |
| 6222 | if( peer_crt == NULL ) |
| 6223 | return( -1 ); |
| 6224 | |
| 6225 | if( peer_crt->raw.len != crt_buf_len ) |
| 6226 | return( -1 ); |
| 6227 | |
Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6228 | return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6229 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6230 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | df75938 | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6231 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6232 | unsigned char *crt_buf, |
| 6233 | size_t crt_buf_len ) |
| 6234 | { |
| 6235 | int ret; |
| 6236 | unsigned char const * const peer_cert_digest = |
| 6237 | ssl->session->peer_cert_digest; |
| 6238 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6239 | ssl->session->peer_cert_digest_type; |
| 6240 | mbedtls_md_info_t const * const digest_info = |
| 6241 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6242 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6243 | size_t digest_len; |
| 6244 | |
| 6245 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6246 | return( -1 ); |
| 6247 | |
| 6248 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6249 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6250 | return( -1 ); |
| 6251 | |
| 6252 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6253 | if( ret != 0 ) |
| 6254 | return( -1 ); |
| 6255 | |
| 6256 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6257 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6258 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6259 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6260 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6261 | /* |
| 6262 | * Once the certificate message is read, parse it into a cert chain and |
| 6263 | * perform basic checks, but leave actual verification to the caller |
| 6264 | */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6265 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6266 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6267 | { |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6268 | int ret; |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6269 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6270 | int crt_cnt=0; |
| 6271 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6272 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 6273 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6274 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6275 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6276 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6277 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6278 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6279 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6280 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6281 | } |
| 6282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6283 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 6284 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6285 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6286 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6287 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6288 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6289 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6290 | } |
| 6291 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6292 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6293 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6294 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6295 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6296 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6297 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6298 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6299 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6300 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6301 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6302 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6303 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6304 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6305 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6308 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6309 | i += 3; |
| 6310 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6311 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6312 | while( i < ssl->in_hslen ) |
| 6313 | { |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6314 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6315 | if ( i + 3 > ssl->in_hslen ) { |
| 6316 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6317 | mbedtls_ssl_send_alert_message( ssl, |
| 6318 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6319 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6320 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6321 | } |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6322 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6323 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6324 | if( ssl->in_msg[i] != 0 ) |
| 6325 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6326 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6327 | mbedtls_ssl_send_alert_message( ssl, |
| 6328 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6329 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6330 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6331 | } |
| 6332 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6333 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6334 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6335 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6336 | i += 3; |
| 6337 | |
| 6338 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6339 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6340 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6341 | mbedtls_ssl_send_alert_message( ssl, |
| 6342 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6343 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6344 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6347 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6348 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6349 | if( crt_cnt++ == 0 && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6350 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6351 | MBEDTLS_SSL_IS_CLIENT && |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6352 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6353 | { |
Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6354 | /* During client-side renegotiation, check that the server's |
| 6355 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6356 | * mitigating the triple handshake attack. On success, reuse |
| 6357 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6358 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6359 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6360 | &ssl->in_msg[i], |
| 6361 | n ) != 0 ) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6362 | { |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6363 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6364 | mbedtls_ssl_send_alert_message( ssl, |
| 6365 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6366 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6367 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6368 | } |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6369 | |
| 6370 | /* Now we can safely free the original chain. */ |
| 6371 | ssl_clear_peer_cert( ssl->session ); |
| 6372 | } |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6373 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6374 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6375 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6376 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6377 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6378 | #else |
Hanno Becker | 42de8f8 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 6379 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0cc7af5 | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6380 | * it in-place from the input buffer instead of making a copy. */ |
| 6381 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6382 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6383 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6384 | { |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6385 | case 0: /*ok*/ |
| 6386 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6387 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6388 | prior certificate was already trusted. */ |
| 6389 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6390 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6391 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6392 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6393 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6394 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6395 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6396 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6397 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6398 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6399 | default: |
| 6400 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6401 | crt_parse_der_failed: |
| 6402 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6403 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6404 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
| 6407 | i += n; |
| 6408 | } |
| 6409 | |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6410 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6411 | return( 0 ); |
| 6412 | } |
| 6413 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6414 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6415 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6416 | { |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6417 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT ) |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6418 | return( -1 ); |
| 6419 | |
| 6420 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 6421 | /* |
| 6422 | * Check if the client sent an empty certificate |
| 6423 | */ |
| 6424 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 6425 | { |
| 6426 | if( ssl->in_msglen == 2 && |
| 6427 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 6428 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6429 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6430 | { |
| 6431 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 6432 | return( 0 ); |
| 6433 | } |
| 6434 | |
| 6435 | return( -1 ); |
| 6436 | } |
| 6437 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 6438 | |
| 6439 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 6440 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6441 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6442 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6443 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6444 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6445 | { |
| 6446 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 6447 | return( 0 ); |
| 6448 | } |
| 6449 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6450 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 6451 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 17daaa5 | 2019-06-18 12:31:45 +0100 | [diff] [blame] | 6452 | |
| 6453 | return( -1 ); |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6454 | } |
| 6455 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6456 | |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6457 | /* Check if a certificate message is expected. |
| 6458 | * Return either |
| 6459 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6460 | * - SSL_CERTIFICATE_SKIP |
| 6461 | * indicating whether a Certificate message is expected or not. |
| 6462 | */ |
| 6463 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6464 | #define SSL_CERTIFICATE_SKIP 1 |
| 6465 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6466 | int authmode ) |
| 6467 | { |
| 6468 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6469 | ssl->handshake->ciphersuite_info; |
| 6470 | |
| 6471 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6472 | return( SSL_CERTIFICATE_SKIP ); |
| 6473 | |
| 6474 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6475 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER ) |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6476 | { |
| 6477 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6478 | return( SSL_CERTIFICATE_SKIP ); |
| 6479 | |
| 6480 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6481 | { |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6482 | ssl->session_negotiate->verify_result = |
| 6483 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6484 | return( SSL_CERTIFICATE_SKIP ); |
| 6485 | } |
| 6486 | } |
Hanno Becker | fd5dc8a | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 6487 | #else |
| 6488 | ((void) authmode); |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6489 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6490 | |
| 6491 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6492 | } |
| 6493 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6494 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6495 | int authmode, |
| 6496 | mbedtls_x509_crt *chain, |
| 6497 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6498 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6499 | int verify_ret; |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6500 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6501 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6502 | mbedtls_x509_crt *ca_chain; |
| 6503 | mbedtls_x509_crl *ca_crl; |
| 6504 | |
| 6505 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6506 | return( 0 ); |
| 6507 | |
| 6508 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6509 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6510 | { |
| 6511 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6512 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6513 | } |
| 6514 | else |
| 6515 | #endif |
| 6516 | { |
| 6517 | ca_chain = ssl->conf->ca_chain; |
| 6518 | ca_crl = ssl->conf->ca_crl; |
| 6519 | } |
| 6520 | |
| 6521 | /* |
| 6522 | * Main check: verify certificate |
| 6523 | */ |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6524 | verify_ret = mbedtls_x509_crt_verify_restartable( |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6525 | chain, |
| 6526 | ca_chain, ca_crl, |
| 6527 | ssl->conf->cert_profile, |
| 6528 | ssl->hostname, |
| 6529 | &ssl->session_negotiate->verify_result, |
| 6530 | ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx ); |
| 6531 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6532 | if( verify_ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6533 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6534 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
| 6537 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6538 | if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6539 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6540 | #endif |
| 6541 | |
| 6542 | /* |
| 6543 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6544 | */ |
| 6545 | |
| 6546 | #if defined(MBEDTLS_ECP_C) |
| 6547 | { |
Manuel Pégourié-Gonnard | b391766 | 2019-07-03 11:19:30 +0200 | [diff] [blame] | 6548 | int ret; |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6549 | mbedtls_pk_context *pk; |
| 6550 | ret = mbedtls_x509_crt_pk_acquire( chain, &pk ); |
| 6551 | if( ret != 0 ) |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 6552 | { |
| 6553 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6554 | return( ret ); |
Hanno Becker | 2224ccf | 2019-06-28 10:52:45 +0100 | [diff] [blame] | 6555 | } |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6556 | |
| 6557 | /* If certificate uses an EC key, make sure the curve is OK */ |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6558 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) ) |
| 6559 | ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ); |
| 6560 | |
Hanno Becker | c6d1c3e | 2019-03-05 13:50:56 +0000 | [diff] [blame] | 6561 | mbedtls_x509_crt_pk_release( chain ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6562 | |
| 6563 | if( ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6564 | { |
| 6565 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6566 | |
| 6567 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6568 | if( verify_ret == 0 ) |
| 6569 | verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6570 | } |
| 6571 | } |
| 6572 | #endif /* MBEDTLS_ECP_C */ |
| 6573 | |
| 6574 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6575 | ciphersuite_info, |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 6576 | ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 6577 | MBEDTLS_SSL_IS_CLIENT ), |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6578 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6579 | { |
| 6580 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6581 | if( verify_ret == 0 ) |
| 6582 | verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
| 6585 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6586 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6587 | * with details encoded in the verification flags. All other kinds |
| 6588 | * of error codes, including those from the user provided f_vrfy |
| 6589 | * functions, are treated as fatal and lead to a failure of |
| 6590 | * ssl_parse_certificate even if verification was optional. */ |
| 6591 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6592 | ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6593 | verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6594 | { |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6595 | verify_ret = 0; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
| 6598 | if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6599 | { |
| 6600 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6601 | verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6602 | } |
| 6603 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6604 | if( verify_ret != 0 ) |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6605 | { |
| 6606 | uint8_t alert; |
| 6607 | |
| 6608 | /* The certificate may have been rejected for several reasons. |
| 6609 | Pick one and send the corresponding alert. Which alert to send |
| 6610 | may be a subject of debate in some cases. */ |
| 6611 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6612 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6613 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6614 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6615 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6616 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6617 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6618 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6619 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6620 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6621 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6622 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6623 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6624 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6625 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6626 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6627 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6628 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6629 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6630 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6631 | else |
| 6632 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6633 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6634 | alert ); |
| 6635 | } |
| 6636 | |
| 6637 | #if defined(MBEDTLS_DEBUG_C) |
| 6638 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6639 | { |
| 6640 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 6641 | ssl->session_negotiate->verify_result ) ); |
| 6642 | } |
| 6643 | else |
| 6644 | { |
| 6645 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6646 | } |
| 6647 | #endif /* MBEDTLS_DEBUG_C */ |
| 6648 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6649 | return( verify_ret ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6650 | } |
| 6651 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6652 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6653 | |
| 6654 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6655 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6656 | unsigned char *start, size_t len ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6657 | { |
| 6658 | int ret; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6659 | /* Remember digest of the peer's end-CRT. */ |
| 6660 | ssl->session_negotiate->peer_cert_digest = |
| 6661 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6662 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6663 | { |
| 6664 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6665 | sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) ); |
| 6666 | mbedtls_ssl_send_alert_message( ssl, |
| 6667 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6668 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6669 | |
| 6670 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6671 | } |
| 6672 | |
| 6673 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6674 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6675 | start, len, |
| 6676 | ssl->session_negotiate->peer_cert_digest ); |
| 6677 | |
| 6678 | ssl->session_negotiate->peer_cert_digest_type = |
| 6679 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6680 | ssl->session_negotiate->peer_cert_digest_len = |
| 6681 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6682 | |
| 6683 | return( ret ); |
| 6684 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6685 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6686 | |
| 6687 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6688 | unsigned char *start, size_t len ) |
| 6689 | { |
| 6690 | unsigned char *end = start + len; |
| 6691 | int ret; |
| 6692 | |
| 6693 | /* Make a copy of the peer's raw public key. */ |
| 6694 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6695 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6696 | &ssl->handshake->peer_pubkey ); |
| 6697 | if( ret != 0 ) |
| 6698 | { |
| 6699 | /* We should have parsed the public key before. */ |
| 6700 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6701 | } |
| 6702 | |
| 6703 | return( 0 ); |
| 6704 | } |
| 6705 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6706 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6707 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6708 | { |
| 6709 | int ret = 0; |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6710 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6711 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6712 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6713 | ? ssl->handshake->sni_authmode |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 6714 | : mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6715 | #else |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 6716 | const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6717 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6718 | void *rs_ctx = NULL; |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6719 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6720 | |
| 6721 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6722 | |
Hanno Becker | 6b9a6f3 | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6723 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6724 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6725 | { |
| 6726 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6727 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6728 | } |
| 6729 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6730 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 6731 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 6732 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6733 | { |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6734 | chain = ssl->handshake->ecrs_peer_cert; |
| 6735 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6736 | goto crt_verify; |
| 6737 | } |
| 6738 | #endif |
| 6739 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 6740 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6741 | { |
| 6742 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6743 | let it decide whether to alert. */ |
| 6744 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6745 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6746 | } |
| 6747 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6748 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6749 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6750 | { |
| 6751 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6752 | |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6753 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6754 | ret = 0; |
| 6755 | else |
| 6756 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6757 | |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6758 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6759 | } |
Hanno Becker | b8a0857 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6760 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6761 | |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6762 | /* Clear existing peer CRT structure in case we tried to |
| 6763 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | a46c287 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 6764 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6765 | |
| 6766 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6767 | if( chain == NULL ) |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6768 | { |
| 6769 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6770 | sizeof( mbedtls_x509_crt ) ) ); |
| 6771 | mbedtls_ssl_send_alert_message( ssl, |
| 6772 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6773 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | a46c287 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 6774 | |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6775 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6776 | goto exit; |
| 6777 | } |
| 6778 | mbedtls_x509_crt_init( chain ); |
| 6779 | |
| 6780 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | 35e4177 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6781 | if( ret != 0 ) |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6782 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6783 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6784 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 6785 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 6786 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6787 | |
| 6788 | crt_verify: |
| 6789 | if( ssl->handshake->ecrs_enabled) |
| 6790 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6791 | #endif |
| 6792 | |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6793 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6794 | chain, rs_ctx ); |
Hanno Becker | 3cf5061 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6795 | if( ret != 0 ) |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6796 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6797 | |
Hanno Becker | 3008d28 | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 6798 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6799 | { |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6800 | size_t pk_len; |
| 6801 | unsigned char *pk_start; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6802 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6803 | /* We parse the CRT chain without copying, so |
| 6804 | * these pointers point into the input buffer, |
| 6805 | * and are hence still valid after freeing the |
| 6806 | * CRT chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6807 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6808 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6809 | unsigned char *crt_start; |
| 6810 | size_t crt_len; |
| 6811 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6812 | crt_start = chain->raw.p; |
| 6813 | crt_len = chain->raw.len; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6814 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6815 | |
Hanno Becker | 8c13ee6 | 2019-02-26 16:48:17 +0000 | [diff] [blame] | 6816 | pk_start = chain->cache->pk_raw.p; |
| 6817 | pk_len = chain->cache->pk_raw.len; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6818 | |
| 6819 | /* Free the CRT structures before computing |
| 6820 | * digest and copying the peer's public key. */ |
| 6821 | mbedtls_x509_crt_free( chain ); |
| 6822 | mbedtls_free( chain ); |
| 6823 | chain = NULL; |
| 6824 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6825 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6826 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6827 | if( ret != 0 ) |
Hanno Becker | cf291d6 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 6828 | goto exit; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 6829 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6830 | |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6831 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6832 | if( ret != 0 ) |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6833 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6834 | } |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6835 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6836 | /* Pass ownership to session structure. */ |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6837 | ssl->session_negotiate->peer_cert = chain; |
| 6838 | chain = NULL; |
Hanno Becker | 34106f6 | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6839 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6840 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6841 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6842 | |
Hanno Becker | 613d490 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6843 | exit: |
| 6844 | |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6845 | if( ret == 0 ) |
| 6846 | ssl->state++; |
| 6847 | |
| 6848 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 6849 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 6850 | { |
| 6851 | ssl->handshake->ecrs_peer_cert = chain; |
| 6852 | chain = NULL; |
| 6853 | } |
| 6854 | #endif |
| 6855 | |
| 6856 | if( chain != NULL ) |
| 6857 | { |
| 6858 | mbedtls_x509_crt_free( chain ); |
| 6859 | mbedtls_free( chain ); |
| 6860 | } |
| 6861 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6862 | return( ret ); |
| 6863 | } |
Hanno Becker | b71e90a | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6864 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6865 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6866 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6867 | { |
| 6868 | int ret; |
| 6869 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6870 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6871 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6872 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6873 | ssl->out_msglen = 1; |
| 6874 | ssl->out_msg[0] = 1; |
| 6875 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6876 | ssl->state++; |
| 6877 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6878 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6879 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6880 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6881 | return( ret ); |
| 6882 | } |
| 6883 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6884 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6885 | |
| 6886 | return( 0 ); |
| 6887 | } |
| 6888 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6889 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6890 | { |
| 6891 | int ret; |
| 6892 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6893 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6894 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 6895 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6896 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6897 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6898 | return( ret ); |
| 6899 | } |
| 6900 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6901 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6902 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6903 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6904 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6905 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6906 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6909 | /* CCS records are only accepted if they have length 1 and content '1', |
| 6910 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6911 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6912 | /* |
| 6913 | * Switch to our negotiated transform and session parameters for inbound |
| 6914 | * data. |
| 6915 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6916 | 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] | 6917 | ssl->transform_in = ssl->transform_negotiate; |
| 6918 | ssl->session_in = ssl->session_negotiate; |
| 6919 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6920 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6921 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6922 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6923 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6924 | ssl_dtls_replay_reset( ssl ); |
| 6925 | #endif |
| 6926 | |
| 6927 | /* Increment epoch */ |
| 6928 | if( ++ssl->in_epoch == 0 ) |
| 6929 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6930 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6931 | /* This is highly unlikely to happen for legitimate reasons, so |
| 6932 | 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] | 6933 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6934 | } |
| 6935 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6936 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6937 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6938 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 6939 | { |
| 6940 | memset( ssl->in_ctr, 0, 8 ); |
| 6941 | } |
| 6942 | #endif |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6943 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 6944 | ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6946 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 6947 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6949 | 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] | 6950 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6951 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6952 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6953 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6954 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6955 | } |
| 6956 | } |
| 6957 | #endif |
| 6958 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6959 | ssl->state++; |
| 6960 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6961 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6962 | |
| 6963 | return( 0 ); |
| 6964 | } |
| 6965 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6966 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 6967 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6968 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 6969 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6970 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6971 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6972 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 6973 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6974 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6975 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6976 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6977 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6978 | #if defined(MBEDTLS_SHA512_C) |
| 6979 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6980 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 6981 | else |
| 6982 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6983 | #if defined(MBEDTLS_SHA256_C) |
| 6984 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6985 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6986 | else |
| 6987 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6988 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 6989 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6990 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6991 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 6992 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6993 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 6994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6995 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6996 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6997 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6998 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6999 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 7000 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7001 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7002 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7003 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7004 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7005 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7006 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7007 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7008 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7009 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7010 | } |
| 7011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7012 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7013 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7014 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7015 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7016 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7017 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7018 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7019 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7020 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7021 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7022 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7023 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7024 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7025 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7026 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7027 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7028 | } |
| 7029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7030 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7031 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7032 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7033 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7034 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7035 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7036 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7037 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7038 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7040 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7041 | #if defined(MBEDTLS_SHA256_C) |
| 7042 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7043 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7044 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7045 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7046 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7047 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7048 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7049 | #if defined(MBEDTLS_SHA512_C) |
| 7050 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7051 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7052 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7053 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7054 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7055 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7056 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7058 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7059 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7060 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7061 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7062 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7063 | mbedtls_md5_context md5; |
| 7064 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7065 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7066 | unsigned char padbuf[48]; |
| 7067 | unsigned char md5sum[16]; |
| 7068 | unsigned char sha1sum[20]; |
| 7069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7070 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7071 | if( !session ) |
| 7072 | session = ssl->session; |
| 7073 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7074 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7075 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7076 | mbedtls_md5_init( &md5 ); |
| 7077 | mbedtls_sha1_init( &sha1 ); |
| 7078 | |
| 7079 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7080 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7081 | |
| 7082 | /* |
| 7083 | * SSLv3: |
| 7084 | * hash = |
| 7085 | * MD5( master + pad2 + |
| 7086 | * MD5( handshake + sender + master + pad1 ) ) |
| 7087 | * + SHA1( master + pad2 + |
| 7088 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7089 | */ |
| 7090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7091 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7092 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7093 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7094 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7096 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7097 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7098 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7099 | #endif |
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 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7102 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7103 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7104 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7105 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7106 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 7107 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7108 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7109 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7110 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7111 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 7112 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7113 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 7114 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7115 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7116 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7117 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7118 | mbedtls_md5_starts_ret( &md5 ); |
| 7119 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7120 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7121 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 7122 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7123 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7124 | mbedtls_sha1_starts_ret( &sha1 ); |
| 7125 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7126 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 7127 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 7128 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7130 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7131 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7132 | mbedtls_md5_free( &md5 ); |
| 7133 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7134 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7135 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7136 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 7137 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7139 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7140 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7141 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7143 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7144 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7145 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7146 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7147 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7148 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7149 | mbedtls_md5_context md5; |
| 7150 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7151 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7153 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7154 | if( !session ) |
| 7155 | session = ssl->session; |
| 7156 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7157 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7158 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7159 | mbedtls_md5_init( &md5 ); |
| 7160 | mbedtls_sha1_init( &sha1 ); |
| 7161 | |
| 7162 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7163 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7164 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7165 | /* |
| 7166 | * TLSv1: |
| 7167 | * hash = PRF( master, finished_label, |
| 7168 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 7169 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7171 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7172 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7173 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7174 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7176 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7177 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7178 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7179 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7181 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7182 | ? "client finished" |
| 7183 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7184 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7185 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 7186 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7187 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7188 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7189 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7190 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7191 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7192 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7193 | mbedtls_md5_free( &md5 ); |
| 7194 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7195 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7196 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7198 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7199 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7200 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7202 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7203 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7204 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7205 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7206 | { |
| 7207 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7208 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7209 | mbedtls_sha256_context sha256; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7210 | unsigned char padbuf[32]; |
| 7211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7212 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7213 | if( !session ) |
| 7214 | session = ssl->session; |
| 7215 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7216 | mbedtls_sha256_init( &sha256 ); |
| 7217 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7218 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7219 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7220 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7221 | |
| 7222 | /* |
| 7223 | * TLSv1.2: |
| 7224 | * hash = PRF( master, finished_label, |
| 7225 | * Hash( handshake ) )[0.11] |
| 7226 | */ |
| 7227 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7228 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7229 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7230 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7231 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7233 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7234 | ? "client finished" |
| 7235 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7236 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7237 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7238 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7239 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7240 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7242 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7243 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7244 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7245 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7246 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7248 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7249 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7250 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7252 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7253 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7254 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7255 | { |
| 7256 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7257 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7258 | mbedtls_sha512_context sha512; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7259 | unsigned char padbuf[48]; |
| 7260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7261 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7262 | if( !session ) |
| 7263 | session = ssl->session; |
| 7264 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7265 | mbedtls_sha512_init( &sha512 ); |
| 7266 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7267 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7268 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7269 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7270 | |
| 7271 | /* |
| 7272 | * TLSv1.2: |
| 7273 | * hash = PRF( master, finished_label, |
| 7274 | * Hash( handshake ) )[0.11] |
| 7275 | */ |
| 7276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7277 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7278 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7279 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7280 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7282 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7283 | ? "client finished" |
| 7284 | : "server finished"; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7285 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7286 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7287 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7288 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7289 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7291 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7292 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7293 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7294 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7295 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7298 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7299 | #endif /* MBEDTLS_SHA512_C */ |
| 7300 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7302 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7304 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7305 | |
| 7306 | /* |
| 7307 | * Free our handshake params |
| 7308 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7309 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7310 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7311 | ssl->handshake = NULL; |
| 7312 | |
| 7313 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7314 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7315 | */ |
| 7316 | if( ssl->transform ) |
| 7317 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7318 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7319 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7320 | } |
| 7321 | ssl->transform = ssl->transform_negotiate; |
| 7322 | ssl->transform_negotiate = NULL; |
| 7323 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7324 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7325 | } |
| 7326 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7327 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7328 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7329 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7331 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7332 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7333 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7334 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7335 | ssl->renego_records_seen = 0; |
| 7336 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7337 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7338 | |
| 7339 | /* |
| 7340 | * Free the previous session and switch in the current one |
| 7341 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7342 | if( ssl->session ) |
| 7343 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7344 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 7345 | /* RFC 7366 3.1: keep the EtM state */ |
| 7346 | ssl->session_negotiate->encrypt_then_mac = |
| 7347 | ssl->session->encrypt_then_mac; |
| 7348 | #endif |
| 7349 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7350 | mbedtls_ssl_session_free( ssl->session ); |
| 7351 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7352 | } |
| 7353 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7354 | ssl->session_negotiate = NULL; |
| 7355 | |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7356 | #if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7357 | /* |
| 7358 | * Add cache entry |
| 7359 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7360 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 7361 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7362 | ssl->handshake->resume == 0 ) |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7363 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7364 | 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] | 7365 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7366 | } |
Manuel Pégourié-Gonnard | 7b80c64 | 2019-07-02 16:21:30 +0200 | [diff] [blame] | 7367 | #endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7368 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7369 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7370 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7371 | ssl->handshake->flight != NULL ) |
| 7372 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 7373 | /* Cancel handshake timer */ |
| 7374 | ssl_set_timer( ssl, 0 ); |
| 7375 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7376 | /* Keep last flight around in case we need to resend it: |
| 7377 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7378 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7379 | } |
| 7380 | else |
| 7381 | #endif |
| 7382 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7383 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7384 | ssl->state++; |
| 7385 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7386 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7387 | } |
| 7388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7389 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7390 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7391 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7392 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7393 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7394 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7395 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 7396 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7397 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, |
| 7398 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7399 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 7400 | /* |
| 7401 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7402 | * may define some other value. Currently (early 2016), no defined |
| 7403 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7404 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7405 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7406 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7408 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7409 | ssl->verify_data_len = hash_len; |
| 7410 | 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] | 7411 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7412 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7413 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7414 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7415 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7416 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7417 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7418 | /* |
| 7419 | * In case of session resuming, invert the client and server |
| 7420 | * ChangeCipherSpec messages order. |
| 7421 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7422 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7423 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7424 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7425 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 7426 | MBEDTLS_SSL_IS_CLIENT ) |
| 7427 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7428 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7429 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7430 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7431 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7432 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 7433 | MBEDTLS_SSL_IS_SERVER ) |
| 7434 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7435 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7436 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7437 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7438 | } |
| 7439 | else |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7440 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7441 | ssl->state++; |
| 7442 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7443 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7444 | * Switch to our negotiated transform and session parameters for outbound |
| 7445 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7446 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7447 | 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] | 7448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7449 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7450 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7451 | { |
| 7452 | unsigned char i; |
| 7453 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7454 | /* Remember current epoch settings for resending */ |
| 7455 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7456 | 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] | 7457 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7458 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7459 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7460 | |
| 7461 | /* Increment epoch */ |
| 7462 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7463 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7464 | break; |
| 7465 | |
| 7466 | /* The loop goes to its end iff the counter is wrapping */ |
| 7467 | if( i == 0 ) |
| 7468 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7469 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7470 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7471 | } |
| 7472 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7473 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7474 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7475 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 7476 | { |
| 7477 | memset( ssl->cur_out_ctr, 0, 8 ); |
| 7478 | } |
| 7479 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7480 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7481 | ssl->transform_out = ssl->transform_negotiate; |
| 7482 | ssl->session_out = ssl->session_negotiate; |
| 7483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7484 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7485 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7486 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7487 | 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] | 7488 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7489 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 7490 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7491 | } |
| 7492 | } |
| 7493 | #endif |
| 7494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7495 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7496 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7497 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 7498 | #endif |
| 7499 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7500 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7501 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7502 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7503 | return( ret ); |
| 7504 | } |
| 7505 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7506 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7507 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7508 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7509 | { |
| 7510 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7511 | return( ret ); |
| 7512 | } |
| 7513 | #endif |
| 7514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7515 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7516 | |
| 7517 | return( 0 ); |
| 7518 | } |
| 7519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7520 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7521 | #define SSL_MAX_HASH_LEN 36 |
| 7522 | #else |
| 7523 | #define SSL_MAX_HASH_LEN 12 |
| 7524 | #endif |
| 7525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7526 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7527 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 7528 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7529 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7530 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7532 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7533 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7534 | ssl->handshake->calc_finished( ssl, buf, |
| 7535 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7536 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7537 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7538 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7539 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7540 | return( ret ); |
| 7541 | } |
| 7542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7543 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7544 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7545 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7546 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7547 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7548 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7549 | } |
| 7550 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7551 | /* 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] | 7552 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 7553 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7554 | hash_len = 36; |
| 7555 | else |
| 7556 | #endif |
| 7557 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7559 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 7560 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7561 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7562 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7563 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7564 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7565 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7566 | } |
| 7567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7568 | if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 7569 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7570 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7571 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7572 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7573 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7574 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7575 | } |
| 7576 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7577 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7578 | ssl->verify_data_len = hash_len; |
| 7579 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7580 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7581 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7582 | #if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7583 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7584 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7585 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7586 | 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] | 7587 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7588 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7589 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7590 | 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] | 7591 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7592 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7593 | } |
| 7594 | else |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 7595 | #endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7596 | ssl->state++; |
| 7597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7598 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7599 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7600 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7601 | #endif |
| 7602 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7603 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7604 | |
| 7605 | return( 0 ); |
| 7606 | } |
| 7607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7608 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7609 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7610 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7612 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7613 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7614 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 7615 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7616 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 7617 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7618 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7619 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7620 | #if defined(MBEDTLS_SHA256_C) |
| 7621 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7622 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7623 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7624 | #if defined(MBEDTLS_SHA512_C) |
| 7625 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7626 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7627 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7628 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7629 | |
| 7630 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7631 | |
| 7632 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 7633 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 7634 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 7635 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7637 | #if defined(MBEDTLS_DHM_C) |
| 7638 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7639 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7640 | #if defined(MBEDTLS_ECDH_C) |
| 7641 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7642 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 7643 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 7644 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 7645 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7646 | handshake->ecjpake_cache = NULL; |
| 7647 | handshake->ecjpake_cache_len = 0; |
| 7648 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 7649 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7650 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 7651 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 7652 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 7653 | #endif |
| 7654 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7655 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7656 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 7657 | #endif |
Hanno Becker | 3bf8cdf | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 7658 | |
| 7659 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 7660 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7661 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 7662 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7663 | } |
| 7664 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 7665 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7666 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7667 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 7668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7669 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 7670 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 7671 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 7672 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7673 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 7674 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 7675 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7676 | } |
| 7677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7678 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7679 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7680 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7681 | } |
| 7682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7683 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7684 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7685 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7686 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7687 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7688 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7689 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7690 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7691 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7692 | |
| 7693 | /* |
| 7694 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 7695 | * Now allocate missing structures. |
| 7696 | */ |
| 7697 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7698 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7699 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7700 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7701 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7702 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7703 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7704 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7705 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7706 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 7707 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7708 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7709 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7710 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7711 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7712 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7713 | if( ssl->handshake == NULL || |
| 7714 | ssl->transform_negotiate == NULL || |
| 7715 | ssl->session_negotiate == NULL ) |
| 7716 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 7717 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7718 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7719 | mbedtls_free( ssl->handshake ); |
| 7720 | mbedtls_free( ssl->transform_negotiate ); |
| 7721 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7722 | |
| 7723 | ssl->handshake = NULL; |
| 7724 | ssl->transform_negotiate = NULL; |
| 7725 | ssl->session_negotiate = NULL; |
| 7726 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 7727 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7728 | } |
| 7729 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7730 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7731 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 7732 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 7733 | ssl_handshake_params_init( ssl->handshake ); |
| 7734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7735 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7736 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 7737 | { |
| 7738 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7739 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 7740 | 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] | 7741 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 7742 | else |
| 7743 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 7744 | |
| 7745 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 7746 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7747 | #endif |
| 7748 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7749 | return( 0 ); |
| 7750 | } |
| 7751 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 7752 | #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] | 7753 | /* Dummy cookie callbacks for defaults */ |
| 7754 | static int ssl_cookie_write_dummy( void *ctx, |
| 7755 | unsigned char **p, unsigned char *end, |
| 7756 | const unsigned char *cli_id, size_t cli_id_len ) |
| 7757 | { |
| 7758 | ((void) ctx); |
| 7759 | ((void) p); |
| 7760 | ((void) end); |
| 7761 | ((void) cli_id); |
| 7762 | ((void) cli_id_len); |
| 7763 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7764 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 7765 | } |
| 7766 | |
| 7767 | static int ssl_cookie_check_dummy( void *ctx, |
| 7768 | const unsigned char *cookie, size_t cookie_len, |
| 7769 | const unsigned char *cli_id, size_t cli_id_len ) |
| 7770 | { |
| 7771 | ((void) ctx); |
| 7772 | ((void) cookie); |
| 7773 | ((void) cookie_len); |
| 7774 | ((void) cli_id); |
| 7775 | ((void) cli_id_len); |
| 7776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7777 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 7778 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 7779 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 7780 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7781 | /* Once ssl->out_hdr as the address of the beginning of the |
| 7782 | * next outgoing record is set, deduce the other pointers. |
| 7783 | * |
| 7784 | * Note: For TLS, we save the implicit record sequence number |
| 7785 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 7786 | * and the caller has to make sure there's space for this. |
| 7787 | */ |
| 7788 | |
| 7789 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 7790 | mbedtls_ssl_transform *transform ) |
| 7791 | { |
| 7792 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7793 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7794 | { |
| 7795 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7796 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7797 | ssl->out_cid = ssl->out_ctr + 8; |
| 7798 | ssl->out_len = ssl->out_cid; |
| 7799 | if( transform != NULL ) |
| 7800 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7801 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7802 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7803 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7804 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7805 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7806 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 7807 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7808 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7809 | { |
| 7810 | ssl->out_ctr = ssl->out_hdr - 8; |
| 7811 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7812 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 7813 | ssl->out_cid = ssl->out_len; |
| 7814 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7815 | ssl->out_iv = ssl->out_hdr + 5; |
| 7816 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7817 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7818 | |
| 7819 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 7820 | if( transform != NULL && |
| 7821 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 7822 | { |
| 7823 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 7824 | } |
| 7825 | else |
| 7826 | ssl->out_msg = ssl->out_iv; |
| 7827 | } |
| 7828 | |
| 7829 | /* Once ssl->in_hdr as the address of the beginning of the |
| 7830 | * next incoming record is set, deduce the other pointers. |
| 7831 | * |
| 7832 | * Note: For TLS, we save the implicit record sequence number |
| 7833 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 7834 | * and the caller has to make sure there's space for this. |
| 7835 | */ |
| 7836 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7837 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7838 | { |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7839 | /* This function sets the pointers to match the case |
| 7840 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 7841 | * and ssl->in_msg pointing to the beginning of the record |
| 7842 | * content. |
| 7843 | * |
| 7844 | * When decrypting a protected record, ssl->in_msg |
| 7845 | * will be shifted to point to the beginning of the |
| 7846 | * record plaintext. |
| 7847 | */ |
| 7848 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7849 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7850 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7851 | { |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7852 | /* This sets the header pointers to match records |
| 7853 | * without CID. When we receive a record containing |
| 7854 | * a CID, the fields are shifted accordingly in |
| 7855 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7856 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7857 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7858 | ssl->in_cid = ssl->in_ctr + 8; |
| 7859 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7860 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7861 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7862 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7863 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7864 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7865 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 7866 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7867 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7868 | { |
| 7869 | ssl->in_ctr = ssl->in_hdr - 8; |
| 7870 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7871 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 7872 | ssl->in_cid = ssl->in_len; |
| 7873 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7874 | ssl->in_iv = ssl->in_hdr + 5; |
| 7875 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7876 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7877 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7878 | /* This will be adjusted at record decryption time. */ |
| 7879 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7880 | } |
| 7881 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7882 | /* |
| 7883 | * Initialize an SSL context |
| 7884 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 7885 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 7886 | { |
| 7887 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 7888 | } |
| 7889 | |
| 7890 | /* |
| 7891 | * Setup an SSL context |
| 7892 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7893 | |
| 7894 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
| 7895 | { |
| 7896 | /* Set the incoming and outgoing record pointers. */ |
| 7897 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7898 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7899 | { |
| 7900 | ssl->out_hdr = ssl->out_buf; |
| 7901 | ssl->in_hdr = ssl->in_buf; |
| 7902 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7903 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7904 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7905 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7906 | { |
| 7907 | ssl->out_hdr = ssl->out_buf + 8; |
| 7908 | ssl->in_hdr = ssl->in_buf + 8; |
| 7909 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7910 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7911 | |
| 7912 | /* Derive other internal pointers. */ |
| 7913 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7914 | ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7915 | } |
| 7916 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 7917 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 7918 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7919 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7920 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7921 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 7922 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 7923 | |
| 7924 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 7925 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 7926 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 7927 | |
| 7928 | /* Set to NULL in case of an error condition */ |
| 7929 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7930 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7931 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 7932 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7933 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7934 | 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] | 7935 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7936 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7937 | } |
| 7938 | |
| 7939 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 7940 | if( ssl->out_buf == NULL ) |
| 7941 | { |
| 7942 | 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] | 7943 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7944 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7945 | } |
| 7946 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7947 | ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 7948 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7949 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7950 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7951 | |
| 7952 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7953 | |
| 7954 | error: |
| 7955 | mbedtls_free( ssl->in_buf ); |
| 7956 | mbedtls_free( ssl->out_buf ); |
| 7957 | |
| 7958 | ssl->conf = NULL; |
| 7959 | |
| 7960 | ssl->in_buf = NULL; |
| 7961 | ssl->out_buf = NULL; |
| 7962 | |
| 7963 | ssl->in_hdr = NULL; |
| 7964 | ssl->in_ctr = NULL; |
| 7965 | ssl->in_len = NULL; |
| 7966 | ssl->in_iv = NULL; |
| 7967 | ssl->in_msg = NULL; |
| 7968 | |
| 7969 | ssl->out_hdr = NULL; |
| 7970 | ssl->out_ctr = NULL; |
| 7971 | ssl->out_len = NULL; |
| 7972 | ssl->out_iv = NULL; |
| 7973 | ssl->out_msg = NULL; |
| 7974 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 7975 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7976 | } |
| 7977 | |
| 7978 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7979 | * Reset an initialized and used SSL context for re-use while retaining |
| 7980 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7981 | * |
| 7982 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 7983 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7984 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7985 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7986 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7987 | int ret; |
| 7988 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 7989 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 7990 | !defined(MBEDTLS_SSL_SRV_C) |
| 7991 | ((void) partial); |
| 7992 | #endif |
| 7993 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7994 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7995 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 7996 | /* Cancel any possibly running timer */ |
| 7997 | ssl_set_timer( ssl, 0 ); |
| 7998 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7999 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8000 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8001 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8002 | |
| 8003 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8004 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 8005 | 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] | 8006 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8007 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8008 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8009 | ssl->in_offt = NULL; |
Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 8010 | ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8011 | |
| 8012 | ssl->in_msgtype = 0; |
| 8013 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8014 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8015 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 8016 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8017 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8018 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 8019 | ssl_dtls_replay_reset( ssl ); |
| 8020 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8021 | |
| 8022 | ssl->in_hslen = 0; |
| 8023 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 8024 | |
| 8025 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8026 | |
| 8027 | ssl->out_msgtype = 0; |
| 8028 | ssl->out_msglen = 0; |
| 8029 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8030 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 8031 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8032 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8033 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8034 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8035 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 8036 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8037 | ssl->transform_in = NULL; |
| 8038 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8039 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8040 | ssl->session_in = NULL; |
| 8041 | ssl->session_out = NULL; |
| 8042 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8043 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8044 | |
| 8045 | #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] | 8046 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8047 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 8048 | { |
| 8049 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8050 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8051 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8053 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8054 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8055 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8056 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 8057 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8058 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8059 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 8060 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8061 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8062 | } |
| 8063 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8064 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8065 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8066 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8067 | mbedtls_ssl_transform_free( ssl->transform ); |
| 8068 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8069 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8070 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8071 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8072 | if( ssl->session ) |
| 8073 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8074 | mbedtls_ssl_session_free( ssl->session ); |
| 8075 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8076 | ssl->session = NULL; |
| 8077 | } |
| 8078 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8079 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8080 | ssl->alpn_chosen = NULL; |
| 8081 | #endif |
| 8082 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8083 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8084 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8085 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8086 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8087 | { |
| 8088 | mbedtls_free( ssl->cli_id ); |
| 8089 | ssl->cli_id = NULL; |
| 8090 | ssl->cli_id_len = 0; |
| 8091 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8092 | #endif |
| 8093 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8094 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 8095 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8096 | |
| 8097 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 8100 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8101 | * Reset an initialized and used SSL context for re-use while retaining |
| 8102 | * all application-set variables, function pointers and data. |
| 8103 | */ |
| 8104 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 8105 | { |
| 8106 | return( ssl_session_reset_int( ssl, 0 ) ); |
| 8107 | } |
| 8108 | |
| 8109 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8110 | * SSL set accessors |
| 8111 | */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8112 | #if !defined(MBEDTLS_SSL_CONF_ENDPOINT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8113 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8114 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8115 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8116 | } |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8117 | #endif /* MBEDTLS_SSL_CONF_ENDPOINT */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8118 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8119 | 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] | 8120 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8121 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8122 | } |
| 8123 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8124 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \ |
| 8125 | !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8126 | 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] | 8127 | { |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8128 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8129 | } |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 8130 | #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] | 8131 | |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 8132 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \ |
| 8133 | !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT) |
| 8134 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, |
| 8135 | unsigned limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8136 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8137 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8138 | } |
Hanno Becker | de67154 | 2019-06-12 16:30:46 +0100 | [diff] [blame] | 8139 | #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] | 8140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8141 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8142 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 8143 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 8144 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8145 | { |
| 8146 | ssl->disable_datagram_packing = !allow_packing; |
| 8147 | } |
| 8148 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8149 | #if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \ |
| 8150 | defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8151 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8152 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8153 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8154 | conf->hs_timeout_min = min; |
| 8155 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8156 | } |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8157 | #else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN && |
| 8158 | MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */ |
| 8159 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8160 | uint32_t min, uint32_t max ) |
| 8161 | { |
| 8162 | ((void) conf); |
| 8163 | ((void) min); |
| 8164 | ((void) max); |
| 8165 | } |
| 8166 | #endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN && |
| 8167 | MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */ |
| 8168 | |
| 8169 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8170 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8171 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8172 | { |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 8173 | #if !defined(MBEDTLS_SSL_CONF_AUTHMODE) |
| 8174 | conf->authmode = authmode; |
| 8175 | #else |
| 8176 | ((void) conf); |
| 8177 | ((void) authmode); |
| 8178 | #endif /* MBEDTLS_SSL_CONF_AUTHMODE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8179 | } |
| 8180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8181 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8182 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8183 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8184 | void *p_vrfy ) |
| 8185 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8186 | conf->f_vrfy = f_vrfy; |
| 8187 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8188 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8189 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8190 | |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame^] | 8191 | #if !defined(MBEDTLS_SSL_CONF_RNG) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8192 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 8193 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8194 | void *p_rng ) |
| 8195 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 8196 | conf->f_rng = f_rng; |
| 8197 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8198 | } |
Hanno Becker | ece325c | 2019-06-13 15:39:27 +0100 | [diff] [blame^] | 8199 | #else |
| 8200 | void mbedtls_ssl_conf_rng_ctx( mbedtls_ssl_config *conf, |
| 8201 | void *p_rng ) |
| 8202 | { |
| 8203 | conf->p_rng = p_rng; |
| 8204 | } |
| 8205 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8206 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8207 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 8208 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8209 | void *p_dbg ) |
| 8210 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8211 | conf->f_dbg = f_dbg; |
| 8212 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8213 | } |
| 8214 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8215 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8216 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8217 | mbedtls_ssl_send_t *f_send, |
| 8218 | mbedtls_ssl_recv_t *f_recv, |
| 8219 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8220 | { |
| 8221 | ssl->p_bio = p_bio; |
| 8222 | ssl->f_send = f_send; |
| 8223 | ssl->f_recv = f_recv; |
| 8224 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8225 | } |
| 8226 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 8227 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8228 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 8229 | { |
| 8230 | ssl->mtu = mtu; |
| 8231 | } |
| 8232 | #endif |
| 8233 | |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8234 | #if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8235 | 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] | 8236 | { |
| 8237 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8238 | } |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 8239 | #endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */ |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8240 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8241 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 8242 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8243 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 8244 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8245 | { |
| 8246 | ssl->p_timer = p_timer; |
| 8247 | ssl->f_set_timer = f_set_timer; |
| 8248 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8249 | |
| 8250 | /* Make sure we start with no timer running */ |
| 8251 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8252 | } |
| 8253 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8254 | #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] | 8255 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8256 | void *p_cache, |
| 8257 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 8258 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8259 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8260 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8261 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8262 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8263 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8264 | #endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8265 | |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8266 | #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] | 8267 | 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] | 8268 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8269 | int ret; |
| 8270 | |
| 8271 | if( ssl == NULL || |
| 8272 | session == NULL || |
| 8273 | ssl->session_negotiate == NULL || |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8274 | 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] | 8275 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8276 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8277 | } |
| 8278 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 8279 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 8280 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8281 | return( ret ); |
| 8282 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8283 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8284 | |
| 8285 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8286 | } |
Jarno Lamsa | 29f2dd0 | 2019-06-20 15:31:52 +0300 | [diff] [blame] | 8287 | #endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8288 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8289 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8290 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8291 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8292 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 8293 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 8294 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 8295 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8296 | } |
| 8297 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8298 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8299 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8300 | int major, int minor ) |
| 8301 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8302 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8303 | return; |
| 8304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8305 | if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8306 | return; |
| 8307 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8308 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8309 | } |
| 8310 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8311 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8312 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 8313 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8314 | { |
| 8315 | conf->cert_profile = profile; |
| 8316 | } |
| 8317 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8318 | /* Append a new keycert entry to a (possibly empty) list */ |
| 8319 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 8320 | mbedtls_x509_crt *cert, |
| 8321 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8322 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8323 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8324 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8325 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 8326 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8327 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8328 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8329 | new_cert->cert = cert; |
| 8330 | new_cert->key = key; |
| 8331 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8332 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8333 | /* Update head is the list was null, else add to the end */ |
| 8334 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8335 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8336 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8337 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8338 | else |
| 8339 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8340 | mbedtls_ssl_key_cert *cur = *head; |
| 8341 | while( cur->next != NULL ) |
| 8342 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8343 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8344 | } |
| 8345 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8346 | return( 0 ); |
| 8347 | } |
| 8348 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8349 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8350 | mbedtls_x509_crt *own_cert, |
| 8351 | mbedtls_pk_context *pk_key ) |
| 8352 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 8353 | 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] | 8354 | } |
| 8355 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8356 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8357 | mbedtls_x509_crt *ca_chain, |
| 8358 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8359 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8360 | conf->ca_chain = ca_chain; |
| 8361 | conf->ca_crl = ca_crl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8362 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8363 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 8364 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8365 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8366 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 8367 | mbedtls_x509_crt *own_cert, |
| 8368 | mbedtls_pk_context *pk_key ) |
| 8369 | { |
| 8370 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 8371 | own_cert, pk_key ) ); |
| 8372 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 8373 | |
| 8374 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 8375 | mbedtls_x509_crt *ca_chain, |
| 8376 | mbedtls_x509_crl *ca_crl ) |
| 8377 | { |
| 8378 | ssl->handshake->sni_ca_chain = ca_chain; |
| 8379 | ssl->handshake->sni_ca_crl = ca_crl; |
| 8380 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8381 | |
| 8382 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 8383 | int authmode ) |
| 8384 | { |
| 8385 | ssl->handshake->sni_authmode = authmode; |
| 8386 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8387 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8388 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8389 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8390 | /* |
| 8391 | * Set EC J-PAKE password for current handshake |
| 8392 | */ |
| 8393 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 8394 | const unsigned char *pw, |
| 8395 | size_t pw_len ) |
| 8396 | { |
| 8397 | mbedtls_ecjpake_role role; |
| 8398 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 8399 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8400 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8401 | |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 8402 | 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] | 8403 | role = MBEDTLS_ECJPAKE_SERVER; |
| 8404 | else |
| 8405 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 8406 | |
| 8407 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 8408 | role, |
| 8409 | MBEDTLS_MD_SHA256, |
| 8410 | MBEDTLS_ECP_DP_SECP256R1, |
| 8411 | pw, pw_len ) ); |
| 8412 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8413 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8415 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8416 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8417 | const unsigned char *psk, size_t psk_len, |
| 8418 | const unsigned char *psk_identity, size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8419 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8420 | if( psk == NULL || psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8421 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8423 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8424 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 8425 | |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8426 | /* Identity len will be encoded on two bytes */ |
| 8427 | if( ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8428 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8429 | { |
| 8430 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8431 | } |
| 8432 | |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8433 | if( conf->psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8434 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8435 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8436 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8437 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 8438 | conf->psk = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8439 | conf->psk_len = 0; |
| 8440 | } |
| 8441 | if( conf->psk_identity != NULL ) |
| 8442 | { |
| 8443 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 8444 | conf->psk_identity = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8445 | conf->psk_identity_len = 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8446 | } |
| 8447 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8448 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL || |
| 8449 | ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL ) |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 8450 | { |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8451 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 8452 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8453 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 8454 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8455 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 8456 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8457 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8458 | conf->psk_len = psk_len; |
| 8459 | conf->psk_identity_len = psk_identity_len; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8460 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8461 | memcpy( conf->psk, psk, conf->psk_len ); |
| 8462 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8463 | |
| 8464 | return( 0 ); |
| 8465 | } |
| 8466 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8467 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 8468 | const unsigned char *psk, size_t psk_len ) |
| 8469 | { |
| 8470 | if( psk == NULL || ssl->handshake == NULL ) |
| 8471 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8472 | |
| 8473 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8474 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8475 | |
| 8476 | if( ssl->handshake->psk != NULL ) |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 8477 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8478 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 8479 | ssl->handshake->psk_len ); |
Simon Butcher | 5b8d1d6 | 2015-10-04 22:06:51 +0100 | [diff] [blame] | 8480 | mbedtls_free( ssl->handshake->psk ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8481 | ssl->handshake->psk_len = 0; |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 8482 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8483 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8484 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8485 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8486 | |
| 8487 | ssl->handshake->psk_len = psk_len; |
| 8488 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 8489 | |
| 8490 | return( 0 ); |
| 8491 | } |
| 8492 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8493 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8494 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8495 | size_t), |
| 8496 | void *p_psk ) |
| 8497 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8498 | conf->f_psk = f_psk; |
| 8499 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8500 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8501 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 8502 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8503 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8504 | |
| 8505 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8506 | 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] | 8507 | { |
| 8508 | int ret; |
| 8509 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8510 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 8511 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 8512 | { |
| 8513 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8514 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8515 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8516 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8517 | |
| 8518 | return( 0 ); |
| 8519 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8520 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8521 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 8522 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 8523 | const unsigned char *dhm_P, size_t P_len, |
| 8524 | const unsigned char *dhm_G, size_t G_len ) |
| 8525 | { |
| 8526 | int ret; |
| 8527 | |
| 8528 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 8529 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 8530 | { |
| 8531 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8532 | mbedtls_mpi_free( &conf->dhm_G ); |
| 8533 | return( ret ); |
| 8534 | } |
| 8535 | |
| 8536 | return( 0 ); |
| 8537 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8538 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8539 | 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] | 8540 | { |
| 8541 | int ret; |
| 8542 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8543 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 8544 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 8545 | { |
| 8546 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8547 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8548 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8549 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8550 | |
| 8551 | return( 0 ); |
| 8552 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8553 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8554 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 8555 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 8556 | /* |
| 8557 | * Set the minimum length for Diffie-Hellman parameters |
| 8558 | */ |
| 8559 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 8560 | unsigned int bitlen ) |
| 8561 | { |
| 8562 | conf->dhm_min_bitlen = bitlen; |
| 8563 | } |
| 8564 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 8565 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 8566 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8567 | /* |
| 8568 | * Set allowed/preferred hashes for handshake signatures |
| 8569 | */ |
| 8570 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 8571 | const int *hashes ) |
| 8572 | { |
| 8573 | conf->sig_hashes = hashes; |
| 8574 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8575 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8576 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 8577 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8578 | /* |
| 8579 | * Set the allowed elliptic curves |
| 8580 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8581 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8582 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8583 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8584 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8585 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8586 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8587 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8588 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8589 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8590 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8591 | /* Initialize to suppress unnecessary compiler warning */ |
| 8592 | size_t hostname_len = 0; |
| 8593 | |
| 8594 | /* Check if new hostname is valid before |
| 8595 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8596 | if( hostname != NULL ) |
| 8597 | { |
| 8598 | hostname_len = strlen( hostname ); |
| 8599 | |
| 8600 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 8601 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8602 | } |
| 8603 | |
| 8604 | /* Now it's clear that we will overwrite the old hostname, |
| 8605 | * so we can free it safely */ |
| 8606 | |
| 8607 | if( ssl->hostname != NULL ) |
| 8608 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8609 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8610 | mbedtls_free( ssl->hostname ); |
| 8611 | } |
| 8612 | |
| 8613 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 8614 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8615 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8616 | { |
| 8617 | ssl->hostname = NULL; |
| 8618 | } |
| 8619 | else |
| 8620 | { |
| 8621 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8622 | if( ssl->hostname == NULL ) |
| 8623 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 8624 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8625 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 8626 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8627 | ssl->hostname[hostname_len] = '\0'; |
| 8628 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8629 | |
| 8630 | return( 0 ); |
| 8631 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 8632 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8633 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8634 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8635 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8636 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8637 | const unsigned char *, size_t), |
| 8638 | void *p_sni ) |
| 8639 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8640 | conf->f_sni = f_sni; |
| 8641 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8642 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8643 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8645 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8646 | 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] | 8647 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 8648 | size_t cur_len, tot_len; |
| 8649 | const char **p; |
| 8650 | |
| 8651 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 8652 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 8653 | * MUST NOT be truncated." |
| 8654 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 8655 | */ |
| 8656 | tot_len = 0; |
| 8657 | for( p = protos; *p != NULL; p++ ) |
| 8658 | { |
| 8659 | cur_len = strlen( *p ); |
| 8660 | tot_len += cur_len; |
| 8661 | |
| 8662 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8663 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 8664 | } |
| 8665 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8666 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 8667 | |
| 8668 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8669 | } |
| 8670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8671 | 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] | 8672 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8673 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8674 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8675 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8676 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8677 | void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor ) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 8678 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8679 | conf->max_major_ver = major; |
| 8680 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 8681 | } |
| 8682 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8683 | void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 8684 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8685 | conf->min_major_ver = major; |
| 8686 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 8687 | } |
| 8688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8689 | #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] | 8690 | 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] | 8691 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 8692 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 8693 | } |
| 8694 | #endif |
| 8695 | |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 8696 | #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] | 8697 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 8698 | char cert_req_ca_list ) |
| 8699 | { |
| 8700 | conf->cert_req_ca_list = cert_req_ca_list; |
| 8701 | } |
| 8702 | #endif |
| 8703 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8704 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8705 | 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] | 8706 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8707 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 8708 | } |
| 8709 | #endif |
| 8710 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8711 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 8712 | #if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8713 | 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] | 8714 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8715 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 8716 | } |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 8717 | #endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */ |
| 8718 | #if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET) |
Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 8719 | void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf, |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 8720 | char ems_enf ) |
Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 8721 | { |
| 8722 | conf->enforce_extended_master_secret = ems_enf; |
| 8723 | } |
Hanno Becker | f765ce6 | 2019-06-21 13:17:14 +0100 | [diff] [blame] | 8724 | #endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 8725 | #endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 8726 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 8727 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8728 | 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] | 8729 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8730 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 8731 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 8732 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 8733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8734 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8735 | 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] | 8736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8737 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8738 | 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] | 8739 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8740 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 8741 | } |
| 8742 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 8743 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 8744 | |
| 8745 | return( 0 ); |
| 8746 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8747 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 8748 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8749 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8750 | 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] | 8751 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8752 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 8753 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8754 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 8755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8756 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8757 | 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] | 8758 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 8759 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8760 | } |
| 8761 | #endif |
| 8762 | |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 8763 | #if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8764 | 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] | 8765 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8766 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8767 | } |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 8768 | #endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8769 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8770 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8771 | 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] | 8772 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8773 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8774 | } |
| 8775 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8776 | 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] | 8777 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8778 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 8779 | } |
| 8780 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8781 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 8782 | const unsigned char period[8] ) |
| 8783 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8784 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 8785 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8786 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8787 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8788 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8789 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8790 | 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] | 8791 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 8792 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 8793 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8794 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 8795 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8796 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 8797 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 8798 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 8799 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 8800 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 8801 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 8802 | conf->f_ticket_write = f_ticket_write; |
| 8803 | conf->f_ticket_parse = f_ticket_parse; |
| 8804 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 8805 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8806 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8807 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 8808 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 8809 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 8810 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 8811 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 8812 | void *p_export_keys ) |
| 8813 | { |
| 8814 | conf->f_export_keys = f_export_keys; |
| 8815 | conf->p_export_keys = p_export_keys; |
| 8816 | } |
| 8817 | #endif |
| 8818 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 8819 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 8820 | void mbedtls_ssl_conf_async_private_cb( |
| 8821 | mbedtls_ssl_config *conf, |
| 8822 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 8823 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 8824 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 8825 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8826 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 8827 | { |
| 8828 | conf->f_async_sign_start = f_async_sign; |
| 8829 | conf->f_async_decrypt_start = f_async_decrypt; |
| 8830 | conf->f_async_resume = f_async_resume; |
| 8831 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8832 | conf->p_async_config_data = async_config_data; |
| 8833 | } |
| 8834 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 8835 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 8836 | { |
| 8837 | return( conf->p_async_config_data ); |
| 8838 | } |
| 8839 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 8840 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8841 | { |
| 8842 | if( ssl->handshake == NULL ) |
| 8843 | return( NULL ); |
| 8844 | else |
| 8845 | return( ssl->handshake->user_async_ctx ); |
| 8846 | } |
| 8847 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 8848 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8849 | void *ctx ) |
| 8850 | { |
| 8851 | if( ssl->handshake != NULL ) |
| 8852 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 8853 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 8854 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 8855 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8856 | /* |
| 8857 | * SSL get accessors |
| 8858 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8859 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8860 | { |
| 8861 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 8862 | } |
| 8863 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 8864 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 8865 | { |
| 8866 | /* |
| 8867 | * Case A: We're currently holding back |
| 8868 | * a message for further processing. |
| 8869 | */ |
| 8870 | |
| 8871 | if( ssl->keep_current_message == 1 ) |
| 8872 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 8873 | 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] | 8874 | return( 1 ); |
| 8875 | } |
| 8876 | |
| 8877 | /* |
| 8878 | * Case B: Further records are pending in the current datagram. |
| 8879 | */ |
| 8880 | |
| 8881 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8882 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 8883 | ssl->in_left > ssl->next_record_offset ) |
| 8884 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 8885 | 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] | 8886 | return( 1 ); |
| 8887 | } |
| 8888 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8889 | |
| 8890 | /* |
| 8891 | * Case C: A handshake message is being processed. |
| 8892 | */ |
| 8893 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 8894 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 8895 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 8896 | 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] | 8897 | return( 1 ); |
| 8898 | } |
| 8899 | |
| 8900 | /* |
| 8901 | * Case D: An application data message is being processed |
| 8902 | */ |
| 8903 | if( ssl->in_offt != NULL ) |
| 8904 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 8905 | 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] | 8906 | return( 1 ); |
| 8907 | } |
| 8908 | |
| 8909 | /* |
| 8910 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 8911 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 8912 | * we implement support for multiple alerts in single records. |
| 8913 | */ |
| 8914 | |
| 8915 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 8916 | return( 0 ); |
| 8917 | } |
| 8918 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8919 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8920 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 8921 | if( ssl->session != NULL ) |
| 8922 | return( ssl->session->verify_result ); |
| 8923 | |
| 8924 | if( ssl->session_negotiate != NULL ) |
| 8925 | return( ssl->session_negotiate->verify_result ); |
| 8926 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 8927 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8928 | } |
| 8929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8930 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 8931 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 8932 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8933 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 8934 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8935 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 8936 | } |
| 8937 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8938 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8939 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8940 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8941 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 8942 | { |
| 8943 | switch( ssl->minor_ver ) |
| 8944 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8945 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 8946 | return( "DTLSv1.0" ); |
| 8947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8948 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 8949 | return( "DTLSv1.2" ); |
| 8950 | |
| 8951 | default: |
| 8952 | return( "unknown (DTLS)" ); |
| 8953 | } |
| 8954 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8955 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 8956 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8957 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8958 | { |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8959 | switch( ssl->minor_ver ) |
| 8960 | { |
| 8961 | case MBEDTLS_SSL_MINOR_VERSION_0: |
| 8962 | return( "SSLv3.0" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8963 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8964 | case MBEDTLS_SSL_MINOR_VERSION_1: |
| 8965 | return( "TLSv1.0" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8966 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8967 | case MBEDTLS_SSL_MINOR_VERSION_2: |
| 8968 | return( "TLSv1.1" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8969 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8970 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 8971 | return( "TLSv1.2" ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8972 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8973 | default: |
| 8974 | return( "unknown" ); |
| 8975 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8976 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8977 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8978 | } |
| 8979 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8980 | 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] | 8981 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 8982 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8983 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8984 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8985 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 8986 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 8987 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8988 | if( transform == NULL ) |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 8989 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8991 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 8992 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 8993 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8994 | #endif |
| 8995 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8996 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8997 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8998 | case MBEDTLS_MODE_GCM: |
| 8999 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9000 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9001 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9002 | transform_expansion = transform->minlen; |
| 9003 | break; |
| 9004 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9005 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9006 | |
| 9007 | block_size = mbedtls_cipher_get_block_size( |
| 9008 | &transform->cipher_ctx_enc ); |
| 9009 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9010 | /* Expansion due to the addition of the MAC. */ |
| 9011 | transform_expansion += transform->maclen; |
| 9012 | |
| 9013 | /* Expansion due to the addition of CBC padding; |
| 9014 | * Theoretically up to 256 bytes, but we never use |
| 9015 | * more than the block size of the underlying cipher. */ |
| 9016 | transform_expansion += block_size; |
| 9017 | |
| 9018 | /* For TLS 1.1 or higher, an explicit IV is added |
| 9019 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9020 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9021 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9022 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9023 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9024 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9025 | break; |
| 9026 | |
| 9027 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9029 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9030 | } |
| 9031 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9032 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9033 | if( transform->out_cid_len != 0 ) |
| 9034 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9035 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9036 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9037 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9038 | } |
| 9039 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9040 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9041 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 9042 | { |
| 9043 | size_t max_len; |
| 9044 | |
| 9045 | /* |
| 9046 | * Assume mfl_code is correct since it was checked when set |
| 9047 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9048 | 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] | 9049 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9050 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9051 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9052 | 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] | 9053 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9054 | 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] | 9055 | } |
| 9056 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9057 | /* During a handshake, use the value being negotiated */ |
| 9058 | if( ssl->session_negotiate != NULL && |
| 9059 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 9060 | { |
| 9061 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 9062 | } |
| 9063 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9064 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9065 | } |
| 9066 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9067 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9068 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9069 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 9070 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9071 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9072 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT && |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9073 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 9074 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 9075 | return ( 0 ); |
| 9076 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9077 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 9078 | return( ssl->mtu ); |
| 9079 | |
| 9080 | if( ssl->mtu == 0 ) |
| 9081 | return( ssl->handshake->mtu ); |
| 9082 | |
| 9083 | return( ssl->mtu < ssl->handshake->mtu ? |
| 9084 | ssl->mtu : ssl->handshake->mtu ); |
| 9085 | } |
| 9086 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9087 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9088 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 9089 | { |
| 9090 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 9091 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 9092 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9093 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9094 | (void) ssl; |
| 9095 | #endif |
| 9096 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9097 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9098 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 9099 | |
| 9100 | if( max_len > mfl ) |
| 9101 | max_len = mfl; |
| 9102 | #endif |
| 9103 | |
| 9104 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9105 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9106 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9107 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9108 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 9109 | const size_t overhead = (size_t) ret; |
| 9110 | |
| 9111 | if( ret < 0 ) |
| 9112 | return( ret ); |
| 9113 | |
| 9114 | if( mtu <= overhead ) |
| 9115 | { |
| 9116 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 9117 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 9118 | } |
| 9119 | |
| 9120 | if( max_len > mtu - overhead ) |
| 9121 | max_len = mtu - overhead; |
| 9122 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9123 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9124 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 9125 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9126 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9127 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9128 | #endif |
| 9129 | |
| 9130 | return( (int) max_len ); |
| 9131 | } |
| 9132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9133 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9134 | 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] | 9135 | { |
| 9136 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9137 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9138 | |
Hanno Becker | bfab9df | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9139 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9140 | return( ssl->session->peer_cert ); |
Hanno Becker | bfab9df | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9141 | #else |
| 9142 | return( NULL ); |
| 9143 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9144 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9145 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9147 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 933b9fc | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 9148 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 9149 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9150 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9151 | if( ssl == NULL || |
| 9152 | dst == NULL || |
| 9153 | ssl->session == NULL || |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9154 | 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] | 9155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9156 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9157 | } |
| 9158 | |
Hanno Becker | 58fccf2 | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9159 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9160 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9161 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9162 | |
Manuel Pégourié-Gonnard | 37a5324 | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 9163 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) |
| 9164 | { |
| 9165 | if( ssl == NULL ) |
| 9166 | return( NULL ); |
| 9167 | |
| 9168 | return( ssl->session ); |
| 9169 | } |
| 9170 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9171 | /* |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9172 | * Define ticket header determining Mbed TLS version |
| 9173 | * and structure of the ticket. |
| 9174 | */ |
| 9175 | |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9176 | /* |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9177 | * Define bitflag determining compile-time settings influencing |
| 9178 | * structure of serialized SSL sessions. |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9179 | */ |
| 9180 | |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9181 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9182 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9183 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9184 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9185 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9186 | |
| 9187 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9188 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9189 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9190 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9191 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9192 | |
| 9193 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9194 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9195 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9196 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9197 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 9198 | |
| 9199 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9200 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9201 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9202 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9203 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9204 | |
| 9205 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9206 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9207 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9208 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9209 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 9210 | |
| 9211 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9212 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9213 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9214 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9215 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 9216 | |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9217 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 9218 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 9219 | #else |
| 9220 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 9221 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 9222 | |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9223 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 9224 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1 |
| 9225 | #else |
| 9226 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0 |
| 9227 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 9228 | |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9229 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 9230 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 9231 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 9232 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 9233 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4 |
| 9234 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5 |
| 9235 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6 |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9236 | #define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7 |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9237 | |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9238 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 9239 | ( (uint16_t) ( \ |
| 9240 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 9241 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 9242 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 9243 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
| 9244 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \ |
| 9245 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9246 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \ |
| 9247 | ( 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] | 9248 | |
Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9249 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 9250 | MBEDTLS_VERSION_MAJOR, |
| 9251 | MBEDTLS_VERSION_MINOR, |
| 9252 | MBEDTLS_VERSION_PATCH, |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9253 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 9254 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 9255 | }; |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9256 | |
| 9257 | /* |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9258 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9259 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 9260 | * |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9261 | * opaque mbedtls_version[3]; // major, minor, patch |
| 9262 | * opaque session_format[2]; // version-specific 16-bit field determining |
| 9263 | * // the format of the remaining |
| 9264 | * // serialized data. |
Hanno Becker | b36db4f | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 9265 | * |
| 9266 | * Note: When updating the format, remember to keep |
| 9267 | * these version+format bytes. |
| 9268 | * |
Hanno Becker | 7bf7710 | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 9269 | * // In this version, `session_format` determines |
| 9270 | * // the setting of those compile-time |
| 9271 | * // configuration options which influence |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9272 | * // the structure of mbedtls_ssl_session. |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9273 | * uint64 start_time; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9274 | * uint8 ciphersuite[2]; // defined by the standard |
| 9275 | * uint8 compression; // 0 or 1 |
| 9276 | * uint8 session_id_len; // at most 32 |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9277 | * opaque session_id[32]; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9278 | * opaque master[48]; // fixed length in the standard |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9279 | * uint32 verify_result; |
Hanno Becker | 0528f82 | 2019-06-18 12:45:31 +0100 | [diff] [blame] | 9280 | * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) { |
| 9281 | * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert |
| 9282 | * case disabled: uint8_t peer_cert_digest_type; |
| 9283 | * opaque peer_cert_digest<0..2^8-1>; |
| 9284 | * } |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9285 | * 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] | 9286 | * uint32 ticket_lifetime; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 9287 | * uint8 mfl_code; // up to 255 according to standard |
| 9288 | * uint8 trunc_hmac; // 0 or 1 |
| 9289 | * uint8 encrypt_then_mac; // 0 or 1 |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9290 | * |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9291 | * The order is the same as in the definition of the structure, except |
| 9292 | * verify_result is put before peer_cert so that all mandatory fields come |
| 9293 | * together in one block. |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9294 | */ |
| 9295 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 9296 | unsigned char *buf, |
| 9297 | size_t buf_len, |
| 9298 | size_t *olen ) |
| 9299 | { |
| 9300 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9301 | size_t used = 0; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9302 | #if defined(MBEDTLS_HAVE_TIME) |
| 9303 | uint64_t start; |
| 9304 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9305 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9306 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9307 | size_t cert_len; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9308 | #endif |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9309 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9310 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9311 | /* |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9312 | * Add version identifier |
| 9313 | */ |
| 9314 | |
| 9315 | used += sizeof( ssl_serialized_session_header ); |
| 9316 | |
| 9317 | if( used <= buf_len ) |
| 9318 | { |
| 9319 | memcpy( p, ssl_serialized_session_header, |
| 9320 | sizeof( ssl_serialized_session_header ) ); |
| 9321 | p += sizeof( ssl_serialized_session_header ); |
| 9322 | } |
| 9323 | |
| 9324 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9325 | * Time |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9326 | */ |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9327 | #if defined(MBEDTLS_HAVE_TIME) |
| 9328 | used += 8; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9329 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9330 | if( used <= buf_len ) |
| 9331 | { |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9332 | start = (uint64_t) session->start; |
| 9333 | |
| 9334 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); |
| 9335 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); |
| 9336 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); |
| 9337 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); |
| 9338 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); |
| 9339 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); |
| 9340 | *p++ = (unsigned char)( ( start >> 8 ) & 0xFF ); |
| 9341 | *p++ = (unsigned char)( ( start ) & 0xFF ); |
| 9342 | } |
| 9343 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9344 | |
| 9345 | /* |
| 9346 | * Basic mandatory fields |
| 9347 | */ |
| 9348 | used += 2 /* ciphersuite */ |
| 9349 | + 1 /* compression */ |
| 9350 | + 1 /* id_len */ |
| 9351 | + sizeof( session->id ) |
| 9352 | + sizeof( session->master ) |
| 9353 | + 4; /* verify_result */ |
| 9354 | |
| 9355 | if( used <= buf_len ) |
| 9356 | { |
| 9357 | *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF ); |
| 9358 | *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF ); |
| 9359 | |
| 9360 | *p++ = (unsigned char)( session->compression & 0xFF ); |
| 9361 | |
| 9362 | *p++ = (unsigned char)( session->id_len & 0xFF ); |
| 9363 | memcpy( p, session->id, 32 ); |
| 9364 | p += 32; |
| 9365 | |
| 9366 | memcpy( p, session->master, 48 ); |
| 9367 | p += 48; |
| 9368 | |
| 9369 | *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); |
| 9370 | *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); |
| 9371 | *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF ); |
| 9372 | *p++ = (unsigned char)( ( session->verify_result ) & 0xFF ); |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9373 | } |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9374 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9375 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9376 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9377 | */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9378 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9379 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9380 | if( session->peer_cert == NULL ) |
| 9381 | cert_len = 0; |
| 9382 | else |
| 9383 | cert_len = session->peer_cert->raw.len; |
| 9384 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9385 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9386 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9387 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9388 | { |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9389 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 9390 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 9391 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
| 9392 | |
| 9393 | if( session->peer_cert != NULL ) |
| 9394 | { |
| 9395 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 9396 | p += cert_len; |
| 9397 | } |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9398 | } |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9399 | |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9400 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9401 | /* Digest of peer certificate */ |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9402 | if( session->peer_cert_digest != NULL ) |
| 9403 | { |
| 9404 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 9405 | if( used <= buf_len ) |
| 9406 | { |
| 9407 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 9408 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 9409 | memcpy( p, session->peer_cert_digest, |
| 9410 | session->peer_cert_digest_len ); |
| 9411 | p += session->peer_cert_digest_len; |
| 9412 | } |
| 9413 | } |
| 9414 | else |
| 9415 | { |
| 9416 | used += 2; |
| 9417 | if( used <= buf_len ) |
| 9418 | { |
| 9419 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 9420 | *p++ = 0; |
| 9421 | } |
| 9422 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9423 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9424 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9425 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9426 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9427 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9428 | */ |
| 9429 | #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] | 9430 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9431 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9432 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9433 | { |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9434 | *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); |
| 9435 | *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF ); |
| 9436 | *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF ); |
| 9437 | |
| 9438 | if( session->ticket != NULL ) |
| 9439 | { |
| 9440 | memcpy( p, session->ticket, session->ticket_len ); |
| 9441 | p += session->ticket_len; |
| 9442 | } |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9443 | |
| 9444 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); |
| 9445 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); |
| 9446 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF ); |
| 9447 | *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF ); |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9448 | } |
| 9449 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9450 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9451 | /* |
| 9452 | * Misc extension-related info |
| 9453 | */ |
| 9454 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9455 | used += 1; |
| 9456 | |
| 9457 | if( used <= buf_len ) |
| 9458 | *p++ = session->mfl_code; |
| 9459 | #endif |
| 9460 | |
| 9461 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 9462 | used += 1; |
| 9463 | |
| 9464 | if( used <= buf_len ) |
| 9465 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); |
| 9466 | #endif |
| 9467 | |
| 9468 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9469 | used += 1; |
| 9470 | |
| 9471 | if( used <= buf_len ) |
| 9472 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); |
| 9473 | #endif |
| 9474 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9475 | /* Done */ |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9476 | *olen = used; |
| 9477 | |
| 9478 | if( used > buf_len ) |
| 9479 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9480 | |
| 9481 | return( 0 ); |
| 9482 | } |
| 9483 | |
| 9484 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9485 | * Unserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9486 | * |
| 9487 | * This internal version is wrapped by a public function that cleans up in |
| 9488 | * case of error. |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9489 | */ |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9490 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 9491 | const unsigned char *buf, |
| 9492 | size_t len ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9493 | { |
| 9494 | const unsigned char *p = buf; |
| 9495 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9496 | #if defined(MBEDTLS_HAVE_TIME) |
| 9497 | uint64_t start; |
| 9498 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9499 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9500 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9501 | size_t cert_len; |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9502 | #endif |
| 9503 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9504 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9505 | /* |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9506 | * Check version identifier |
| 9507 | */ |
| 9508 | |
| 9509 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
Hanno Becker | 1d8b6d7 | 2019-05-28 13:59:44 +0100 | [diff] [blame] | 9510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9511 | |
| 9512 | if( memcmp( p, ssl_serialized_session_header, |
| 9513 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 9514 | { |
Hanno Becker | 5dbcc9f | 2019-06-03 12:58:39 +0100 | [diff] [blame] | 9515 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9516 | } |
| 9517 | p += sizeof( ssl_serialized_session_header ); |
| 9518 | |
| 9519 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9520 | * Time |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9521 | */ |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9522 | #if defined(MBEDTLS_HAVE_TIME) |
| 9523 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9524 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9525 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9526 | start = ( (uint64_t) p[0] << 56 ) | |
| 9527 | ( (uint64_t) p[1] << 48 ) | |
| 9528 | ( (uint64_t) p[2] << 40 ) | |
| 9529 | ( (uint64_t) p[3] << 32 ) | |
| 9530 | ( (uint64_t) p[4] << 24 ) | |
| 9531 | ( (uint64_t) p[5] << 16 ) | |
| 9532 | ( (uint64_t) p[6] << 8 ) | |
| 9533 | ( (uint64_t) p[7] ); |
| 9534 | p += 8; |
| 9535 | |
| 9536 | session->start = (time_t) start; |
| 9537 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9538 | |
| 9539 | /* |
| 9540 | * Basic mandatory fields |
| 9541 | */ |
| 9542 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 9543 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9544 | |
| 9545 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 9546 | p += 2; |
| 9547 | |
| 9548 | session->compression = *p++; |
| 9549 | |
| 9550 | session->id_len = *p++; |
| 9551 | memcpy( session->id, p, 32 ); |
| 9552 | p += 32; |
| 9553 | |
| 9554 | memcpy( session->master, p, 48 ); |
| 9555 | p += 48; |
| 9556 | |
| 9557 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 9558 | ( (uint32_t) p[1] << 16 ) | |
| 9559 | ( (uint32_t) p[2] << 8 ) | |
| 9560 | ( (uint32_t) p[3] ); |
| 9561 | p += 4; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9562 | |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9563 | /* Immediately clear invalid pointer values that have been read, in case |
| 9564 | * we exit early before we replaced them with valid ones. */ |
| 9565 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9566 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9567 | session->peer_cert = NULL; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9568 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9569 | session->peer_cert_digest = NULL; |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9570 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9571 | #endif |
| 9572 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9573 | session->ticket = NULL; |
| 9574 | #endif |
| 9575 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9576 | /* |
| 9577 | * Peer certificate |
| 9578 | */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9579 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2e6d347 | 2019-02-06 15:40:27 +0000 | [diff] [blame] | 9580 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9581 | if( 3 > (size_t)( end - p ) ) |
| 9582 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9583 | |
| 9584 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 9585 | p += 3; |
| 9586 | |
| 9587 | if( cert_len == 0 ) |
| 9588 | { |
| 9589 | session->peer_cert = NULL; |
| 9590 | } |
| 9591 | else |
| 9592 | { |
| 9593 | int ret; |
| 9594 | |
| 9595 | if( cert_len > (size_t)( end - p ) ) |
| 9596 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9597 | |
| 9598 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 9599 | |
| 9600 | if( session->peer_cert == NULL ) |
| 9601 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9602 | |
| 9603 | mbedtls_x509_crt_init( session->peer_cert ); |
| 9604 | |
| 9605 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 9606 | p, cert_len ) ) != 0 ) |
| 9607 | { |
| 9608 | mbedtls_x509_crt_free( session->peer_cert ); |
| 9609 | mbedtls_free( session->peer_cert ); |
| 9610 | session->peer_cert = NULL; |
| 9611 | return( ret ); |
| 9612 | } |
| 9613 | |
| 9614 | p += cert_len; |
| 9615 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9616 | #elif defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9617 | /* Deserialize CRT digest from the end of the ticket. */ |
| 9618 | if( 2 > (size_t)( end - p ) ) |
| 9619 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9620 | |
| 9621 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 9622 | session->peer_cert_digest_len = (size_t) *p++; |
| 9623 | |
| 9624 | if( session->peer_cert_digest_len != 0 ) |
| 9625 | { |
Hanno Becker | 2326d20 | 2019-06-06 14:54:55 +0100 | [diff] [blame] | 9626 | const mbedtls_md_info_t *md_info = |
| 9627 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 9628 | if( md_info == NULL ) |
| 9629 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9630 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 9631 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9632 | |
Hanno Becker | 4a2f8e5 | 2019-02-06 15:23:38 +0000 | [diff] [blame] | 9633 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 9634 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9635 | |
| 9636 | session->peer_cert_digest = |
| 9637 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 9638 | if( session->peer_cert_digest == NULL ) |
| 9639 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9640 | |
| 9641 | memcpy( session->peer_cert_digest, p, |
| 9642 | session->peer_cert_digest_len ); |
| 9643 | p += session->peer_cert_digest_len; |
| 9644 | } |
Hanno Becker | 5882dd0 | 2019-06-06 16:25:57 +0100 | [diff] [blame] | 9645 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9646 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9647 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9648 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9649 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9650 | */ |
| 9651 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9652 | if( 3 > (size_t)( end - p ) ) |
| 9653 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9654 | |
| 9655 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 9656 | p += 3; |
| 9657 | |
| 9658 | if( session->ticket_len != 0 ) |
| 9659 | { |
| 9660 | if( session->ticket_len > (size_t)( end - p ) ) |
| 9661 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9662 | |
| 9663 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 9664 | if( session->ticket == NULL ) |
| 9665 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9666 | |
| 9667 | memcpy( session->ticket, p, session->ticket_len ); |
| 9668 | p += session->ticket_len; |
| 9669 | } |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9670 | |
| 9671 | if( 4 > (size_t)( end - p ) ) |
| 9672 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9673 | |
| 9674 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 9675 | ( (uint32_t) p[1] << 16 ) | |
| 9676 | ( (uint32_t) p[2] << 8 ) | |
| 9677 | ( (uint32_t) p[3] ); |
| 9678 | p += 4; |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9679 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9680 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9681 | /* |
| 9682 | * Misc extension-related info |
| 9683 | */ |
| 9684 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9685 | if( 1 > (size_t)( end - p ) ) |
| 9686 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9687 | |
| 9688 | session->mfl_code = *p++; |
| 9689 | #endif |
| 9690 | |
| 9691 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 9692 | if( 1 > (size_t)( end - p ) ) |
| 9693 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9694 | |
| 9695 | session->trunc_hmac = *p++; |
| 9696 | #endif |
| 9697 | |
| 9698 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9699 | if( 1 > (size_t)( end - p ) ) |
| 9700 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9701 | |
| 9702 | session->encrypt_then_mac = *p++; |
| 9703 | #endif |
| 9704 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9705 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9706 | if( p != end ) |
| 9707 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9708 | |
| 9709 | return( 0 ); |
| 9710 | } |
| 9711 | |
| 9712 | /* |
Manuel Pégourié-Gonnard | 35ccdbb | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 9713 | * Unserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9714 | */ |
| 9715 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 9716 | const unsigned char *buf, |
| 9717 | size_t len ) |
| 9718 | { |
| 9719 | int ret = ssl_session_load( session, buf, len ); |
| 9720 | |
| 9721 | if( ret != 0 ) |
| 9722 | mbedtls_ssl_session_free( session ); |
| 9723 | |
| 9724 | return( ret ); |
| 9725 | } |
| 9726 | |
| 9727 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9728 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9729 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9730 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9731 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9732 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9733 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9734 | if( ssl == NULL || ssl->conf == NULL ) |
| 9735 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9737 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9738 | 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] | 9739 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9740 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9741 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9742 | 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] | 9743 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9744 | #endif |
| 9745 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9746 | return( ret ); |
| 9747 | } |
| 9748 | |
| 9749 | /* |
| 9750 | * Perform the SSL handshake |
| 9751 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9752 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9753 | { |
| 9754 | int ret = 0; |
| 9755 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9756 | if( ssl == NULL || ssl->conf == NULL ) |
| 9757 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9759 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9761 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9762 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9763 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9764 | |
| 9765 | if( ret != 0 ) |
| 9766 | break; |
| 9767 | } |
| 9768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9769 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9770 | |
| 9771 | return( ret ); |
| 9772 | } |
| 9773 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9774 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 9775 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9776 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9777 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9778 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9779 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9780 | { |
| 9781 | int ret; |
| 9782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9783 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9784 | |
| 9785 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9786 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 9787 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9788 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 9789 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9790 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 9791 | 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] | 9792 | return( ret ); |
| 9793 | } |
| 9794 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9795 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9796 | |
| 9797 | return( 0 ); |
| 9798 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9799 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9800 | |
| 9801 | /* |
| 9802 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9803 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 9804 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 9805 | * - 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] | 9806 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9807 | * 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] | 9808 | * 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] | 9809 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9810 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9811 | { |
| 9812 | int ret; |
| 9813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9814 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9815 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9816 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 9817 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9818 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9819 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 9820 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9821 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9822 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9823 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9824 | { |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9825 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 9826 | MBEDTLS_SSL_IS_SERVER ) |
| 9827 | { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 9828 | ssl->handshake->out_msg_seq = 1; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9829 | } |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 9830 | else |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9831 | { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 9832 | ssl->handshake->in_msg_seq = 1; |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9833 | } |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9834 | } |
| 9835 | #endif |
| 9836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9837 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 9838 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9840 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9841 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9842 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9843 | return( ret ); |
| 9844 | } |
| 9845 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9846 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9847 | |
| 9848 | return( 0 ); |
| 9849 | } |
| 9850 | |
| 9851 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9852 | * Renegotiate current connection on client, |
| 9853 | * or request renegotiation on server |
| 9854 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9855 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9856 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9857 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9858 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9859 | if( ssl == NULL || ssl->conf == NULL ) |
| 9860 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9861 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9862 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9863 | /* On server, just send the request */ |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 9864 | 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] | 9865 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9866 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 9867 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9868 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9869 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 9870 | |
| 9871 | /* Did we already try/start sending HelloRequest? */ |
| 9872 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9873 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 9874 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9875 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9876 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9877 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9879 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9880 | /* |
| 9881 | * On client, either start the renegotiation process or, |
| 9882 | * if already in progress, continue the handshake |
| 9883 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9884 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9886 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 9887 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9888 | |
| 9889 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 9890 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9891 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9892 | return( ret ); |
| 9893 | } |
| 9894 | } |
| 9895 | else |
| 9896 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9897 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9898 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9899 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9900 | return( ret ); |
| 9901 | } |
| 9902 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9903 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9904 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 9905 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9906 | } |
| 9907 | |
| 9908 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9909 | * Check record counters and renegotiate if they're above the limit. |
| 9910 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9911 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9912 | { |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9913 | size_t ep_len = ssl_ep_len( ssl ); |
| 9914 | int in_ctr_cmp; |
| 9915 | int out_ctr_cmp; |
| 9916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9917 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 9918 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9919 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9920 | { |
| 9921 | return( 0 ); |
| 9922 | } |
| 9923 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9924 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 9925 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 9926 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9927 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 9928 | |
| 9929 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9930 | { |
| 9931 | return( 0 ); |
| 9932 | } |
| 9933 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9935 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9936 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9937 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9938 | |
| 9939 | /* |
| 9940 | * Receive application data decrypted from the SSL layer |
| 9941 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9942 | 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] | 9943 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9944 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 9945 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9946 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9947 | if( ssl == NULL || ssl->conf == NULL ) |
| 9948 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9950 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9952 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9953 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9954 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9955 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9956 | return( ret ); |
| 9957 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9958 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9959 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9960 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 9961 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9962 | return( ret ); |
| 9963 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9964 | } |
| 9965 | #endif |
| 9966 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9967 | /* |
| 9968 | * Check if renegotiation is necessary and/or handshake is |
| 9969 | * in process. If yes, perform/continue, and fall through |
| 9970 | * if an unexpected packet is received while the client |
| 9971 | * is waiting for the ServerHello. |
| 9972 | * |
| 9973 | * (There is no equivalent to the last condition on |
| 9974 | * the server-side as it is not treated as within |
| 9975 | * a handshake while waiting for the ClientHello |
| 9976 | * after a renegotiation request.) |
| 9977 | */ |
| 9978 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9979 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9980 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 9981 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 9982 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9983 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9984 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9985 | return( ret ); |
| 9986 | } |
| 9987 | #endif |
| 9988 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9989 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9990 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9991 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9992 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 9993 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9994 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9995 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9996 | return( ret ); |
| 9997 | } |
| 9998 | } |
| 9999 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 10000 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10001 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10002 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10003 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10004 | if( ssl->f_get_timer != NULL && |
| 10005 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 10006 | { |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 10007 | ssl_set_timer( ssl, |
| 10008 | mbedtls_ssl_conf_get_read_timeout( ssl->conf ) ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 10009 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10010 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10011 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10012 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10013 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 10014 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10015 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10016 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 10017 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10018 | } |
| 10019 | |
| 10020 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10021 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10022 | { |
| 10023 | /* |
| 10024 | * OpenSSL sends empty messages to randomize the IV |
| 10025 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 10026 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10027 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10028 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 10029 | return( 0 ); |
| 10030 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10031 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10032 | return( ret ); |
| 10033 | } |
| 10034 | } |
| 10035 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10036 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10037 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10038 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10039 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10040 | /* |
| 10041 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 10042 | * - For server-side, expect CLIENT_HELLO. |
| 10043 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 10044 | */ |
| 10045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10046 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10047 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10048 | MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10049 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10050 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10052 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10053 | |
| 10054 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10055 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10056 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10057 | { |
| 10058 | continue; |
| 10059 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10060 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10061 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10062 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10063 | { |
| 10064 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 10065 | } |
| 10066 | #endif |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10067 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10068 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10069 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10070 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10071 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10072 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10073 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10074 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10075 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10076 | |
| 10077 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10078 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10079 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10080 | { |
| 10081 | continue; |
| 10082 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10083 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 10084 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 10085 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10086 | { |
| 10087 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 10088 | } |
| 10089 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10090 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10091 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 10092 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10093 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10094 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10095 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 10096 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Hanno Becker | b0b2b67 | 2019-06-12 16:58:10 +0100 | [diff] [blame] | 10097 | mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) == |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10098 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 10099 | { |
| 10100 | /* |
| 10101 | * Accept renegotiation request |
| 10102 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10103 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10104 | /* DTLS clients need to know renego is server-initiated */ |
| 10105 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10106 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10107 | mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10108 | MBEDTLS_SSL_IS_CLIENT ) |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 10109 | { |
| 10110 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 10111 | } |
| 10112 | #endif |
| 10113 | ret = ssl_start_renegotiation( ssl ); |
| 10114 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 10115 | ret != 0 ) |
| 10116 | { |
| 10117 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 10118 | return( ret ); |
| 10119 | } |
| 10120 | } |
| 10121 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10122 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10123 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10124 | /* |
| 10125 | * Refuse renegotiation |
| 10126 | */ |
| 10127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10128 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10130 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 10131 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10132 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 10133 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 10134 | we send a fatal alert and abort the connection. */ |
| 10135 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 10136 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 10137 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10138 | } |
| 10139 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10140 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 10141 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 10142 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10143 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10144 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10145 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10146 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10147 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 10148 | { |
| 10149 | return( ret ); |
| 10150 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10151 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 10152 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10153 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 10154 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10156 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 10157 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 10158 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10159 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10160 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 10161 | /* At this point, we don't know whether the renegotiation has been |
| 10162 | * completed or not. The cases to consider are the following: |
| 10163 | * 1) The renegotiation is complete. In this case, no new record |
| 10164 | * has been read yet. |
| 10165 | * 2) The renegotiation is incomplete because the client received |
| 10166 | * an application data record while awaiting the ServerHello. |
| 10167 | * 3) The renegotiation is incomplete because the client received |
| 10168 | * a non-handshake, non-application data message while awaiting |
| 10169 | * the ServerHello. |
| 10170 | * In each of these case, looping will be the proper action: |
| 10171 | * - For 1), the next iteration will read a new record and check |
| 10172 | * if it's application data. |
| 10173 | * - For 2), the loop condition isn't satisfied as application data |
| 10174 | * is present, hence continue is the same as break |
| 10175 | * - For 3), the loop condition is satisfied and read_record |
| 10176 | * will re-deliver the message that was held back by the client |
| 10177 | * when expecting the ServerHello. |
| 10178 | */ |
| 10179 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10180 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 10181 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10182 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10183 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10184 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10185 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10186 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10188 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10189 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10190 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10191 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 10192 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 10193 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10194 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10196 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 10197 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10198 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10199 | 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] | 10200 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 10201 | } |
| 10202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10203 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10204 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10205 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 10206 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10207 | } |
| 10208 | |
| 10209 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 10210 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10211 | /* We're going to return something now, cancel timer, |
| 10212 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10213 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 10214 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10215 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 10216 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10217 | /* If we requested renego but received AppData, resend HelloRequest. |
| 10218 | * Do it now, after setting in_offt, to avoid taking this branch |
| 10219 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10220 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 2d9623f | 2019-06-13 12:07:05 +0100 | [diff] [blame] | 10221 | if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == |
| 10222 | MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10223 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10224 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10225 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10227 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10228 | return( ret ); |
| 10229 | } |
| 10230 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10231 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10232 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10233 | } |
| 10234 | |
| 10235 | n = ( len < ssl->in_msglen ) |
| 10236 | ? len : ssl->in_msglen; |
| 10237 | |
| 10238 | memcpy( buf, ssl->in_offt, n ); |
| 10239 | ssl->in_msglen -= n; |
| 10240 | |
| 10241 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10242 | { |
| 10243 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10244 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 10245 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10246 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10247 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10248 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10249 | /* more data available */ |
| 10250 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10251 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10253 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10254 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10255 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10256 | } |
| 10257 | |
| 10258 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10259 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 10260 | * fragment length and buffer size. |
| 10261 | * |
| 10262 | * According to RFC 5246 Section 6.2.1: |
| 10263 | * |
| 10264 | * Zero-length fragments of Application data MAY be sent as they are |
| 10265 | * potentially useful as a traffic analysis countermeasure. |
| 10266 | * |
| 10267 | * Therefore, it is possible that the input message length is 0 and the |
| 10268 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10269 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10270 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10271 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10272 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 10273 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 10274 | const size_t max_len = (size_t) ret; |
| 10275 | |
| 10276 | if( ret < 0 ) |
| 10277 | { |
| 10278 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 10279 | return( ret ); |
| 10280 | } |
| 10281 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10282 | if( len > max_len ) |
| 10283 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10284 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10285 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10286 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10288 | "maximum fragment length: %d > %d", |
| 10289 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10290 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10291 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10292 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10293 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10294 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 10295 | { |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10296 | len = max_len; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10297 | } |
| 10298 | #endif |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10299 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10300 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10301 | if( ssl->out_left != 0 ) |
| 10302 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10303 | /* |
| 10304 | * The user has previously tried to send the data and |
| 10305 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 10306 | * written. In this case, we expect the high-level write function |
| 10307 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 10308 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10309 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10310 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10311 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10312 | return( ret ); |
| 10313 | } |
| 10314 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10315 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 10316 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10317 | /* |
| 10318 | * The user is trying to send a message the first time, so we need to |
| 10319 | * copy the data into the internal buffers and setup the data structure |
| 10320 | * to keep track of partial writes |
| 10321 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10322 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10323 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10324 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10325 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 10326 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10327 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10328 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10329 | return( ret ); |
| 10330 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10331 | } |
| 10332 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10333 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10334 | } |
| 10335 | |
| 10336 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10337 | * Write application data, doing 1/n-1 splitting if necessary. |
| 10338 | * |
| 10339 | * 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] | 10340 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 10341 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10342 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10343 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10344 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10345 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10346 | { |
| 10347 | int ret; |
| 10348 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10349 | if( ssl->conf->cbc_record_splitting == |
| 10350 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 10351 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10352 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
| 10353 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 10354 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10355 | { |
| 10356 | return( ssl_write_real( ssl, buf, len ) ); |
| 10357 | } |
| 10358 | |
| 10359 | if( ssl->split_done == 0 ) |
| 10360 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10361 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10362 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10363 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10364 | } |
| 10365 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10366 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 10367 | return( ret ); |
| 10368 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10369 | |
| 10370 | return( ret + 1 ); |
| 10371 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10372 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10373 | |
| 10374 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10375 | * Write application data (public-facing wrapper) |
| 10376 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10377 | 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] | 10378 | { |
| 10379 | int ret; |
| 10380 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10381 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10382 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10383 | if( ssl == NULL || ssl->conf == NULL ) |
| 10384 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10385 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10386 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10387 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 10388 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10389 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10390 | return( ret ); |
| 10391 | } |
| 10392 | #endif |
| 10393 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10394 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10395 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10396 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10397 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 10398 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10399 | return( ret ); |
| 10400 | } |
| 10401 | } |
| 10402 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10403 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10404 | ret = ssl_write_split( ssl, buf, len ); |
| 10405 | #else |
| 10406 | ret = ssl_write_real( ssl, buf, len ); |
| 10407 | #endif |
| 10408 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10409 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10410 | |
| 10411 | return( ret ); |
| 10412 | } |
| 10413 | |
| 10414 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10415 | * Notify the peer that the connection is being closed |
| 10416 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10417 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10418 | { |
| 10419 | int ret; |
| 10420 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10421 | if( ssl == NULL || ssl->conf == NULL ) |
| 10422 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10425 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10426 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10427 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10429 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10430 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10431 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10432 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10433 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10434 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10435 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10436 | return( ret ); |
| 10437 | } |
| 10438 | } |
| 10439 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10440 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10441 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10442 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10443 | } |
| 10444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10445 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10446 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10447 | if( transform == NULL ) |
| 10448 | return; |
| 10449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10450 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10451 | deflateEnd( &transform->ctx_deflate ); |
| 10452 | inflateEnd( &transform->ctx_inflate ); |
| 10453 | #endif |
| 10454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10455 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 10456 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 10457 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10458 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10459 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 10460 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10461 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10462 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10463 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10464 | } |
| 10465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10466 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10467 | 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] | 10468 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10469 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10470 | |
| 10471 | while( cur != NULL ) |
| 10472 | { |
| 10473 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10474 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10475 | cur = next; |
| 10476 | } |
| 10477 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10478 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10479 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10480 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10481 | |
| 10482 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 10483 | { |
| 10484 | unsigned offset; |
| 10485 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10486 | |
| 10487 | if( hs == NULL ) |
| 10488 | return; |
| 10489 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 10490 | ssl_free_buffered_record( ssl ); |
| 10491 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10492 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10493 | ssl_buffering_free_slot( ssl, offset ); |
| 10494 | } |
| 10495 | |
| 10496 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 10497 | uint8_t slot ) |
| 10498 | { |
| 10499 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10500 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 10501 | |
| 10502 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 10503 | return; |
| 10504 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10505 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10506 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10507 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 10508 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10509 | mbedtls_free( hs_buf->data ); |
| 10510 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10511 | } |
| 10512 | } |
| 10513 | |
| 10514 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 10515 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10516 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10517 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10518 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 10519 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10520 | if( handshake == NULL ) |
| 10521 | return; |
| 10522 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10523 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 10524 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 10525 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 10526 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10527 | handshake->async_in_progress = 0; |
| 10528 | } |
| 10529 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 10530 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10531 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 10532 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 10533 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 10534 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 10535 | #endif |
| 10536 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10537 | #if defined(MBEDTLS_SHA256_C) |
| 10538 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 10539 | #endif |
| 10540 | #if defined(MBEDTLS_SHA512_C) |
| 10541 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 10542 | #endif |
| 10543 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 10544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10545 | #if defined(MBEDTLS_DHM_C) |
| 10546 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10547 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10548 | #if defined(MBEDTLS_ECDH_C) |
| 10549 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10550 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 10551 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10552 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 10553 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10554 | mbedtls_free( handshake->ecjpake_cache ); |
| 10555 | handshake->ecjpake_cache = NULL; |
| 10556 | handshake->ecjpake_cache_len = 0; |
| 10557 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10558 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10559 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 10560 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 10561 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 10562 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10563 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 10564 | #endif |
| 10565 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10566 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10567 | if( handshake->psk != NULL ) |
| 10568 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10569 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10570 | mbedtls_free( handshake->psk ); |
| 10571 | } |
| 10572 | #endif |
| 10573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10574 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10575 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10576 | /* |
| 10577 | * Free only the linked list wrapper, not the keys themselves |
| 10578 | * since the belong to the SNI callback |
| 10579 | */ |
| 10580 | if( handshake->sni_key_cert != NULL ) |
| 10581 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10582 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10583 | |
| 10584 | while( cur != NULL ) |
| 10585 | { |
| 10586 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10587 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10588 | cur = next; |
| 10589 | } |
| 10590 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10591 | #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] | 10592 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10593 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 10594 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | e4aeb76 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 10595 | if( handshake->ecrs_peer_cert != NULL ) |
| 10596 | { |
| 10597 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 10598 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 10599 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10600 | #endif |
| 10601 | |
Hanno Becker | 3bf8cdf | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 10602 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10603 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10604 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 10605 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10606 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10607 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10608 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 10609 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10610 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 10611 | #endif |
| 10612 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10613 | mbedtls_platform_zeroize( handshake, |
| 10614 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10615 | } |
| 10616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10617 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10618 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10619 | if( session == NULL ) |
| 10620 | return; |
| 10621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10622 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 2214159 | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 10623 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 10624 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 10625 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 10626 | #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] | 10627 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 10628 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 10629 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10630 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10631 | } |
| 10632 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10633 | /* |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 10634 | * Serialize a full SSL context |
| 10635 | */ |
| 10636 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 10637 | unsigned char *buf, |
| 10638 | size_t buf_len, |
| 10639 | size_t *olen ) |
| 10640 | { |
Manuel Pégourié-Gonnard | cc71c77 | 2019-06-05 09:47:18 +0200 | [diff] [blame] | 10641 | /* Unimplemented */ |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 10642 | (void) ssl; |
| 10643 | |
| 10644 | if( buf != NULL ) |
| 10645 | memset( buf, 0, buf_len ); |
| 10646 | |
| 10647 | *olen = 0; |
| 10648 | |
| 10649 | return( 0 ); |
| 10650 | } |
| 10651 | |
| 10652 | /* |
| 10653 | * Deserialize a full SSL context |
| 10654 | */ |
| 10655 | int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl, |
| 10656 | const unsigned char *buf, |
| 10657 | size_t len ) |
| 10658 | { |
Manuel Pégourié-Gonnard | cc71c77 | 2019-06-05 09:47:18 +0200 | [diff] [blame] | 10659 | /* Unimplemented */ |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 10660 | (void) ssl; |
| 10661 | (void) buf; |
| 10662 | (void) len; |
| 10663 | |
| 10664 | return( 0 ); |
| 10665 | } |
| 10666 | |
| 10667 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10668 | * Free an SSL context |
| 10669 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10670 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10671 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10672 | if( ssl == NULL ) |
| 10673 | return; |
| 10674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10675 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10676 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 10677 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10678 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10679 | 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] | 10680 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10681 | } |
| 10682 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 10683 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10684 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10685 | 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] | 10686 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10687 | } |
| 10688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10689 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 10690 | if( ssl->compress_buf != NULL ) |
| 10691 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10692 | 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] | 10693 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 10694 | } |
| 10695 | #endif |
| 10696 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10697 | if( ssl->transform ) |
| 10698 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10699 | mbedtls_ssl_transform_free( ssl->transform ); |
| 10700 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10701 | } |
| 10702 | |
| 10703 | if( ssl->handshake ) |
| 10704 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10705 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10706 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 10707 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10709 | mbedtls_free( ssl->handshake ); |
| 10710 | mbedtls_free( ssl->transform_negotiate ); |
| 10711 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10712 | } |
| 10713 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 10714 | if( ssl->session ) |
| 10715 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10716 | mbedtls_ssl_session_free( ssl->session ); |
| 10717 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 10718 | } |
| 10719 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 10720 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 10721 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10722 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10723 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10724 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10725 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 10726 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10727 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10728 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 10729 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 10730 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10731 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 10732 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 10733 | } |
| 10734 | #endif |
| 10735 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 10736 | #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] | 10737 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 10738 | #endif |
| 10739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10740 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 10741 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 10742 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10743 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10744 | } |
| 10745 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10746 | /* |
| 10747 | * Initialze mbedtls_ssl_config |
| 10748 | */ |
| 10749 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 10750 | { |
| 10751 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | e744eab | 2019-03-18 10:51:18 +0100 | [diff] [blame] | 10752 | |
| 10753 | #if !defined(MBEDTLS_SSL_PROTO_TLS) |
| 10754 | conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
| 10755 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10756 | } |
| 10757 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 10758 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10759 | static int ssl_preset_default_hashes[] = { |
| 10760 | #if defined(MBEDTLS_SHA512_C) |
| 10761 | MBEDTLS_MD_SHA512, |
| 10762 | MBEDTLS_MD_SHA384, |
| 10763 | #endif |
| 10764 | #if defined(MBEDTLS_SHA256_C) |
| 10765 | MBEDTLS_MD_SHA256, |
| 10766 | MBEDTLS_MD_SHA224, |
| 10767 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 10768 | #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] | 10769 | MBEDTLS_MD_SHA1, |
| 10770 | #endif |
| 10771 | MBEDTLS_MD_NONE |
| 10772 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 10773 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10774 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10775 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 10776 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 10777 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 10778 | 0 |
| 10779 | }; |
| 10780 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10781 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10782 | static int ssl_preset_suiteb_hashes[] = { |
| 10783 | MBEDTLS_MD_SHA256, |
| 10784 | MBEDTLS_MD_SHA384, |
| 10785 | MBEDTLS_MD_NONE |
| 10786 | }; |
| 10787 | #endif |
| 10788 | |
| 10789 | #if defined(MBEDTLS_ECP_C) |
| 10790 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 10791 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10792 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 10793 | #endif |
| 10794 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10795 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 10796 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10797 | MBEDTLS_ECP_DP_NONE |
| 10798 | }; |
| 10799 | #endif |
| 10800 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10801 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 10802 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10803 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10804 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10805 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10806 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 10807 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10808 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 10809 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10810 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 10811 | /* Use the functions here so that they are covered in tests, |
| 10812 | * but otherwise access member directly for efficiency */ |
| 10813 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 10814 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10815 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10816 | /* |
| 10817 | * Things that are common to all presets |
| 10818 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10819 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10820 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 10821 | { |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 10822 | #if !defined(MBEDTLS_SSL_CONF_AUTHMODE) |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10823 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
Hanno Becker | acd4fc0 | 2019-06-12 16:40:50 +0100 | [diff] [blame] | 10824 | #endif /* !MBEDTLS_SSL_CONF_AUTHMODE */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10825 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 10826 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 10827 | #endif |
| 10828 | } |
| 10829 | #endif |
| 10830 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 10831 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10832 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 10833 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10834 | |
| 10835 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10836 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 10837 | #endif |
| 10838 | |
| 10839 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 10840 | #if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10841 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 10842 | #endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */ |
| 10843 | #if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET) |
Jarno Lamsa | d9382f8 | 2019-06-10 10:27:14 +0300 | [diff] [blame] | 10844 | conf->enforce_extended_master_secret = |
Jarno Lamsa | 18b9a49 | 2019-06-10 15:23:29 +0300 | [diff] [blame] | 10845 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED; |
Hanno Becker | aabbb58 | 2019-06-11 13:43:27 +0100 | [diff] [blame] | 10846 | #endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10847 | #endif |
| 10848 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10849 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 10850 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 10851 | #endif |
| 10852 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 10853 | #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] | 10854 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 10855 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 10856 | #endif |
| 10857 | |
Hanno Becker | 7f376f4 | 2019-06-12 16:20:48 +0100 | [diff] [blame] | 10858 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \ |
| 10859 | !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10860 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 10861 | #endif |
| 10862 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 10863 | #if defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 10864 | #if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST) |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 10865 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
Hanno Becker | c2cfdaa | 2019-06-13 12:33:03 +0100 | [diff] [blame] | 10866 | #endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */ |
| 10867 | #endif /* MBEDTLS_SSL_SRV_C */ |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 10868 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10869 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 10870 | #if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10871 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 10872 | #endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */ |
| 10873 | #if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10874 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
Hanno Becker | 1f835fa | 2019-06-13 10:14:59 +0100 | [diff] [blame] | 10875 | #endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */ |
| 10876 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10877 | |
| 10878 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 10879 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10880 | memset( conf->renego_period, 0x00, 2 ); |
| 10881 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10882 | #endif |
| 10883 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10884 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 10885 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 10886 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 10887 | const unsigned char dhm_p[] = |
| 10888 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 10889 | const unsigned char dhm_g[] = |
| 10890 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 10891 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 10892 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 10893 | dhm_p, sizeof( dhm_p ), |
| 10894 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10895 | { |
| 10896 | return( ret ); |
| 10897 | } |
| 10898 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 10899 | #endif |
| 10900 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10901 | /* |
| 10902 | * Preset-specific defaults |
| 10903 | */ |
| 10904 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10905 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10906 | /* |
| 10907 | * NSA Suite B |
| 10908 | */ |
| 10909 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 10910 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 10911 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 10912 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 10913 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 10914 | |
| 10915 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 10916 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 10917 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 10918 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 10919 | ssl_preset_suiteb_ciphersuites; |
| 10920 | |
| 10921 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10922 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10923 | #endif |
| 10924 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10925 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10926 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 10927 | #endif |
| 10928 | |
| 10929 | #if defined(MBEDTLS_ECP_C) |
| 10930 | conf->curve_list = ssl_preset_suiteb_curves; |
| 10931 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 10932 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10933 | |
| 10934 | /* |
| 10935 | * Default |
| 10936 | */ |
| 10937 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 10938 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 10939 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 10940 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 10941 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 10942 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 10943 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 10944 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 10945 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10946 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 10947 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 10948 | |
| 10949 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10950 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10951 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 10952 | #endif |
| 10953 | |
| 10954 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 10955 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 10956 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 10957 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 10958 | mbedtls_ssl_list_ciphersuites(); |
| 10959 | |
| 10960 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10961 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 10962 | #endif |
| 10963 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10964 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10965 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10966 | #endif |
| 10967 | |
| 10968 | #if defined(MBEDTLS_ECP_C) |
| 10969 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 10970 | #endif |
| 10971 | |
| 10972 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 10973 | conf->dhm_min_bitlen = 1024; |
| 10974 | #endif |
| 10975 | } |
| 10976 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10977 | return( 0 ); |
| 10978 | } |
| 10979 | |
| 10980 | /* |
| 10981 | * Free mbedtls_ssl_config |
| 10982 | */ |
| 10983 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 10984 | { |
| 10985 | #if defined(MBEDTLS_DHM_C) |
| 10986 | mbedtls_mpi_free( &conf->dhm_P ); |
| 10987 | mbedtls_mpi_free( &conf->dhm_G ); |
| 10988 | #endif |
| 10989 | |
| 10990 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10991 | if( conf->psk != NULL ) |
| 10992 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10993 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10994 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 10995 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10996 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 10997 | } |
| 10998 | |
| 10999 | if( conf->psk_identity != NULL ) |
| 11000 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11001 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 11002 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 11003 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11004 | conf->psk_identity_len = 0; |
| 11005 | } |
| 11006 | #endif |
| 11007 | |
| 11008 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11009 | ssl_key_cert_free( conf->key_cert ); |
| 11010 | #endif |
| 11011 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 11012 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 11013 | } |
| 11014 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 11015 | #if defined(MBEDTLS_PK_C) && \ |
| 11016 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11017 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11018 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11019 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11020 | 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] | 11021 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11022 | #if defined(MBEDTLS_RSA_C) |
| 11023 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 11024 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11025 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11026 | #if defined(MBEDTLS_ECDSA_C) |
| 11027 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 11028 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11029 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11030 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 11031 | } |
| 11032 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 11033 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 11034 | { |
| 11035 | switch( type ) { |
| 11036 | case MBEDTLS_PK_RSA: |
| 11037 | return( MBEDTLS_SSL_SIG_RSA ); |
| 11038 | case MBEDTLS_PK_ECDSA: |
| 11039 | case MBEDTLS_PK_ECKEY: |
| 11040 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 11041 | default: |
| 11042 | return( MBEDTLS_SSL_SIG_ANON ); |
| 11043 | } |
| 11044 | } |
| 11045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11046 | 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] | 11047 | { |
| 11048 | switch( sig ) |
| 11049 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11050 | #if defined(MBEDTLS_RSA_C) |
| 11051 | case MBEDTLS_SSL_SIG_RSA: |
| 11052 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11053 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11054 | #if defined(MBEDTLS_ECDSA_C) |
| 11055 | case MBEDTLS_SSL_SIG_ECDSA: |
| 11056 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11057 | #endif |
| 11058 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11059 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11060 | } |
| 11061 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 11062 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11063 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 11064 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 11065 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 11066 | |
| 11067 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 11068 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 11069 | mbedtls_pk_type_t sig_alg ) |
| 11070 | { |
| 11071 | switch( sig_alg ) |
| 11072 | { |
| 11073 | case MBEDTLS_PK_RSA: |
| 11074 | return( set->rsa ); |
| 11075 | case MBEDTLS_PK_ECDSA: |
| 11076 | return( set->ecdsa ); |
| 11077 | default: |
| 11078 | return( MBEDTLS_MD_NONE ); |
| 11079 | } |
| 11080 | } |
| 11081 | |
| 11082 | /* Add a signature-hash-pair to a signature-hash set */ |
| 11083 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 11084 | mbedtls_pk_type_t sig_alg, |
| 11085 | mbedtls_md_type_t md_alg ) |
| 11086 | { |
| 11087 | switch( sig_alg ) |
| 11088 | { |
| 11089 | case MBEDTLS_PK_RSA: |
| 11090 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 11091 | set->rsa = md_alg; |
| 11092 | break; |
| 11093 | |
| 11094 | case MBEDTLS_PK_ECDSA: |
| 11095 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 11096 | set->ecdsa = md_alg; |
| 11097 | break; |
| 11098 | |
| 11099 | default: |
| 11100 | break; |
| 11101 | } |
| 11102 | } |
| 11103 | |
| 11104 | /* Allow exactly one hash algorithm for each signature. */ |
| 11105 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 11106 | mbedtls_md_type_t md_alg ) |
| 11107 | { |
| 11108 | set->rsa = md_alg; |
| 11109 | set->ecdsa = md_alg; |
| 11110 | } |
| 11111 | |
| 11112 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 11113 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 11114 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 11115 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11116 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 11117 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11118 | 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] | 11119 | { |
| 11120 | switch( hash ) |
| 11121 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11122 | #if defined(MBEDTLS_MD5_C) |
| 11123 | case MBEDTLS_SSL_HASH_MD5: |
| 11124 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11125 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11126 | #if defined(MBEDTLS_SHA1_C) |
| 11127 | case MBEDTLS_SSL_HASH_SHA1: |
| 11128 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11129 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11130 | #if defined(MBEDTLS_SHA256_C) |
| 11131 | case MBEDTLS_SSL_HASH_SHA224: |
| 11132 | return( MBEDTLS_MD_SHA224 ); |
| 11133 | case MBEDTLS_SSL_HASH_SHA256: |
| 11134 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11135 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11136 | #if defined(MBEDTLS_SHA512_C) |
| 11137 | case MBEDTLS_SSL_HASH_SHA384: |
| 11138 | return( MBEDTLS_MD_SHA384 ); |
| 11139 | case MBEDTLS_SSL_HASH_SHA512: |
| 11140 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11141 | #endif |
| 11142 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11143 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 11144 | } |
| 11145 | } |
| 11146 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11147 | /* |
| 11148 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 11149 | */ |
| 11150 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 11151 | { |
| 11152 | switch( md ) |
| 11153 | { |
| 11154 | #if defined(MBEDTLS_MD5_C) |
| 11155 | case MBEDTLS_MD_MD5: |
| 11156 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 11157 | #endif |
| 11158 | #if defined(MBEDTLS_SHA1_C) |
| 11159 | case MBEDTLS_MD_SHA1: |
| 11160 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 11161 | #endif |
| 11162 | #if defined(MBEDTLS_SHA256_C) |
| 11163 | case MBEDTLS_MD_SHA224: |
| 11164 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 11165 | case MBEDTLS_MD_SHA256: |
| 11166 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 11167 | #endif |
| 11168 | #if defined(MBEDTLS_SHA512_C) |
| 11169 | case MBEDTLS_MD_SHA384: |
| 11170 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 11171 | case MBEDTLS_MD_SHA512: |
| 11172 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 11173 | #endif |
| 11174 | default: |
| 11175 | return( MBEDTLS_SSL_HASH_NONE ); |
| 11176 | } |
| 11177 | } |
| 11178 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 11179 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11180 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11181 | * 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] | 11182 | * 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] | 11183 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11184 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11186 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11187 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11188 | if( ssl->conf->curve_list == NULL ) |
| 11189 | return( -1 ); |
| 11190 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 11191 | for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11192 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11193 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11194 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 11195 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 11196 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 11197 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11198 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11199 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11200 | /* |
| 11201 | * Check if a hash proposed by the peer is in our list. |
| 11202 | * Return 0 if we're willing to use it, -1 otherwise. |
| 11203 | */ |
| 11204 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 11205 | mbedtls_md_type_t md ) |
| 11206 | { |
| 11207 | const int *cur; |
| 11208 | |
| 11209 | if( ssl->conf->sig_hashes == NULL ) |
| 11210 | return( -1 ); |
| 11211 | |
| 11212 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 11213 | if( *cur == (int) md ) |
| 11214 | return( 0 ); |
| 11215 | |
| 11216 | return( -1 ); |
| 11217 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 11218 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 11219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11220 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 11221 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 11222 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11223 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 11224 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11225 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11226 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11227 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11228 | int usage = 0; |
| 11229 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11230 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11231 | const char *ext_oid; |
| 11232 | size_t ext_len; |
| 11233 | #endif |
| 11234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11235 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 11236 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11237 | ((void) cert); |
| 11238 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11239 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11240 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11242 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 11243 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11244 | { |
| 11245 | /* Server part of the key exchange */ |
| 11246 | switch( ciphersuite->key_exchange ) |
| 11247 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11248 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 11249 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11250 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11251 | break; |
| 11252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11253 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 11254 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 11255 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 11256 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11257 | break; |
| 11258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11259 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 11260 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11261 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11262 | break; |
| 11263 | |
| 11264 | /* 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] | 11265 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 11266 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 11267 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 11268 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 11269 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11270 | usage = 0; |
| 11271 | } |
| 11272 | } |
| 11273 | else |
| 11274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11275 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 11276 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11277 | } |
| 11278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11279 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11280 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11281 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11282 | ret = -1; |
| 11283 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11284 | #else |
| 11285 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11286 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11287 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11288 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 11289 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11290 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11291 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 11292 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11293 | } |
| 11294 | else |
| 11295 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11296 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 11297 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11298 | } |
| 11299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11300 | 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] | 11301 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11302 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11303 | ret = -1; |
| 11304 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11305 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11306 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11307 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11308 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11309 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 11310 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11311 | /* |
| 11312 | * Convert version numbers to/from wire format |
| 11313 | * and, for DTLS, to/from TLS equivalent. |
| 11314 | * |
| 11315 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 11316 | * For DTLS, use 1's complement (v -> 255 - v, and then map as follows: |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11317 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 11318 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 11319 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11320 | void mbedtls_ssl_write_version( int major, int minor, int transport, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11321 | unsigned char ver[2] ) |
| 11322 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11323 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) |
| 11324 | ((void) transport); |
| 11325 | #endif |
| 11326 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11327 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11328 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11329 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11330 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11331 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 11332 | |
| 11333 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 11334 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 11335 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11336 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11337 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11338 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11339 | { |
| 11340 | ver[0] = (unsigned char) major; |
| 11341 | ver[1] = (unsigned char) minor; |
| 11342 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11343 | #endif |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11344 | } |
| 11345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11346 | void mbedtls_ssl_read_version( int *major, int *minor, int transport, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11347 | const unsigned char ver[2] ) |
| 11348 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11349 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) |
| 11350 | ((void) transport); |
| 11351 | #endif |
| 11352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11353 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11354 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11355 | { |
| 11356 | *major = 255 - ver[0] + 2; |
| 11357 | *minor = 255 - ver[1] + 1; |
| 11358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11359 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11360 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 11361 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11362 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 8794a42 | 2019-06-11 10:04:57 +0200 | [diff] [blame] | 11363 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 11364 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11365 | { |
| 11366 | *major = ver[0]; |
| 11367 | *minor = ver[1]; |
| 11368 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 11369 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11370 | } |
| 11371 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 11372 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 11373 | { |
| 11374 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 11375 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 11376 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11377 | |
| 11378 | switch( md ) |
| 11379 | { |
| 11380 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 11381 | #if defined(MBEDTLS_MD5_C) |
| 11382 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 11383 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 11384 | #endif |
| 11385 | #if defined(MBEDTLS_SHA1_C) |
| 11386 | case MBEDTLS_SSL_HASH_SHA1: |
| 11387 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 11388 | break; |
| 11389 | #endif |
| 11390 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 11391 | #if defined(MBEDTLS_SHA512_C) |
| 11392 | case MBEDTLS_SSL_HASH_SHA384: |
| 11393 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 11394 | break; |
| 11395 | #endif |
| 11396 | #if defined(MBEDTLS_SHA256_C) |
| 11397 | case MBEDTLS_SSL_HASH_SHA256: |
| 11398 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 11399 | break; |
| 11400 | #endif |
| 11401 | default: |
| 11402 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11403 | } |
| 11404 | |
| 11405 | return 0; |
| 11406 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11407 | (void) ssl; |
| 11408 | (void) md; |
| 11409 | |
| 11410 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11411 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11412 | } |
| 11413 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11414 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 11415 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 11416 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 11417 | unsigned char *output, |
| 11418 | unsigned char *data, size_t data_len ) |
| 11419 | { |
| 11420 | int ret = 0; |
| 11421 | mbedtls_md5_context mbedtls_md5; |
| 11422 | mbedtls_sha1_context mbedtls_sha1; |
| 11423 | |
| 11424 | mbedtls_md5_init( &mbedtls_md5 ); |
| 11425 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 11426 | |
| 11427 | /* |
| 11428 | * digitally-signed struct { |
| 11429 | * opaque md5_hash[16]; |
| 11430 | * opaque sha_hash[20]; |
| 11431 | * }; |
| 11432 | * |
| 11433 | * md5_hash |
| 11434 | * MD5(ClientHello.random + ServerHello.random |
| 11435 | * + ServerParams); |
| 11436 | * sha_hash |
| 11437 | * SHA(ClientHello.random + ServerHello.random |
| 11438 | * + ServerParams); |
| 11439 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11440 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11441 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11442 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11443 | goto exit; |
| 11444 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11445 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11446 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11447 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11448 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11449 | goto exit; |
| 11450 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11451 | 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] | 11452 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11453 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11454 | goto exit; |
| 11455 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11456 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11457 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11458 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11459 | goto exit; |
| 11460 | } |
| 11461 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11462 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11463 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11464 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11465 | goto exit; |
| 11466 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11467 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11468 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11469 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11470 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11471 | goto exit; |
| 11472 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11473 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11474 | data_len ) ) != 0 ) |
| 11475 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11476 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11477 | goto exit; |
| 11478 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11479 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11480 | output + 16 ) ) != 0 ) |
| 11481 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11482 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11483 | goto exit; |
| 11484 | } |
| 11485 | |
| 11486 | exit: |
| 11487 | mbedtls_md5_free( &mbedtls_md5 ); |
| 11488 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 11489 | |
| 11490 | if( ret != 0 ) |
| 11491 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11492 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 11493 | |
| 11494 | return( ret ); |
| 11495 | |
| 11496 | } |
| 11497 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 11498 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 11499 | |
| 11500 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 11501 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 11502 | 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] | 11503 | unsigned char *hash, size_t *hashlen, |
| 11504 | unsigned char *data, size_t data_len, |
| 11505 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11506 | { |
| 11507 | int ret = 0; |
| 11508 | mbedtls_md_context_t ctx; |
| 11509 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 11510 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11511 | |
| 11512 | mbedtls_md_init( &ctx ); |
| 11513 | |
| 11514 | /* |
| 11515 | * digitally-signed struct { |
| 11516 | * opaque client_random[32]; |
| 11517 | * opaque server_random[32]; |
| 11518 | * ServerDHParams params; |
| 11519 | * }; |
| 11520 | */ |
| 11521 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 11522 | { |
| 11523 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 11524 | goto exit; |
| 11525 | } |
| 11526 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 11527 | { |
| 11528 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 11529 | goto exit; |
| 11530 | } |
| 11531 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11532 | { |
| 11533 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 11534 | goto exit; |
| 11535 | } |
| 11536 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 11537 | { |
| 11538 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 11539 | goto exit; |
| 11540 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 11541 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11542 | { |
| 11543 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 11544 | goto exit; |
| 11545 | } |
| 11546 | |
| 11547 | exit: |
| 11548 | mbedtls_md_free( &ctx ); |
| 11549 | |
| 11550 | if( ret != 0 ) |
| 11551 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11552 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 11553 | |
| 11554 | return( ret ); |
| 11555 | } |
| 11556 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 11557 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11559 | #endif /* MBEDTLS_SSL_TLS_C */ |