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 |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | */ |
| 48 | /* |
| 49 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 50 | * and became an IETF standard in 1999. |
| 51 | * |
| 52 | * http://wp.netscape.com/eng/ssl3/ |
| 53 | * http://www.ietf.org/rfc/rfc2246.txt |
| 54 | * http://www.ietf.org/rfc/rfc4346.txt |
| 55 | */ |
| 56 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 58 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 59 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 61 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 65 | #if defined(MBEDTLS_PLATFORM_C) |
| 66 | #include "mbedtls/platform.h" |
| 67 | #else |
| 68 | #include <stdlib.h> |
| 69 | #define mbedtls_calloc calloc |
| 70 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 71 | #endif |
| 72 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 73 | #include "mbedtls/debug.h" |
| 74 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 75 | #include "mbedtls/ssl_internal.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 76 | #include "mbedtls/platform_util.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 77 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 78 | #include <string.h> |
| 79 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 80 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 81 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 82 | #endif |
| 83 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 84 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 85 | 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] | 86 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 87 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | 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] | 89 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 91 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 92 | return( 2 ); |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 93 | #else |
| 94 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 95 | #endif |
| 96 | return( 0 ); |
| 97 | } |
| 98 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Start a timer. |
| 101 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 102 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | 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] | 104 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 105 | if( ssl->f_set_timer == NULL ) |
| 106 | return; |
| 107 | |
| 108 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 109 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Return -1 is timer is expired, 0 if it isn't. |
| 114 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 116 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 117 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 118 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 119 | |
| 120 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 121 | { |
| 122 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 123 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 124 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 125 | |
| 126 | return( 0 ); |
| 127 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 128 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 129 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 130 | mbedtls_ssl_transform *transform ); |
| 131 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl, |
| 132 | mbedtls_ssl_transform *transform ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 133 | |
| 134 | #define SSL_DONT_FORCE_FLUSH 0 |
| 135 | #define SSL_FORCE_FLUSH 1 |
| 136 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 137 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 138 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 139 | /* Forward declarations for functions related to message buffering. */ |
| 140 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 141 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 142 | uint8_t slot ); |
| 143 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 144 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 145 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 146 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
| 147 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 148 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 149 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 150 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 151 | 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] | 152 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 153 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 154 | |
| 155 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 156 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 157 | |
| 158 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 159 | } |
| 160 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 161 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 162 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 163 | size_t const bytes_written = ssl->out_left; |
| 164 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 165 | |
| 166 | /* Double-check that the write-index hasn't gone |
| 167 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 168 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 169 | { |
| 170 | /* Should never happen... */ |
| 171 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 172 | } |
| 173 | |
| 174 | return( (int) ( mtu - bytes_written ) ); |
| 175 | } |
| 176 | |
| 177 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 178 | { |
| 179 | int ret; |
| 180 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 181 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 182 | |
| 183 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 184 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 185 | |
| 186 | if( max_len > mfl ) |
| 187 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 188 | |
| 189 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 190 | * only limits the maximum record payload size, so in theory |
| 191 | * we would be allowed to pack multiple records of payload size |
| 192 | * MFL into a single datagram. However, this would mean that there's |
| 193 | * no way to explicitly communicate MTU restrictions to the peer. |
| 194 | * |
| 195 | * The following reduction of max_len makes sure that we never |
| 196 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 197 | */ |
| 198 | if( max_len <= ssl->out_left ) |
| 199 | return( 0 ); |
| 200 | |
| 201 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 202 | #endif |
| 203 | |
| 204 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 205 | if( ret < 0 ) |
| 206 | return( ret ); |
| 207 | remaining = (size_t) ret; |
| 208 | |
| 209 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 210 | if( ret < 0 ) |
| 211 | return( ret ); |
| 212 | expansion = (size_t) ret; |
| 213 | |
| 214 | if( remaining <= expansion ) |
| 215 | return( 0 ); |
| 216 | |
| 217 | remaining -= expansion; |
| 218 | if( remaining >= max_len ) |
| 219 | remaining = max_len; |
| 220 | |
| 221 | return( (int) remaining ); |
| 222 | } |
| 223 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 224 | /* |
| 225 | * Double the retransmit timeout value, within the allowed range, |
| 226 | * returning -1 if the maximum value has already been reached. |
| 227 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 229 | { |
| 230 | uint32_t new_timeout; |
| 231 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 232 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 233 | return( -1 ); |
| 234 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 235 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 236 | * in the following way: after the initial transmission and a first |
| 237 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 238 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 239 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 240 | * on most non-IP stacks too. */ |
| 241 | if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 242 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 243 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 244 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 245 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 246 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 247 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 248 | |
| 249 | /* Avoid arithmetic overflow and range overflow */ |
| 250 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 251 | new_timeout > ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 252 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 253 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | 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] | 258 | ssl->handshake->retransmit_timeout ) ); |
| 259 | |
| 260 | return( 0 ); |
| 261 | } |
| 262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 264 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 265 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 266 | 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] | 267 | ssl->handshake->retransmit_timeout ) ); |
| 268 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 272 | /* |
| 273 | * Convert max_fragment_length codes to length. |
| 274 | * RFC 6066 says: |
| 275 | * enum{ |
| 276 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 277 | * } MaxFragmentLength; |
| 278 | * and we add 0 -> extension unused |
| 279 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 280 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 281 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 282 | switch( mfl ) |
| 283 | { |
| 284 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 285 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 286 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 287 | return 512; |
| 288 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 289 | return 1024; |
| 290 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 291 | return 2048; |
| 292 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 293 | return 4096; |
| 294 | default: |
| 295 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 296 | } |
| 297 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 299 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 300 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 302 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | mbedtls_ssl_session_free( dst ); |
| 304 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 305 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 307 | if( src->peer_cert != NULL ) |
| 308 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 309 | int ret; |
| 310 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 311 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 312 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 313 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | 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] | 318 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 319 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 321 | dst->peer_cert = NULL; |
| 322 | return( ret ); |
| 323 | } |
| 324 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 325 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 326 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 327 | #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] | 328 | if( src->ticket != NULL ) |
| 329 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 330 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 331 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 332 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 333 | |
| 334 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 335 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 336 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 337 | |
| 338 | return( 0 ); |
| 339 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 340 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 342 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 343 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 344 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 345 | size_t keylen, |
| 346 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 347 | size_t ivlen, |
| 348 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 349 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 351 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 352 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 353 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 354 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 355 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 356 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 357 | /* |
| 358 | * Key material generation |
| 359 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 361 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 362 | const char *label, |
| 363 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 364 | unsigned char *dstbuf, size_t dlen ) |
| 365 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 366 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 367 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 368 | mbedtls_md5_context md5; |
| 369 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 370 | unsigned char padding[16]; |
| 371 | unsigned char sha1sum[20]; |
| 372 | ((void)label); |
| 373 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 374 | mbedtls_md5_init( &md5 ); |
| 375 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 376 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 377 | /* |
| 378 | * SSLv3: |
| 379 | * block = |
| 380 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 381 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 382 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 383 | * ... |
| 384 | */ |
| 385 | for( i = 0; i < dlen / 16; i++ ) |
| 386 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 387 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 388 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 389 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 390 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 391 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 392 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 393 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 394 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 395 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 396 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 397 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 398 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 399 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 400 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 401 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 402 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 403 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 404 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 405 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 406 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 407 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 410 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 411 | mbedtls_md5_free( &md5 ); |
| 412 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 413 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 414 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 415 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 416 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 417 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 418 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 420 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 422 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 423 | const char *label, |
| 424 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 425 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 426 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 427 | size_t nb, hs; |
| 428 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 429 | const unsigned char *S1, *S2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 430 | unsigned char tmp[128]; |
| 431 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | const mbedtls_md_info_t *md_info; |
| 433 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 434 | int ret; |
| 435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 436 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 437 | |
| 438 | if( sizeof( tmp ) < 20 + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 440 | |
| 441 | hs = ( slen + 1 ) / 2; |
| 442 | S1 = secret; |
| 443 | S2 = secret + slen - hs; |
| 444 | |
| 445 | nb = strlen( label ); |
| 446 | memcpy( tmp + 20, label, nb ); |
| 447 | memcpy( tmp + 20 + nb, random, rlen ); |
| 448 | nb += rlen; |
| 449 | |
| 450 | /* |
| 451 | * First compute P_md5(secret,label+random)[0..dlen] |
| 452 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
| 454 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 455 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | 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] | 457 | return( ret ); |
| 458 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 460 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 461 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 462 | |
| 463 | for( i = 0; i < dlen; i += 16 ) |
| 464 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 466 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 467 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 469 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 470 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 471 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 472 | |
| 473 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 474 | |
| 475 | for( j = 0; j < k; j++ ) |
| 476 | dstbuf[i + j] = h_i[j]; |
| 477 | } |
| 478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 480 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 481 | /* |
| 482 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 483 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
| 485 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 486 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 487 | 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] | 488 | return( ret ); |
| 489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 491 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 492 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 493 | |
| 494 | for( i = 0; i < dlen; i += 20 ) |
| 495 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 496 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 497 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 498 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 499 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 500 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 501 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 502 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 503 | |
| 504 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 505 | |
| 506 | for( j = 0; j < k; j++ ) |
| 507 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 508 | } |
| 509 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 511 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 512 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 513 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 514 | |
| 515 | return( 0 ); |
| 516 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 519 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 520 | 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] | 521 | const unsigned char *secret, size_t slen, |
| 522 | const char *label, |
| 523 | const unsigned char *random, size_t rlen, |
| 524 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 525 | { |
| 526 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 527 | size_t i, j, k, md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 528 | unsigned char tmp[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 529 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 530 | const mbedtls_md_info_t *md_info; |
| 531 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 532 | int ret; |
| 533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 537 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 539 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 540 | |
| 541 | if( sizeof( tmp ) < md_len + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 543 | |
| 544 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 545 | memcpy( tmp + md_len, label, nb ); |
| 546 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 547 | nb += rlen; |
| 548 | |
| 549 | /* |
| 550 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 551 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | 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] | 553 | return( ret ); |
| 554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 555 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 556 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 557 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 558 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 559 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 561 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 562 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 563 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 566 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 567 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 568 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 569 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 570 | |
| 571 | for( j = 0; j < k; j++ ) |
| 572 | dstbuf[i + j] = h_i[j]; |
| 573 | } |
| 574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 576 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 577 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 578 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 579 | |
| 580 | return( 0 ); |
| 581 | } |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 584 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 585 | const char *label, |
| 586 | const unsigned char *random, size_t rlen, |
| 587 | unsigned char *dstbuf, size_t dlen ) |
| 588 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 590 | label, random, rlen, dstbuf, dlen ) ); |
| 591 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 595 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 596 | const char *label, |
| 597 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 598 | unsigned char *dstbuf, size_t dlen ) |
| 599 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 601 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 602 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 603 | #endif /* MBEDTLS_SHA512_C */ |
| 604 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 606 | 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] | 607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 608 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 609 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 610 | 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] | 611 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 612 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 614 | static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * ); |
| 615 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 616 | #endif |
| 617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 619 | static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * ); |
| 620 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 621 | #endif |
| 622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 624 | #if defined(MBEDTLS_SHA256_C) |
| 625 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 626 | static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * ); |
| 627 | 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] | 628 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 629 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | #if defined(MBEDTLS_SHA512_C) |
| 631 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 632 | static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * ); |
| 633 | 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] | 634 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 638 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 639 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 640 | unsigned char tmp[64]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 641 | unsigned char keyblk[256]; |
| 642 | unsigned char *key1; |
| 643 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 644 | unsigned char *mac_enc; |
| 645 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 646 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 647 | size_t iv_copy_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 648 | const mbedtls_cipher_info_t *cipher_info; |
| 649 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 652 | mbedtls_ssl_transform *transform = ssl->transform_negotiate; |
| 653 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 658 | if( cipher_info == NULL ) |
| 659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 660 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 661 | transform->ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 663 | } |
| 664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 666 | if( md_info == NULL ) |
| 667 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 669 | transform->ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 670 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 673 | /* |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 674 | * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 675 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 676 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 677 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 678 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 679 | handshake->tls_prf = ssl3_prf; |
| 680 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 681 | handshake->calc_finished = ssl_calc_finished_ssl; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 682 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 683 | else |
| 684 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 685 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 686 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 687 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 688 | handshake->tls_prf = tls1_prf; |
| 689 | handshake->calc_verify = ssl_calc_verify_tls; |
| 690 | handshake->calc_finished = ssl_calc_finished_tls; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 691 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 692 | else |
| 693 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 695 | #if defined(MBEDTLS_SHA512_C) |
| 696 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 697 | transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 698 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 699 | handshake->tls_prf = tls_prf_sha384; |
| 700 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 701 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 702 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 703 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 704 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | #if defined(MBEDTLS_SHA256_C) |
| 706 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 707 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 708 | handshake->tls_prf = tls_prf_sha256; |
| 709 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 710 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 711 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 712 | else |
| 713 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 714 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 715 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 716 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 717 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 718 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 719 | |
| 720 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 721 | * SSLv3: |
| 722 | * master = |
| 723 | * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) + |
| 724 | * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) + |
| 725 | * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) ) |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 726 | * |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 727 | * TLSv1+: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 728 | * master = PRF( premaster, "master secret", randbytes )[0..47] |
| 729 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 730 | if( handshake->resume == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 731 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 733 | handshake->pmslen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 736 | if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 737 | { |
| 738 | unsigned char session_hash[48]; |
| 739 | size_t hash_len; |
| 740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 742 | |
| 743 | ssl->handshake->calc_verify( ssl, session_hash ); |
| 744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 745 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 746 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 747 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 749 | if( ssl->transform_negotiate->ciphersuite_info->mac == |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 751 | { |
| 752 | hash_len = 48; |
| 753 | } |
| 754 | else |
| 755 | #endif |
| 756 | hash_len = 32; |
| 757 | } |
| 758 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 759 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 760 | hash_len = 36; |
| 761 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 762 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 763 | |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 764 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 765 | "extended master secret", |
| 766 | session_hash, hash_len, |
| 767 | session->master, 48 ); |
| 768 | if( ret != 0 ) |
| 769 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 771 | return( ret ); |
| 772 | } |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 773 | |
| 774 | } |
| 775 | else |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 776 | #endif |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 777 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 778 | "master secret", |
| 779 | handshake->randbytes, 64, |
| 780 | session->master, 48 ); |
| 781 | if( ret != 0 ) |
| 782 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 784 | return( ret ); |
| 785 | } |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 786 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 787 | mbedtls_platform_zeroize( handshake->premaster, |
| 788 | sizeof(handshake->premaster) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 789 | } |
| 790 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 791 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 792 | |
| 793 | /* |
| 794 | * Swap the client and server random values. |
| 795 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 796 | memcpy( tmp, handshake->randbytes, 64 ); |
| 797 | memcpy( handshake->randbytes, tmp + 32, 32 ); |
| 798 | memcpy( handshake->randbytes + 32, tmp, 32 ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 799 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 800 | |
| 801 | /* |
| 802 | * SSLv3: |
| 803 | * key block = |
| 804 | * MD5( master + SHA1( 'A' + master + randbytes ) ) + |
| 805 | * MD5( master + SHA1( 'BB' + master + randbytes ) ) + |
| 806 | * MD5( master + SHA1( 'CCC' + master + randbytes ) ) + |
| 807 | * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) + |
| 808 | * ... |
| 809 | * |
| 810 | * TLSv1: |
| 811 | * key block = PRF( master, "key expansion", randbytes ) |
| 812 | */ |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 813 | ret = handshake->tls_prf( session->master, 48, "key expansion", |
| 814 | handshake->randbytes, 64, keyblk, 256 ); |
| 815 | if( ret != 0 ) |
| 816 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 817 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 818 | return( ret ); |
| 819 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 820 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 821 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 822 | mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) ); |
| 823 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 ); |
| 824 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 ); |
| 825 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 826 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 827 | mbedtls_platform_zeroize( handshake->randbytes, |
| 828 | sizeof( handshake->randbytes ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * Determine the appropriate key, IV and MAC length. |
| 832 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 833 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 834 | transform->keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 835 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 837 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 838 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 839 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 840 | size_t taglen, explicit_ivlen; |
| 841 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 842 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 843 | mac_key_len = 0; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 844 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 845 | /* All modes haves 96-bit IVs; |
| 846 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 847 | * ChachaPoly has all 12 bytes implicit |
| 848 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 849 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 850 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 851 | transform->fixed_ivlen = 12; |
| 852 | else |
| 853 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 854 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 855 | /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */ |
| 856 | taglen = transform->ciphersuite_info->flags & |
| 857 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 858 | |
| 859 | |
| 860 | /* Minimum length of encrypted record */ |
| 861 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 862 | transform->minlen = explicit_ivlen + taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 863 | } |
| 864 | else |
| 865 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 866 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 867 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 868 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 869 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 871 | return( ret ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 872 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 873 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 874 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 875 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 876 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 879 | /* |
| 880 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 881 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 882 | * so we only need to adjust the length here. |
| 883 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 884 | if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 887 | |
| 888 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 889 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 890 | * HMAC implementation which also truncates the key |
| 891 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 892 | mac_key_len = transform->maclen; |
| 893 | #endif |
| 894 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 895 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 896 | |
| 897 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 898 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 899 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 900 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 902 | transform->minlen = transform->maclen; |
| 903 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 904 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 905 | /* |
| 906 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 907 | * 1. if EtM is in use: one block plus MAC |
| 908 | * otherwise: * first multiple of blocklen greater than maclen |
| 909 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 910 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 911 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 912 | if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 913 | { |
| 914 | transform->minlen = transform->maclen |
| 915 | + cipher_info->block_size; |
| 916 | } |
| 917 | else |
| 918 | #endif |
| 919 | { |
| 920 | transform->minlen = transform->maclen |
| 921 | + cipher_info->block_size |
| 922 | - transform->maclen % cipher_info->block_size; |
| 923 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 924 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 925 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
| 926 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 927 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 928 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 929 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 930 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 931 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 932 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 933 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 934 | { |
| 935 | transform->minlen += transform->ivlen; |
| 936 | } |
| 937 | else |
| 938 | #endif |
| 939 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 940 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 941 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 942 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 943 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 946 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 947 | transform->keylen, transform->minlen, transform->ivlen, |
| 948 | transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 949 | |
| 950 | /* |
| 951 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 952 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 953 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 954 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 955 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 956 | key1 = keyblk + mac_key_len * 2; |
| 957 | key2 = keyblk + mac_key_len * 2 + transform->keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 958 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 959 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 960 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 961 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 962 | /* |
| 963 | * This is not used in TLS v1.1. |
| 964 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 965 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 966 | transform->fixed_ivlen : transform->ivlen; |
| 967 | memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len ); |
| 968 | memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 969 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 970 | } |
| 971 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 972 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 973 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 974 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 975 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 976 | key1 = keyblk + mac_key_len * 2 + transform->keylen; |
| 977 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 978 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 979 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 980 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 981 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 982 | /* |
| 983 | * This is not used in TLS v1.1. |
| 984 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 985 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 986 | transform->fixed_ivlen : transform->ivlen; |
| 987 | memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len ); |
| 988 | memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 989 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 990 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 991 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 993 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 994 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 995 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 996 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 997 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 999 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1000 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1001 | if( mac_key_len > sizeof transform->mac_enc ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1002 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1003 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1004 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1007 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1008 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1009 | } |
| 1010 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1012 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1013 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1014 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1015 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1016 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1017 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1018 | if( mac_key_len != 0 ) |
| 1019 | { |
| 1020 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1021 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1022 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1023 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1024 | else |
| 1025 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1027 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1028 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1029 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1030 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1031 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1032 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1033 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1034 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1035 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1036 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1037 | transform->iv_enc, transform->iv_dec, |
| 1038 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1039 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1040 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1041 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1042 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
| 1043 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1046 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1047 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1048 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1049 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1050 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1051 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
| 1052 | session->master, keyblk, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1053 | mac_key_len, transform->keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1054 | iv_copy_len ); |
| 1055 | } |
| 1056 | #endif |
| 1057 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1058 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1059 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1060 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1061 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1062 | return( ret ); |
| 1063 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1064 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1065 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1066 | cipher_info ) ) != 0 ) |
| 1067 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1068 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1069 | return( ret ); |
| 1070 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1071 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1072 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1073 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1077 | return( ret ); |
| 1078 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1079 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1080 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1081 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1083 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1085 | return( ret ); |
| 1086 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1087 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1088 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1089 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1090 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1092 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1093 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1094 | 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] | 1095 | return( ret ); |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1096 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1097 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1099 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1100 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1101 | 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] | 1102 | return( ret ); |
| 1103 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1104 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1106 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1107 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1108 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1109 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1110 | // Initialize compression |
| 1111 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1112 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1113 | { |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1114 | if( ssl->compress_buf == NULL ) |
| 1115 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1116 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1117 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1118 | if( ssl->compress_buf == NULL ) |
| 1119 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1120 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1121 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1122 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1126 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1127 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1128 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1129 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1130 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1131 | if( deflateInit( &transform->ctx_deflate, |
| 1132 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1133 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1134 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1135 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
| 1136 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1141 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1142 | |
| 1143 | return( 0 ); |
| 1144 | } |
| 1145 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1146 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1147 | void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1148 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1149 | mbedtls_md5_context md5; |
| 1150 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1151 | unsigned char pad_1[48]; |
| 1152 | unsigned char pad_2[48]; |
| 1153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1154 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1155 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1156 | mbedtls_md5_init( &md5 ); |
| 1157 | mbedtls_sha1_init( &sha1 ); |
| 1158 | |
| 1159 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1160 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1161 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1162 | memset( pad_1, 0x36, 48 ); |
| 1163 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1164 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1165 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1166 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1167 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1168 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1169 | mbedtls_md5_starts_ret( &md5 ); |
| 1170 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1171 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1172 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1173 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1174 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1175 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1176 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1177 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1178 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1179 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1180 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1181 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1182 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1183 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1184 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1185 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); |
| 1186 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1187 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1188 | mbedtls_md5_free( &md5 ); |
| 1189 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1190 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1191 | return; |
| 1192 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1193 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1195 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 1196 | void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1197 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1198 | mbedtls_md5_context md5; |
| 1199 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1201 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1202 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1203 | mbedtls_md5_init( &md5 ); |
| 1204 | mbedtls_sha1_init( &sha1 ); |
| 1205 | |
| 1206 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1207 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1208 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1209 | mbedtls_md5_finish_ret( &md5, hash ); |
| 1210 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); |
| 1213 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1214 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1215 | mbedtls_md5_free( &md5 ); |
| 1216 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1217 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1218 | return; |
| 1219 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1220 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1222 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1223 | #if defined(MBEDTLS_SHA256_C) |
| 1224 | void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1225 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1226 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1227 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1228 | mbedtls_sha256_init( &sha256 ); |
| 1229 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1230 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1231 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1232 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1233 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1235 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 ); |
| 1236 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1237 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1238 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1239 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1240 | return; |
| 1241 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1242 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1243 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1244 | #if defined(MBEDTLS_SHA512_C) |
| 1245 | void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1246 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1247 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1248 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1249 | mbedtls_sha512_init( &sha512 ); |
| 1250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1251 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1252 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1253 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1254 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1255 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1256 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 ); |
| 1257 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1258 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1259 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1260 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1261 | return; |
| 1262 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1263 | #endif /* MBEDTLS_SHA512_C */ |
| 1264 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1267 | 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] | 1268 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1269 | unsigned char *p = ssl->handshake->premaster; |
| 1270 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1271 | const unsigned char *psk = ssl->conf->psk; |
| 1272 | size_t psk_len = ssl->conf->psk_len; |
| 1273 | |
| 1274 | /* If the psk callback was called, use its result */ |
| 1275 | if( ssl->handshake->psk != NULL ) |
| 1276 | { |
| 1277 | psk = ssl->handshake->psk; |
| 1278 | psk_len = ssl->handshake->psk_len; |
| 1279 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1280 | |
| 1281 | /* |
| 1282 | * PMS = struct { |
| 1283 | * opaque other_secret<0..2^16-1>; |
| 1284 | * opaque psk<0..2^16-1>; |
| 1285 | * }; |
| 1286 | * with "other_secret" depending on the particular key exchange |
| 1287 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1288 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1289 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1290 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1291 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1292 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1293 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1294 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1295 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1296 | |
| 1297 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1298 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1299 | |
| 1300 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1301 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1302 | } |
| 1303 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1304 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1305 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1306 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1307 | { |
| 1308 | /* |
| 1309 | * other_secret already set by the ClientKeyExchange message, |
| 1310 | * and is 48 bytes long |
| 1311 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1312 | if( end - p < 2 ) |
| 1313 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1314 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1315 | *p++ = 0; |
| 1316 | *p++ = 48; |
| 1317 | p += 48; |
| 1318 | } |
| 1319 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1320 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1321 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1322 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1323 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1324 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1325 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1326 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1327 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1328 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1329 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1330 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1331 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1332 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1333 | return( ret ); |
| 1334 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1335 | *(p++) = (unsigned char)( len >> 8 ); |
| 1336 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1337 | p += len; |
| 1338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1339 | 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] | 1340 | } |
| 1341 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1342 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1343 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1344 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1345 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1346 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1347 | size_t zlen; |
| 1348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1349 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1350 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1351 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1352 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1353 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1354 | return( ret ); |
| 1355 | } |
| 1356 | |
| 1357 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 1358 | *(p++) = (unsigned char)( zlen ); |
| 1359 | p += zlen; |
| 1360 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 1361 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1362 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1363 | } |
| 1364 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1367 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1368 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1372 | if( end - p < 2 ) |
| 1373 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1374 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1375 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1376 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1377 | |
| 1378 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1379 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1380 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1381 | memcpy( p, psk, psk_len ); |
| 1382 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1383 | |
| 1384 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1385 | |
| 1386 | return( 0 ); |
| 1387 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1388 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1390 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1391 | /* |
| 1392 | * SSLv3.0 MAC functions |
| 1393 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1394 | #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] | 1395 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 1396 | const unsigned char *secret, |
| 1397 | const unsigned char *buf, size_t len, |
| 1398 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1399 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1400 | { |
| 1401 | unsigned char header[11]; |
| 1402 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1403 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1404 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 1405 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1406 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1407 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1408 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1409 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1410 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1411 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1412 | |
| 1413 | memcpy( header, ctr, 8 ); |
| 1414 | header[ 8] = (unsigned char) type; |
| 1415 | header[ 9] = (unsigned char)( len >> 8 ); |
| 1416 | header[10] = (unsigned char)( len ); |
| 1417 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1418 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1419 | mbedtls_md_starts( md_ctx ); |
| 1420 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1421 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 1422 | mbedtls_md_update( md_ctx, header, 11 ); |
| 1423 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1424 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1425 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1426 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1427 | mbedtls_md_starts( md_ctx ); |
| 1428 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1429 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1430 | mbedtls_md_update( md_ctx, out, md_size ); |
| 1431 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1432 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1433 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1435 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \ |
Manuel Pégourié-Gonnard | a60d0f2 | 2020-07-28 09:55:33 +0200 | [diff] [blame] | 1436 | defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1437 | #define SSL_SOME_MODES_USE_MAC |
Manuel Pégourié-Gonnard | 8e4b337 | 2014-11-17 15:06:13 +0100 | [diff] [blame] | 1438 | #endif |
| 1439 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1440 | /* The function below is only used in the Lucky 13 counter-measure in |
| 1441 | * ssl_decrypt_buf(). These are the defines that guard the call site. */ |
| 1442 | #if defined(SSL_SOME_MODES_USE_MAC) && \ |
| 1443 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 1444 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1445 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 1446 | /* This function makes sure every byte in the memory region is accessed |
| 1447 | * (in ascending addresses order) */ |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1448 | static void ssl_read_memory( const unsigned char *p, size_t len ) |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1449 | { |
| 1450 | unsigned char acc = 0; |
| 1451 | volatile unsigned char force; |
| 1452 | |
| 1453 | for( ; len != 0; p++, len-- ) |
| 1454 | acc ^= *p; |
| 1455 | |
| 1456 | force = acc; |
| 1457 | (void) force; |
| 1458 | } |
| 1459 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 1460 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1461 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1462 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 1463 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1464 | static int ssl_encrypt_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1465 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1466 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1467 | int auth_done = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1469 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1470 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1471 | if( ssl->session_out == NULL || ssl->transform_out == NULL ) |
| 1472 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1473 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1474 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1475 | } |
| 1476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1477 | mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1479 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 1480 | ssl->out_msg, ssl->out_msglen ); |
| 1481 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1482 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1483 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1484 | */ |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1485 | #if defined(SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1486 | if( mode == MBEDTLS_MODE_STREAM || |
| 1487 | ( mode == MBEDTLS_MODE_CBC |
| 1488 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1489 | && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1490 | #endif |
| 1491 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1492 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1493 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1494 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1495 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1496 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1497 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1498 | ssl_mac( &ssl->transform_out->md_ctx_enc, |
| 1499 | ssl->transform_out->mac_enc, |
| 1500 | ssl->out_msg, ssl->out_msglen, |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1501 | ssl->out_ctr, ssl->out_msgtype, |
| 1502 | mac ); |
| 1503 | |
| 1504 | memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1505 | } |
| 1506 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1507 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1508 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1509 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1510 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1511 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1512 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 1513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1514 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 ); |
| 1515 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 ); |
| 1516 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 ); |
| 1517 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1518 | ssl->out_msg, ssl->out_msglen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1519 | mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1520 | mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1521 | |
| 1522 | memcpy( ssl->out_msg + ssl->out_msglen, mac, ssl->transform_out->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1523 | } |
| 1524 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1525 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1526 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1527 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1528 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1531 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1532 | ssl->out_msg + ssl->out_msglen, |
| 1533 | ssl->transform_out->maclen ); |
| 1534 | |
| 1535 | ssl->out_msglen += ssl->transform_out->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1536 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1537 | } |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1538 | #endif /* AEAD not the only option */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1539 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1540 | /* |
| 1541 | * Encrypt |
| 1542 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 1544 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1545 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1546 | int ret; |
| 1547 | size_t olen = 0; |
| 1548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1550 | "including %d bytes of padding", |
| 1551 | ssl->out_msglen, 0 ) ); |
| 1552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc, |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1554 | ssl->transform_out->iv_enc, |
Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1555 | ssl->transform_out->ivlen, |
| 1556 | ssl->out_msg, ssl->out_msglen, |
| 1557 | ssl->out_msg, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1560 | return( ret ); |
| 1561 | } |
| 1562 | |
| 1563 | if( ssl->out_msglen != olen ) |
| 1564 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1565 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1566 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1567 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1568 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1569 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1570 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1571 | #if defined(MBEDTLS_GCM_C) || \ |
| 1572 | defined(MBEDTLS_CCM_C) || \ |
| 1573 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1574 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1575 | mode == MBEDTLS_MODE_CCM || |
| 1576 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1577 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1578 | int ret; |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1579 | size_t enc_msglen, olen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1580 | unsigned char *enc_msg; |
| 1581 | unsigned char add_data[13]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1582 | unsigned char iv[12]; |
| 1583 | mbedtls_ssl_transform *transform = ssl->transform_out; |
| 1584 | unsigned char taglen = transform->ciphersuite_info->flags & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1585 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1586 | size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1587 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1588 | /* |
| 1589 | * Prepare additional authenticated data |
| 1590 | */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1591 | memcpy( add_data, ssl->out_ctr, 8 ); |
| 1592 | add_data[8] = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1593 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1594 | ssl->conf->transport, add_data + 9 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1595 | add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF; |
| 1596 | add_data[12] = ssl->out_msglen & 0xFF; |
| 1597 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1598 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1599 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1600 | /* |
| 1601 | * Generate IV |
| 1602 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1603 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 1604 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 1605 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1606 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 1607 | memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 ); |
| 1608 | memcpy( ssl->out_iv, ssl->out_ctr, 8 ); |
| 1609 | |
| 1610 | } |
| 1611 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 1612 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 1613 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1614 | unsigned char i; |
| 1615 | |
| 1616 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 1617 | |
| 1618 | for( i = 0; i < 8; i++ ) |
| 1619 | iv[i+4] ^= ssl->out_ctr[i]; |
| 1620 | } |
| 1621 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 1622 | { |
| 1623 | /* 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] | 1624 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1625 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1628 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 1629 | iv, transform->ivlen ); |
| 1630 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
| 1631 | ssl->out_iv, explicit_ivlen ); |
Manuel Pégourié-Gonnard | 226d5da | 2013-09-05 13:19:22 +0200 | [diff] [blame] | 1632 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1633 | /* |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1634 | * Fix message length with added IV |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1635 | */ |
| 1636 | enc_msg = ssl->out_msg; |
| 1637 | enc_msglen = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1638 | ssl->out_msglen += explicit_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1641 | "including 0 bytes of padding", |
| 1642 | ssl->out_msglen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1643 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1644 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1645 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1646 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1647 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
| 1648 | iv, transform->ivlen, |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1649 | add_data, 13, |
| 1650 | enc_msg, enc_msglen, |
| 1651 | enc_msg, &olen, |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1652 | enc_msg + enc_msglen, taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1653 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1654 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1655 | return( ret ); |
| 1656 | } |
| 1657 | |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1658 | if( olen != enc_msglen ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1660 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1661 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1662 | } |
| 1663 | |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1664 | ssl->out_msglen += taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1665 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1667 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1668 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1669 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1670 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
Manuel Pégourié-Gonnard | a60d0f2 | 2020-07-28 09:55:33 +0200 | [diff] [blame] | 1671 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1672 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1673 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1674 | int ret; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1675 | unsigned char *enc_msg; |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 1676 | size_t enc_msglen, padlen, olen = 0, i; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1677 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1678 | padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) % |
| 1679 | ssl->transform_out->ivlen; |
| 1680 | if( padlen == ssl->transform_out->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1681 | padlen = 0; |
| 1682 | |
| 1683 | for( i = 0; i <= padlen; i++ ) |
| 1684 | ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen; |
| 1685 | |
| 1686 | ssl->out_msglen += padlen + 1; |
| 1687 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1688 | enc_msglen = ssl->out_msglen; |
| 1689 | enc_msg = ssl->out_msg; |
| 1690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1691 | #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] | 1692 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1693 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 1694 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1695 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1696 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1697 | { |
| 1698 | /* |
| 1699 | * Generate IV |
| 1700 | */ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 1701 | ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1702 | ssl->transform_out->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1703 | if( ret != 0 ) |
| 1704 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1705 | |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 1706 | memcpy( ssl->out_iv, ssl->transform_out->iv_enc, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1707 | ssl->transform_out->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1708 | |
| 1709 | /* |
| 1710 | * Fix pointer positions and message length with added IV |
| 1711 | */ |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 1712 | enc_msg = ssl->out_msg; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1713 | enc_msglen = ssl->out_msglen; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1714 | ssl->out_msglen += ssl->transform_out->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1715 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1716 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1718 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1719 | "including %d bytes of IV and %d bytes of padding", |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1720 | ssl->out_msglen, ssl->transform_out->ivlen, |
| 1721 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1723 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc, |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1724 | ssl->transform_out->iv_enc, |
Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1725 | ssl->transform_out->ivlen, |
| 1726 | enc_msg, enc_msglen, |
| 1727 | enc_msg, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1728 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1729 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1730 | return( ret ); |
| 1731 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1732 | |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1733 | if( enc_msglen != olen ) |
| 1734 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1735 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1736 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1737 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1738 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1739 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
| 1740 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1741 | { |
| 1742 | /* |
| 1743 | * Save IV in SSL3 and TLS1 |
| 1744 | */ |
| 1745 | memcpy( ssl->transform_out->iv_enc, |
| 1746 | ssl->transform_out->cipher_ctx_enc.iv, |
| 1747 | ssl->transform_out->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1748 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1749 | #endif |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1751 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1752 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1753 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1754 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 1755 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1756 | /* |
| 1757 | * MAC(MAC_write_key, seq_num + |
| 1758 | * TLSCipherText.type + |
| 1759 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1760 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1761 | * IV + // except for TLS 1.0 |
| 1762 | * ENC(content + padding + padding_length)); |
| 1763 | */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1764 | unsigned char pseudo_hdr[13]; |
| 1765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1766 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1767 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1768 | memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 ); |
| 1769 | memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1770 | pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF ); |
| 1771 | pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF ); |
| 1772 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1773 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1774 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1775 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 ); |
| 1776 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1777 | ssl->out_iv, ssl->out_msglen ); |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1778 | mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, mac ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1779 | mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1780 | |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1781 | memcpy( ssl->out_iv + ssl->out_msglen, mac, |
| 1782 | ssl->transform_out->maclen ); |
| 1783 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1784 | ssl->out_msglen += ssl->transform_out->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1785 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1786 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1787 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1788 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1789 | else |
Manuel Pégourié-Gonnard | a60d0f2 | 2020-07-28 09:55:33 +0200 | [diff] [blame] | 1790 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1791 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1792 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1793 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1794 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1795 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1796 | /* Make extra sure authentication was performed, exactly once */ |
| 1797 | if( auth_done != 1 ) |
| 1798 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1799 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1800 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1803 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1804 | |
| 1805 | return( 0 ); |
| 1806 | } |
| 1807 | |
Manuel Pégourié-Gonnard | 1e94128 | 2020-07-28 11:35:39 +0200 | [diff] [blame] | 1808 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 1809 | /* |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1810 | * Constant-flow conditional memcpy: |
| 1811 | * - if c1 == c2, equivalent to memcpy(dst, src, len), |
| 1812 | * - otherwise, a no-op, |
| 1813 | * but with execution flow independent of the values of c1 and c2. |
| 1814 | * |
| 1815 | * Use only bit operations to avoid branches that could be used by some |
| 1816 | * compilers on some platforms to translate comparison operators. |
| 1817 | */ |
Manuel Pégourié-Gonnard | 94fd8dc | 2020-07-28 11:56:05 +0200 | [diff] [blame^] | 1818 | static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst, |
| 1819 | const unsigned char *src, |
| 1820 | size_t len, |
| 1821 | size_t c1, size_t c2 ) |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1822 | { |
| 1823 | /* diff = 0 if c1 == c2, non-zero otherwise */ |
| 1824 | const size_t diff = c1 ^ c2; |
| 1825 | |
| 1826 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 1827 | * but this is well-defined and precisely what we want to do here. */ |
| 1828 | #if defined(_MSC_VER) |
| 1829 | #pragma warning( push ) |
| 1830 | #pragma warning( disable : 4146 ) |
| 1831 | #endif |
| 1832 | |
| 1833 | /* diff_msb's most significant bit is bit equal to c1 != c2 */ |
| 1834 | const size_t diff_msb = ( diff | -diff ); |
| 1835 | |
| 1836 | /* diff1 = c1 != c2 */ |
| 1837 | const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 ); |
| 1838 | |
| 1839 | /* mask = c1 != c2 ? 0xff : 0x00 */ |
| 1840 | unsigned char mask = (unsigned char) -diff1; |
| 1841 | |
| 1842 | #if defined(_MSC_VER) |
| 1843 | #pragma warning( pop ) |
| 1844 | #endif |
| 1845 | |
| 1846 | /* dst[i] = c1 != c2 ? dst[i] : src[i] */ |
| 1847 | for( size_t i = 0; i < len; i++ ) |
| 1848 | dst[i] = ( dst[i] & mask ) | ( src[i] & ~mask ); |
| 1849 | } |
| 1850 | |
| 1851 | /* |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 1852 | * Compute HMAC of variable-length data with constant flow. |
| 1853 | */ |
| 1854 | int mbedtls_ssl_cf_hmac( |
| 1855 | mbedtls_md_context_t *ctx, |
| 1856 | const unsigned char *add_data, size_t add_data_len, |
| 1857 | const unsigned char *data, size_t data_len_secret, |
| 1858 | size_t min_data_len, size_t max_data_len, |
| 1859 | unsigned char *output ) |
| 1860 | { |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1861 | /* |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1862 | * This function breaks the HMAC abstraction and uses the md_clone() |
| 1863 | * extension to the MD API in order to get constant-flow behaviour. |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1864 | * |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1865 | * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means |
Manuel Pégourié-Gonnard | 74503bb | 2020-07-28 11:42:31 +0200 | [diff] [blame] | 1866 | * concatenation, and okey/ikey are the XOR of the key with some fixed bit |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1867 | * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx. |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1868 | * |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1869 | * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to |
| 1870 | * minlen, then cloning the context, and for each byte up to maxlen |
| 1871 | * finishing up the hash computation, keeping only the correct result. |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1872 | * |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1873 | * Then we only need to compute HASH(okey + inner_hash) and we're done. |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1874 | */ |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1875 | const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info ); |
Manuel Pégourié-Gonnard | 74503bb | 2020-07-28 11:42:31 +0200 | [diff] [blame] | 1876 | /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5, |
| 1877 | * all of which have the same block size except SHA-384. */ |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1878 | const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64; |
Manuel Pégourié-Gonnard | a6c1317 | 2020-07-28 11:45:02 +0200 | [diff] [blame] | 1879 | const unsigned char * const ikey = ctx->hmac_ctx; |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1880 | const unsigned char * const okey = ikey + block_size; |
| 1881 | const size_t hash_size = mbedtls_md_get_size( ctx->md_info ); |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1882 | |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1883 | unsigned char aux_out[MBEDTLS_MD_MAX_SIZE]; |
| 1884 | mbedtls_md_context_t aux; |
| 1885 | size_t offset; |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1886 | int ret; |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1887 | |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1888 | mbedtls_md_init( &aux ); |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1889 | |
| 1890 | #define MD_CHK( func_call ) \ |
| 1891 | do { \ |
| 1892 | ret = (func_call); \ |
| 1893 | if( ret != 0 ) \ |
| 1894 | goto cleanup; \ |
| 1895 | } while( 0 ) |
| 1896 | |
| 1897 | MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) ); |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1898 | |
| 1899 | /* After hmac_start() of hmac_reset(), ikey has already been hashed, |
| 1900 | * so we can start directly with the message */ |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1901 | MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) ); |
| 1902 | MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) ); |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1903 | |
| 1904 | /* For each possible length, compute the hash up to that point */ |
| 1905 | for( offset = min_data_len; offset <= max_data_len; offset++ ) |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1906 | { |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1907 | MD_CHK( mbedtls_md_clone( &aux, ctx ) ); |
| 1908 | MD_CHK( mbedtls_md_finish( &aux, aux_out ) ); |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1909 | /* Keep only the correct inner_hash in the output buffer */ |
| 1910 | mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size, |
| 1911 | offset, data_len_secret ); |
| 1912 | |
| 1913 | if( offset < max_data_len ) |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1914 | MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) ); |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1915 | } |
| 1916 | |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1917 | /* Now compute HASH(okey + inner_hash) */ |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1918 | MD_CHK( mbedtls_md_starts( ctx ) ); |
| 1919 | MD_CHK( mbedtls_md_update( ctx, okey, block_size ) ); |
| 1920 | MD_CHK( mbedtls_md_update( ctx, output, hash_size ) ); |
| 1921 | MD_CHK( mbedtls_md_finish( ctx, output ) ); |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 1922 | |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1923 | /* Done, get ready for next time */ |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1924 | MD_CHK( mbedtls_md_hmac_reset( ctx ) ); |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 1925 | |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1926 | #undef MD_CHK |
| 1927 | |
| 1928 | cleanup: |
Manuel Pégourié-Gonnard | de02b58 | 2020-07-28 11:25:34 +0200 | [diff] [blame] | 1929 | mbedtls_md_free( &aux ); |
Manuel Pégourié-Gonnard | 5bb6f3c | 2020-07-28 11:49:42 +0200 | [diff] [blame] | 1930 | return( ret ); |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 1931 | } |
Manuel Pégourié-Gonnard | 1e94128 | 2020-07-28 11:35:39 +0200 | [diff] [blame] | 1932 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */ |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 1933 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1934 | static int ssl_decrypt_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1935 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1936 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1937 | int auth_done = 0; |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1938 | #if defined(SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1939 | size_t padlen = 0, correct = 1; |
| 1940 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1941 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1942 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1943 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1944 | if( ssl->session_in == NULL || ssl->transform_in == NULL ) |
| 1945 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1946 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1947 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1950 | mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1951 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1952 | if( ssl->in_msglen < ssl->transform_in->minlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1953 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1954 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1955 | ssl->in_msglen, ssl->transform_in->minlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1956 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1959 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 1960 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1961 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1962 | int ret; |
| 1963 | size_t olen = 0; |
| 1964 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1965 | padlen = 0; |
| 1966 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1967 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec, |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1968 | ssl->transform_in->iv_dec, |
Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1969 | ssl->transform_in->ivlen, |
| 1970 | ssl->in_msg, ssl->in_msglen, |
| 1971 | ssl->in_msg, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1972 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1973 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1974 | return( ret ); |
| 1975 | } |
| 1976 | |
| 1977 | if( ssl->in_msglen != olen ) |
| 1978 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1979 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1980 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1981 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1982 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1983 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1984 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1985 | #if defined(MBEDTLS_GCM_C) || \ |
| 1986 | defined(MBEDTLS_CCM_C) || \ |
| 1987 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1988 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1989 | mode == MBEDTLS_MODE_CCM || |
| 1990 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1991 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1992 | int ret; |
| 1993 | size_t dec_msglen, olen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1994 | unsigned char *dec_msg; |
| 1995 | unsigned char *dec_msg_result; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1996 | unsigned char add_data[13]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1997 | unsigned char iv[12]; |
| 1998 | mbedtls_ssl_transform *transform = ssl->transform_in; |
| 1999 | unsigned char taglen = transform->ciphersuite_info->flags & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2000 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2001 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2002 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2003 | /* |
| 2004 | * Compute and update sizes |
| 2005 | */ |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 2006 | if( ssl->in_msglen < explicit_iv_len + taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2007 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2008 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2009 | "+ taglen (%d)", ssl->in_msglen, |
| 2010 | explicit_iv_len, taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2011 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2012 | } |
| 2013 | dec_msglen = ssl->in_msglen - explicit_iv_len - taglen; |
| 2014 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2015 | dec_msg = ssl->in_msg; |
| 2016 | dec_msg_result = ssl->in_msg; |
| 2017 | ssl->in_msglen = dec_msglen; |
| 2018 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2019 | /* |
| 2020 | * Prepare additional authenticated data |
| 2021 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2022 | memcpy( add_data, ssl->in_ctr, 8 ); |
| 2023 | add_data[8] = ssl->in_msgtype; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2024 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2025 | ssl->conf->transport, add_data + 9 ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2026 | add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF; |
| 2027 | add_data[12] = ssl->in_msglen & 0xFF; |
| 2028 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2029 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2030 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2031 | /* |
| 2032 | * Prepare IV |
| 2033 | */ |
| 2034 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2035 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2036 | /* GCM and CCM: fixed || explicit (transmitted) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2037 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2038 | memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2039 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2040 | } |
| 2041 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2042 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2043 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2044 | unsigned char i; |
| 2045 | |
| 2046 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2047 | |
| 2048 | for( i = 0; i < 8; i++ ) |
| 2049 | iv[i+4] ^= ssl->in_ctr[i]; |
| 2050 | } |
| 2051 | else |
| 2052 | { |
| 2053 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2054 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2055 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2056 | } |
| 2057 | |
| 2058 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2059 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2060 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2061 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2062 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2063 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2064 | if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2065 | iv, transform->ivlen, |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2066 | add_data, 13, |
| 2067 | dec_msg, dec_msglen, |
| 2068 | dec_msg_result, &olen, |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2069 | dec_msg + dec_msglen, taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2070 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2071 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2073 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2074 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2075 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2076 | return( ret ); |
| 2077 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2078 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2079 | |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2080 | if( olen != dec_msglen ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2081 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2082 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2083 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2084 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2085 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2086 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2087 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
Manuel Pégourié-Gonnard | a60d0f2 | 2020-07-28 09:55:33 +0200 | [diff] [blame] | 2088 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2089 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2090 | { |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2091 | /* |
| 2092 | * Decrypt and check the padding |
| 2093 | */ |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2094 | int ret; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2095 | unsigned char *dec_msg; |
| 2096 | unsigned char *dec_msg_result; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2097 | size_t dec_msglen; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2098 | size_t minlen = 0; |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2099 | size_t olen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2100 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2101 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2102 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2103 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2104 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2105 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2106 | minlen += ssl->transform_in->ivlen; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2107 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2108 | |
| 2109 | if( ssl->in_msglen < minlen + ssl->transform_in->ivlen || |
| 2110 | ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 ) |
| 2111 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2112 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2113 | "+ 1 ) ( + expl IV )", ssl->in_msglen, |
| 2114 | ssl->transform_in->ivlen, |
| 2115 | ssl->transform_in->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2116 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2119 | dec_msglen = ssl->in_msglen; |
| 2120 | dec_msg = ssl->in_msg; |
| 2121 | dec_msg_result = ssl->in_msg; |
| 2122 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2123 | /* |
| 2124 | * Authenticate before decrypt if enabled |
| 2125 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2126 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 2127 | if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2128 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2129 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2130 | unsigned char pseudo_hdr[13]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2132 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2133 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2134 | dec_msglen -= ssl->transform_in->maclen; |
| 2135 | ssl->in_msglen -= ssl->transform_in->maclen; |
| 2136 | |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2137 | memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 ); |
| 2138 | memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 ); |
| 2139 | pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF ); |
| 2140 | pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF ); |
| 2141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2142 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2144 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 ); |
| 2145 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2146 | ssl->in_iv, ssl->in_msglen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2147 | mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2148 | mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2150 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2151 | ssl->transform_in->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2152 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2153 | ssl->transform_in->maclen ); |
| 2154 | |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2155 | if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect, |
| 2156 | ssl->transform_in->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2157 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2158 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2160 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2161 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2162 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2163 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2164 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2165 | |
| 2166 | /* |
| 2167 | * Check length sanity |
| 2168 | */ |
| 2169 | if( ssl->in_msglen % ssl->transform_in->ivlen != 0 ) |
| 2170 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2171 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2172 | ssl->in_msglen, ssl->transform_in->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2173 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2174 | } |
| 2175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2176 | #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] | 2177 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2178 | * 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] | 2179 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2180 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2181 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2182 | unsigned char i; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2183 | dec_msglen -= ssl->transform_in->ivlen; |
| 2184 | ssl->in_msglen -= ssl->transform_in->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2185 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2186 | for( i = 0; i < ssl->transform_in->ivlen; i++ ) |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 2187 | ssl->transform_in->iv_dec[i] = ssl->in_iv[i]; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2188 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2189 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2190 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2191 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec, |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2192 | ssl->transform_in->iv_dec, |
Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 2193 | ssl->transform_in->ivlen, |
| 2194 | dec_msg, dec_msglen, |
| 2195 | dec_msg_result, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2197 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2198 | return( ret ); |
| 2199 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2200 | |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2201 | if( dec_msglen != olen ) |
| 2202 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2203 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2204 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2205 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2207 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
| 2208 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2209 | { |
| 2210 | /* |
| 2211 | * Save IV in SSL3 and TLS1 |
| 2212 | */ |
| 2213 | memcpy( ssl->transform_in->iv_dec, |
| 2214 | ssl->transform_in->cipher_ctx_dec.iv, |
| 2215 | ssl->transform_in->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2216 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2217 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2218 | |
| 2219 | padlen = 1 + ssl->in_msg[ssl->in_msglen - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2220 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2221 | if( ssl->in_msglen < ssl->transform_in->maclen + padlen && |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2222 | auth_done == 0 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2223 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2224 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2225 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2226 | ssl->in_msglen, ssl->transform_in->maclen, padlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2227 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2228 | padlen = 0; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2229 | correct = 0; |
| 2230 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2231 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2232 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2233 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2234 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2235 | if( padlen > ssl->transform_in->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2236 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2237 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2238 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2239 | "should be no more than %d", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2240 | padlen, ssl->transform_in->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2241 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2242 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2246 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2247 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2248 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2249 | if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2250 | { |
| 2251 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2252 | * TLSv1+: always check the padding up to the first failure |
| 2253 | * and fake check up to 256 bytes of padding |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2254 | */ |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2255 | size_t pad_count = 0, real_count = 1; |
Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2256 | size_t padding_idx = ssl->in_msglen - padlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2257 | size_t i; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2258 | |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2259 | /* |
| 2260 | * Padding is guaranteed to be incorrect if: |
Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2261 | * 1. padlen > ssl->in_msglen |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2262 | * |
Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2263 | * 2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN + |
Paul Bakker | 61885c7 | 2014-04-25 12:59:03 +0200 | [diff] [blame] | 2264 | * ssl->transform_in->maclen |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2265 | * |
| 2266 | * In both cases we reset padding_idx to a safe value (0) to |
| 2267 | * prevent out-of-buffer reads. |
| 2268 | */ |
Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2269 | correct &= ( padlen <= ssl->in_msglen ); |
| 2270 | correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN + |
Paul Bakker | 61885c7 | 2014-04-25 12:59:03 +0200 | [diff] [blame] | 2271 | ssl->transform_in->maclen ); |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2272 | |
| 2273 | padding_idx *= correct; |
| 2274 | |
Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2275 | for( i = 0; i < 256; i++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2276 | { |
Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2277 | real_count &= ( i < padlen ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2278 | pad_count += real_count * |
| 2279 | ( ssl->in_msg[padding_idx + i] == padlen - 1 ); |
| 2280 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2281 | |
| 2282 | correct &= ( pad_count == padlen ); /* Only 1 on correct padding */ |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2284 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2285 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2286 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2287 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2288 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2289 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2290 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2291 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2292 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2293 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2294 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2295 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2296 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2297 | |
| 2298 | ssl->in_msglen -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2299 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2300 | else |
Manuel Pégourié-Gonnard | a60d0f2 | 2020-07-28 09:55:33 +0200 | [diff] [blame] | 2301 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2302 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2303 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2304 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2305 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2306 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2307 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2308 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2309 | ssl->in_msg, ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2310 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2311 | |
| 2312 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2313 | * Authenticate if not done yet. |
| 2314 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2315 | */ |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 2316 | #if defined(SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2317 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2318 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2319 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2320 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2321 | ssl->in_msglen -= ssl->transform_in->maclen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2322 | |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2323 | ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 ); |
| 2324 | ssl->in_len[1] = (unsigned char)( ssl->in_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2326 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 2327 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2328 | { |
| 2329 | ssl_mac( &ssl->transform_in->md_ctx_dec, |
| 2330 | ssl->transform_in->mac_dec, |
| 2331 | ssl->in_msg, ssl->in_msglen, |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2332 | ssl->in_ctr, ssl->in_msgtype, |
| 2333 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2334 | } |
| 2335 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2336 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2337 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2338 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2339 | if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2340 | { |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 2341 | int ret; |
| 2342 | unsigned char add_data[13]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2343 | |
| 2344 | /* |
| 2345 | * The next two sizes are the minimum and maximum values of |
| 2346 | * in_msglen over all padlen values. |
| 2347 | * |
| 2348 | * They're independent of padlen, since we previously did |
| 2349 | * in_msglen -= padlen. |
| 2350 | * |
| 2351 | * Note that max_len + maclen is never more than the buffer |
| 2352 | * length, as we previously did in_msglen -= maclen too. |
| 2353 | */ |
| 2354 | const size_t max_len = ssl->in_msglen + padlen; |
| 2355 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 2356 | |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 2357 | memcpy( add_data + 0, ssl->in_ctr, 8 ); |
| 2358 | memcpy( add_data + 8, ssl->in_hdr, 3 ); |
| 2359 | memcpy( add_data + 11, ssl->in_len, 2 ); |
| 2360 | |
| 2361 | ret = mbedtls_ssl_cf_hmac( &ssl->transform_in->md_ctx_dec, |
| 2362 | add_data, sizeof( add_data ), |
| 2363 | ssl->in_msg, ssl->in_msglen, |
| 2364 | min_len, max_len, |
| 2365 | mac_expect ); |
| 2366 | if( ret != 0 ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2367 | { |
Manuel Pégourié-Gonnard | 368fc65 | 2020-07-28 10:43:03 +0200 | [diff] [blame] | 2368 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret ); |
| 2369 | return( ret ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2370 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2371 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2372 | /* Make sure we access all the memory that could contain the MAC, |
| 2373 | * before we check it in the next code block. This makes the |
| 2374 | * synchronisation requirements for just-in-time Prime+Probe |
| 2375 | * attacks much tighter and hopefully impractical. */ |
| 2376 | ssl_read_memory( ssl->in_msg + min_len, |
| 2377 | max_len - min_len + ssl->transform_in->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2378 | } |
| 2379 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2380 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2381 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2383 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2384 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2385 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2386 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2387 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2388 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen ); |
| 2389 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_msg + ssl->in_msglen, |
| 2390 | ssl->transform_in->maclen ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2391 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2392 | |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2393 | if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect, |
| 2394 | ssl->transform_in->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2395 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2396 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2397 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2398 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2399 | correct = 0; |
| 2400 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2401 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2402 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 2403 | |
| 2404 | /* |
| 2405 | * Finally check the correct flag |
| 2406 | */ |
| 2407 | if( correct == 0 ) |
| 2408 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 2409 | #endif /* SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2410 | |
| 2411 | /* Make extra sure authentication was performed, exactly once */ |
| 2412 | if( auth_done != 1 ) |
| 2413 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2414 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2415 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2416 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2417 | |
| 2418 | if( ssl->in_msglen == 0 ) |
| 2419 | { |
Angus Gratton | 3481792 | 2018-06-19 15:58:22 +1000 | [diff] [blame] | 2420 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2421 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
| 2422 | && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 2423 | { |
| 2424 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 2425 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 2426 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 2427 | } |
| 2428 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2429 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2430 | ssl->nb_zero++; |
| 2431 | |
| 2432 | /* |
| 2433 | * Three or more empty messages may be a DoS attack |
| 2434 | * (excessive CPU consumption). |
| 2435 | */ |
| 2436 | if( ssl->nb_zero > 3 ) |
| 2437 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2438 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2439 | "messages, possible DoS attack" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2440 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | else |
| 2444 | ssl->nb_zero = 0; |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2446 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2447 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2448 | { |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 2449 | ; /* in_ctr read from peer, not maintained internally */ |
Manuel Pégourié-Gonnard | ea22ce5 | 2014-09-24 09:46:10 +0200 | [diff] [blame] | 2450 | } |
| 2451 | else |
| 2452 | #endif |
| 2453 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2454 | unsigned char i; |
Manuel Pégourié-Gonnard | ea22ce5 | 2014-09-24 09:46:10 +0200 | [diff] [blame] | 2455 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 2456 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 2457 | break; |
| 2458 | |
| 2459 | /* The loop goes to its end iff the counter is wrapping */ |
| 2460 | if( i == ssl_ep_len( ssl ) ) |
| 2461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 2463 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | ea22ce5 | 2014-09-24 09:46:10 +0200 | [diff] [blame] | 2464 | } |
Manuel Pégourié-Gonnard | 83cdffc | 2014-03-10 21:20:29 +0100 | [diff] [blame] | 2465 | } |
| 2466 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2467 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2468 | |
| 2469 | return( 0 ); |
| 2470 | } |
| 2471 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2472 | #undef MAC_NONE |
| 2473 | #undef MAC_PLAINTEXT |
| 2474 | #undef MAC_CIPHERTEXT |
| 2475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2476 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2477 | /* |
| 2478 | * Compression/decompression functions |
| 2479 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2480 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2481 | { |
| 2482 | int ret; |
| 2483 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 2484 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2485 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2486 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2488 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2489 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2490 | if( len_pre == 0 ) |
| 2491 | return( 0 ); |
| 2492 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2493 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 2494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2496 | ssl->out_msglen ) ); |
| 2497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2498 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2499 | ssl->out_msg, ssl->out_msglen ); |
| 2500 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2501 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 2502 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 2503 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2504 | 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] | 2505 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2506 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2507 | if( ret != Z_OK ) |
| 2508 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2509 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 2510 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2513 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 2514 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2516 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2517 | ssl->out_msglen ) ); |
| 2518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2519 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2520 | ssl->out_msg, ssl->out_msglen ); |
| 2521 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2522 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2523 | |
| 2524 | return( 0 ); |
| 2525 | } |
| 2526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2527 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2528 | { |
| 2529 | int ret; |
| 2530 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2531 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2532 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2533 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2535 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2536 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2537 | if( len_pre == 0 ) |
| 2538 | return( 0 ); |
| 2539 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2540 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 2541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2542 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2543 | ssl->in_msglen ) ); |
| 2544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2545 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2546 | ssl->in_msg, ssl->in_msglen ); |
| 2547 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2548 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 2549 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 2550 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2551 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2552 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2553 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2554 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2555 | if( ret != Z_OK ) |
| 2556 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2557 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 2558 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2561 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2562 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2564 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2565 | ssl->in_msglen ) ); |
| 2566 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2567 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2568 | ssl->in_msg, ssl->in_msglen ); |
| 2569 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2570 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2571 | |
| 2572 | return( 0 ); |
| 2573 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2574 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2576 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2577 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2579 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2580 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2581 | { |
| 2582 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 2583 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2584 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2585 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2586 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2587 | unsigned char doublings = 1; |
| 2588 | |
| 2589 | while( ratio != 0 ) |
| 2590 | { |
| 2591 | ++doublings; |
| 2592 | ratio >>= 1; |
| 2593 | } |
| 2594 | |
| 2595 | if( ++ssl->renego_records_seen > doublings ) |
| 2596 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2597 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2598 | return( 0 ); |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | return( ssl_write_hello_request( ssl ) ); |
| 2603 | } |
| 2604 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2605 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2606 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2607 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2608 | * Fill the input message buffer by appending data to it. |
| 2609 | * 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] | 2610 | * |
| 2611 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 2612 | * available (from this read and/or a previous one). Otherwise, an error code |
| 2613 | * is returned (possibly EOF or WANT_READ). |
| 2614 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2615 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 2616 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 2617 | * since we always read a whole datagram at once. |
| 2618 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 2619 | * 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] | 2620 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2621 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2622 | 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] | 2623 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2624 | int ret; |
| 2625 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2627 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2628 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2629 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 2630 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2631 | 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] | 2632 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2633 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2634 | } |
| 2635 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2636 | 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] | 2637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2638 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 2639 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 2640 | } |
| 2641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2642 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2643 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2644 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2645 | uint32_t timeout; |
| 2646 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 2647 | /* Just to be sure */ |
| 2648 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) |
| 2649 | { |
| 2650 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 2651 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 2652 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2653 | } |
| 2654 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2655 | /* |
| 2656 | * The point is, we need to always read a full datagram at once, so we |
| 2657 | * sometimes read more then requested, and handle the additional data. |
| 2658 | * It could be the rest of the current record (while fetching the |
| 2659 | * header) and/or some other records in the same datagram. |
| 2660 | */ |
| 2661 | |
| 2662 | /* |
| 2663 | * Move to the next record in the already read datagram if applicable |
| 2664 | */ |
| 2665 | if( ssl->next_record_offset != 0 ) |
| 2666 | { |
| 2667 | if( ssl->in_left < ssl->next_record_offset ) |
| 2668 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2669 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2670 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | ssl->in_left -= ssl->next_record_offset; |
| 2674 | |
| 2675 | if( ssl->in_left != 0 ) |
| 2676 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2677 | 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] | 2678 | ssl->next_record_offset ) ); |
| 2679 | memmove( ssl->in_hdr, |
| 2680 | ssl->in_hdr + ssl->next_record_offset, |
| 2681 | ssl->in_left ); |
| 2682 | } |
| 2683 | |
| 2684 | ssl->next_record_offset = 0; |
| 2685 | } |
| 2686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2687 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2688 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2689 | |
| 2690 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2691 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2692 | */ |
| 2693 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2694 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2695 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2696 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2697 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2698 | |
| 2699 | /* |
Antonin Décimo | d5f4759 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 2700 | * 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] | 2701 | * are not at the beginning of a new record, the caller did something |
| 2702 | * wrong. |
| 2703 | */ |
| 2704 | if( ssl->in_left != 0 ) |
| 2705 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2706 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2707 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2708 | } |
| 2709 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2710 | /* |
| 2711 | * Don't even try to read if time's out already. |
| 2712 | * This avoids by-passing the timer when repeatedly receiving messages |
| 2713 | * that will end up being dropped. |
| 2714 | */ |
| 2715 | if( ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 2716 | { |
| 2717 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2718 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 2719 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2720 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2721 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2722 | 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] | 2723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2724 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2725 | timeout = ssl->handshake->retransmit_timeout; |
| 2726 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2727 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2729 | 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] | 2730 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 2731 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2732 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 2733 | timeout ); |
| 2734 | else |
| 2735 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 2736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2737 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2738 | |
| 2739 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2740 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2741 | } |
| 2742 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2743 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2744 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2746 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2748 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 2749 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2750 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 2751 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2752 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2753 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2754 | } |
| 2755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2756 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2758 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2759 | return( ret ); |
| 2760 | } |
| 2761 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2762 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 2763 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2764 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2765 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2766 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2767 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2768 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2769 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2770 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2771 | return( ret ); |
| 2772 | } |
| 2773 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2774 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2775 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2776 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2779 | if( ret < 0 ) |
| 2780 | return( ret ); |
| 2781 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2782 | ssl->in_left = ret; |
| 2783 | } |
| 2784 | else |
| 2785 | #endif |
| 2786 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2787 | 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] | 2788 | ssl->in_left, nb_want ) ); |
| 2789 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2790 | while( ssl->in_left < nb_want ) |
| 2791 | { |
| 2792 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 2793 | |
| 2794 | if( ssl_check_timer( ssl ) != 0 ) |
| 2795 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 2796 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 2797 | { |
| 2798 | if( ssl->f_recv_timeout != NULL ) |
| 2799 | { |
| 2800 | ret = ssl->f_recv_timeout( ssl->p_bio, |
| 2801 | ssl->in_hdr + ssl->in_left, len, |
| 2802 | ssl->conf->read_timeout ); |
| 2803 | } |
| 2804 | else |
| 2805 | { |
| 2806 | ret = ssl->f_recv( ssl->p_bio, |
| 2807 | ssl->in_hdr + ssl->in_left, len ); |
| 2808 | } |
| 2809 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2810 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2811 | 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] | 2812 | ssl->in_left, nb_want ) ); |
| 2813 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2814 | |
| 2815 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2816 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2817 | |
| 2818 | if( ret < 0 ) |
| 2819 | return( ret ); |
| 2820 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 2821 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 2822 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 2823 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2824 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 2825 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 2826 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2827 | } |
| 2828 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2829 | ssl->in_left += ret; |
| 2830 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2834 | |
| 2835 | return( 0 ); |
| 2836 | } |
| 2837 | |
| 2838 | /* |
| 2839 | * Flush any data not yet written |
| 2840 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2841 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2842 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2843 | int ret; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2844 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2845 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2846 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2847 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2848 | if( ssl->f_send == NULL ) |
| 2849 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2850 | 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] | 2851 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2852 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2853 | } |
| 2854 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2855 | /* Avoid incrementing counter if data is flushed */ |
| 2856 | if( ssl->out_left == 0 ) |
| 2857 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2858 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2859 | return( 0 ); |
| 2860 | } |
| 2861 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2862 | while( ssl->out_left > 0 ) |
| 2863 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2864 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
| 2865 | mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2866 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2867 | buf = ssl->out_hdr - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2868 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 2869 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2870 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2871 | |
| 2872 | if( ret <= 0 ) |
| 2873 | return( ret ); |
| 2874 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 2875 | 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] | 2876 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 2877 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 2878 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 2879 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 2880 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2881 | } |
| 2882 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2883 | ssl->out_left -= ret; |
| 2884 | } |
| 2885 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2886 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2887 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2888 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2889 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2890 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2891 | else |
| 2892 | #endif |
| 2893 | { |
| 2894 | ssl->out_hdr = ssl->out_buf + 8; |
| 2895 | } |
| 2896 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2897 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2898 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2899 | |
| 2900 | return( 0 ); |
| 2901 | } |
| 2902 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2903 | /* |
| 2904 | * Functions to handle the DTLS retransmission state machine |
| 2905 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2906 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2907 | /* |
| 2908 | * Append current handshake message to current outgoing flight |
| 2909 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2910 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2911 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2912 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 2913 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 2914 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 2915 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2916 | |
| 2917 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2918 | 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] | 2919 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2920 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2921 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2922 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2923 | } |
| 2924 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2925 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2926 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2927 | 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] | 2928 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2929 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | /* Copy current handshake message with headers */ |
| 2933 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 2934 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2935 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2936 | msg->next = NULL; |
| 2937 | |
| 2938 | /* Append to the current flight */ |
| 2939 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2940 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2941 | else |
| 2942 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2943 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2944 | while( cur->next != NULL ) |
| 2945 | cur = cur->next; |
| 2946 | cur->next = msg; |
| 2947 | } |
| 2948 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 2949 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2950 | return( 0 ); |
| 2951 | } |
| 2952 | |
| 2953 | /* |
| 2954 | * Free the current flight of handshake messages |
| 2955 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2956 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2957 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2958 | mbedtls_ssl_flight_item *cur = flight; |
| 2959 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2960 | |
| 2961 | while( cur != NULL ) |
| 2962 | { |
| 2963 | next = cur->next; |
| 2964 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2965 | mbedtls_free( cur->p ); |
| 2966 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2967 | |
| 2968 | cur = next; |
| 2969 | } |
| 2970 | } |
| 2971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2972 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 2973 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 2974 | #endif |
| 2975 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2976 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2977 | * Swap transform_out and out_ctr with the alternative ones |
| 2978 | */ |
Andres Amaya Garcia | 52dbda6 | 2018-12-05 21:57:57 +0000 | [diff] [blame] | 2979 | static int ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2980 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2981 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2982 | unsigned char tmp_out_ctr[8]; |
Andres Amaya Garcia | 52dbda6 | 2018-12-05 21:57:57 +0000 | [diff] [blame] | 2983 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 2984 | int ret; |
| 2985 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2986 | |
| 2987 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 2988 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2989 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Andres Amaya Garcia | 52dbda6 | 2018-12-05 21:57:57 +0000 | [diff] [blame] | 2990 | return( 0 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2991 | } |
| 2992 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2993 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2994 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2995 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2996 | tmp_transform = ssl->transform_out; |
| 2997 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 2998 | ssl->handshake->alt_transform_out = tmp_transform; |
| 2999 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3000 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3001 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3002 | 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] | 3003 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3004 | |
| 3005 | /* Adjust to the newly activated transform */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 3006 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3008 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3009 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3010 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3011 | 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] | 3012 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3013 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3014 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3015 | } |
| 3016 | } |
Andres Amaya Garcia | 52dbda6 | 2018-12-05 21:57:57 +0000 | [diff] [blame] | 3017 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
| 3018 | |
| 3019 | return( 0 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /* |
| 3023 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3024 | */ |
| 3025 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3026 | { |
| 3027 | int ret = 0; |
| 3028 | |
| 3029 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3030 | |
| 3031 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3032 | |
| 3033 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3034 | |
| 3035 | return( ret ); |
| 3036 | } |
| 3037 | |
| 3038 | /* |
| 3039 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3040 | * |
| 3041 | * Need to remember the current message in case flush_output returns |
| 3042 | * 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] | 3043 | * 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] | 3044 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3045 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3046 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3047 | int ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3048 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3049 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3050 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3051 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3052 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3053 | |
| 3054 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3055 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Andres Amaya Garcia | 52dbda6 | 2018-12-05 21:57:57 +0000 | [diff] [blame] | 3056 | if( ( ret = ssl_swap_epochs( ssl ) ) != 0 ) |
| 3057 | return( ret ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3058 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3059 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3060 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3061 | |
| 3062 | while( ssl->handshake->cur_msg != NULL ) |
| 3063 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3064 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3065 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3066 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3067 | int const is_finished = |
| 3068 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3069 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 3070 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3071 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 3072 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 3073 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3074 | /* Swap epochs before sending Finished: we can't do it after |
| 3075 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 3076 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3077 | 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] | 3078 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3079 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) ); |
Andres Amaya Garcia | 52dbda6 | 2018-12-05 21:57:57 +0000 | [diff] [blame] | 3080 | if( ( ret = ssl_swap_epochs( ssl ) ) != 0 ) |
| 3081 | return( ret ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3082 | } |
| 3083 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3084 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3085 | if( ret < 0 ) |
| 3086 | return( ret ); |
| 3087 | max_frag_len = (size_t) ret; |
| 3088 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3089 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 3090 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3091 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3092 | if( max_frag_len == 0 ) |
| 3093 | { |
| 3094 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3095 | return( ret ); |
| 3096 | |
| 3097 | continue; |
| 3098 | } |
| 3099 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3100 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3101 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3102 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3103 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3104 | /* Update position inside current message */ |
| 3105 | ssl->handshake->cur_msg_p += cur->len; |
| 3106 | } |
| 3107 | else |
| 3108 | { |
| 3109 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 3110 | const size_t hs_len = cur->len - 12; |
| 3111 | const size_t frag_off = p - ( cur->p + 12 ); |
| 3112 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3113 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3114 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3115 | 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] | 3116 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3117 | if( is_finished ) |
Andres Amaya Garcia | 52dbda6 | 2018-12-05 21:57:57 +0000 | [diff] [blame] | 3118 | { |
| 3119 | if( ( ret = ssl_swap_epochs( ssl ) ) != 0 ) |
| 3120 | return( ret ); |
| 3121 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3122 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3123 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3124 | return( ret ); |
| 3125 | |
| 3126 | continue; |
| 3127 | } |
| 3128 | max_hs_frag_len = max_frag_len - 12; |
| 3129 | |
| 3130 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 3131 | max_hs_frag_len : rem_len; |
| 3132 | |
| 3133 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3134 | { |
| 3135 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3136 | (unsigned) cur_hs_frag_len, |
| 3137 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3138 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 3139 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3140 | /* Messages are stored with handshake headers as if not fragmented, |
| 3141 | * copy beginning of headers then fill fragmentation fields. |
| 3142 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 3143 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3144 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3145 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 3146 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 3147 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 3148 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3149 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 3150 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 3151 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3152 | |
| 3153 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 3154 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 3155 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3156 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 3157 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3158 | ssl->out_msgtype = cur->type; |
| 3159 | |
| 3160 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3161 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | /* If done with the current message move to the next one if any */ |
| 3165 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 3166 | { |
| 3167 | if( cur->next != NULL ) |
| 3168 | { |
| 3169 | ssl->handshake->cur_msg = cur->next; |
| 3170 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 3171 | } |
| 3172 | else |
| 3173 | { |
| 3174 | ssl->handshake->cur_msg = NULL; |
| 3175 | ssl->handshake->cur_msg_p = NULL; |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3180 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3181 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3182 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3183 | return( ret ); |
| 3184 | } |
| 3185 | } |
| 3186 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3187 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3188 | return( ret ); |
| 3189 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3190 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3191 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 3192 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 3193 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3194 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3195 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3196 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 3197 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3198 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3199 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3200 | |
| 3201 | return( 0 ); |
| 3202 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3203 | |
| 3204 | /* |
| 3205 | * To be called when the last message of an incoming flight is received. |
| 3206 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3207 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3208 | { |
| 3209 | /* We won't need to resend that one any more */ |
| 3210 | ssl_flight_free( ssl->handshake->flight ); |
| 3211 | ssl->handshake->flight = NULL; |
| 3212 | ssl->handshake->cur_msg = NULL; |
| 3213 | |
| 3214 | /* The next incoming flight will start with this msg_seq */ |
| 3215 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 3216 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3217 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3218 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3219 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3220 | /* Clear future message buffering structure. */ |
| 3221 | ssl_buffering_free( ssl ); |
| 3222 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3223 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3224 | ssl_set_timer( ssl, 0 ); |
| 3225 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3226 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3227 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3228 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3229 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3230 | } |
| 3231 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3232 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3233 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3234 | |
| 3235 | /* |
| 3236 | * To be called when the last message of an outgoing flight is send. |
| 3237 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3238 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3239 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3240 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3241 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3243 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3244 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3246 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3247 | } |
| 3248 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3249 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3250 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3251 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3252 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3253 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3254 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3255 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3256 | |
| 3257 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3258 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3259 | * |
| 3260 | * - fill in handshake headers |
| 3261 | * - update handshake checksum |
| 3262 | * - DTLS: save message for resending |
| 3263 | * - then pass to the record layer |
| 3264 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3265 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 3266 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3267 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3268 | * Inputs: |
| 3269 | * - ssl->out_msglen: 4 + actual handshake message len |
| 3270 | * (4 is the size of handshake headers for TLS) |
| 3271 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 3272 | * - ssl->out_msg + 4: the handshake message body |
| 3273 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 3274 | * 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] | 3275 | * - ssl->out_msglen: the length of the record contents |
| 3276 | * (including handshake headers but excluding record headers) |
| 3277 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3278 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3279 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3280 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3281 | int ret; |
| 3282 | const size_t hs_len = ssl->out_msglen - 4; |
| 3283 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3284 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3285 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 3286 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3287 | /* |
| 3288 | * Sanity checks |
| 3289 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3290 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3291 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3292 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3293 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 3294 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
| 3295 | if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 3296 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 3297 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ) |
| 3298 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 3299 | { |
| 3300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3301 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3302 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3303 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3304 | |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3305 | /* Whenever we send anything different from a |
| 3306 | * HelloRequest we should be in a handshake - double check. */ |
| 3307 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3308 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3309 | ssl->handshake == NULL ) |
| 3310 | { |
| 3311 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3312 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3313 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3315 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3316 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3317 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3318 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3319 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3321 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3322 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3323 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3324 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 3325 | /* Double-check that we did not exceed the bounds |
| 3326 | * of the outgoing record buffer. |
| 3327 | * This should never fail as the various message |
| 3328 | * writing functions must obey the bounds of the |
| 3329 | * outgoing record buffer, but better be safe. |
| 3330 | * |
| 3331 | * Note: We deliberately do not check for the MTU or MFL here. |
| 3332 | */ |
| 3333 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 3334 | { |
| 3335 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 3336 | "size %u, maximum %u", |
| 3337 | (unsigned) ssl->out_msglen, |
| 3338 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 3339 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3340 | } |
| 3341 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3342 | /* |
| 3343 | * Fill handshake headers |
| 3344 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3346 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3347 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 3348 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 3349 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3350 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3351 | /* |
| 3352 | * DTLS has additional fields in the Handshake layer, |
| 3353 | * between the length field and the actual payload: |
| 3354 | * uint16 message_seq; |
| 3355 | * uint24 fragment_offset; |
| 3356 | * uint24 fragment_length; |
| 3357 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3358 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3359 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3360 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3361 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3362 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 3363 | { |
| 3364 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 3365 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3366 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3367 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 3368 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3369 | } |
| 3370 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3371 | 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] | 3372 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3373 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3374 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3375 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3376 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 3377 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 3378 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 3379 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3380 | } |
| 3381 | else |
| 3382 | { |
| 3383 | ssl->out_msg[4] = 0; |
| 3384 | ssl->out_msg[5] = 0; |
| 3385 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3386 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3387 | /* Handshake hashes are computed without fragmentation, |
| 3388 | * 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] | 3389 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 3390 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3391 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3392 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3393 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 3394 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3395 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 3396 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3399 | /* 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] | 3400 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3401 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3402 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3403 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3404 | { |
| 3405 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 3406 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3407 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3408 | return( ret ); |
| 3409 | } |
| 3410 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3411 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3412 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3413 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3414 | 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] | 3415 | { |
| 3416 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 3417 | return( ret ); |
| 3418 | } |
| 3419 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3420 | |
| 3421 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 3422 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3423 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | /* |
| 3427 | * Record layer functions |
| 3428 | */ |
| 3429 | |
| 3430 | /* |
| 3431 | * Write current record. |
| 3432 | * |
| 3433 | * Uses: |
| 3434 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 3435 | * - ssl->out_msglen: length of the record content (excl headers) |
| 3436 | * - ssl->out_msg: record content |
| 3437 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3438 | 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] | 3439 | { |
| 3440 | int ret, done = 0; |
| 3441 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3442 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3443 | |
| 3444 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3446 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3447 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3448 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3449 | { |
| 3450 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 3451 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3452 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3453 | return( ret ); |
| 3454 | } |
| 3455 | |
| 3456 | len = ssl->out_msglen; |
| 3457 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3458 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3459 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3460 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3461 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3462 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3463 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3465 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 3466 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3467 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3468 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 3469 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3470 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3471 | |
| 3472 | if( ret == 0 ) |
| 3473 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3474 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3475 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3476 | if( !done ) |
| 3477 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3478 | unsigned i; |
| 3479 | size_t protected_record_size; |
| 3480 | |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3481 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3482 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3483 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3484 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3485 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3486 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 3487 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3488 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3489 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3490 | { |
| 3491 | if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 ) |
| 3492 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3493 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3494 | return( ret ); |
| 3495 | } |
| 3496 | |
| 3497 | len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3498 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 3499 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3502 | protected_record_size = len + mbedtls_ssl_hdr_len( ssl ); |
| 3503 | |
| 3504 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3505 | /* In case of DTLS, double-check that we don't exceed |
| 3506 | * the remaining space in the datagram. */ |
| 3507 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3508 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 3509 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3510 | if( ret < 0 ) |
| 3511 | return( ret ); |
| 3512 | |
| 3513 | if( protected_record_size > (size_t) ret ) |
| 3514 | { |
| 3515 | /* Should never happen */ |
| 3516 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3517 | } |
| 3518 | } |
| 3519 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3521 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 3522 | "version = [%d:%d], msglen = %d", |
| 3523 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 3524 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3526 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 3527 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3528 | |
| 3529 | ssl->out_left += protected_record_size; |
| 3530 | ssl->out_hdr += protected_record_size; |
| 3531 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
| 3532 | |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3533 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 3534 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 3535 | break; |
| 3536 | |
| 3537 | /* The loop goes to its end iff the counter is wrapping */ |
| 3538 | if( i == ssl_ep_len( ssl ) ) |
| 3539 | { |
| 3540 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 3541 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 3542 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3545 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 3546 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3547 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3548 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 3549 | size_t remaining; |
| 3550 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3551 | if( ret < 0 ) |
| 3552 | { |
| 3553 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 3554 | ret ); |
| 3555 | return( ret ); |
| 3556 | } |
| 3557 | |
| 3558 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3559 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 3560 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3561 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 3562 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3563 | else |
| 3564 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 3565 | 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] | 3566 | } |
| 3567 | } |
| 3568 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3569 | |
| 3570 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 3571 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3574 | return( ret ); |
| 3575 | } |
| 3576 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3577 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3578 | |
| 3579 | return( 0 ); |
| 3580 | } |
| 3581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3582 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3583 | |
| 3584 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 3585 | { |
| 3586 | if( ssl->in_msglen < ssl->in_hslen || |
| 3587 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 3588 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 3589 | { |
| 3590 | return( 1 ); |
| 3591 | } |
| 3592 | return( 0 ); |
| 3593 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3594 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 3595 | 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] | 3596 | { |
| 3597 | return( ( ssl->in_msg[9] << 16 ) | |
| 3598 | ( ssl->in_msg[10] << 8 ) | |
| 3599 | ssl->in_msg[11] ); |
| 3600 | } |
| 3601 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 3602 | 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] | 3603 | { |
| 3604 | return( ( ssl->in_msg[6] << 16 ) | |
| 3605 | ( ssl->in_msg[7] << 8 ) | |
| 3606 | ssl->in_msg[8] ); |
| 3607 | } |
| 3608 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 3609 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3610 | { |
| 3611 | uint32_t msg_len, frag_off, frag_len; |
| 3612 | |
| 3613 | msg_len = ssl_get_hs_total_len( ssl ); |
| 3614 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 3615 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 3616 | |
| 3617 | if( frag_off > msg_len ) |
| 3618 | return( -1 ); |
| 3619 | |
| 3620 | if( frag_len > msg_len - frag_off ) |
| 3621 | return( -1 ); |
| 3622 | |
| 3623 | if( frag_len + 12 > ssl->in_msglen ) |
| 3624 | return( -1 ); |
| 3625 | |
| 3626 | return( 0 ); |
| 3627 | } |
| 3628 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3629 | /* |
| 3630 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 3631 | */ |
| 3632 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 3633 | { |
| 3634 | unsigned int start_bits, end_bits; |
| 3635 | |
| 3636 | start_bits = 8 - ( offset % 8 ); |
| 3637 | if( start_bits != 8 ) |
| 3638 | { |
| 3639 | size_t first_byte_idx = offset / 8; |
| 3640 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 3641 | /* Special case */ |
| 3642 | if( len <= start_bits ) |
| 3643 | { |
| 3644 | for( ; len != 0; len-- ) |
| 3645 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 3646 | |
| 3647 | /* Avoid potential issues with offset or len becoming invalid */ |
| 3648 | return; |
| 3649 | } |
| 3650 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3651 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 3652 | len -= start_bits; |
| 3653 | |
| 3654 | for( ; start_bits != 0; start_bits-- ) |
| 3655 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 3656 | } |
| 3657 | |
| 3658 | end_bits = len % 8; |
| 3659 | if( end_bits != 0 ) |
| 3660 | { |
| 3661 | size_t last_byte_idx = ( offset + len ) / 8; |
| 3662 | |
| 3663 | len -= end_bits; /* Now len % 8 == 0 */ |
| 3664 | |
| 3665 | for( ; end_bits != 0; end_bits-- ) |
| 3666 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 3667 | } |
| 3668 | |
| 3669 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 3670 | } |
| 3671 | |
| 3672 | /* |
| 3673 | * Check that bitmask is full |
| 3674 | */ |
| 3675 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 3676 | { |
| 3677 | size_t i; |
| 3678 | |
| 3679 | for( i = 0; i < len / 8; i++ ) |
| 3680 | if( mask[i] != 0xFF ) |
| 3681 | return( -1 ); |
| 3682 | |
| 3683 | for( i = 0; i < len % 8; i++ ) |
| 3684 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 3685 | return( -1 ); |
| 3686 | |
| 3687 | return( 0 ); |
| 3688 | } |
| 3689 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3690 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 3691 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 3692 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3693 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3694 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3695 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3696 | alloc_len = 12; /* Handshake header */ |
| 3697 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3698 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 3699 | if( add_bitmap ) |
| 3700 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3701 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 3702 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3703 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3704 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3705 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3706 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 3707 | 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] | 3708 | { |
| 3709 | return( ( ssl->in_msg[1] << 16 ) | |
| 3710 | ( ssl->in_msg[2] << 8 ) | |
| 3711 | ssl->in_msg[3] ); |
| 3712 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3713 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3714 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3715 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3716 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3717 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3718 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3719 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3720 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3721 | } |
| 3722 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 3723 | 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] | 3724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3725 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3726 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3727 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3729 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3730 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3731 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3732 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3733 | 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] | 3734 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3735 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 3736 | { |
| 3737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 3738 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3739 | } |
| 3740 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3741 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 3742 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 3743 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 3744 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 3745 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3746 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 3747 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 3748 | { |
| 3749 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 3750 | recv_msg_seq, |
| 3751 | ssl->handshake->in_msg_seq ) ); |
| 3752 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 3753 | } |
| 3754 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 3755 | /* Retransmit only on last message from previous flight, to avoid |
| 3756 | * too many retransmissions. |
| 3757 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 3758 | 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] | 3759 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3760 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3761 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3762 | "message_seq = %d, start_of_flight = %d", |
| 3763 | recv_msg_seq, |
| 3764 | ssl->handshake->in_flight_start_seq ) ); |
| 3765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3766 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3767 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3768 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3769 | return( ret ); |
| 3770 | } |
| 3771 | } |
| 3772 | else |
| 3773 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3774 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3775 | "message_seq = %d, expected = %d", |
| 3776 | recv_msg_seq, |
| 3777 | ssl->handshake->in_msg_seq ) ); |
| 3778 | } |
| 3779 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 3780 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3781 | } |
| 3782 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3783 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 3784 | /* Message reassembly is handled alongside buffering of future |
| 3785 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 3786 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 3787 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3788 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3790 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 3791 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3792 | } |
| 3793 | } |
| 3794 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3795 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3796 | /* With TLS we don't handle fragmentation (for now) */ |
| 3797 | if( ssl->in_msglen < ssl->in_hslen ) |
| 3798 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3799 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 3800 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3801 | } |
| 3802 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3803 | return( 0 ); |
| 3804 | } |
| 3805 | |
| 3806 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 3807 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3808 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3809 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3810 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 3811 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3812 | 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] | 3813 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3814 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3815 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3816 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3817 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3818 | ssl->handshake != NULL ) |
| 3819 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3820 | unsigned offset; |
| 3821 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3822 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3823 | /* Increment handshake sequence number */ |
| 3824 | hs->in_msg_seq++; |
| 3825 | |
| 3826 | /* |
| 3827 | * Clear up handshake buffering and reassembly structure. |
| 3828 | */ |
| 3829 | |
| 3830 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 3831 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3832 | |
| 3833 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 3834 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 3835 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3836 | offset++, hs_buf++ ) |
| 3837 | { |
| 3838 | *hs_buf = *(hs_buf + 1); |
| 3839 | } |
| 3840 | |
| 3841 | /* Create a fresh last entry */ |
| 3842 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3843 | } |
| 3844 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3845 | } |
| 3846 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3847 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3848 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 3849 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3850 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 3851 | * Bit n is set iff record number in_window_top - n has been seen. |
| 3852 | * |
| 3853 | * Usually, in_window_top is the last record number seen and the lsb of |
| 3854 | * in_window is set. The only exception is the initial state (record number 0 |
| 3855 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3856 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3857 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3858 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3859 | { |
| 3860 | ssl->in_window_top = 0; |
| 3861 | ssl->in_window = 0; |
| 3862 | } |
| 3863 | |
| 3864 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 3865 | { |
| 3866 | return( ( (uint64_t) buf[0] << 40 ) | |
| 3867 | ( (uint64_t) buf[1] << 32 ) | |
| 3868 | ( (uint64_t) buf[2] << 24 ) | |
| 3869 | ( (uint64_t) buf[3] << 16 ) | |
| 3870 | ( (uint64_t) buf[4] << 8 ) | |
| 3871 | ( (uint64_t) buf[5] ) ); |
| 3872 | } |
| 3873 | |
| 3874 | /* |
| 3875 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 3876 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3877 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3878 | { |
| 3879 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 3880 | uint64_t bit; |
| 3881 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3882 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3883 | return( 0 ); |
| 3884 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3885 | if( rec_seqnum > ssl->in_window_top ) |
| 3886 | return( 0 ); |
| 3887 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3888 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3889 | |
| 3890 | if( bit >= 64 ) |
| 3891 | return( -1 ); |
| 3892 | |
| 3893 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 3894 | return( -1 ); |
| 3895 | |
| 3896 | return( 0 ); |
| 3897 | } |
| 3898 | |
| 3899 | /* |
| 3900 | * Update replay window on new validated record |
| 3901 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3902 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3903 | { |
| 3904 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 3905 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3906 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3907 | return; |
| 3908 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3909 | if( rec_seqnum > ssl->in_window_top ) |
| 3910 | { |
| 3911 | /* Update window_top and the contents of the window */ |
| 3912 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 3913 | |
| 3914 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3915 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3916 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3917 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3918 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3919 | ssl->in_window |= 1; |
| 3920 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3921 | |
| 3922 | ssl->in_window_top = rec_seqnum; |
| 3923 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3924 | else |
| 3925 | { |
| 3926 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3927 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3928 | |
| 3929 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 3930 | ssl->in_window |= (uint64_t) 1 << bit; |
| 3931 | } |
| 3932 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3933 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3934 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3935 | #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] | 3936 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3937 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); |
| 3938 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3939 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3940 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 3941 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 3942 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3943 | * |
| 3944 | * - if cookie is valid, return 0 |
| 3945 | * - if ClientHello looks superficially valid but cookie is not, |
| 3946 | * fill obuf and set olen, then |
| 3947 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 3948 | * - otherwise return a specific error code |
| 3949 | */ |
| 3950 | static int ssl_check_dtls_clihlo_cookie( |
| 3951 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 3952 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 3953 | void *p_cookie, |
| 3954 | const unsigned char *cli_id, size_t cli_id_len, |
| 3955 | const unsigned char *in, size_t in_len, |
| 3956 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 3957 | { |
| 3958 | size_t sid_len, cookie_len; |
| 3959 | unsigned char *p; |
| 3960 | |
| 3961 | if( f_cookie_write == NULL || f_cookie_check == NULL ) |
| 3962 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3963 | |
| 3964 | /* |
| 3965 | * Structure of ClientHello with record and handshake headers, |
| 3966 | * and expected values. We don't need to check a lot, more checks will be |
| 3967 | * done when actually parsing the ClientHello - skipping those checks |
| 3968 | * avoids code duplication and does not make cookie forging any easier. |
| 3969 | * |
| 3970 | * 0-0 ContentType type; copied, must be handshake |
| 3971 | * 1-2 ProtocolVersion version; copied |
| 3972 | * 3-4 uint16 epoch; copied, must be 0 |
| 3973 | * 5-10 uint48 sequence_number; copied |
| 3974 | * 11-12 uint16 length; (ignored) |
| 3975 | * |
| 3976 | * 13-13 HandshakeType msg_type; (ignored) |
| 3977 | * 14-16 uint24 length; (ignored) |
| 3978 | * 17-18 uint16 message_seq; copied |
| 3979 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 3980 | * 22-24 uint24 fragment_length; (ignored) |
| 3981 | * |
| 3982 | * 25-26 ProtocolVersion client_version; (ignored) |
| 3983 | * 27-58 Random random; (ignored) |
| 3984 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 3985 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 3986 | * ... |
| 3987 | * |
| 3988 | * Minimum length is 61 bytes. |
| 3989 | */ |
| 3990 | if( in_len < 61 || |
| 3991 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 3992 | in[3] != 0 || in[4] != 0 || |
| 3993 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 3994 | { |
| 3995 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 3996 | } |
| 3997 | |
| 3998 | sid_len = in[59]; |
| 3999 | if( sid_len > in_len - 61 ) |
| 4000 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4001 | |
| 4002 | cookie_len = in[60 + sid_len]; |
| 4003 | if( cookie_len > in_len - 60 ) |
| 4004 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4005 | |
| 4006 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 4007 | cli_id, cli_id_len ) == 0 ) |
| 4008 | { |
| 4009 | /* Valid cookie */ |
| 4010 | return( 0 ); |
| 4011 | } |
| 4012 | |
| 4013 | /* |
| 4014 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 4015 | * |
| 4016 | * 0-0 ContentType type; copied |
| 4017 | * 1-2 ProtocolVersion version; copied |
| 4018 | * 3-4 uint16 epoch; copied |
| 4019 | * 5-10 uint48 sequence_number; copied |
| 4020 | * 11-12 uint16 length; olen - 13 |
| 4021 | * |
| 4022 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 4023 | * 14-16 uint24 length; olen - 25 |
| 4024 | * 17-18 uint16 message_seq; copied |
| 4025 | * 19-21 uint24 fragment_offset; copied |
| 4026 | * 22-24 uint24 fragment_length; olen - 25 |
| 4027 | * |
| 4028 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 4029 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 4030 | * |
| 4031 | * Minimum length is 28. |
| 4032 | */ |
| 4033 | if( buf_len < 28 ) |
| 4034 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4035 | |
| 4036 | /* Copy most fields and adapt others */ |
| 4037 | memcpy( obuf, in, 25 ); |
| 4038 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 4039 | obuf[25] = 0xfe; |
| 4040 | obuf[26] = 0xff; |
| 4041 | |
| 4042 | /* Generate and write actual cookie */ |
| 4043 | p = obuf + 28; |
| 4044 | if( f_cookie_write( p_cookie, |
| 4045 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 4046 | { |
| 4047 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4048 | } |
| 4049 | |
| 4050 | *olen = p - obuf; |
| 4051 | |
| 4052 | /* Go back and fill length fields */ |
| 4053 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 4054 | |
| 4055 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 4056 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 4057 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 4058 | |
| 4059 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 4060 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 4061 | |
| 4062 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 4063 | } |
| 4064 | |
| 4065 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4066 | * Handle possible client reconnect with the same UDP quadruplet |
| 4067 | * (RFC 6347 Section 4.2.8). |
| 4068 | * |
| 4069 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 4070 | * that looks like a ClientHello. |
| 4071 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4072 | * - if the input looks like a ClientHello without cookies, |
| 4073 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4074 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4075 | * - if the input looks like a ClientHello with a valid cookie, |
| 4076 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 4077 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4078 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4079 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4080 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 4081 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 4082 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4083 | */ |
| 4084 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 4085 | { |
| 4086 | int ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4087 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4088 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4089 | ret = ssl_check_dtls_clihlo_cookie( |
| 4090 | ssl->conf->f_cookie_write, |
| 4091 | ssl->conf->f_cookie_check, |
| 4092 | ssl->conf->p_cookie, |
| 4093 | ssl->cli_id, ssl->cli_id_len, |
| 4094 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4095 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4096 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4097 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 4098 | |
| 4099 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4100 | { |
Manuel Pégourié-Gonnard | b08a334 | 2020-03-31 12:31:24 +0200 | [diff] [blame] | 4101 | int send_ret; |
| 4102 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) ); |
| 4103 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
| 4104 | ssl->out_buf, len ); |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4105 | /* 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] | 4106 | * If the error is permanent we'll catch it later, |
| 4107 | * if it's not, then hopefully it'll work next time. */ |
Manuel Pégourié-Gonnard | b08a334 | 2020-03-31 12:31:24 +0200 | [diff] [blame] | 4108 | send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len ); |
| 4109 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret ); |
| 4110 | (void) send_ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4111 | |
| 4112 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4113 | } |
| 4114 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4115 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4116 | { |
Manuel Pégourié-Gonnard | b08a334 | 2020-03-31 12:31:24 +0200 | [diff] [blame] | 4117 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) ); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4118 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
| 4119 | { |
| 4120 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 4121 | return( ret ); |
| 4122 | } |
| 4123 | |
| 4124 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4125 | } |
| 4126 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4127 | return( ret ); |
| 4128 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4129 | #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] | 4130 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4131 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4132 | * ContentType type; |
| 4133 | * ProtocolVersion version; |
| 4134 | * uint16 epoch; // DTLS only |
| 4135 | * uint48 sequence_number; // DTLS only |
| 4136 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4137 | * |
| 4138 | * 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] | 4139 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4140 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 4141 | * |
| 4142 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 4143 | * 1. proceed with the record if this function returns 0 |
| 4144 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 4145 | * 3. return CLIENT_RECONNECT if this function return that value |
| 4146 | * 4. drop the whole datagram if this function returns anything else. |
| 4147 | * Point 2 is needed when the peer is resending, and we have already received |
| 4148 | * 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] | 4149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4150 | static int ssl_parse_record_header( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4151 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4152 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4154 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) ); |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 4155 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4156 | ssl->in_msgtype = ssl->in_hdr[0]; |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4157 | ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4158 | 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] | 4159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4160 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4161 | "version = [%d:%d], msglen = %d", |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4162 | ssl->in_msgtype, |
| 4163 | major_ver, minor_ver, ssl->in_msglen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4164 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4165 | /* Check record type */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4166 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4167 | ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT && |
| 4168 | ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 4169 | ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4170 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4171 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4172 | |
| 4173 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Andres Amaya Garcia | 0169253 | 2017-06-28 09:26:46 +0100 | [diff] [blame] | 4174 | /* Silently ignore invalid DTLS records as recommended by RFC 6347 |
| 4175 | * Section 4.1.2.7 */ |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4176 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4177 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4178 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4179 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 4180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4181 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | /* Check version */ |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4185 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4186 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4187 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 4188 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4191 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4192 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4193 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 4194 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4197 | /* Check length against the size of our buffer */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4198 | if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4199 | - (size_t)( ssl->in_msg - ssl->in_buf ) ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4200 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4201 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4202 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4203 | } |
| 4204 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4205 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4206 | * DTLS-related tests. |
| 4207 | * Check epoch before checking length constraint because |
| 4208 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 4209 | * message gets duplicated before the corresponding Finished message, |
| 4210 | * the second ChangeCipherSpec should be discarded because it belongs |
| 4211 | * to an old epoch, but not because its length is shorter than |
| 4212 | * the minimum record length for packets using the new record transform. |
| 4213 | * Note that these two kinds of failures are handled differently, |
| 4214 | * as an unexpected record is silently skipped but an invalid |
| 4215 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4216 | */ |
| 4217 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4218 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4219 | { |
| 4220 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 4221 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4222 | /* Check epoch (and sequence number) with DTLS */ |
| 4223 | if( rec_epoch != ssl->in_epoch ) |
| 4224 | { |
| 4225 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 4226 | "expected %d, received %d", |
| 4227 | ssl->in_epoch, rec_epoch ) ); |
| 4228 | |
| 4229 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 4230 | /* |
| 4231 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 4232 | * access the first byte of record content (handshake type), as we |
| 4233 | * have an active transform (possibly iv_len != 0), so use the |
| 4234 | * fact that the record header len is 13 instead. |
| 4235 | */ |
| 4236 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 4237 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4238 | rec_epoch == 0 && |
| 4239 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4240 | ssl->in_left > 13 && |
| 4241 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 4242 | { |
| 4243 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 4244 | "from the same port" ) ); |
| 4245 | return( ssl_handle_possible_reconnect( ssl ) ); |
| 4246 | } |
| 4247 | else |
| 4248 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4249 | { |
| 4250 | /* Consider buffering the record. */ |
| 4251 | if( rec_epoch == (unsigned int) ssl->in_epoch + 1 ) |
| 4252 | { |
| 4253 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 4254 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4255 | } |
| 4256 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4257 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4258 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4262 | /* Replay detection only works for the current epoch */ |
| 4263 | if( rec_epoch == ssl->in_epoch && |
| 4264 | mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
| 4265 | { |
| 4266 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 4267 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 4268 | } |
| 4269 | #endif |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4270 | |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4271 | /* Drop unexpected ApplicationData records, |
| 4272 | * except at the beginning of renegotiations */ |
| 4273 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 4274 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 4275 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4276 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 4277 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
| 4278 | #endif |
| 4279 | ) |
| 4280 | { |
| 4281 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 4282 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 4283 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4284 | } |
| 4285 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4286 | |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4287 | |
| 4288 | /* Check length against bounds of the current transform and version */ |
| 4289 | if( ssl->transform_in == NULL ) |
| 4290 | { |
| 4291 | if( ssl->in_msglen < 1 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4292 | ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4293 | { |
| 4294 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4295 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4296 | } |
| 4297 | } |
| 4298 | else |
| 4299 | { |
| 4300 | if( ssl->in_msglen < ssl->transform_in->minlen ) |
| 4301 | { |
| 4302 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4303 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4304 | } |
| 4305 | |
| 4306 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 4307 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4308 | ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4309 | { |
| 4310 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4311 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4312 | } |
| 4313 | #endif |
| 4314 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 4315 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4316 | /* |
| 4317 | * TLS encrypted messages can have up to 256 bytes of padding |
| 4318 | */ |
| 4319 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 && |
| 4320 | ssl->in_msglen > ssl->transform_in->minlen + |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4321 | MBEDTLS_SSL_IN_CONTENT_LEN + 256 ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4322 | { |
| 4323 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4324 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4325 | } |
| 4326 | #endif |
| 4327 | } |
| 4328 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4329 | return( 0 ); |
| 4330 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4331 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4332 | /* |
| 4333 | * If applicable, decrypt (and decompress) record content |
| 4334 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4335 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4336 | { |
| 4337 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 4338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4339 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
| 4340 | ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4342 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4343 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4344 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4345 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4346 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4347 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 4348 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4350 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 4351 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4352 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4353 | |
| 4354 | if( ret == 0 ) |
| 4355 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4356 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4357 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4358 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4359 | { |
| 4360 | if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 ) |
| 4361 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4362 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4363 | return( ret ); |
| 4364 | } |
| 4365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4366 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4367 | ssl->in_msg, ssl->in_msglen ); |
| 4368 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4369 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4370 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4371 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4372 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4373 | } |
| 4374 | } |
| 4375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4376 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4377 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4378 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4379 | { |
| 4380 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 4381 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4382 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4383 | return( ret ); |
| 4384 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4385 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4386 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4388 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4389 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 4390 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4391 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 4392 | } |
| 4393 | #endif |
| 4394 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4395 | return( 0 ); |
| 4396 | } |
| 4397 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4398 | 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] | 4399 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4400 | /* |
| 4401 | * Read a record. |
| 4402 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4403 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 4404 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 4405 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4406 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4407 | |
| 4408 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 4409 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4410 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 4411 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 4412 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4413 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 4414 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4415 | { |
| 4416 | int ret; |
| 4417 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4418 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4419 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4420 | if( ssl->keep_current_message == 0 ) |
| 4421 | { |
| 4422 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4423 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 4424 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4425 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4426 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 4427 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4428 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4429 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4430 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4431 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4432 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4433 | /* We only check for buffered messages if the |
| 4434 | * current datagram is fully consumed. */ |
| 4435 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 4436 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4437 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4438 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 4439 | have_buffered = 1; |
| 4440 | } |
| 4441 | |
| 4442 | if( have_buffered == 0 ) |
| 4443 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4444 | { |
| 4445 | ret = ssl_get_next_record( ssl ); |
| 4446 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 4447 | continue; |
| 4448 | |
| 4449 | if( ret != 0 ) |
| 4450 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 4451 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4452 | return( ret ); |
| 4453 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4454 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 4458 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4459 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4460 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 4461 | { |
| 4462 | /* Buffer future message */ |
| 4463 | ret = ssl_buffer_message( ssl ); |
| 4464 | if( ret != 0 ) |
| 4465 | return( ret ); |
| 4466 | |
| 4467 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 4468 | } |
| 4469 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4470 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4471 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 4472 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4473 | |
| 4474 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4475 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 4476 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4477 | return( ret ); |
| 4478 | } |
| 4479 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4480 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 4481 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4482 | { |
| 4483 | mbedtls_ssl_update_handshake_status( ssl ); |
| 4484 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4485 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4486 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4487 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 4488 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4489 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 4493 | |
| 4494 | return( 0 ); |
| 4495 | } |
| 4496 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4497 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 4498 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4499 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4500 | if( ssl->in_left > ssl->next_record_offset ) |
| 4501 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4502 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4503 | return( 0 ); |
| 4504 | } |
| 4505 | |
| 4506 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 4507 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4508 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4509 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4510 | int ret = 0; |
| 4511 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4512 | if( hs == NULL ) |
| 4513 | return( -1 ); |
| 4514 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 4515 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 4516 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4517 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 4518 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 4519 | { |
| 4520 | /* Check if we have seen a ChangeCipherSpec before. |
| 4521 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 4522 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4523 | { |
| 4524 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 4525 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 4526 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4527 | } |
| 4528 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 4529 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4530 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 4531 | ssl->in_msglen = 1; |
| 4532 | ssl->in_msg[0] = 1; |
| 4533 | |
| 4534 | /* As long as they are equal, the exact value doesn't matter. */ |
| 4535 | ssl->in_left = 0; |
| 4536 | ssl->next_record_offset = 0; |
| 4537 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4538 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4539 | goto exit; |
| 4540 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4541 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 4542 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4543 | /* Debug only */ |
| 4544 | { |
| 4545 | unsigned offset; |
| 4546 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 4547 | { |
| 4548 | hs_buf = &hs->buffering.hs[offset]; |
| 4549 | if( hs_buf->is_valid == 1 ) |
| 4550 | { |
| 4551 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 4552 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 4553 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4554 | } |
| 4555 | } |
| 4556 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 4557 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4558 | |
| 4559 | /* Check if we have buffered and/or fully reassembled the |
| 4560 | * next handshake message. */ |
| 4561 | hs_buf = &hs->buffering.hs[0]; |
| 4562 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 4563 | { |
| 4564 | /* Synthesize a record containing the buffered HS message. */ |
| 4565 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 4566 | ( hs_buf->data[2] << 8 ) | |
| 4567 | hs_buf->data[3]; |
| 4568 | |
| 4569 | /* Double-check that we haven't accidentally buffered |
| 4570 | * a message that doesn't fit into the input buffer. */ |
| 4571 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 4572 | { |
| 4573 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4574 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4575 | } |
| 4576 | |
| 4577 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 4578 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 4579 | hs_buf->data, msg_len + 12 ); |
| 4580 | |
| 4581 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4582 | ssl->in_hslen = msg_len + 12; |
| 4583 | ssl->in_msglen = msg_len + 12; |
| 4584 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 4585 | |
| 4586 | ret = 0; |
| 4587 | goto exit; |
| 4588 | } |
| 4589 | else |
| 4590 | { |
| 4591 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 4592 | hs->in_msg_seq ) ); |
| 4593 | } |
| 4594 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4595 | ret = -1; |
| 4596 | |
| 4597 | exit: |
| 4598 | |
| 4599 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 4600 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4601 | } |
| 4602 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4603 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 4604 | size_t desired ) |
| 4605 | { |
| 4606 | int offset; |
| 4607 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 4608 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 4609 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4610 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4611 | /* Get rid of future records epoch first, if such exist. */ |
| 4612 | ssl_free_buffered_record( ssl ); |
| 4613 | |
| 4614 | /* Check if we have enough space available now. */ |
| 4615 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4616 | hs->buffering.total_bytes_buffered ) ) |
| 4617 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 4618 | 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] | 4619 | return( 0 ); |
| 4620 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4621 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 4622 | /* We don't have enough space to buffer the next expected handshake |
| 4623 | * message. Remove buffers used for future messages to gain space, |
| 4624 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4625 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 4626 | offset >= 0; offset-- ) |
| 4627 | { |
| 4628 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 4629 | offset ) ); |
| 4630 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 4631 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4632 | |
| 4633 | /* Check if we have enough space available now. */ |
| 4634 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4635 | hs->buffering.total_bytes_buffered ) ) |
| 4636 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 4637 | 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] | 4638 | return( 0 ); |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | return( -1 ); |
| 4643 | } |
| 4644 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4645 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 4646 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4647 | int ret = 0; |
| 4648 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 4649 | |
| 4650 | if( hs == NULL ) |
| 4651 | return( 0 ); |
| 4652 | |
| 4653 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 4654 | |
| 4655 | switch( ssl->in_msgtype ) |
| 4656 | { |
| 4657 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 4658 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4659 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4660 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4661 | break; |
| 4662 | |
| 4663 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4664 | { |
| 4665 | unsigned recv_msg_seq_offset; |
| 4666 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 4667 | mbedtls_ssl_hs_buffer *hs_buf; |
| 4668 | size_t msg_len = ssl->in_hslen - 12; |
| 4669 | |
| 4670 | /* We should never receive an old handshake |
| 4671 | * message - double-check nonetheless. */ |
| 4672 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 4673 | { |
| 4674 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4675 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4676 | } |
| 4677 | |
| 4678 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 4679 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 4680 | { |
| 4681 | /* Silently ignore -- message too far in the future */ |
| 4682 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 4683 | ( "Ignore future HS message with sequence number %u, " |
| 4684 | "buffering window %u - %u", |
| 4685 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 4686 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 4687 | |
| 4688 | goto exit; |
| 4689 | } |
| 4690 | |
| 4691 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 4692 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 4693 | |
| 4694 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 4695 | |
| 4696 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 4697 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4698 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4699 | size_t reassembly_buf_sz; |
| 4700 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4701 | hs_buf->is_fragmented = |
| 4702 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 4703 | |
| 4704 | /* We copy the message back into the input buffer |
| 4705 | * after reassembly, so check that it's not too large. |
| 4706 | * This is an implementation-specific limitation |
| 4707 | * and not one from the standard, hence it is not |
| 4708 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 4709 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4710 | { |
| 4711 | /* Ignore message */ |
| 4712 | goto exit; |
| 4713 | } |
| 4714 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4715 | /* Check if we have enough space to buffer the message. */ |
| 4716 | if( hs->buffering.total_bytes_buffered > |
| 4717 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 4718 | { |
| 4719 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4720 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4721 | } |
| 4722 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4723 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 4724 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4725 | |
| 4726 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4727 | hs->buffering.total_bytes_buffered ) ) |
| 4728 | { |
| 4729 | if( recv_msg_seq_offset > 0 ) |
| 4730 | { |
| 4731 | /* If we can't buffer a future message because |
| 4732 | * of space limitations -- ignore. */ |
| 4733 | 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", |
| 4734 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4735 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 4736 | goto exit; |
| 4737 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 4738 | else |
| 4739 | { |
| 4740 | 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", |
| 4741 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4742 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 4743 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4744 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4745 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 4746 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 4747 | 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", |
| 4748 | (unsigned) msg_len, |
| 4749 | (unsigned) reassembly_buf_sz, |
| 4750 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4751 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 4752 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4753 | goto exit; |
| 4754 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 4758 | msg_len ) ); |
| 4759 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4760 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 4761 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4762 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4763 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4764 | goto exit; |
| 4765 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4766 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4767 | |
| 4768 | /* Prepare final header: copy msg_type, length and message_seq, |
| 4769 | * then add standardised fragment_offset and fragment_length */ |
| 4770 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 4771 | memset( hs_buf->data + 6, 0, 3 ); |
| 4772 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 4773 | |
| 4774 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4775 | |
| 4776 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4777 | } |
| 4778 | else |
| 4779 | { |
| 4780 | /* Make sure msg_type and length are consistent */ |
| 4781 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 4782 | { |
| 4783 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 4784 | /* Ignore */ |
| 4785 | goto exit; |
| 4786 | } |
| 4787 | } |
| 4788 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 4789 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4790 | { |
| 4791 | size_t frag_len, frag_off; |
| 4792 | unsigned char * const msg = hs_buf->data + 12; |
| 4793 | |
| 4794 | /* |
| 4795 | * Check and copy current fragment |
| 4796 | */ |
| 4797 | |
| 4798 | /* Validation of header fields already done in |
| 4799 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 4800 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4801 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4802 | |
| 4803 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 4804 | frag_off, frag_len ) ); |
| 4805 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 4806 | |
| 4807 | if( hs_buf->is_fragmented ) |
| 4808 | { |
| 4809 | unsigned char * const bitmask = msg + msg_len; |
| 4810 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 4811 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 4812 | msg_len ) == 0 ); |
| 4813 | } |
| 4814 | else |
| 4815 | { |
| 4816 | hs_buf->is_complete = 1; |
| 4817 | } |
| 4818 | |
| 4819 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 4820 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 4821 | } |
| 4822 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4823 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4824 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4825 | |
| 4826 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 4827 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4828 | break; |
| 4829 | } |
| 4830 | |
| 4831 | exit: |
| 4832 | |
| 4833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 4834 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4835 | } |
| 4836 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4837 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4838 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4839 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4840 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4841 | * Consume last content-layer message and potentially |
| 4842 | * update in_msglen which keeps track of the contents' |
| 4843 | * consumption state. |
| 4844 | * |
| 4845 | * (1) Handshake messages: |
| 4846 | * Remove last handshake message, move content |
| 4847 | * and adapt in_msglen. |
| 4848 | * |
| 4849 | * (2) Alert messages: |
| 4850 | * Consume whole record content, in_msglen = 0. |
| 4851 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4852 | * (3) Change cipher spec: |
| 4853 | * Consume whole record content, in_msglen = 0. |
| 4854 | * |
| 4855 | * (4) Application data: |
| 4856 | * Don't do anything - the record layer provides |
| 4857 | * the application data as a stream transport |
| 4858 | * and consumes through mbedtls_ssl_read only. |
| 4859 | * |
| 4860 | */ |
| 4861 | |
| 4862 | /* Case (1): Handshake messages */ |
| 4863 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4864 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4865 | /* Hard assertion to be sure that no application data |
| 4866 | * is in flight, as corrupting ssl->in_msglen during |
| 4867 | * ssl->in_offt != NULL is fatal. */ |
| 4868 | if( ssl->in_offt != NULL ) |
| 4869 | { |
| 4870 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4871 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4872 | } |
| 4873 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4874 | /* |
| 4875 | * Get next Handshake message in the current record |
| 4876 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4877 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4878 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4879 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4880 | * current handshake content: If DTLS handshake |
| 4881 | * fragmentation is used, that's the fragment |
| 4882 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4883 | * size here is faulty and should be changed at |
| 4884 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4885 | * (2) While it doesn't seem to cause problems, one |
| 4886 | * has to be very careful not to assume that in_hslen |
| 4887 | * is always <= in_msglen in a sensible communication. |
| 4888 | * Again, it's wrong for DTLS handshake fragmentation. |
| 4889 | * The following check is therefore mandatory, and |
| 4890 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4891 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 4892 | * bounds after handling a DTLS message with an unexpected |
| 4893 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4894 | */ |
| 4895 | if( ssl->in_hslen < ssl->in_msglen ) |
| 4896 | { |
| 4897 | ssl->in_msglen -= ssl->in_hslen; |
| 4898 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 4899 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4900 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4901 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 4902 | ssl->in_msg, ssl->in_msglen ); |
| 4903 | } |
| 4904 | else |
| 4905 | { |
| 4906 | ssl->in_msglen = 0; |
| 4907 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 4908 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4909 | ssl->in_hslen = 0; |
| 4910 | } |
| 4911 | /* Case (4): Application data */ |
| 4912 | else if( ssl->in_offt != NULL ) |
| 4913 | { |
| 4914 | return( 0 ); |
| 4915 | } |
| 4916 | /* Everything else (CCS & Alerts) */ |
| 4917 | else |
| 4918 | { |
| 4919 | ssl->in_msglen = 0; |
| 4920 | } |
| 4921 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4922 | return( 0 ); |
| 4923 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4924 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4925 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 4926 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4927 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4928 | return( 1 ); |
| 4929 | |
| 4930 | return( 0 ); |
| 4931 | } |
| 4932 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4933 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4934 | |
| 4935 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 4936 | { |
| 4937 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 4938 | if( hs == NULL ) |
| 4939 | return; |
| 4940 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4941 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4942 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4943 | hs->buffering.total_bytes_buffered -= |
| 4944 | hs->buffering.future_record.len; |
| 4945 | |
| 4946 | mbedtls_free( hs->buffering.future_record.data ); |
| 4947 | hs->buffering.future_record.data = NULL; |
| 4948 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 4952 | { |
| 4953 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 4954 | unsigned char * rec; |
| 4955 | size_t rec_len; |
| 4956 | unsigned rec_epoch; |
| 4957 | |
| 4958 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4959 | return( 0 ); |
| 4960 | |
| 4961 | if( hs == NULL ) |
| 4962 | return( 0 ); |
| 4963 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4964 | rec = hs->buffering.future_record.data; |
| 4965 | rec_len = hs->buffering.future_record.len; |
| 4966 | rec_epoch = hs->buffering.future_record.epoch; |
| 4967 | |
| 4968 | if( rec == NULL ) |
| 4969 | return( 0 ); |
| 4970 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4971 | /* Only consider loading future records if the |
| 4972 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 4973 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4974 | return( 0 ); |
| 4975 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4976 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 4977 | |
| 4978 | if( rec_epoch != ssl->in_epoch ) |
| 4979 | { |
| 4980 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 4981 | goto exit; |
| 4982 | } |
| 4983 | |
| 4984 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 4985 | |
| 4986 | /* Double-check that the record is not too large */ |
| 4987 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 4988 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 4989 | { |
| 4990 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4991 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4992 | } |
| 4993 | |
| 4994 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 4995 | ssl->in_left = rec_len; |
| 4996 | ssl->next_record_offset = 0; |
| 4997 | |
| 4998 | ssl_free_buffered_record( ssl ); |
| 4999 | |
| 5000 | exit: |
| 5001 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 5002 | return( 0 ); |
| 5003 | } |
| 5004 | |
| 5005 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ) |
| 5006 | { |
| 5007 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5008 | size_t const rec_hdr_len = 13; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5009 | size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5010 | |
| 5011 | /* Don't buffer future records outside handshakes. */ |
| 5012 | if( hs == NULL ) |
| 5013 | return( 0 ); |
| 5014 | |
| 5015 | /* Only buffer handshake records (we are only interested |
| 5016 | * in Finished messages). */ |
| 5017 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 5018 | return( 0 ); |
| 5019 | |
| 5020 | /* Don't buffer more than one future epoch record. */ |
| 5021 | if( hs->buffering.future_record.data != NULL ) |
| 5022 | return( 0 ); |
| 5023 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5024 | /* Don't buffer record if there's not enough buffering space remaining. */ |
| 5025 | if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5026 | hs->buffering.total_bytes_buffered ) ) |
| 5027 | { |
| 5028 | 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", |
| 5029 | (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5030 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5031 | return( 0 ); |
| 5032 | } |
| 5033 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5034 | /* Buffer record */ |
| 5035 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 5036 | ssl->in_epoch + 1 ) ); |
| 5037 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr, |
| 5038 | rec_hdr_len + ssl->in_msglen ); |
| 5039 | |
| 5040 | /* ssl_parse_record_header() only considers records |
| 5041 | * of the next epoch as candidates for buffering. */ |
| 5042 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5043 | hs->buffering.future_record.len = total_buf_sz; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5044 | |
| 5045 | hs->buffering.future_record.data = |
| 5046 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 5047 | if( hs->buffering.future_record.data == NULL ) |
| 5048 | { |
| 5049 | /* If we run out of RAM trying to buffer a |
| 5050 | * record from the next epoch, just ignore. */ |
| 5051 | return( 0 ); |
| 5052 | } |
| 5053 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5054 | memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5055 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5056 | hs->buffering.total_bytes_buffered += total_buf_sz; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5057 | return( 0 ); |
| 5058 | } |
| 5059 | |
| 5060 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5061 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5062 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5063 | { |
| 5064 | int ret; |
| 5065 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5066 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5067 | /* We might have buffered a future record; if so, |
| 5068 | * and if the epoch matches now, load it. |
| 5069 | * On success, this call will set ssl->in_left to |
| 5070 | * the length of the buffered record, so that |
| 5071 | * the calls to ssl_fetch_input() below will |
| 5072 | * essentially be no-ops. */ |
| 5073 | ret = ssl_load_buffered_record( ssl ); |
| 5074 | if( ret != 0 ) |
| 5075 | return( ret ); |
| 5076 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5078 | if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5079 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5080 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5081 | return( ret ); |
| 5082 | } |
| 5083 | |
| 5084 | if( ( ret = ssl_parse_record_header( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5085 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5086 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 5087 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5088 | ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5089 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5090 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5091 | { |
| 5092 | ret = ssl_buffer_future_record( ssl ); |
| 5093 | if( ret != 0 ) |
| 5094 | return( ret ); |
| 5095 | |
| 5096 | /* Fall through to handling of unexpected records */ |
| 5097 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 5098 | } |
| 5099 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5100 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 5101 | { |
| 5102 | /* Skip unexpected record (but not whole datagram) */ |
| 5103 | ssl->next_record_offset = ssl->in_msglen |
| 5104 | + mbedtls_ssl_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5105 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5106 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 5107 | "(header)" ) ); |
| 5108 | } |
| 5109 | else |
| 5110 | { |
| 5111 | /* Skip invalid record and the rest of the datagram */ |
| 5112 | ssl->next_record_offset = 0; |
| 5113 | ssl->in_left = 0; |
| 5114 | |
| 5115 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 5116 | "(header)" ) ); |
| 5117 | } |
| 5118 | |
| 5119 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5120 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5121 | } |
| 5122 | #endif |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5123 | return( ret ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5124 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5125 | |
| 5126 | /* |
| 5127 | * Read and optionally decrypt the message contents |
| 5128 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5129 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
| 5130 | mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5131 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5132 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5133 | return( ret ); |
| 5134 | } |
| 5135 | |
| 5136 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5137 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5138 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5139 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5140 | ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl ); |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5141 | if( ssl->next_record_offset < ssl->in_left ) |
| 5142 | { |
| 5143 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 5144 | } |
| 5145 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5146 | else |
| 5147 | #endif |
| 5148 | ssl->in_left = 0; |
| 5149 | |
| 5150 | if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5152 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5153 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5154 | { |
| 5155 | /* Silently discard invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5156 | if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD || |
| 5157 | ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5158 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 5159 | /* Except when waiting for Finished as a bad mac here |
| 5160 | * probably means something went wrong in the handshake |
| 5161 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 5162 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 5163 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 5164 | { |
| 5165 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 5166 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 5167 | { |
| 5168 | mbedtls_ssl_send_alert_message( ssl, |
| 5169 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5170 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 5171 | } |
| 5172 | #endif |
| 5173 | return( ret ); |
| 5174 | } |
| 5175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5176 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5177 | if( ssl->conf->badmac_limit != 0 && |
| 5178 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5179 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5180 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 5181 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5182 | } |
| 5183 | #endif |
| 5184 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5185 | /* As above, invalid records cause |
| 5186 | * dismissal of the whole datagram. */ |
| 5187 | |
| 5188 | ssl->next_record_offset = 0; |
| 5189 | ssl->in_left = 0; |
| 5190 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5191 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5192 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | return( ret ); |
| 5196 | } |
| 5197 | else |
| 5198 | #endif |
| 5199 | { |
| 5200 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5201 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 5202 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5203 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5204 | mbedtls_ssl_send_alert_message( ssl, |
| 5205 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5206 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5207 | } |
| 5208 | #endif |
| 5209 | return( ret ); |
| 5210 | } |
| 5211 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5212 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5213 | return( 0 ); |
| 5214 | } |
| 5215 | |
| 5216 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 5217 | { |
| 5218 | int ret; |
| 5219 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5220 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5221 | * Handle particular types of records |
| 5222 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5223 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5224 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5225 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 5226 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 5227 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5228 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5231 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5232 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5233 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5234 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5235 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 5236 | ssl->in_msglen ) ); |
| 5237 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5238 | } |
| 5239 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5240 | if( ssl->in_msg[0] != 1 ) |
| 5241 | { |
| 5242 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 5243 | ssl->in_msg[0] ) ); |
| 5244 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5245 | } |
| 5246 | |
| 5247 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5248 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5249 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 5250 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5251 | { |
| 5252 | if( ssl->handshake == NULL ) |
| 5253 | { |
| 5254 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 5255 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5256 | } |
| 5257 | |
| 5258 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 5259 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 5260 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5261 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5262 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5264 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5265 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 5266 | if( ssl->in_msglen != 2 ) |
| 5267 | { |
| 5268 | /* Note: Standard allows for more than one 2 byte alert |
| 5269 | to be packed in a single message, but Mbed TLS doesn't |
| 5270 | currently support this. */ |
| 5271 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 5272 | ssl->in_msglen ) ); |
| 5273 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5274 | } |
| 5275 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5276 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5277 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 5278 | |
| 5279 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 5280 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5281 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5282 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5283 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5284 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5285 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5286 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5289 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5290 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5291 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5292 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 5293 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5294 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5295 | |
| 5296 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 5297 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5298 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 5299 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5300 | 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] | 5301 | /* Will be handled when trying to parse ServerHello */ |
| 5302 | return( 0 ); |
| 5303 | } |
| 5304 | #endif |
| 5305 | |
| 5306 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
| 5307 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 5308 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5309 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5310 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 5311 | { |
| 5312 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 5313 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 5314 | return( 0 ); |
| 5315 | } |
| 5316 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 5317 | |
| 5318 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5319 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5322 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5323 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5324 | ssl->handshake != NULL && |
| 5325 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5326 | { |
| 5327 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 5328 | } |
| 5329 | #endif |
| 5330 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5331 | return( 0 ); |
| 5332 | } |
| 5333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5334 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5335 | { |
| 5336 | int ret; |
| 5337 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5338 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 5339 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5340 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5341 | { |
| 5342 | return( ret ); |
| 5343 | } |
| 5344 | |
| 5345 | return( 0 ); |
| 5346 | } |
| 5347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5348 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5349 | unsigned char level, |
| 5350 | unsigned char message ) |
| 5351 | { |
| 5352 | int ret; |
| 5353 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5354 | if( ssl == NULL || ssl->conf == NULL ) |
| 5355 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5357 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5358 | 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] | 5359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5360 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5361 | ssl->out_msglen = 2; |
| 5362 | ssl->out_msg[0] = level; |
| 5363 | ssl->out_msg[1] = message; |
| 5364 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 5365 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5367 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5368 | return( ret ); |
| 5369 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5370 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5371 | |
| 5372 | return( 0 ); |
| 5373 | } |
| 5374 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5375 | /* |
| 5376 | * Handshake functions |
| 5377 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5378 | #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \ |
| 5379 | !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \ |
| 5380 | !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \ |
| 5381 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ |
| 5382 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \ |
| 5383 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \ |
| 5384 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 5385 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5386 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5387 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5388 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5390 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5391 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5392 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 5393 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 5394 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 5395 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5396 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5397 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5398 | ssl->state++; |
| 5399 | return( 0 ); |
| 5400 | } |
| 5401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5402 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5403 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5404 | } |
| 5405 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5406 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5407 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5408 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5410 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5411 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5412 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 5413 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 5414 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 5415 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5416 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5417 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5418 | ssl->state++; |
| 5419 | return( 0 ); |
| 5420 | } |
| 5421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5422 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5423 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5424 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 5425 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5426 | #else |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 5427 | /* Some certificate support -> implement write and parse */ |
| 5428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5429 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5430 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5431 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5432 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5433 | const mbedtls_x509_crt *crt; |
| 5434 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5436 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5438 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 5439 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 5440 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 5441 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5443 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5444 | ssl->state++; |
| 5445 | return( 0 ); |
| 5446 | } |
| 5447 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5448 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5449 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5450 | { |
| 5451 | if( ssl->client_auth == 0 ) |
| 5452 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5453 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5454 | ssl->state++; |
| 5455 | return( 0 ); |
| 5456 | } |
| 5457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5458 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5459 | /* |
| 5460 | * If using SSLv3 and got no cert, send an Alert message |
| 5461 | * (otherwise an empty Certificate message will be sent). |
| 5462 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5463 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 5464 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5465 | { |
| 5466 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5467 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 5468 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 5469 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5471 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5472 | goto write_msg; |
| 5473 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5474 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5475 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5476 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 5477 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5478 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5479 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5480 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5481 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5482 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 5483 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5484 | } |
| 5485 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 5486 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5488 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5489 | |
| 5490 | /* |
| 5491 | * 0 . 0 handshake type |
| 5492 | * 1 . 3 handshake length |
| 5493 | * 4 . 6 length of all certs |
| 5494 | * 7 . 9 length of cert. 1 |
| 5495 | * 10 . n-1 peer certificate |
| 5496 | * n . n+2 length of cert. 2 |
| 5497 | * n+3 . ... upper level cert, etc. |
| 5498 | */ |
| 5499 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5500 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5501 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 5502 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5503 | { |
| 5504 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5505 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5506 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5507 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5508 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5509 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 5513 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 5514 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 5515 | |
| 5516 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 5517 | i += n; crt = crt->next; |
| 5518 | } |
| 5519 | |
| 5520 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 5521 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 5522 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 5523 | |
| 5524 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5525 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5526 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5527 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 5528 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5529 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 5530 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5531 | |
| 5532 | ssl->state++; |
| 5533 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5534 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5535 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5536 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5537 | return( ret ); |
| 5538 | } |
| 5539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5540 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5541 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 5542 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5545 | /* |
| 5546 | * Once the certificate message is read, parse it into a cert chain and |
| 5547 | * perform basic checks, but leave actual verification to the caller |
| 5548 | */ |
| 5549 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5550 | { |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5551 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 5552 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 5553 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5555 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5556 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5557 | /* |
| 5558 | * Check if the client sent an empty certificate |
| 5559 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5560 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5561 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5562 | { |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 5563 | if( ssl->in_msglen == 2 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5564 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 5565 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5566 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5567 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5568 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5569 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5570 | /* The client was asked for a certificate but didn't send |
| 5571 | one. The client should know what's going on, so we |
| 5572 | don't send an alert. */ |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5573 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5574 | return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5575 | } |
| 5576 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5577 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5579 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 5580 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5581 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5582 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5583 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5584 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 5585 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5586 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 5587 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5588 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5589 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5590 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5591 | /* The client was asked for a certificate but didn't send |
| 5592 | one. The client should know what's going on, so we |
| 5593 | don't send an alert. */ |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5594 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5595 | return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5596 | } |
| 5597 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5598 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 5599 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5600 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5602 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5603 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5604 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5605 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5606 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5607 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5610 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 5611 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5612 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5613 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5614 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5615 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5616 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5617 | } |
| 5618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5619 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 5620 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5621 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5622 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5623 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 5624 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5625 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 5626 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5627 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5629 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5630 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5631 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5632 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
Manuel Pégourié-Gonnard | bfb355c | 2013-09-07 17:27:43 +0200 | [diff] [blame] | 5635 | /* In case we tried to reuse a session but it failed */ |
| 5636 | if( ssl->session_negotiate->peer_cert != NULL ) |
| 5637 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5638 | mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert ); |
| 5639 | mbedtls_free( ssl->session_negotiate->peer_cert ); |
Manuel Pégourié-Gonnard | bfb355c | 2013-09-07 17:27:43 +0200 | [diff] [blame] | 5640 | } |
| 5641 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 5642 | if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5643 | sizeof( mbedtls_x509_crt ) ) ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5644 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 5645 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5646 | sizeof( mbedtls_x509_crt ) ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5647 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5648 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 5649 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5650 | } |
| 5651 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5652 | mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5653 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 5654 | i += 3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5655 | |
| 5656 | while( i < ssl->in_hslen ) |
| 5657 | { |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 5658 | if ( i + 3 > ssl->in_hslen ) { |
| 5659 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5660 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5661 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5662 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 5663 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5664 | if( ssl->in_msg[i] != 0 ) |
| 5665 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5666 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5667 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5668 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5669 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
| 5672 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 5673 | | (unsigned int) ssl->in_msg[i + 2]; |
| 5674 | i += 3; |
| 5675 | |
| 5676 | if( n < 128 || i + n > ssl->in_hslen ) |
| 5677 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5678 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5679 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5680 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5681 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5684 | ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 5685 | ssl->in_msg + i, n ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5686 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5687 | { |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5688 | case 0: /*ok*/ |
| 5689 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 5690 | /* Ignore certificate with an unknown algorithm: maybe a |
| 5691 | prior certificate was already trusted. */ |
| 5692 | break; |
| 5693 | |
| 5694 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 5695 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 5696 | goto crt_parse_der_failed; |
| 5697 | |
| 5698 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 5699 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 5700 | goto crt_parse_der_failed; |
| 5701 | |
| 5702 | default: |
| 5703 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 5704 | crt_parse_der_failed: |
| 5705 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5706 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5707 | return( ret ); |
| 5708 | } |
| 5709 | |
| 5710 | i += n; |
| 5711 | } |
| 5712 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5713 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5714 | |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5715 | /* |
| 5716 | * On client, make sure the server cert doesn't change during renego to |
| 5717 | * avoid "triple handshake" attack: https://secure-resumption.com/ |
| 5718 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5719 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5720 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5721 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5722 | { |
| 5723 | if( ssl->session->peer_cert == NULL ) |
| 5724 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5725 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5726 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5727 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5728 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5729 | } |
| 5730 | |
| 5731 | if( ssl->session->peer_cert->raw.len != |
| 5732 | ssl->session_negotiate->peer_cert->raw.len || |
| 5733 | memcmp( ssl->session->peer_cert->raw.p, |
| 5734 | ssl->session_negotiate->peer_cert->raw.p, |
| 5735 | ssl->session->peer_cert->raw.len ) != 0 ) |
| 5736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5738 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5739 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5740 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5741 | } |
| 5742 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5743 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5744 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5745 | return( 0 ); |
| 5746 | } |
| 5747 | |
| 5748 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 5749 | { |
| 5750 | int ret; |
| 5751 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5752 | ssl->transform_negotiate->ciphersuite_info; |
| 5753 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 5754 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 5755 | ? ssl->handshake->sni_authmode |
| 5756 | : ssl->conf->authmode; |
| 5757 | #else |
| 5758 | const int authmode = ssl->conf->authmode; |
| 5759 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5760 | void *rs_ctx = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5761 | |
| 5762 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 5763 | |
| 5764 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 5765 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 5766 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 5767 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 5768 | { |
| 5769 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 5770 | ssl->state++; |
| 5771 | return( 0 ); |
| 5772 | } |
| 5773 | |
| 5774 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5775 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5776 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 5777 | { |
| 5778 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 5779 | ssl->state++; |
| 5780 | return( 0 ); |
| 5781 | } |
| 5782 | |
| 5783 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5784 | authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 5785 | { |
| 5786 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 5787 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5788 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5789 | ssl->state++; |
| 5790 | return( 0 ); |
| 5791 | } |
| 5792 | #endif |
| 5793 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5794 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 5795 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 5796 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5797 | { |
| 5798 | goto crt_verify; |
| 5799 | } |
| 5800 | #endif |
| 5801 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 5802 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5803 | { |
| 5804 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 5805 | let it decide whether to alert. */ |
| 5806 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 5807 | return( ret ); |
| 5808 | } |
| 5809 | |
| 5810 | if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 ) |
| 5811 | { |
| 5812 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5813 | if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE && |
| 5814 | authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 5815 | { |
| 5816 | ret = 0; |
| 5817 | } |
| 5818 | #endif |
| 5819 | |
| 5820 | ssl->state++; |
| 5821 | return( ret ); |
| 5822 | } |
| 5823 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5824 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 5825 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 5826 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5827 | |
| 5828 | crt_verify: |
| 5829 | if( ssl->handshake->ecrs_enabled) |
| 5830 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 5831 | #endif |
| 5832 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 5833 | if( authmode != MBEDTLS_SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5834 | { |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 5835 | mbedtls_x509_crt *ca_chain; |
| 5836 | mbedtls_x509_crl *ca_crl; |
| 5837 | |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5838 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 5839 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 5840 | { |
| 5841 | ca_chain = ssl->handshake->sni_ca_chain; |
| 5842 | ca_crl = ssl->handshake->sni_ca_crl; |
| 5843 | } |
| 5844 | else |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5845 | #endif |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 5846 | { |
| 5847 | ca_chain = ssl->conf->ca_chain; |
| 5848 | ca_crl = ssl->conf->ca_crl; |
| 5849 | } |
| 5850 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5851 | /* |
| 5852 | * Main check: verify certificate |
| 5853 | */ |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5854 | ret = mbedtls_x509_crt_verify_restartable( |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 5855 | ssl->session_negotiate->peer_cert, |
| 5856 | ca_chain, ca_crl, |
| 5857 | ssl->conf->cert_profile, |
| 5858 | ssl->hostname, |
| 5859 | &ssl->session_negotiate->verify_result, |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5860 | ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5861 | |
| 5862 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5863 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5864 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5865 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5866 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5867 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 5868 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 5869 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5870 | #endif |
| 5871 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5872 | /* |
| 5873 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 5874 | */ |
| 5875 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5876 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5877 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5878 | const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5879 | |
| 5880 | /* If certificate uses an EC key, make sure the curve is OK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5881 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5882 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5883 | { |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5884 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 5885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5886 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5887 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5888 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5889 | } |
| 5890 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5891 | #endif /* MBEDTLS_ECP_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5892 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5893 | if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert, |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5894 | ciphersuite_info, |
| 5895 | ! ssl->conf->endpoint, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5896 | &ssl->session_negotiate->verify_result ) != 0 ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5897 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5898 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5899 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5900 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5901 | } |
| 5902 | |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5903 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 5904 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 5905 | * with details encoded in the verification flags. All other kinds |
| 5906 | * of error codes, including those from the user provided f_vrfy |
| 5907 | * functions, are treated as fatal and lead to a failure of |
| 5908 | * ssl_parse_certificate even if verification was optional. */ |
| 5909 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 5910 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 5911 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
| 5912 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5913 | ret = 0; |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 5917 | { |
| 5918 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 5919 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 5920 | } |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5921 | |
| 5922 | if( ret != 0 ) |
| 5923 | { |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5924 | uint8_t alert; |
| 5925 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5926 | /* The certificate may have been rejected for several reasons. |
| 5927 | Pick one and send the corresponding alert. Which alert to send |
| 5928 | may be a subject of debate in some cases. */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5929 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 5930 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 5931 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 5932 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 5933 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 5934 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 5935 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 5936 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 5937 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 5938 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 5939 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 5940 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 5941 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 5942 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 5943 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 5944 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 5945 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 5946 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 5947 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 5948 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
Gilles Peskine | 8498cb3 | 2017-05-10 15:39:40 +0200 | [diff] [blame] | 5949 | else |
| 5950 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5951 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5952 | alert ); |
| 5953 | } |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5954 | |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 5955 | #if defined(MBEDTLS_DEBUG_C) |
| 5956 | if( ssl->session_negotiate->verify_result != 0 ) |
| 5957 | { |
| 5958 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 5959 | ssl->session_negotiate->verify_result ) ); |
| 5960 | } |
| 5961 | else |
| 5962 | { |
| 5963 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 5964 | } |
| 5965 | #endif /* MBEDTLS_DEBUG_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5968 | ssl->state++; |
| 5969 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5970 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5971 | |
| 5972 | return( ret ); |
| 5973 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5974 | #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED |
| 5975 | !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED |
| 5976 | !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED |
| 5977 | !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED |
| 5978 | !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED |
| 5979 | !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED |
| 5980 | !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5982 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5983 | { |
| 5984 | int ret; |
| 5985 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5986 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5988 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5989 | ssl->out_msglen = 1; |
| 5990 | ssl->out_msg[0] = 1; |
| 5991 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5992 | ssl->state++; |
| 5993 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5994 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5995 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5996 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5997 | return( ret ); |
| 5998 | } |
| 5999 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6000 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6001 | |
| 6002 | return( 0 ); |
| 6003 | } |
| 6004 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6005 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6006 | { |
| 6007 | int ret; |
| 6008 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6009 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6010 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 6011 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6012 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6013 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6014 | return( ret ); |
| 6015 | } |
| 6016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6017 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6018 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6019 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6020 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6021 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6022 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6023 | } |
| 6024 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6025 | /* CCS records are only accepted if they have length 1 and content '1', |
| 6026 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6027 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6028 | /* |
| 6029 | * Switch to our negotiated transform and session parameters for inbound |
| 6030 | * data. |
| 6031 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6032 | 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] | 6033 | ssl->transform_in = ssl->transform_negotiate; |
| 6034 | ssl->session_in = ssl->session_negotiate; |
| 6035 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6036 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6037 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6038 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6039 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6040 | ssl_dtls_replay_reset( ssl ); |
| 6041 | #endif |
| 6042 | |
| 6043 | /* Increment epoch */ |
| 6044 | if( ++ssl->in_epoch == 0 ) |
| 6045 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6046 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6047 | /* This is highly unlikely to happen for legitimate reasons, so |
| 6048 | 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] | 6049 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6050 | } |
| 6051 | } |
| 6052 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6053 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6054 | memset( ssl->in_ctr, 0, 8 ); |
| 6055 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 6056 | ssl_update_in_pointers( ssl, ssl->transform_negotiate ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6058 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 6059 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6060 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6061 | 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] | 6062 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6063 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6064 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6065 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6066 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6067 | } |
| 6068 | } |
| 6069 | #endif |
| 6070 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6071 | ssl->state++; |
| 6072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6073 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6074 | |
| 6075 | return( 0 ); |
| 6076 | } |
| 6077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6078 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 6079 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6080 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 6081 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6082 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6083 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6084 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 6085 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6086 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6087 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6088 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6089 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6090 | #if defined(MBEDTLS_SHA512_C) |
| 6091 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6092 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 6093 | else |
| 6094 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6095 | #if defined(MBEDTLS_SHA256_C) |
| 6096 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6097 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6098 | else |
| 6099 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6100 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 6101 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6102 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6103 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 6104 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6105 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 6106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6107 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6108 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6109 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6110 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6111 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 6112 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6113 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6114 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6115 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6116 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6117 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6118 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6119 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6120 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6121 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6122 | } |
| 6123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6124 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6125 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6126 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6127 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6128 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6129 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 6130 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6131 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6132 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6133 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6134 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6135 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6136 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6137 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6138 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6139 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6142 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6143 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 6144 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6145 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6146 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6147 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 6148 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6149 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6150 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6152 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6153 | #if defined(MBEDTLS_SHA256_C) |
| 6154 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6155 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6156 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6157 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6158 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6159 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6161 | #if defined(MBEDTLS_SHA512_C) |
| 6162 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6163 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6164 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6165 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6166 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6167 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6168 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6170 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6171 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6172 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6173 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6174 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6175 | mbedtls_md5_context md5; |
| 6176 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6177 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6178 | unsigned char padbuf[48]; |
| 6179 | unsigned char md5sum[16]; |
| 6180 | unsigned char sha1sum[20]; |
| 6181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6182 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6183 | if( !session ) |
| 6184 | session = ssl->session; |
| 6185 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6186 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6187 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6188 | mbedtls_md5_init( &md5 ); |
| 6189 | mbedtls_sha1_init( &sha1 ); |
| 6190 | |
| 6191 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 6192 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6193 | |
| 6194 | /* |
| 6195 | * SSLv3: |
| 6196 | * hash = |
| 6197 | * MD5( master + pad2 + |
| 6198 | * MD5( handshake + sender + master + pad1 ) ) |
| 6199 | * + SHA1( master + pad2 + |
| 6200 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6201 | */ |
| 6202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6203 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6204 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 6205 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6206 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6208 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6209 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 6210 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6211 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6213 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6214 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6215 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6216 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6217 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6218 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 6219 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 6220 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 6221 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6222 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6223 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 6224 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 6225 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 6226 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6227 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6228 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6229 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6230 | mbedtls_md5_starts_ret( &md5 ); |
| 6231 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 6232 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 6233 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 6234 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6235 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6236 | mbedtls_sha1_starts_ret( &sha1 ); |
| 6237 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 6238 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 6239 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 6240 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6242 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6243 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6244 | mbedtls_md5_free( &md5 ); |
| 6245 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6246 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6247 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6248 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 6249 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6251 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6252 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6253 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6254 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6255 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6256 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6257 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6258 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6259 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6260 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6261 | mbedtls_md5_context md5; |
| 6262 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6263 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6264 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6265 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6266 | if( !session ) |
| 6267 | session = ssl->session; |
| 6268 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6269 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6270 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6271 | mbedtls_md5_init( &md5 ); |
| 6272 | mbedtls_sha1_init( &sha1 ); |
| 6273 | |
| 6274 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 6275 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6276 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6277 | /* |
| 6278 | * TLSv1: |
| 6279 | * hash = PRF( master, finished_label, |
| 6280 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 6281 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6283 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6284 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 6285 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6286 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6287 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6288 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6289 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 6290 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6291 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6292 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6293 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6294 | ? "client finished" |
| 6295 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6296 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6297 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 6298 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6299 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6300 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6301 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6303 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6304 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6305 | mbedtls_md5_free( &md5 ); |
| 6306 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6307 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6308 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6310 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6311 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6312 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6313 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6314 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6315 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6316 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6317 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6318 | { |
| 6319 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6320 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6321 | mbedtls_sha256_context sha256; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6322 | unsigned char padbuf[32]; |
| 6323 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6324 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6325 | if( !session ) |
| 6326 | session = ssl->session; |
| 6327 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6328 | mbedtls_sha256_init( &sha256 ); |
| 6329 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6330 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6331 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6332 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6333 | |
| 6334 | /* |
| 6335 | * TLSv1.2: |
| 6336 | * hash = PRF( master, finished_label, |
| 6337 | * Hash( handshake ) )[0.11] |
| 6338 | */ |
| 6339 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6340 | #if !defined(MBEDTLS_SHA256_ALT) |
| 6341 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6342 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6343 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6345 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6346 | ? "client finished" |
| 6347 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6348 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6349 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6350 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6351 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6352 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6354 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6355 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6356 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6357 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6358 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6360 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6361 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6362 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6364 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6365 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6366 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6367 | { |
| 6368 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6369 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6370 | mbedtls_sha512_context sha512; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6371 | unsigned char padbuf[48]; |
| 6372 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6373 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6374 | if( !session ) |
| 6375 | session = ssl->session; |
| 6376 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6377 | mbedtls_sha512_init( &sha512 ); |
| 6378 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6379 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6380 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6381 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6382 | |
| 6383 | /* |
| 6384 | * TLSv1.2: |
| 6385 | * hash = PRF( master, finished_label, |
| 6386 | * Hash( handshake ) )[0.11] |
| 6387 | */ |
| 6388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6389 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6390 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 6391 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6392 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6394 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6395 | ? "client finished" |
| 6396 | : "server finished"; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6397 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6398 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6399 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6400 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6401 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6403 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6404 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6405 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6406 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6407 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6409 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6410 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6411 | #endif /* MBEDTLS_SHA512_C */ |
| 6412 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6413 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6414 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6415 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6416 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6417 | |
| 6418 | /* |
| 6419 | * Free our handshake params |
| 6420 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6421 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6422 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6423 | ssl->handshake = NULL; |
| 6424 | |
| 6425 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6426 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6427 | */ |
| 6428 | if( ssl->transform ) |
| 6429 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6430 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6431 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6432 | } |
| 6433 | ssl->transform = ssl->transform_negotiate; |
| 6434 | ssl->transform_negotiate = NULL; |
| 6435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6436 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6437 | } |
| 6438 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6439 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6440 | { |
| 6441 | int resume = ssl->handshake->resume; |
| 6442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6443 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6445 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6446 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6447 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6448 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6449 | ssl->renego_records_seen = 0; |
| 6450 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 6451 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6452 | |
| 6453 | /* |
| 6454 | * Free the previous session and switch in the current one |
| 6455 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6456 | if( ssl->session ) |
| 6457 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6458 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 6459 | /* RFC 7366 3.1: keep the EtM state */ |
| 6460 | ssl->session_negotiate->encrypt_then_mac = |
| 6461 | ssl->session->encrypt_then_mac; |
| 6462 | #endif |
| 6463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6464 | mbedtls_ssl_session_free( ssl->session ); |
| 6465 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6466 | } |
| 6467 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6468 | ssl->session_negotiate = NULL; |
| 6469 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6470 | /* |
| 6471 | * Add cache entry |
| 6472 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6473 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 6474 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 6475 | resume == 0 ) |
| 6476 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 6477 | 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] | 6478 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 6479 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6481 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6482 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6483 | ssl->handshake->flight != NULL ) |
| 6484 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 6485 | /* Cancel handshake timer */ |
| 6486 | ssl_set_timer( ssl, 0 ); |
| 6487 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6488 | /* Keep last flight around in case we need to resend it: |
| 6489 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6490 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6491 | } |
| 6492 | else |
| 6493 | #endif |
| 6494 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6495 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6496 | ssl->state++; |
| 6497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6498 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6501 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6502 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6503 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6505 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6506 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 6507 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 6508 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6509 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6510 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 6511 | /* |
| 6512 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 6513 | * may define some other value. Currently (early 2016), no defined |
| 6514 | * ciphersuite does this (and this is unlikely to change as activity has |
| 6515 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 6516 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6517 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6519 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6520 | ssl->verify_data_len = hash_len; |
| 6521 | 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] | 6522 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6523 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6524 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6525 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6526 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6527 | |
| 6528 | /* |
| 6529 | * In case of session resuming, invert the client and server |
| 6530 | * ChangeCipherSpec messages order. |
| 6531 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6532 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6533 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6534 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6535 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6536 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6537 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6538 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6539 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6540 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6541 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6542 | } |
| 6543 | else |
| 6544 | ssl->state++; |
| 6545 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6546 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 6547 | * Switch to our negotiated transform and session parameters for outbound |
| 6548 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6549 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6550 | 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] | 6551 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6552 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6553 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6554 | { |
| 6555 | unsigned char i; |
| 6556 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6557 | /* Remember current epoch settings for resending */ |
| 6558 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6559 | 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] | 6560 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6561 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6562 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6563 | |
| 6564 | /* Increment epoch */ |
| 6565 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6566 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6567 | break; |
| 6568 | |
| 6569 | /* The loop goes to its end iff the counter is wrapping */ |
| 6570 | if( i == 0 ) |
| 6571 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6572 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 6573 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6574 | } |
| 6575 | } |
| 6576 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6577 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6578 | memset( ssl->cur_out_ctr, 0, 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6579 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6580 | ssl->transform_out = ssl->transform_negotiate; |
| 6581 | ssl->session_out = ssl->session_negotiate; |
| 6582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6583 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 6584 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 6585 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6586 | 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] | 6587 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6588 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 6589 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 6590 | } |
| 6591 | } |
| 6592 | #endif |
| 6593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6594 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6595 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6596 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 6597 | #endif |
| 6598 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6599 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6600 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6601 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6602 | return( ret ); |
| 6603 | } |
| 6604 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 6605 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6606 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6607 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 6608 | { |
| 6609 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 6610 | return( ret ); |
| 6611 | } |
| 6612 | #endif |
| 6613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6614 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6615 | |
| 6616 | return( 0 ); |
| 6617 | } |
| 6618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6619 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 6620 | #define SSL_MAX_HASH_LEN 36 |
| 6621 | #else |
| 6622 | #define SSL_MAX_HASH_LEN 12 |
| 6623 | #endif |
| 6624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6625 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6626 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6627 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6628 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 6629 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6630 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6631 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6632 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6633 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6634 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 6635 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6636 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6637 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6638 | return( ret ); |
| 6639 | } |
| 6640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6641 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6642 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6643 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6644 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6645 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6646 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6647 | } |
| 6648 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 6649 | /* 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] | 6650 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 6651 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 6652 | hash_len = 36; |
| 6653 | else |
| 6654 | #endif |
| 6655 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6657 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 6658 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6660 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6661 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6662 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6663 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6664 | } |
| 6665 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6666 | 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] | 6667 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6668 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6669 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6670 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6671 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6672 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6673 | } |
| 6674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6675 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6676 | ssl->verify_data_len = hash_len; |
| 6677 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 6678 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6679 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6680 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6681 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6682 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6683 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6684 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6685 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6686 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6687 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6688 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6689 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6690 | } |
| 6691 | else |
| 6692 | ssl->state++; |
| 6693 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6694 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6695 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6696 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6697 | #endif |
| 6698 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6699 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6700 | |
| 6701 | return( 0 ); |
| 6702 | } |
| 6703 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6704 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6705 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6706 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6707 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6708 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6709 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 6710 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 6711 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6712 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 6713 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6714 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6715 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6716 | #if defined(MBEDTLS_SHA256_C) |
| 6717 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6718 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6719 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6720 | #if defined(MBEDTLS_SHA512_C) |
| 6721 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6722 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6723 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6724 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6725 | |
| 6726 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6727 | |
| 6728 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 6729 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 6730 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 6731 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6732 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6733 | #if defined(MBEDTLS_DHM_C) |
| 6734 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6735 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6736 | #if defined(MBEDTLS_ECDH_C) |
| 6737 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6738 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 6739 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 6740 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 6741 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6742 | handshake->ecjpake_cache = NULL; |
| 6743 | handshake->ecjpake_cache_len = 0; |
| 6744 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 6745 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 6746 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 6747 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 6748 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 6749 | #endif |
| 6750 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 6751 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6752 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 6753 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6754 | } |
| 6755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6756 | static void ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6758 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 6759 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6760 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 6761 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 6762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6763 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 6764 | mbedtls_md_init( &transform->md_ctx_dec ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6765 | } |
| 6766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6767 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6768 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6769 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6770 | } |
| 6771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6772 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6773 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6774 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6775 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6776 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6777 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6778 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6779 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6780 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6781 | |
| 6782 | /* |
| 6783 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 6784 | * Now allocate missing structures. |
| 6785 | */ |
| 6786 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6787 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 6788 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6789 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6790 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6791 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6792 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 6793 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6794 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6795 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 6796 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6797 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 6798 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6799 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6800 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6801 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6802 | if( ssl->handshake == NULL || |
| 6803 | ssl->transform_negotiate == NULL || |
| 6804 | ssl->session_negotiate == NULL ) |
| 6805 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 6806 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6807 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6808 | mbedtls_free( ssl->handshake ); |
| 6809 | mbedtls_free( ssl->transform_negotiate ); |
| 6810 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6811 | |
| 6812 | ssl->handshake = NULL; |
| 6813 | ssl->transform_negotiate = NULL; |
| 6814 | ssl->session_negotiate = NULL; |
| 6815 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 6816 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6817 | } |
| 6818 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6819 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6820 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6821 | ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 6822 | ssl_handshake_params_init( ssl->handshake ); |
| 6823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6824 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 6825 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6826 | { |
| 6827 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6828 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 6829 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6830 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 6831 | else |
| 6832 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 6833 | |
| 6834 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 6835 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6836 | #endif |
| 6837 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6838 | return( 0 ); |
| 6839 | } |
| 6840 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6841 | #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] | 6842 | /* Dummy cookie callbacks for defaults */ |
| 6843 | static int ssl_cookie_write_dummy( void *ctx, |
| 6844 | unsigned char **p, unsigned char *end, |
| 6845 | const unsigned char *cli_id, size_t cli_id_len ) |
| 6846 | { |
| 6847 | ((void) ctx); |
| 6848 | ((void) p); |
| 6849 | ((void) end); |
| 6850 | ((void) cli_id); |
| 6851 | ((void) cli_id_len); |
| 6852 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6853 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 6854 | } |
| 6855 | |
| 6856 | static int ssl_cookie_check_dummy( void *ctx, |
| 6857 | const unsigned char *cookie, size_t cookie_len, |
| 6858 | const unsigned char *cli_id, size_t cli_id_len ) |
| 6859 | { |
| 6860 | ((void) ctx); |
| 6861 | ((void) cookie); |
| 6862 | ((void) cookie_len); |
| 6863 | ((void) cli_id); |
| 6864 | ((void) cli_id_len); |
| 6865 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6866 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 6867 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6868 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 6869 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 6870 | /* Once ssl->out_hdr as the address of the beginning of the |
| 6871 | * next outgoing record is set, deduce the other pointers. |
| 6872 | * |
| 6873 | * Note: For TLS, we save the implicit record sequence number |
| 6874 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 6875 | * and the caller has to make sure there's space for this. |
| 6876 | */ |
| 6877 | |
| 6878 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 6879 | mbedtls_ssl_transform *transform ) |
| 6880 | { |
| 6881 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6882 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6883 | { |
| 6884 | ssl->out_ctr = ssl->out_hdr + 3; |
| 6885 | ssl->out_len = ssl->out_hdr + 11; |
| 6886 | ssl->out_iv = ssl->out_hdr + 13; |
| 6887 | } |
| 6888 | else |
| 6889 | #endif |
| 6890 | { |
| 6891 | ssl->out_ctr = ssl->out_hdr - 8; |
| 6892 | ssl->out_len = ssl->out_hdr + 3; |
| 6893 | ssl->out_iv = ssl->out_hdr + 5; |
| 6894 | } |
| 6895 | |
| 6896 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 6897 | if( transform != NULL && |
| 6898 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 6899 | { |
| 6900 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 6901 | } |
| 6902 | else |
| 6903 | ssl->out_msg = ssl->out_iv; |
| 6904 | } |
| 6905 | |
| 6906 | /* Once ssl->in_hdr as the address of the beginning of the |
| 6907 | * next incoming record is set, deduce the other pointers. |
| 6908 | * |
| 6909 | * Note: For TLS, we save the implicit record sequence number |
| 6910 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 6911 | * and the caller has to make sure there's space for this. |
| 6912 | */ |
| 6913 | |
| 6914 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl, |
| 6915 | mbedtls_ssl_transform *transform ) |
| 6916 | { |
| 6917 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6918 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6919 | { |
| 6920 | ssl->in_ctr = ssl->in_hdr + 3; |
| 6921 | ssl->in_len = ssl->in_hdr + 11; |
| 6922 | ssl->in_iv = ssl->in_hdr + 13; |
| 6923 | } |
| 6924 | else |
| 6925 | #endif |
| 6926 | { |
| 6927 | ssl->in_ctr = ssl->in_hdr - 8; |
| 6928 | ssl->in_len = ssl->in_hdr + 3; |
| 6929 | ssl->in_iv = ssl->in_hdr + 5; |
| 6930 | } |
| 6931 | |
| 6932 | /* Offset in_msg from in_iv to allow space for explicit IV, if used. */ |
| 6933 | if( transform != NULL && |
| 6934 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 6935 | { |
| 6936 | ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen; |
| 6937 | } |
| 6938 | else |
| 6939 | ssl->in_msg = ssl->in_iv; |
| 6940 | } |
| 6941 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6942 | /* |
| 6943 | * Initialize an SSL context |
| 6944 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 6945 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 6946 | { |
| 6947 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 6948 | } |
| 6949 | |
| 6950 | /* |
| 6951 | * Setup an SSL context |
| 6952 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 6953 | |
| 6954 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
| 6955 | { |
| 6956 | /* Set the incoming and outgoing record pointers. */ |
| 6957 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6958 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6959 | { |
| 6960 | ssl->out_hdr = ssl->out_buf; |
| 6961 | ssl->in_hdr = ssl->in_buf; |
| 6962 | } |
| 6963 | else |
| 6964 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6965 | { |
| 6966 | ssl->out_hdr = ssl->out_buf + 8; |
| 6967 | ssl->in_hdr = ssl->in_buf + 8; |
| 6968 | } |
| 6969 | |
| 6970 | /* Derive other internal pointers. */ |
| 6971 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
| 6972 | ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ ); |
| 6973 | } |
| 6974 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 6975 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 6976 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6977 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6978 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6979 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 6980 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 6981 | |
| 6982 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 6983 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 6984 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 6985 | |
| 6986 | /* Set to NULL in case of an error condition */ |
| 6987 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 6988 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6989 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 6990 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6991 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6992 | 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] | 6993 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 6994 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 6998 | if( ssl->out_buf == NULL ) |
| 6999 | { |
| 7000 | 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] | 7001 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7002 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7003 | } |
| 7004 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7005 | ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 7006 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7007 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7008 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7009 | |
| 7010 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7011 | |
| 7012 | error: |
| 7013 | mbedtls_free( ssl->in_buf ); |
| 7014 | mbedtls_free( ssl->out_buf ); |
| 7015 | |
| 7016 | ssl->conf = NULL; |
| 7017 | |
| 7018 | ssl->in_buf = NULL; |
| 7019 | ssl->out_buf = NULL; |
| 7020 | |
| 7021 | ssl->in_hdr = NULL; |
| 7022 | ssl->in_ctr = NULL; |
| 7023 | ssl->in_len = NULL; |
| 7024 | ssl->in_iv = NULL; |
| 7025 | ssl->in_msg = NULL; |
| 7026 | |
| 7027 | ssl->out_hdr = NULL; |
| 7028 | ssl->out_ctr = NULL; |
| 7029 | ssl->out_len = NULL; |
| 7030 | ssl->out_iv = NULL; |
| 7031 | ssl->out_msg = NULL; |
| 7032 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 7033 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7037 | * Reset an initialized and used SSL context for re-use while retaining |
| 7038 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7039 | * |
| 7040 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 7041 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7042 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7043 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7044 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7045 | int ret; |
| 7046 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 7047 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 7048 | !defined(MBEDTLS_SSL_SRV_C) |
| 7049 | ((void) partial); |
| 7050 | #endif |
| 7051 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7052 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7053 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 7054 | /* Cancel any possibly running timer */ |
| 7055 | ssl_set_timer( ssl, 0 ); |
| 7056 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7057 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7058 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7059 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7060 | |
| 7061 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7062 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 7063 | 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] | 7064 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7065 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7066 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7067 | ssl->in_offt = NULL; |
Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 7068 | ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7069 | |
| 7070 | ssl->in_msgtype = 0; |
| 7071 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7072 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 7073 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7074 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 7075 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7076 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 7077 | ssl_dtls_replay_reset( ssl ); |
| 7078 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7079 | |
| 7080 | ssl->in_hslen = 0; |
| 7081 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 7082 | |
| 7083 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7084 | |
| 7085 | ssl->out_msgtype = 0; |
| 7086 | ssl->out_msglen = 0; |
| 7087 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7088 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 7089 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 7090 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 7091 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7092 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7093 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7094 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7095 | ssl->transform_in = NULL; |
| 7096 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7097 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 7098 | ssl->session_in = NULL; |
| 7099 | ssl->session_out = NULL; |
| 7100 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7101 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7102 | |
| 7103 | #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] | 7104 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7105 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 7106 | { |
| 7107 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7108 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7109 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7111 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7112 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7113 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7114 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 7115 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7116 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7117 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 7118 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7119 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7120 | } |
| 7121 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7122 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7123 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7124 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7125 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7126 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7127 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7128 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7129 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 7130 | if( ssl->session ) |
| 7131 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7132 | mbedtls_ssl_session_free( ssl->session ); |
| 7133 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 7134 | ssl->session = NULL; |
| 7135 | } |
| 7136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7137 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7138 | ssl->alpn_chosen = NULL; |
| 7139 | #endif |
| 7140 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 7141 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7142 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7143 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7144 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7145 | { |
| 7146 | mbedtls_free( ssl->cli_id ); |
| 7147 | ssl->cli_id = NULL; |
| 7148 | ssl->cli_id_len = 0; |
| 7149 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 7150 | #endif |
| 7151 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7152 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 7153 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7154 | |
| 7155 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7156 | } |
| 7157 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 7158 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7159 | * Reset an initialized and used SSL context for re-use while retaining |
| 7160 | * all application-set variables, function pointers and data. |
| 7161 | */ |
| 7162 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 7163 | { |
| 7164 | return( ssl_session_reset_int( ssl, 0 ) ); |
| 7165 | } |
| 7166 | |
| 7167 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7168 | * SSL set accessors |
| 7169 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7170 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7171 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7172 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7173 | } |
| 7174 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7175 | 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] | 7176 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7177 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 7178 | } |
| 7179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7180 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7181 | 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] | 7182 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7183 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 7184 | } |
| 7185 | #endif |
| 7186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7187 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7188 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 7189 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7190 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 7191 | } |
| 7192 | #endif |
| 7193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7194 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 7195 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 7196 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 7197 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 7198 | { |
| 7199 | ssl->disable_datagram_packing = !allow_packing; |
| 7200 | } |
| 7201 | |
| 7202 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 7203 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 7204 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7205 | conf->hs_timeout_min = min; |
| 7206 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 7207 | } |
| 7208 | #endif |
| 7209 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7210 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7211 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7212 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7213 | } |
| 7214 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7215 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7216 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7217 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7218 | void *p_vrfy ) |
| 7219 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7220 | conf->f_vrfy = f_vrfy; |
| 7221 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7222 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7223 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7224 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7225 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 7226 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7227 | void *p_rng ) |
| 7228 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 7229 | conf->f_rng = f_rng; |
| 7230 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7231 | } |
| 7232 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7233 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 7234 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7235 | void *p_dbg ) |
| 7236 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7237 | conf->f_dbg = f_dbg; |
| 7238 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7239 | } |
| 7240 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7241 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7242 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 7243 | mbedtls_ssl_send_t *f_send, |
| 7244 | mbedtls_ssl_recv_t *f_recv, |
| 7245 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7246 | { |
| 7247 | ssl->p_bio = p_bio; |
| 7248 | ssl->f_send = f_send; |
| 7249 | ssl->f_recv = f_recv; |
| 7250 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 7251 | } |
| 7252 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 7253 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7254 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 7255 | { |
| 7256 | ssl->mtu = mtu; |
| 7257 | } |
| 7258 | #endif |
| 7259 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7260 | 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] | 7261 | { |
| 7262 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7263 | } |
| 7264 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7265 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 7266 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 7267 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 7268 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7269 | { |
| 7270 | ssl->p_timer = p_timer; |
| 7271 | ssl->f_set_timer = f_set_timer; |
| 7272 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 7273 | |
| 7274 | /* Make sure we start with no timer running */ |
| 7275 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7276 | } |
| 7277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7278 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7279 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7280 | void *p_cache, |
| 7281 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 7282 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7283 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7284 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7285 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7286 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7287 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7288 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7290 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7291 | 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] | 7292 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7293 | int ret; |
| 7294 | |
| 7295 | if( ssl == NULL || |
| 7296 | session == NULL || |
| 7297 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7298 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7300 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7301 | } |
| 7302 | |
| 7303 | if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 ) |
| 7304 | return( ret ); |
| 7305 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7306 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7307 | |
| 7308 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7309 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7310 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7311 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7312 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7313 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7314 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7315 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 7316 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 7317 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 7318 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 7319 | } |
| 7320 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7321 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7322 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 7323 | int major, int minor ) |
| 7324 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7325 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 7326 | return; |
| 7327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7328 | 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] | 7329 | return; |
| 7330 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7331 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7332 | } |
| 7333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7334 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 7335 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 7336 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 7337 | { |
| 7338 | conf->cert_profile = profile; |
| 7339 | } |
| 7340 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7341 | /* Append a new keycert entry to a (possibly empty) list */ |
| 7342 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 7343 | mbedtls_x509_crt *cert, |
| 7344 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7345 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7346 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7347 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7348 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 7349 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 7350 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7351 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7352 | new_cert->cert = cert; |
| 7353 | new_cert->key = key; |
| 7354 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7355 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7356 | /* Update head is the list was null, else add to the end */ |
| 7357 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 7358 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7359 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 7360 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7361 | else |
| 7362 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7363 | mbedtls_ssl_key_cert *cur = *head; |
| 7364 | while( cur->next != NULL ) |
| 7365 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7366 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7367 | } |
| 7368 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7369 | return( 0 ); |
| 7370 | } |
| 7371 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7372 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7373 | mbedtls_x509_crt *own_cert, |
| 7374 | mbedtls_pk_context *pk_key ) |
| 7375 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 7376 | 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] | 7377 | } |
| 7378 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7379 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 7380 | mbedtls_x509_crt *ca_chain, |
| 7381 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7382 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 7383 | conf->ca_chain = ca_chain; |
| 7384 | conf->ca_crl = ca_crl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7385 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7386 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 7387 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 7388 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7389 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 7390 | mbedtls_x509_crt *own_cert, |
| 7391 | mbedtls_pk_context *pk_key ) |
| 7392 | { |
| 7393 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 7394 | own_cert, pk_key ) ); |
| 7395 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 7396 | |
| 7397 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 7398 | mbedtls_x509_crt *ca_chain, |
| 7399 | mbedtls_x509_crl *ca_crl ) |
| 7400 | { |
| 7401 | ssl->handshake->sni_ca_chain = ca_chain; |
| 7402 | ssl->handshake->sni_ca_crl = ca_crl; |
| 7403 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7404 | |
| 7405 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 7406 | int authmode ) |
| 7407 | { |
| 7408 | ssl->handshake->sni_authmode = authmode; |
| 7409 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 7410 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 7411 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 7412 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 7413 | /* |
| 7414 | * Set EC J-PAKE password for current handshake |
| 7415 | */ |
| 7416 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 7417 | const unsigned char *pw, |
| 7418 | size_t pw_len ) |
| 7419 | { |
| 7420 | mbedtls_ecjpake_role role; |
| 7421 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 7422 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 7423 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7424 | |
| 7425 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7426 | role = MBEDTLS_ECJPAKE_SERVER; |
| 7427 | else |
| 7428 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 7429 | |
| 7430 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 7431 | role, |
| 7432 | MBEDTLS_MD_SHA256, |
| 7433 | MBEDTLS_ECP_DP_SECP256R1, |
| 7434 | pw, pw_len ) ); |
| 7435 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 7436 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 7437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7438 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7439 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 7440 | const unsigned char *psk, size_t psk_len, |
| 7441 | const unsigned char *psk_identity, size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 7442 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7443 | if( psk == NULL || psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7444 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7446 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 7447 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 7448 | |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 7449 | /* Identity len will be encoded on two bytes */ |
| 7450 | if( ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7451 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 7452 | { |
| 7453 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7454 | } |
| 7455 | |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7456 | if( conf->psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7457 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7458 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7459 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7460 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 7461 | conf->psk = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7462 | conf->psk_len = 0; |
| 7463 | } |
| 7464 | if( conf->psk_identity != NULL ) |
| 7465 | { |
| 7466 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 7467 | conf->psk_identity = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7468 | conf->psk_identity_len = 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7469 | } |
| 7470 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7471 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL || |
| 7472 | ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL ) |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 7473 | { |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7474 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 7475 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7476 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 7477 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 7478 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 7479 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7480 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7481 | conf->psk_len = psk_len; |
| 7482 | conf->psk_identity_len = psk_identity_len; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7483 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7484 | memcpy( conf->psk, psk, conf->psk_len ); |
| 7485 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7486 | |
| 7487 | return( 0 ); |
| 7488 | } |
| 7489 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 7490 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 7491 | const unsigned char *psk, size_t psk_len ) |
| 7492 | { |
| 7493 | if( psk == NULL || ssl->handshake == NULL ) |
| 7494 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7495 | |
| 7496 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 7497 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7498 | |
| 7499 | if( ssl->handshake->psk != NULL ) |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 7500 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7501 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 7502 | ssl->handshake->psk_len ); |
Simon Butcher | 5b8d1d6 | 2015-10-04 22:06:51 +0100 | [diff] [blame] | 7503 | mbedtls_free( ssl->handshake->psk ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7504 | ssl->handshake->psk_len = 0; |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 7505 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 7506 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7507 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 7508 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 7509 | |
| 7510 | ssl->handshake->psk_len = psk_len; |
| 7511 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 7512 | |
| 7513 | return( 0 ); |
| 7514 | } |
| 7515 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7516 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7517 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7518 | size_t), |
| 7519 | void *p_psk ) |
| 7520 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7521 | conf->f_psk = f_psk; |
| 7522 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 7523 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7524 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 7525 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 7526 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 7527 | |
| 7528 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7529 | 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] | 7530 | { |
| 7531 | int ret; |
| 7532 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 7533 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 7534 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 7535 | { |
| 7536 | mbedtls_mpi_free( &conf->dhm_P ); |
| 7537 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7538 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 7539 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7540 | |
| 7541 | return( 0 ); |
| 7542 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 7543 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7544 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 7545 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 7546 | const unsigned char *dhm_P, size_t P_len, |
| 7547 | const unsigned char *dhm_G, size_t G_len ) |
| 7548 | { |
| 7549 | int ret; |
| 7550 | |
| 7551 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 7552 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 7553 | { |
| 7554 | mbedtls_mpi_free( &conf->dhm_P ); |
| 7555 | mbedtls_mpi_free( &conf->dhm_G ); |
| 7556 | return( ret ); |
| 7557 | } |
| 7558 | |
| 7559 | return( 0 ); |
| 7560 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7561 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7562 | 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] | 7563 | { |
| 7564 | int ret; |
| 7565 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 7566 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 7567 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 7568 | { |
| 7569 | mbedtls_mpi_free( &conf->dhm_P ); |
| 7570 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 7571 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 7572 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 7573 | |
| 7574 | return( 0 ); |
| 7575 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 7576 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 7577 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 7578 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 7579 | /* |
| 7580 | * Set the minimum length for Diffie-Hellman parameters |
| 7581 | */ |
| 7582 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 7583 | unsigned int bitlen ) |
| 7584 | { |
| 7585 | conf->dhm_min_bitlen = bitlen; |
| 7586 | } |
| 7587 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 7588 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 7589 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 7590 | /* |
| 7591 | * Set allowed/preferred hashes for handshake signatures |
| 7592 | */ |
| 7593 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 7594 | const int *hashes ) |
| 7595 | { |
| 7596 | conf->sig_hashes = hashes; |
| 7597 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7598 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 7599 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7600 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 7601 | /* |
| 7602 | * Set the allowed elliptic curves |
| 7603 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7604 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7605 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 7606 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7607 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 7608 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7609 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 7610 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 7611 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7612 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7613 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7614 | /* Initialize to suppress unnecessary compiler warning */ |
| 7615 | size_t hostname_len = 0; |
| 7616 | |
| 7617 | /* Check if new hostname is valid before |
| 7618 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7619 | if( hostname != NULL ) |
| 7620 | { |
| 7621 | hostname_len = strlen( hostname ); |
| 7622 | |
| 7623 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 7624 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7625 | } |
| 7626 | |
| 7627 | /* Now it's clear that we will overwrite the old hostname, |
| 7628 | * so we can free it safely */ |
| 7629 | |
| 7630 | if( ssl->hostname != NULL ) |
| 7631 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7632 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7633 | mbedtls_free( ssl->hostname ); |
| 7634 | } |
| 7635 | |
| 7636 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 7637 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7638 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7639 | { |
| 7640 | ssl->hostname = NULL; |
| 7641 | } |
| 7642 | else |
| 7643 | { |
| 7644 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7645 | if( ssl->hostname == NULL ) |
| 7646 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 7647 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7648 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 7649 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7650 | ssl->hostname[hostname_len] = '\0'; |
| 7651 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7652 | |
| 7653 | return( 0 ); |
| 7654 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 7655 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7656 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 7657 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7658 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7659 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 7660 | const unsigned char *, size_t), |
| 7661 | void *p_sni ) |
| 7662 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7663 | conf->f_sni = f_sni; |
| 7664 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 7665 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7666 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 7667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7668 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7669 | 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] | 7670 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 7671 | size_t cur_len, tot_len; |
| 7672 | const char **p; |
| 7673 | |
| 7674 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 7675 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 7676 | * MUST NOT be truncated." |
| 7677 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 7678 | */ |
| 7679 | tot_len = 0; |
| 7680 | for( p = protos; *p != NULL; p++ ) |
| 7681 | { |
| 7682 | cur_len = strlen( *p ); |
| 7683 | tot_len += cur_len; |
| 7684 | |
Ronald Cron | 157cffe | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 7685 | if( ( cur_len == 0 ) || |
| 7686 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 7687 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7688 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 7689 | } |
| 7690 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7691 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 7692 | |
| 7693 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7694 | } |
| 7695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7696 | 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] | 7697 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 7698 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7699 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7700 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7701 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7702 | 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] | 7703 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7704 | conf->max_major_ver = major; |
| 7705 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 7706 | } |
| 7707 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7708 | 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] | 7709 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7710 | conf->min_major_ver = major; |
| 7711 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 7712 | } |
| 7713 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7714 | #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] | 7715 | 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] | 7716 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 7717 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 7718 | } |
| 7719 | #endif |
| 7720 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 7721 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7722 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 7723 | char cert_req_ca_list ) |
| 7724 | { |
| 7725 | conf->cert_req_ca_list = cert_req_ca_list; |
| 7726 | } |
| 7727 | #endif |
| 7728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7729 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7730 | 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] | 7731 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7732 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 7733 | } |
| 7734 | #endif |
| 7735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7736 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7737 | 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] | 7738 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7739 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 7740 | } |
| 7741 | #endif |
| 7742 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 7743 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7744 | 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] | 7745 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7746 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 7747 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 7748 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 7749 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7750 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7751 | 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] | 7752 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7753 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7754 | 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] | 7755 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7756 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 7757 | } |
| 7758 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 7759 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 7760 | |
| 7761 | return( 0 ); |
| 7762 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7763 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 7764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7765 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7766 | 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] | 7767 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7768 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 7769 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7770 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 7771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7772 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7773 | 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] | 7774 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 7775 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 7776 | } |
| 7777 | #endif |
| 7778 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7779 | 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] | 7780 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7781 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7782 | } |
| 7783 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7784 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7785 | 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] | 7786 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7787 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7788 | } |
| 7789 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7790 | 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] | 7791 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7792 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 7793 | } |
| 7794 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7795 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 7796 | const unsigned char period[8] ) |
| 7797 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7798 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 7799 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7800 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7801 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7802 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 7803 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7804 | 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] | 7805 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 7806 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 7807 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 7808 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 7809 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 7810 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 7811 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 7812 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 7813 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 7814 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 7815 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 7816 | conf->f_ticket_write = f_ticket_write; |
| 7817 | conf->f_ticket_parse = f_ticket_parse; |
| 7818 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 7819 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 7820 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7821 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 7822 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 7823 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 7824 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 7825 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 7826 | void *p_export_keys ) |
| 7827 | { |
| 7828 | conf->f_export_keys = f_export_keys; |
| 7829 | conf->p_export_keys = p_export_keys; |
| 7830 | } |
| 7831 | #endif |
| 7832 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 7833 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 7834 | void mbedtls_ssl_conf_async_private_cb( |
| 7835 | mbedtls_ssl_config *conf, |
| 7836 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 7837 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 7838 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 7839 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 7840 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 7841 | { |
| 7842 | conf->f_async_sign_start = f_async_sign; |
| 7843 | conf->f_async_decrypt_start = f_async_decrypt; |
| 7844 | conf->f_async_resume = f_async_resume; |
| 7845 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 7846 | conf->p_async_config_data = async_config_data; |
| 7847 | } |
| 7848 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 7849 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 7850 | { |
| 7851 | return( conf->p_async_config_data ); |
| 7852 | } |
| 7853 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 7854 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 7855 | { |
| 7856 | if( ssl->handshake == NULL ) |
| 7857 | return( NULL ); |
| 7858 | else |
| 7859 | return( ssl->handshake->user_async_ctx ); |
| 7860 | } |
| 7861 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 7862 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 7863 | void *ctx ) |
| 7864 | { |
| 7865 | if( ssl->handshake != NULL ) |
| 7866 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 7867 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 7868 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 7869 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7870 | /* |
| 7871 | * SSL get accessors |
| 7872 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7873 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7874 | { |
| 7875 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 7876 | } |
| 7877 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 7878 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 7879 | { |
| 7880 | /* |
| 7881 | * Case A: We're currently holding back |
| 7882 | * a message for further processing. |
| 7883 | */ |
| 7884 | |
| 7885 | if( ssl->keep_current_message == 1 ) |
| 7886 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 7887 | 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] | 7888 | return( 1 ); |
| 7889 | } |
| 7890 | |
| 7891 | /* |
| 7892 | * Case B: Further records are pending in the current datagram. |
| 7893 | */ |
| 7894 | |
| 7895 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7896 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7897 | ssl->in_left > ssl->next_record_offset ) |
| 7898 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 7899 | 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] | 7900 | return( 1 ); |
| 7901 | } |
| 7902 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7903 | |
| 7904 | /* |
| 7905 | * Case C: A handshake message is being processed. |
| 7906 | */ |
| 7907 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 7908 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 7909 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 7910 | 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] | 7911 | return( 1 ); |
| 7912 | } |
| 7913 | |
| 7914 | /* |
| 7915 | * Case D: An application data message is being processed |
| 7916 | */ |
| 7917 | if( ssl->in_offt != NULL ) |
| 7918 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 7919 | 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] | 7920 | return( 1 ); |
| 7921 | } |
| 7922 | |
| 7923 | /* |
| 7924 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 7925 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 7926 | * we implement support for multiple alerts in single records. |
| 7927 | */ |
| 7928 | |
| 7929 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 7930 | return( 0 ); |
| 7931 | } |
| 7932 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7933 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7934 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 7935 | if( ssl->session != NULL ) |
| 7936 | return( ssl->session->verify_result ); |
| 7937 | |
| 7938 | if( ssl->session_negotiate != NULL ) |
| 7939 | return( ssl->session_negotiate->verify_result ); |
| 7940 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 7941 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7942 | } |
| 7943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7944 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 7945 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 7946 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 7947 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 7948 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7949 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 7950 | } |
| 7951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7952 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7953 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7954 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7955 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 7956 | { |
| 7957 | switch( ssl->minor_ver ) |
| 7958 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7959 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 7960 | return( "DTLSv1.0" ); |
| 7961 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7962 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 7963 | return( "DTLSv1.2" ); |
| 7964 | |
| 7965 | default: |
| 7966 | return( "unknown (DTLS)" ); |
| 7967 | } |
| 7968 | } |
| 7969 | #endif |
| 7970 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7971 | switch( ssl->minor_ver ) |
| 7972 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7973 | case MBEDTLS_SSL_MINOR_VERSION_0: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7974 | return( "SSLv3.0" ); |
| 7975 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7976 | case MBEDTLS_SSL_MINOR_VERSION_1: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7977 | return( "TLSv1.0" ); |
| 7978 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7979 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7980 | return( "TLSv1.1" ); |
| 7981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7982 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7983 | return( "TLSv1.2" ); |
| 7984 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7985 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 7986 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7987 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7988 | } |
| 7989 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7990 | 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] | 7991 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 7992 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7993 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 7994 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 7995 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 7996 | if( transform == NULL ) |
| 7997 | return( (int) mbedtls_ssl_hdr_len( ssl ) ); |
| 7998 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7999 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 8000 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 8001 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8002 | #endif |
| 8003 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8004 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8005 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8006 | case MBEDTLS_MODE_GCM: |
| 8007 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8008 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8009 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8010 | transform_expansion = transform->minlen; |
| 8011 | break; |
| 8012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8013 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8014 | |
| 8015 | block_size = mbedtls_cipher_get_block_size( |
| 8016 | &transform->cipher_ctx_enc ); |
| 8017 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 8018 | /* Expansion due to the addition of the MAC. */ |
| 8019 | transform_expansion += transform->maclen; |
| 8020 | |
| 8021 | /* Expansion due to the addition of CBC padding; |
| 8022 | * Theoretically up to 256 bytes, but we never use |
| 8023 | * more than the block size of the underlying cipher. */ |
| 8024 | transform_expansion += block_size; |
| 8025 | |
| 8026 | /* For TLS 1.1 or higher, an explicit IV is added |
| 8027 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8028 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 8029 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 8030 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8031 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 8032 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8033 | break; |
| 8034 | |
| 8035 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 8036 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8037 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8038 | } |
| 8039 | |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 8040 | return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8041 | } |
| 8042 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 8043 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8044 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 8045 | { |
| 8046 | size_t max_len; |
| 8047 | |
| 8048 | /* |
| 8049 | * Assume mfl_code is correct since it was checked when set |
| 8050 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8051 | 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] | 8052 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 8053 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 8054 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8055 | 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] | 8056 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8057 | 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] | 8058 | } |
| 8059 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 8060 | /* During a handshake, use the value being negotiated */ |
| 8061 | if( ssl->session_negotiate != NULL && |
| 8062 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 8063 | { |
| 8064 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 8065 | } |
| 8066 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8067 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 8068 | } |
| 8069 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 8070 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8071 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8072 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 8073 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 8074 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 8075 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 8076 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 8077 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 8078 | return ( 0 ); |
| 8079 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8080 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 8081 | return( ssl->mtu ); |
| 8082 | |
| 8083 | if( ssl->mtu == 0 ) |
| 8084 | return( ssl->handshake->mtu ); |
| 8085 | |
| 8086 | return( ssl->mtu < ssl->handshake->mtu ? |
| 8087 | ssl->mtu : ssl->handshake->mtu ); |
| 8088 | } |
| 8089 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8090 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8091 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 8092 | { |
| 8093 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 8094 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 8095 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 8096 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8097 | (void) ssl; |
| 8098 | #endif |
| 8099 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8100 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8101 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 8102 | |
| 8103 | if( max_len > mfl ) |
| 8104 | max_len = mfl; |
| 8105 | #endif |
| 8106 | |
| 8107 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8108 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8109 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8110 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8111 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 8112 | const size_t overhead = (size_t) ret; |
| 8113 | |
| 8114 | if( ret < 0 ) |
| 8115 | return( ret ); |
| 8116 | |
| 8117 | if( mtu <= overhead ) |
| 8118 | { |
| 8119 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 8120 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 8121 | } |
| 8122 | |
| 8123 | if( max_len > mtu - overhead ) |
| 8124 | max_len = mtu - overhead; |
| 8125 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8126 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8127 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 8128 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 8129 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8130 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8131 | #endif |
| 8132 | |
| 8133 | return( (int) max_len ); |
| 8134 | } |
| 8135 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8136 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8137 | 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] | 8138 | { |
| 8139 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8140 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8141 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8142 | return( ssl->session->peer_cert ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8143 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8144 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8145 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8146 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8147 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8148 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8149 | if( ssl == NULL || |
| 8150 | dst == NULL || |
| 8151 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8152 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8153 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8154 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8155 | } |
| 8156 | |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8157 | return( ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8158 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8159 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8160 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8161 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8162 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8163 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8164 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8165 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8166 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8167 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8168 | if( ssl == NULL || ssl->conf == NULL ) |
| 8169 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8171 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8172 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8173 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8174 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8175 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8176 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8177 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8178 | #endif |
| 8179 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8180 | return( ret ); |
| 8181 | } |
| 8182 | |
| 8183 | /* |
| 8184 | * Perform the SSL handshake |
| 8185 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8186 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8187 | { |
| 8188 | int ret = 0; |
| 8189 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8190 | if( ssl == NULL || ssl->conf == NULL ) |
| 8191 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8192 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8193 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8195 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8197 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8198 | |
| 8199 | if( ret != 0 ) |
| 8200 | break; |
| 8201 | } |
| 8202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8203 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8204 | |
| 8205 | return( ret ); |
| 8206 | } |
| 8207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8208 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8209 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8210 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8211 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8212 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8213 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8214 | { |
| 8215 | int ret; |
| 8216 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8217 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8218 | |
| 8219 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8220 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 8221 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8222 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8223 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8224 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8225 | 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] | 8226 | return( ret ); |
| 8227 | } |
| 8228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8229 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8230 | |
| 8231 | return( 0 ); |
| 8232 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8233 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8234 | |
| 8235 | /* |
| 8236 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8237 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 8238 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 8239 | * - 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] | 8240 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8241 | * 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] | 8242 | * 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] | 8243 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8244 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8245 | { |
| 8246 | int ret; |
| 8247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8248 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8249 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8250 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 8251 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8252 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 8253 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 8254 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8255 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8256 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8257 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 8258 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8259 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 8260 | ssl->handshake->out_msg_seq = 1; |
| 8261 | else |
| 8262 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 8263 | } |
| 8264 | #endif |
| 8265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8266 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 8267 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8268 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8269 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8270 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8271 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8272 | return( ret ); |
| 8273 | } |
| 8274 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8275 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8276 | |
| 8277 | return( 0 ); |
| 8278 | } |
| 8279 | |
| 8280 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8281 | * Renegotiate current connection on client, |
| 8282 | * or request renegotiation on server |
| 8283 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8284 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8285 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8286 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8287 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8288 | if( ssl == NULL || ssl->conf == NULL ) |
| 8289 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8291 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8292 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8293 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8294 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8295 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 8296 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8298 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 8299 | |
| 8300 | /* Did we already try/start sending HelloRequest? */ |
| 8301 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8302 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 8303 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8304 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8305 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8306 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8308 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8309 | /* |
| 8310 | * On client, either start the renegotiation process or, |
| 8311 | * if already in progress, continue the handshake |
| 8312 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8313 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8314 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8315 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 8316 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8317 | |
| 8318 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 8319 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8320 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8321 | return( ret ); |
| 8322 | } |
| 8323 | } |
| 8324 | else |
| 8325 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8326 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8327 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8328 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8329 | return( ret ); |
| 8330 | } |
| 8331 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8332 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8333 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 8334 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8335 | } |
| 8336 | |
| 8337 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8338 | * Check record counters and renegotiate if they're above the limit. |
| 8339 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8340 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8341 | { |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 8342 | size_t ep_len = ssl_ep_len( ssl ); |
| 8343 | int in_ctr_cmp; |
| 8344 | int out_ctr_cmp; |
| 8345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8346 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 8347 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8348 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8349 | { |
| 8350 | return( 0 ); |
| 8351 | } |
| 8352 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 8353 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 8354 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8355 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 8356 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 8357 | |
| 8358 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8359 | { |
| 8360 | return( 0 ); |
| 8361 | } |
| 8362 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 8363 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8364 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8365 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8366 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8367 | |
| 8368 | /* |
| 8369 | * Receive application data decrypted from the SSL layer |
| 8370 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8371 | 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] | 8372 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8373 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 8374 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8375 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8376 | if( ssl == NULL || ssl->conf == NULL ) |
| 8377 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8378 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8379 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8380 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8381 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8382 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8383 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8384 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8385 | return( ret ); |
| 8386 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8387 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8388 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8389 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 8390 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8391 | return( ret ); |
| 8392 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8393 | } |
| 8394 | #endif |
| 8395 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8396 | /* |
| 8397 | * Check if renegotiation is necessary and/or handshake is |
| 8398 | * in process. If yes, perform/continue, and fall through |
| 8399 | * if an unexpected packet is received while the client |
| 8400 | * is waiting for the ServerHello. |
| 8401 | * |
| 8402 | * (There is no equivalent to the last condition on |
| 8403 | * the server-side as it is not treated as within |
| 8404 | * a handshake while waiting for the ClientHello |
| 8405 | * after a renegotiation request.) |
| 8406 | */ |
| 8407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8408 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8409 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 8410 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 8411 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8412 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8413 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8414 | return( ret ); |
| 8415 | } |
| 8416 | #endif |
| 8417 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8418 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8419 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8420 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8421 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 8422 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8423 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8424 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8425 | return( ret ); |
| 8426 | } |
| 8427 | } |
| 8428 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 8429 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 8430 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8431 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8432 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 8433 | if( ssl->f_get_timer != NULL && |
| 8434 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 8435 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8436 | ssl_set_timer( ssl, ssl->conf->read_timeout ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 8437 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8438 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 8439 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8440 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8441 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 8442 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 8443 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8444 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 8445 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8446 | } |
| 8447 | |
| 8448 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8449 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8450 | { |
| 8451 | /* |
| 8452 | * OpenSSL sends empty messages to randomize the IV |
| 8453 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 8454 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8455 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8456 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 8457 | return( 0 ); |
| 8458 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8459 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8460 | return( ret ); |
| 8461 | } |
| 8462 | } |
| 8463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8464 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8465 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8466 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8467 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8468 | /* |
| 8469 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 8470 | * - For server-side, expect CLIENT_HELLO. |
| 8471 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 8472 | */ |
| 8473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8474 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8475 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8476 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8477 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8478 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8479 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8480 | |
| 8481 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8482 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8483 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 8484 | { |
| 8485 | continue; |
| 8486 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8487 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8488 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8489 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8490 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8491 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8492 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8493 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8494 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8495 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8496 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8497 | |
| 8498 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8499 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8500 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 8501 | { |
| 8502 | continue; |
| 8503 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8504 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8505 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8506 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8507 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 8508 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 8509 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8510 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 8511 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 8512 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 8513 | ssl->conf->allow_legacy_renegotiation == |
| 8514 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 8515 | { |
| 8516 | /* |
| 8517 | * Accept renegotiation request |
| 8518 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8519 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 8520 | /* DTLS clients need to know renego is server-initiated */ |
| 8521 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8522 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 8523 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8524 | { |
| 8525 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 8526 | } |
| 8527 | #endif |
| 8528 | ret = ssl_start_renegotiation( ssl ); |
| 8529 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 8530 | ret != 0 ) |
| 8531 | { |
| 8532 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 8533 | return( ret ); |
| 8534 | } |
| 8535 | } |
| 8536 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 8537 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8538 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8539 | /* |
| 8540 | * Refuse renegotiation |
| 8541 | */ |
| 8542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8543 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8545 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 8546 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8547 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 8548 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 8549 | we send a fatal alert and abort the connection. */ |
| 8550 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 8551 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 8552 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 8553 | } |
| 8554 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8555 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 8556 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 8557 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 8558 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 8559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8560 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 8561 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 8562 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 8563 | { |
| 8564 | return( ret ); |
| 8565 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8566 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 8567 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8568 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 8569 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 8570 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8571 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 8572 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 8573 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8574 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 8575 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 8576 | /* At this point, we don't know whether the renegotiation has been |
| 8577 | * completed or not. The cases to consider are the following: |
| 8578 | * 1) The renegotiation is complete. In this case, no new record |
| 8579 | * has been read yet. |
| 8580 | * 2) The renegotiation is incomplete because the client received |
| 8581 | * an application data record while awaiting the ServerHello. |
| 8582 | * 3) The renegotiation is incomplete because the client received |
| 8583 | * a non-handshake, non-application data message while awaiting |
| 8584 | * the ServerHello. |
| 8585 | * In each of these case, looping will be the proper action: |
| 8586 | * - For 1), the next iteration will read a new record and check |
| 8587 | * if it's application data. |
| 8588 | * - For 2), the loop condition isn't satisfied as application data |
| 8589 | * is present, hence continue is the same as break |
| 8590 | * - For 3), the loop condition is satisfied and read_record |
| 8591 | * will re-deliver the message that was held back by the client |
| 8592 | * when expecting the ServerHello. |
| 8593 | */ |
| 8594 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8595 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 8596 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8597 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 8598 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8599 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 8600 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8601 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 8602 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8603 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 8604 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8605 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 8606 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 8607 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 8608 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8609 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 8610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8611 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 8612 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 8613 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8614 | 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] | 8615 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 8616 | } |
| 8617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8618 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8619 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8620 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 8621 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8622 | } |
| 8623 | |
| 8624 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8625 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 8626 | /* We're going to return something now, cancel timer, |
| 8627 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8628 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 8629 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8630 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8631 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8632 | /* If we requested renego but received AppData, resend HelloRequest. |
| 8633 | * Do it now, after setting in_offt, to avoid taking this branch |
| 8634 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8635 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8636 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8637 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8638 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 8639 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8640 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8641 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8642 | return( ret ); |
| 8643 | } |
| 8644 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8645 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8646 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8647 | } |
| 8648 | |
| 8649 | n = ( len < ssl->in_msglen ) |
| 8650 | ? len : ssl->in_msglen; |
| 8651 | |
| 8652 | memcpy( buf, ssl->in_offt, n ); |
| 8653 | ssl->in_msglen -= n; |
| 8654 | |
| 8655 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8656 | { |
| 8657 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8658 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 8659 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8660 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8661 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8662 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8663 | /* more data available */ |
| 8664 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8665 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8667 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8668 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 8669 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8670 | } |
| 8671 | |
| 8672 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 8673 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 8674 | * fragment length and buffer size. |
| 8675 | * |
| 8676 | * According to RFC 5246 Section 6.2.1: |
| 8677 | * |
| 8678 | * Zero-length fragments of Application data MAY be sent as they are |
| 8679 | * potentially useful as a traffic analysis countermeasure. |
| 8680 | * |
| 8681 | * Therefore, it is possible that the input message length is 0 and the |
| 8682 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8683 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8684 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8685 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8686 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8687 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 8688 | const size_t max_len = (size_t) ret; |
| 8689 | |
| 8690 | if( ret < 0 ) |
| 8691 | { |
| 8692 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 8693 | return( ret ); |
| 8694 | } |
| 8695 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8696 | if( len > max_len ) |
| 8697 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8698 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8699 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8700 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8701 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8702 | "maximum fragment length: %d > %d", |
| 8703 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8704 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8705 | } |
| 8706 | else |
| 8707 | #endif |
| 8708 | len = max_len; |
| 8709 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8710 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8711 | if( ssl->out_left != 0 ) |
| 8712 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 8713 | /* |
| 8714 | * The user has previously tried to send the data and |
| 8715 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 8716 | * written. In this case, we expect the high-level write function |
| 8717 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 8718 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8719 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8720 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8721 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8722 | return( ret ); |
| 8723 | } |
| 8724 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8725 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 8726 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 8727 | /* |
| 8728 | * The user is trying to send a message the first time, so we need to |
| 8729 | * copy the data into the internal buffers and setup the data structure |
| 8730 | * to keep track of partial writes |
| 8731 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8732 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8733 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8734 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8735 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 8736 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8737 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8738 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8739 | return( ret ); |
| 8740 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8741 | } |
| 8742 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8743 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8744 | } |
| 8745 | |
| 8746 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8747 | * Write application data, doing 1/n-1 splitting if necessary. |
| 8748 | * |
| 8749 | * 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] | 8750 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 8751 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8752 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8753 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8754 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8755 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8756 | { |
| 8757 | int ret; |
| 8758 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 8759 | if( ssl->conf->cbc_record_splitting == |
| 8760 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8761 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8762 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
| 8763 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 8764 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8765 | { |
| 8766 | return( ssl_write_real( ssl, buf, len ) ); |
| 8767 | } |
| 8768 | |
| 8769 | if( ssl->split_done == 0 ) |
| 8770 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 8771 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8772 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 8773 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8774 | } |
| 8775 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 8776 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 8777 | return( ret ); |
| 8778 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8779 | |
| 8780 | return( ret + 1 ); |
| 8781 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8782 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8783 | |
| 8784 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8785 | * Write application data (public-facing wrapper) |
| 8786 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8787 | 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] | 8788 | { |
| 8789 | int ret; |
| 8790 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8791 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8792 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8793 | if( ssl == NULL || ssl->conf == NULL ) |
| 8794 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8795 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8796 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8797 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 8798 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8799 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8800 | return( ret ); |
| 8801 | } |
| 8802 | #endif |
| 8803 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8804 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8805 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8806 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8807 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 8808 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8809 | return( ret ); |
| 8810 | } |
| 8811 | } |
| 8812 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8813 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8814 | ret = ssl_write_split( ssl, buf, len ); |
| 8815 | #else |
| 8816 | ret = ssl_write_real( ssl, buf, len ); |
| 8817 | #endif |
| 8818 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8819 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8820 | |
| 8821 | return( ret ); |
| 8822 | } |
| 8823 | |
| 8824 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8825 | * Notify the peer that the connection is being closed |
| 8826 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8827 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8828 | { |
| 8829 | int ret; |
| 8830 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8831 | if( ssl == NULL || ssl->conf == NULL ) |
| 8832 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8833 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8834 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8835 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 8836 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8837 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8838 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8839 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8840 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8841 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 8842 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 8843 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8845 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8846 | return( ret ); |
| 8847 | } |
| 8848 | } |
| 8849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8850 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8851 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 8852 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8853 | } |
| 8854 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8855 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8856 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8857 | if( transform == NULL ) |
| 8858 | return; |
| 8859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8860 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8861 | deflateEnd( &transform->ctx_deflate ); |
| 8862 | inflateEnd( &transform->ctx_inflate ); |
| 8863 | #endif |
| 8864 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8865 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 8866 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 8867 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8868 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 8869 | mbedtls_md_free( &transform->md_ctx_dec ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 8870 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8871 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8872 | } |
| 8873 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8874 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8875 | 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] | 8876 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8877 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 8878 | |
| 8879 | while( cur != NULL ) |
| 8880 | { |
| 8881 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8882 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 8883 | cur = next; |
| 8884 | } |
| 8885 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8886 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 8887 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8888 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8889 | |
| 8890 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 8891 | { |
| 8892 | unsigned offset; |
| 8893 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 8894 | |
| 8895 | if( hs == NULL ) |
| 8896 | return; |
| 8897 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 8898 | ssl_free_buffered_record( ssl ); |
| 8899 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8900 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 8901 | ssl_buffering_free_slot( ssl, offset ); |
| 8902 | } |
| 8903 | |
| 8904 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 8905 | uint8_t slot ) |
| 8906 | { |
| 8907 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 8908 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 8909 | |
| 8910 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 8911 | return; |
| 8912 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 8913 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8914 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 8915 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 8916 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 8917 | mbedtls_free( hs_buf->data ); |
| 8918 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8919 | } |
| 8920 | } |
| 8921 | |
| 8922 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8923 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8924 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8925 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8926 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 8927 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8928 | if( handshake == NULL ) |
| 8929 | return; |
| 8930 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8931 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 8932 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 8933 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 8934 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8935 | handshake->async_in_progress = 0; |
| 8936 | } |
| 8937 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 8938 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 8939 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 8940 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 8941 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 8942 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 8943 | #endif |
| 8944 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 8945 | #if defined(MBEDTLS_SHA256_C) |
| 8946 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 8947 | #endif |
| 8948 | #if defined(MBEDTLS_SHA512_C) |
| 8949 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 8950 | #endif |
| 8951 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8952 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8953 | #if defined(MBEDTLS_DHM_C) |
| 8954 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8955 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8956 | #if defined(MBEDTLS_ECDH_C) |
| 8957 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 8958 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8959 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8960 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 8961 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8962 | mbedtls_free( handshake->ecjpake_cache ); |
| 8963 | handshake->ecjpake_cache = NULL; |
| 8964 | handshake->ecjpake_cache_len = 0; |
| 8965 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8966 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 8967 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 8968 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 8969 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 8970 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8971 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 8972 | #endif |
| 8973 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8974 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 8975 | if( handshake->psk != NULL ) |
| 8976 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8977 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8978 | mbedtls_free( handshake->psk ); |
| 8979 | } |
| 8980 | #endif |
| 8981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8982 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 8983 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 8984 | /* |
| 8985 | * Free only the linked list wrapper, not the keys themselves |
| 8986 | * since the belong to the SNI callback |
| 8987 | */ |
| 8988 | if( handshake->sni_key_cert != NULL ) |
| 8989 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8990 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 8991 | |
| 8992 | while( cur != NULL ) |
| 8993 | { |
| 8994 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8995 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 8996 | cur = next; |
| 8997 | } |
| 8998 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8999 | #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] | 9000 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 9001 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 9002 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 9003 | #endif |
| 9004 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9005 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9006 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 9007 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 9008 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 9009 | #endif |
| 9010 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9011 | mbedtls_platform_zeroize( handshake, |
| 9012 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9013 | } |
| 9014 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9015 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9016 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 9017 | if( session == NULL ) |
| 9018 | return; |
| 9019 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9020 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 9021 | if( session->peer_cert != NULL ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9022 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9023 | mbedtls_x509_crt_free( session->peer_cert ); |
| 9024 | mbedtls_free( session->peer_cert ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9025 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 9026 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 9027 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9028 | #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] | 9029 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 9030 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 9031 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9032 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9033 | } |
| 9034 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9035 | /* |
| 9036 | * Free an SSL context |
| 9037 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9038 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9039 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 9040 | if( ssl == NULL ) |
| 9041 | return; |
| 9042 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9043 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9044 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 9045 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9046 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9047 | 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] | 9048 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9049 | } |
| 9050 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 9051 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9052 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9053 | 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] | 9054 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9055 | } |
| 9056 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9057 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 9058 | if( ssl->compress_buf != NULL ) |
| 9059 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9060 | 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] | 9061 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 9062 | } |
| 9063 | #endif |
| 9064 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9065 | if( ssl->transform ) |
| 9066 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9067 | mbedtls_ssl_transform_free( ssl->transform ); |
| 9068 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9069 | } |
| 9070 | |
| 9071 | if( ssl->handshake ) |
| 9072 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 9073 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9074 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 9075 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9076 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9077 | mbedtls_free( ssl->handshake ); |
| 9078 | mbedtls_free( ssl->transform_negotiate ); |
| 9079 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9080 | } |
| 9081 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 9082 | if( ssl->session ) |
| 9083 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9084 | mbedtls_ssl_session_free( ssl->session ); |
| 9085 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 9086 | } |
| 9087 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 9088 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 9089 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9090 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9091 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9092 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9093 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 9094 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9096 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 9097 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 9098 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9099 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 9100 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 9101 | } |
| 9102 | #endif |
| 9103 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 9104 | #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] | 9105 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 9106 | #endif |
| 9107 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9108 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 9109 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 9110 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9111 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9112 | } |
| 9113 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9114 | /* |
| 9115 | * Initialze mbedtls_ssl_config |
| 9116 | */ |
| 9117 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 9118 | { |
| 9119 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 9120 | } |
| 9121 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 9122 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 9123 | static int ssl_preset_default_hashes[] = { |
| 9124 | #if defined(MBEDTLS_SHA512_C) |
| 9125 | MBEDTLS_MD_SHA512, |
| 9126 | MBEDTLS_MD_SHA384, |
| 9127 | #endif |
| 9128 | #if defined(MBEDTLS_SHA256_C) |
| 9129 | MBEDTLS_MD_SHA256, |
| 9130 | MBEDTLS_MD_SHA224, |
| 9131 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 9132 | #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] | 9133 | MBEDTLS_MD_SHA1, |
| 9134 | #endif |
| 9135 | MBEDTLS_MD_NONE |
| 9136 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 9137 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 9138 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9139 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 9140 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 9141 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 9142 | 0 |
| 9143 | }; |
| 9144 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9145 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9146 | static int ssl_preset_suiteb_hashes[] = { |
| 9147 | MBEDTLS_MD_SHA256, |
| 9148 | MBEDTLS_MD_SHA384, |
| 9149 | MBEDTLS_MD_NONE |
| 9150 | }; |
| 9151 | #endif |
| 9152 | |
| 9153 | #if defined(MBEDTLS_ECP_C) |
| 9154 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 9155 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9156 | MBEDTLS_ECP_DP_SECP256R1, |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 9157 | #endif |
| 9158 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9159 | MBEDTLS_ECP_DP_SECP384R1, |
Jaeden Amero | 16529b2 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 9160 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9161 | MBEDTLS_ECP_DP_NONE |
| 9162 | }; |
| 9163 | #endif |
| 9164 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9165 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 9166 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9167 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 9168 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9169 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9170 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 9171 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9172 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 9173 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9174 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 9175 | /* Use the functions here so that they are covered in tests, |
| 9176 | * but otherwise access member directly for efficiency */ |
| 9177 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 9178 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9179 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9180 | /* |
| 9181 | * Things that are common to all presets |
| 9182 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 9183 | #if defined(MBEDTLS_SSL_CLI_C) |
| 9184 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 9185 | { |
| 9186 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 9187 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 9188 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 9189 | #endif |
| 9190 | } |
| 9191 | #endif |
| 9192 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9193 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9194 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9195 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9196 | |
| 9197 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9198 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 9199 | #endif |
| 9200 | |
| 9201 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 9202 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 9203 | #endif |
| 9204 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9205 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 9206 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 9207 | #endif |
| 9208 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 9209 | #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] | 9210 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 9211 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 9212 | #endif |
| 9213 | |
| 9214 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 9215 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 9216 | #endif |
| 9217 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 9218 | #if defined(MBEDTLS_SSL_SRV_C) |
| 9219 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 9220 | #endif |
| 9221 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9222 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9223 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 9224 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 9225 | #endif |
| 9226 | |
| 9227 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 9228 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9229 | memset( conf->renego_period, 0x00, 2 ); |
| 9230 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9231 | #endif |
| 9232 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9233 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 9234 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 9235 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 9236 | const unsigned char dhm_p[] = |
| 9237 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 9238 | const unsigned char dhm_g[] = |
| 9239 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 9240 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 9241 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 9242 | dhm_p, sizeof( dhm_p ), |
| 9243 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9244 | { |
| 9245 | return( ret ); |
| 9246 | } |
| 9247 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 9248 | #endif |
| 9249 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9250 | /* |
| 9251 | * Preset-specific defaults |
| 9252 | */ |
| 9253 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9254 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9255 | /* |
| 9256 | * NSA Suite B |
| 9257 | */ |
| 9258 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 9259 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 9260 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 9261 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 9262 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 9263 | |
| 9264 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 9265 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 9266 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 9267 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 9268 | ssl_preset_suiteb_ciphersuites; |
| 9269 | |
| 9270 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9271 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9272 | #endif |
| 9273 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9274 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9275 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 9276 | #endif |
| 9277 | |
| 9278 | #if defined(MBEDTLS_ECP_C) |
| 9279 | conf->curve_list = ssl_preset_suiteb_curves; |
| 9280 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 9281 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9282 | |
| 9283 | /* |
| 9284 | * Default |
| 9285 | */ |
| 9286 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 9287 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 9288 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 9289 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 9290 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 9291 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 9292 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 9293 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 9294 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9295 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 9296 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 9297 | |
| 9298 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9299 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 9300 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 9301 | #endif |
| 9302 | |
| 9303 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 9304 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 9305 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 9306 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 9307 | mbedtls_ssl_list_ciphersuites(); |
| 9308 | |
| 9309 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9310 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 9311 | #endif |
| 9312 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9313 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 9314 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9315 | #endif |
| 9316 | |
| 9317 | #if defined(MBEDTLS_ECP_C) |
| 9318 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 9319 | #endif |
| 9320 | |
| 9321 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 9322 | conf->dhm_min_bitlen = 1024; |
| 9323 | #endif |
| 9324 | } |
| 9325 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9326 | return( 0 ); |
| 9327 | } |
| 9328 | |
| 9329 | /* |
| 9330 | * Free mbedtls_ssl_config |
| 9331 | */ |
| 9332 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 9333 | { |
| 9334 | #if defined(MBEDTLS_DHM_C) |
| 9335 | mbedtls_mpi_free( &conf->dhm_P ); |
| 9336 | mbedtls_mpi_free( &conf->dhm_G ); |
| 9337 | #endif |
| 9338 | |
| 9339 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 9340 | if( conf->psk != NULL ) |
| 9341 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9342 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9343 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 9344 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9345 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 9346 | } |
| 9347 | |
| 9348 | if( conf->psk_identity != NULL ) |
| 9349 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9350 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 9351 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 9352 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9353 | conf->psk_identity_len = 0; |
| 9354 | } |
| 9355 | #endif |
| 9356 | |
| 9357 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9358 | ssl_key_cert_free( conf->key_cert ); |
| 9359 | #endif |
| 9360 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9361 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9362 | } |
| 9363 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 9364 | #if defined(MBEDTLS_PK_C) && \ |
| 9365 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9366 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9367 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9368 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9369 | 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] | 9370 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9371 | #if defined(MBEDTLS_RSA_C) |
| 9372 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 9373 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9374 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9375 | #if defined(MBEDTLS_ECDSA_C) |
| 9376 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 9377 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9378 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9379 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9380 | } |
| 9381 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 9382 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 9383 | { |
| 9384 | switch( type ) { |
| 9385 | case MBEDTLS_PK_RSA: |
| 9386 | return( MBEDTLS_SSL_SIG_RSA ); |
| 9387 | case MBEDTLS_PK_ECDSA: |
| 9388 | case MBEDTLS_PK_ECKEY: |
| 9389 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 9390 | default: |
| 9391 | return( MBEDTLS_SSL_SIG_ANON ); |
| 9392 | } |
| 9393 | } |
| 9394 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9395 | 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] | 9396 | { |
| 9397 | switch( sig ) |
| 9398 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9399 | #if defined(MBEDTLS_RSA_C) |
| 9400 | case MBEDTLS_SSL_SIG_RSA: |
| 9401 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9402 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9403 | #if defined(MBEDTLS_ECDSA_C) |
| 9404 | case MBEDTLS_SSL_SIG_ECDSA: |
| 9405 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9406 | #endif |
| 9407 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9408 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9409 | } |
| 9410 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 9411 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9412 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 9413 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 9414 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 9415 | |
| 9416 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 9417 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 9418 | mbedtls_pk_type_t sig_alg ) |
| 9419 | { |
| 9420 | switch( sig_alg ) |
| 9421 | { |
| 9422 | case MBEDTLS_PK_RSA: |
| 9423 | return( set->rsa ); |
| 9424 | case MBEDTLS_PK_ECDSA: |
| 9425 | return( set->ecdsa ); |
| 9426 | default: |
| 9427 | return( MBEDTLS_MD_NONE ); |
| 9428 | } |
| 9429 | } |
| 9430 | |
| 9431 | /* Add a signature-hash-pair to a signature-hash set */ |
| 9432 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 9433 | mbedtls_pk_type_t sig_alg, |
| 9434 | mbedtls_md_type_t md_alg ) |
| 9435 | { |
| 9436 | switch( sig_alg ) |
| 9437 | { |
| 9438 | case MBEDTLS_PK_RSA: |
| 9439 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 9440 | set->rsa = md_alg; |
| 9441 | break; |
| 9442 | |
| 9443 | case MBEDTLS_PK_ECDSA: |
| 9444 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 9445 | set->ecdsa = md_alg; |
| 9446 | break; |
| 9447 | |
| 9448 | default: |
| 9449 | break; |
| 9450 | } |
| 9451 | } |
| 9452 | |
| 9453 | /* Allow exactly one hash algorithm for each signature. */ |
| 9454 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 9455 | mbedtls_md_type_t md_alg ) |
| 9456 | { |
| 9457 | set->rsa = md_alg; |
| 9458 | set->ecdsa = md_alg; |
| 9459 | } |
| 9460 | |
| 9461 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 9462 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 9463 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 9464 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9465 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 9466 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9467 | 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] | 9468 | { |
| 9469 | switch( hash ) |
| 9470 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9471 | #if defined(MBEDTLS_MD5_C) |
| 9472 | case MBEDTLS_SSL_HASH_MD5: |
| 9473 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9474 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9475 | #if defined(MBEDTLS_SHA1_C) |
| 9476 | case MBEDTLS_SSL_HASH_SHA1: |
| 9477 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9478 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9479 | #if defined(MBEDTLS_SHA256_C) |
| 9480 | case MBEDTLS_SSL_HASH_SHA224: |
| 9481 | return( MBEDTLS_MD_SHA224 ); |
| 9482 | case MBEDTLS_SSL_HASH_SHA256: |
| 9483 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9484 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9485 | #if defined(MBEDTLS_SHA512_C) |
| 9486 | case MBEDTLS_SSL_HASH_SHA384: |
| 9487 | return( MBEDTLS_MD_SHA384 ); |
| 9488 | case MBEDTLS_SSL_HASH_SHA512: |
| 9489 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9490 | #endif |
| 9491 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9492 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9493 | } |
| 9494 | } |
| 9495 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9496 | /* |
| 9497 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 9498 | */ |
| 9499 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 9500 | { |
| 9501 | switch( md ) |
| 9502 | { |
| 9503 | #if defined(MBEDTLS_MD5_C) |
| 9504 | case MBEDTLS_MD_MD5: |
| 9505 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 9506 | #endif |
| 9507 | #if defined(MBEDTLS_SHA1_C) |
| 9508 | case MBEDTLS_MD_SHA1: |
| 9509 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 9510 | #endif |
| 9511 | #if defined(MBEDTLS_SHA256_C) |
| 9512 | case MBEDTLS_MD_SHA224: |
| 9513 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 9514 | case MBEDTLS_MD_SHA256: |
| 9515 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 9516 | #endif |
| 9517 | #if defined(MBEDTLS_SHA512_C) |
| 9518 | case MBEDTLS_MD_SHA384: |
| 9519 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 9520 | case MBEDTLS_MD_SHA512: |
| 9521 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 9522 | #endif |
| 9523 | default: |
| 9524 | return( MBEDTLS_SSL_HASH_NONE ); |
| 9525 | } |
| 9526 | } |
| 9527 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 9528 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 9529 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9530 | * 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] | 9531 | * 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] | 9532 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 9533 | 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] | 9534 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9535 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 9536 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 9537 | if( ssl->conf->curve_list == NULL ) |
| 9538 | return( -1 ); |
| 9539 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9540 | 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] | 9541 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 9542 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 9543 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 9544 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 9545 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 9546 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9547 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9548 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9549 | /* |
| 9550 | * Check if a hash proposed by the peer is in our list. |
| 9551 | * Return 0 if we're willing to use it, -1 otherwise. |
| 9552 | */ |
| 9553 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 9554 | mbedtls_md_type_t md ) |
| 9555 | { |
| 9556 | const int *cur; |
| 9557 | |
| 9558 | if( ssl->conf->sig_hashes == NULL ) |
| 9559 | return( -1 ); |
| 9560 | |
| 9561 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 9562 | if( *cur == (int) md ) |
| 9563 | return( 0 ); |
| 9564 | |
| 9565 | return( -1 ); |
| 9566 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9567 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9569 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9570 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 9571 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9572 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9573 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9574 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9575 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9576 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9577 | int usage = 0; |
| 9578 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9579 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9580 | const char *ext_oid; |
| 9581 | size_t ext_len; |
| 9582 | #endif |
| 9583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9584 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 9585 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9586 | ((void) cert); |
| 9587 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9588 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9589 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9590 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9591 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 9592 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9593 | { |
| 9594 | /* Server part of the key exchange */ |
| 9595 | switch( ciphersuite->key_exchange ) |
| 9596 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9597 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 9598 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 9599 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9600 | break; |
| 9601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9602 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 9603 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 9604 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 9605 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9606 | break; |
| 9607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9608 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 9609 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 9610 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9611 | break; |
| 9612 | |
| 9613 | /* 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] | 9614 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 9615 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 9616 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 9617 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 9618 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9619 | usage = 0; |
| 9620 | } |
| 9621 | } |
| 9622 | else |
| 9623 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9624 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 9625 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9626 | } |
| 9627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9628 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9629 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 9630 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9631 | ret = -1; |
| 9632 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9633 | #else |
| 9634 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9635 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9637 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 9638 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9639 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9640 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 9641 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9642 | } |
| 9643 | else |
| 9644 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9645 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 9646 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9647 | } |
| 9648 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9649 | 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] | 9650 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 9651 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9652 | ret = -1; |
| 9653 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9654 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9655 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9656 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9657 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9658 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 9659 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9660 | /* |
| 9661 | * Convert version numbers to/from wire format |
| 9662 | * and, for DTLS, to/from TLS equivalent. |
| 9663 | * |
| 9664 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9665 | * 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] | 9666 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 9667 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 9668 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9669 | 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] | 9670 | unsigned char ver[2] ) |
| 9671 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9672 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9673 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9675 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9676 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 9677 | |
| 9678 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 9679 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 9680 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 9681 | else |
| 9682 | #else |
| 9683 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9684 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 9685 | { |
| 9686 | ver[0] = (unsigned char) major; |
| 9687 | ver[1] = (unsigned char) minor; |
| 9688 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9689 | } |
| 9690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9691 | 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] | 9692 | const unsigned char ver[2] ) |
| 9693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9694 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9695 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9696 | { |
| 9697 | *major = 255 - ver[0] + 2; |
| 9698 | *minor = 255 - ver[1] + 1; |
| 9699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9700 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9701 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 9702 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 9703 | else |
| 9704 | #else |
| 9705 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9706 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 9707 | { |
| 9708 | *major = ver[0]; |
| 9709 | *minor = ver[1]; |
| 9710 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9711 | } |
| 9712 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 9713 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 9714 | { |
| 9715 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9716 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 9717 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 9718 | |
| 9719 | switch( md ) |
| 9720 | { |
| 9721 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 9722 | #if defined(MBEDTLS_MD5_C) |
| 9723 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 9724 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 9725 | #endif |
| 9726 | #if defined(MBEDTLS_SHA1_C) |
| 9727 | case MBEDTLS_SSL_HASH_SHA1: |
| 9728 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 9729 | break; |
| 9730 | #endif |
| 9731 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 9732 | #if defined(MBEDTLS_SHA512_C) |
| 9733 | case MBEDTLS_SSL_HASH_SHA384: |
| 9734 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 9735 | break; |
| 9736 | #endif |
| 9737 | #if defined(MBEDTLS_SHA256_C) |
| 9738 | case MBEDTLS_SSL_HASH_SHA256: |
| 9739 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 9740 | break; |
| 9741 | #endif |
| 9742 | default: |
| 9743 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 9744 | } |
| 9745 | |
| 9746 | return 0; |
| 9747 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9748 | (void) ssl; |
| 9749 | (void) md; |
| 9750 | |
| 9751 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 9752 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9753 | } |
| 9754 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9755 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 9756 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 9757 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 9758 | unsigned char *output, |
| 9759 | unsigned char *data, size_t data_len ) |
| 9760 | { |
| 9761 | int ret = 0; |
| 9762 | mbedtls_md5_context mbedtls_md5; |
| 9763 | mbedtls_sha1_context mbedtls_sha1; |
| 9764 | |
| 9765 | mbedtls_md5_init( &mbedtls_md5 ); |
| 9766 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 9767 | |
| 9768 | /* |
| 9769 | * digitally-signed struct { |
| 9770 | * opaque md5_hash[16]; |
| 9771 | * opaque sha_hash[20]; |
| 9772 | * }; |
| 9773 | * |
| 9774 | * md5_hash |
| 9775 | * MD5(ClientHello.random + ServerHello.random |
| 9776 | * + ServerParams); |
| 9777 | * sha_hash |
| 9778 | * SHA(ClientHello.random + ServerHello.random |
| 9779 | * + ServerParams); |
| 9780 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9781 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9782 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9783 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9784 | goto exit; |
| 9785 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9786 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9787 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 9788 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9789 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9790 | goto exit; |
| 9791 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9792 | 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] | 9793 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9794 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9795 | goto exit; |
| 9796 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9797 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9798 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9799 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9800 | goto exit; |
| 9801 | } |
| 9802 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9803 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9804 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9805 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9806 | goto exit; |
| 9807 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9808 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9809 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 9810 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9811 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9812 | goto exit; |
| 9813 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9814 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9815 | data_len ) ) != 0 ) |
| 9816 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9817 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9818 | goto exit; |
| 9819 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9820 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9821 | output + 16 ) ) != 0 ) |
| 9822 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9823 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9824 | goto exit; |
| 9825 | } |
| 9826 | |
| 9827 | exit: |
| 9828 | mbedtls_md5_free( &mbedtls_md5 ); |
| 9829 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 9830 | |
| 9831 | if( ret != 0 ) |
| 9832 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9833 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 9834 | |
| 9835 | return( ret ); |
| 9836 | |
| 9837 | } |
| 9838 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 9839 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 9840 | |
| 9841 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 9842 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9843 | 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] | 9844 | unsigned char *hash, size_t *hashlen, |
| 9845 | unsigned char *data, size_t data_len, |
| 9846 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9847 | { |
| 9848 | int ret = 0; |
| 9849 | mbedtls_md_context_t ctx; |
| 9850 | 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] | 9851 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9852 | |
| 9853 | mbedtls_md_init( &ctx ); |
| 9854 | |
| 9855 | /* |
| 9856 | * digitally-signed struct { |
| 9857 | * opaque client_random[32]; |
| 9858 | * opaque server_random[32]; |
| 9859 | * ServerDHParams params; |
| 9860 | * }; |
| 9861 | */ |
| 9862 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 9863 | { |
| 9864 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 9865 | goto exit; |
| 9866 | } |
| 9867 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 9868 | { |
| 9869 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 9870 | goto exit; |
| 9871 | } |
| 9872 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 9873 | { |
| 9874 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 9875 | goto exit; |
| 9876 | } |
| 9877 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 9878 | { |
| 9879 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 9880 | goto exit; |
| 9881 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 9882 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9883 | { |
| 9884 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 9885 | goto exit; |
| 9886 | } |
| 9887 | |
| 9888 | exit: |
| 9889 | mbedtls_md_free( &ctx ); |
| 9890 | |
| 9891 | if( ret != 0 ) |
| 9892 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9893 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 9894 | |
| 9895 | return( ret ); |
| 9896 | } |
| 9897 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 9898 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 9899 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9900 | #endif /* MBEDTLS_SSL_TLS_C */ |