| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  SSLv3/TLSv1 shared functions | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ | 
|  | 21 | /* | 
|  | 22 | *  The SSL 3.0 specification was drafted by Netscape in 1996, | 
|  | 23 | *  and became an IETF standard in 1999. | 
|  | 24 | * | 
|  | 25 | *  http://wp.netscape.com/eng/ssl3/ | 
|  | 26 | *  http://www.ietf.org/rfc/rfc2246.txt | 
|  | 27 | *  http://www.ietf.org/rfc/rfc4346.txt | 
|  | 28 | */ | 
|  | 29 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_SSL_TLS_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #if defined(MBEDTLS_PLATFORM_C) | 
|  | 39 | #include "mbedtls/platform.h" | 
|  | 40 | #else | 
|  | 41 | #include <stdlib.h> | 
|  | 42 | #define mbedtls_calloc    calloc | 
|  | 43 | #define mbedtls_free      free | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 44 | #endif | 
|  | 45 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/debug.h" | 
|  | 47 | #include "mbedtls/ssl.h" | 
| Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 48 | #include "mbedtls/ssl_internal.h" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 49 | #include "mbedtls/platform_util.h" | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 50 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 51 | #include <string.h> | 
|  | 52 |  | 
| Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 53 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/oid.h" | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 55 | #endif | 
|  | 56 |  | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 57 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 58 | 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] | 59 |  | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 60 | /* Length of the "epoch" field in the record header */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | 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] | 62 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 64 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 65 | return( 2 ); | 
| Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 66 | #else | 
|  | 67 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 68 | #endif | 
|  | 69 | return( 0 ); | 
|  | 70 | } | 
|  | 71 |  | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 72 | /* | 
|  | 73 | * Start a timer. | 
|  | 74 | * Passing millisecs = 0 cancels a running timer. | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 75 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | 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] | 77 | { | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 78 | if( ssl->f_set_timer == NULL ) | 
|  | 79 | return; | 
|  | 80 |  | 
|  | 81 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); | 
|  | 82 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
|  | 85 | /* | 
|  | 86 | * Return -1 is timer is expired, 0 if it isn't. | 
|  | 87 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 89 | { | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 90 | if( ssl->f_get_timer == NULL ) | 
| Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 91 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 92 |  | 
|  | 93 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 94 | { | 
|  | 95 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 96 | return( -1 ); | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 97 | } | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 98 |  | 
|  | 99 | return( 0 ); | 
|  | 100 | } | 
| Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 101 |  | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 102 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, | 
|  | 103 | mbedtls_ssl_transform *transform ); | 
|  | 104 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl, | 
|  | 105 | mbedtls_ssl_transform *transform ); | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 106 |  | 
|  | 107 | #define SSL_DONT_FORCE_FLUSH 0 | 
|  | 108 | #define SSL_FORCE_FLUSH      1 | 
|  | 109 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 110 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 111 |  | 
| Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 112 | /* Forward declarations for functions related to message buffering. */ | 
|  | 113 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); | 
|  | 114 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, | 
|  | 115 | uint8_t slot ); | 
|  | 116 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); | 
|  | 117 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); | 
|  | 118 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); | 
|  | 119 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); | 
|  | 120 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 121 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 122 |  | 
| Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 123 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 124 | 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] | 125 | { | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 126 | size_t mtu = ssl_get_current_mtu( ssl ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 127 |  | 
|  | 128 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 129 | return( mtu ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 130 |  | 
|  | 131 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
|  | 132 | } | 
|  | 133 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 134 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) | 
|  | 135 | { | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 136 | size_t const bytes_written = ssl->out_left; | 
|  | 137 | size_t const mtu           = ssl_get_maximum_datagram_size( ssl ); | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 138 |  | 
|  | 139 | /* Double-check that the write-index hasn't gone | 
|  | 140 | * past what we can transmit in a single datagram. */ | 
| Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 141 | if( bytes_written > mtu ) | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 142 | { | 
|  | 143 | /* Should never happen... */ | 
|  | 144 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | return( (int) ( mtu - bytes_written ) ); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) | 
|  | 151 | { | 
|  | 152 | int ret; | 
|  | 153 | size_t remaining, expansion; | 
| Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 154 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 155 |  | 
|  | 156 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 157 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); | 
|  | 158 |  | 
|  | 159 | if( max_len > mfl ) | 
|  | 160 | max_len = mfl; | 
| Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 161 |  | 
|  | 162 | /* By the standard (RFC 6066 Sect. 4), the MFL extension | 
|  | 163 | * only limits the maximum record payload size, so in theory | 
|  | 164 | * we would be allowed to pack multiple records of payload size | 
|  | 165 | * MFL into a single datagram. However, this would mean that there's | 
|  | 166 | * no way to explicitly communicate MTU restrictions to the peer. | 
|  | 167 | * | 
|  | 168 | * The following reduction of max_len makes sure that we never | 
|  | 169 | * write datagrams larger than MFL + Record Expansion Overhead. | 
|  | 170 | */ | 
|  | 171 | if( max_len <= ssl->out_left ) | 
|  | 172 | return( 0 ); | 
|  | 173 |  | 
|  | 174 | max_len -= ssl->out_left; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 175 | #endif | 
|  | 176 |  | 
|  | 177 | ret = ssl_get_remaining_space_in_datagram( ssl ); | 
|  | 178 | if( ret < 0 ) | 
|  | 179 | return( ret ); | 
|  | 180 | remaining = (size_t) ret; | 
|  | 181 |  | 
|  | 182 | ret = mbedtls_ssl_get_record_expansion( ssl ); | 
|  | 183 | if( ret < 0 ) | 
|  | 184 | return( ret ); | 
|  | 185 | expansion = (size_t) ret; | 
|  | 186 |  | 
|  | 187 | if( remaining <= expansion ) | 
|  | 188 | return( 0 ); | 
|  | 189 |  | 
|  | 190 | remaining -= expansion; | 
|  | 191 | if( remaining >= max_len ) | 
|  | 192 | remaining = max_len; | 
|  | 193 |  | 
|  | 194 | return( (int) remaining ); | 
|  | 195 | } | 
|  | 196 |  | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 197 | /* | 
|  | 198 | * Double the retransmit timeout value, within the allowed range, | 
|  | 199 | * returning -1 if the maximum value has already been reached. | 
|  | 200 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 202 | { | 
|  | 203 | uint32_t new_timeout; | 
|  | 204 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 205 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 206 | return( -1 ); | 
|  | 207 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 208 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 | 
|  | 209 | * in the following way: after the initial transmission and a first | 
|  | 210 | * retransmission, back off to a temporary estimated MTU of 508 bytes. | 
|  | 211 | * This value is guaranteed to be deliverable (if not guaranteed to be | 
|  | 212 | * delivered) of any compliant IPv4 (and IPv6) network, and should work | 
|  | 213 | * on most non-IP stacks too. */ | 
|  | 214 | if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min ) | 
| Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 215 | { | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 216 | ssl->handshake->mtu = 508; | 
| Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 217 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); | 
|  | 218 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 219 |  | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 220 | new_timeout = 2 * ssl->handshake->retransmit_timeout; | 
|  | 221 |  | 
|  | 222 | /* Avoid arithmetic overflow and range overflow */ | 
|  | 223 | if( new_timeout < ssl->handshake->retransmit_timeout || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 224 | new_timeout > ssl->conf->hs_timeout_max ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 225 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 226 | new_timeout = ssl->conf->hs_timeout_max; | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
|  | 229 | ssl->handshake->retransmit_timeout = new_timeout; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | 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] | 231 | ssl->handshake->retransmit_timeout ) ); | 
|  | 232 |  | 
|  | 233 | return( 0 ); | 
|  | 234 | } | 
|  | 235 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 237 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 238 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | 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] | 240 | ssl->handshake->retransmit_timeout ) ); | 
|  | 241 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 243 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 245 | /* | 
|  | 246 | * Convert max_fragment_length codes to length. | 
|  | 247 | * RFC 6066 says: | 
|  | 248 | *    enum{ | 
|  | 249 | *        2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) | 
|  | 250 | *    } MaxFragmentLength; | 
|  | 251 | * and we add 0 -> extension unused | 
|  | 252 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 253 | static unsigned int ssl_mfl_code_to_length( int mfl ) | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 254 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 255 | switch( mfl ) | 
|  | 256 | { | 
|  | 257 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: | 
|  | 258 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | 
|  | 259 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: | 
|  | 260 | return 512; | 
|  | 261 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: | 
|  | 262 | return 1024; | 
|  | 263 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: | 
|  | 264 | return 2048; | 
|  | 265 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: | 
|  | 266 | return 4096; | 
|  | 267 | default: | 
|  | 268 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); | 
|  | 269 | } | 
|  | 270 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 272 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 273 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | 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] | 275 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | mbedtls_ssl_session_free( dst ); | 
|  | 277 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 278 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 280 | if( src->peer_cert != NULL ) | 
|  | 281 | { | 
| Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 282 | int ret; | 
|  | 283 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 284 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 285 | if( dst->peer_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 286 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 287 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | mbedtls_x509_crt_init( dst->peer_cert ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 289 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | 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] | 291 | src->peer_cert->raw.len ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 292 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | mbedtls_free( dst->peer_cert ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 294 | dst->peer_cert = NULL; | 
|  | 295 | return( ret ); | 
|  | 296 | } | 
|  | 297 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 299 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 300 | #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] | 301 | if( src->ticket != NULL ) | 
|  | 302 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 303 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 304 | if( dst->ticket == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 305 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 306 |  | 
|  | 307 | memcpy( dst->ticket, src->ticket, src->ticket_len ); | 
|  | 308 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 309 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 310 |  | 
|  | 311 | return( 0 ); | 
|  | 312 | } | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 313 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| 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 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 316 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 317 | const unsigned char *key_enc, const unsigned char *key_dec, | 
|  | 318 | size_t keylen, | 
|  | 319 | const unsigned char *iv_enc,  const unsigned char *iv_dec, | 
|  | 320 | size_t ivlen, | 
|  | 321 | const unsigned char *mac_enc, const unsigned char *mac_dec, | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 322 | size_t maclen ) = NULL; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 323 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; | 
|  | 324 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; | 
|  | 325 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; | 
|  | 326 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; | 
|  | 327 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; | 
|  | 328 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 329 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | /* | 
|  | 331 | * Key material generation | 
|  | 332 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 333 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 334 | static int ssl3_prf( const unsigned char *secret, size_t slen, | 
|  | 335 | const char *label, | 
|  | 336 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 337 | unsigned char *dstbuf, size_t dlen ) | 
|  | 338 | { | 
| Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 339 | int ret = 0; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 340 | size_t i; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 341 | mbedtls_md5_context md5; | 
|  | 342 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 343 | unsigned char padding[16]; | 
|  | 344 | unsigned char sha1sum[20]; | 
|  | 345 | ((void)label); | 
|  | 346 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 347 | mbedtls_md5_init(  &md5  ); | 
|  | 348 | mbedtls_sha1_init( &sha1 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 349 |  | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 350 | /* | 
|  | 351 | *  SSLv3: | 
|  | 352 | *    block = | 
|  | 353 | *      MD5( secret + SHA1( 'A'    + secret + random ) ) + | 
|  | 354 | *      MD5( secret + SHA1( 'BB'   + secret + random ) ) + | 
|  | 355 | *      MD5( secret + SHA1( 'CCC'  + secret + random ) ) + | 
|  | 356 | *      ... | 
|  | 357 | */ | 
|  | 358 | for( i = 0; i < dlen / 16; i++ ) | 
|  | 359 | { | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 360 | memset( padding, (unsigned char) ('A' + i), 1 + i ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 361 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 362 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 363 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 364 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 365 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 366 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 367 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 368 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 369 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 370 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 371 | goto exit; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 372 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 373 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 374 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 375 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 376 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 377 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 378 | goto exit; | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 379 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 380 | goto exit; | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 381 | } | 
|  | 382 |  | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 383 | exit: | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 384 | mbedtls_md5_free(  &md5  ); | 
|  | 385 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 386 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 387 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); | 
|  | 388 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 389 |  | 
| Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 390 | return( ret ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 391 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 393 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 395 | static int tls1_prf( const unsigned char *secret, size_t slen, | 
|  | 396 | const char *label, | 
|  | 397 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 398 | unsigned char *dstbuf, size_t dlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 399 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 400 | size_t nb, hs; | 
|  | 401 | size_t i, j, k; | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 402 | const unsigned char *S1, *S2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 403 | unsigned char tmp[128]; | 
|  | 404 | unsigned char h_i[20]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | const mbedtls_md_info_t *md_info; | 
|  | 406 | mbedtls_md_context_t md_ctx; | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 407 | int ret; | 
|  | 408 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | mbedtls_md_init( &md_ctx ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 410 |  | 
|  | 411 | if( sizeof( tmp ) < 20 + strlen( label ) + rlen ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 413 |  | 
|  | 414 | hs = ( slen + 1 ) / 2; | 
|  | 415 | S1 = secret; | 
|  | 416 | S2 = secret + slen - hs; | 
|  | 417 |  | 
|  | 418 | nb = strlen( label ); | 
|  | 419 | memcpy( tmp + 20, label, nb ); | 
|  | 420 | memcpy( tmp + 20 + nb, random, rlen ); | 
|  | 421 | nb += rlen; | 
|  | 422 |  | 
|  | 423 | /* | 
|  | 424 | * First compute P_md5(secret,label+random)[0..dlen] | 
|  | 425 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) | 
|  | 427 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 428 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 429 | 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] | 430 | return( ret ); | 
|  | 431 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); | 
|  | 433 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); | 
|  | 434 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 435 |  | 
|  | 436 | for( i = 0; i < dlen; i += 16 ) | 
|  | 437 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 438 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 439 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); | 
|  | 440 | mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 441 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 442 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 443 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); | 
|  | 444 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 445 |  | 
|  | 446 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; | 
|  | 447 |  | 
|  | 448 | for( j = 0; j < k; j++ ) | 
|  | 449 | dstbuf[i + j]  = h_i[j]; | 
|  | 450 | } | 
|  | 451 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 453 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 454 | /* | 
|  | 455 | * XOR out with P_sha1(secret,label+random)[0..dlen] | 
|  | 456 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) | 
|  | 458 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 459 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 460 | 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] | 461 | return( ret ); | 
|  | 462 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); | 
|  | 464 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); | 
|  | 465 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 466 |  | 
|  | 467 | for( i = 0; i < dlen; i += 20 ) | 
|  | 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, tmp, 20 + nb ); | 
|  | 471 | mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 472 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 473 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 474 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); | 
|  | 475 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 476 |  | 
|  | 477 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; | 
|  | 478 |  | 
|  | 479 | for( j = 0; j < k; j++ ) | 
|  | 480 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); | 
|  | 481 | } | 
|  | 482 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 484 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 485 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
|  | 486 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 |  | 
|  | 488 | return( 0 ); | 
|  | 489 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 491 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 493 | 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] | 494 | const unsigned char *secret, size_t slen, | 
|  | 495 | const char *label, | 
|  | 496 | const unsigned char *random, size_t rlen, | 
|  | 497 | unsigned char *dstbuf, size_t dlen ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 498 | { | 
|  | 499 | size_t nb; | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 500 | size_t i, j, k, md_len; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 501 | unsigned char tmp[128]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 502 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; | 
|  | 503 | const mbedtls_md_info_t *md_info; | 
|  | 504 | mbedtls_md_context_t md_ctx; | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 505 | int ret; | 
|  | 506 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | mbedtls_md_init( &md_ctx ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 508 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) | 
|  | 510 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 511 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | md_len = mbedtls_md_get_size( md_info ); | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 513 |  | 
|  | 514 | if( sizeof( tmp ) < md_len + strlen( label ) + rlen ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 515 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 516 |  | 
|  | 517 | nb = strlen( label ); | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 518 | memcpy( tmp + md_len, label, nb ); | 
|  | 519 | memcpy( tmp + md_len + nb, random, rlen ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 520 | nb += rlen; | 
|  | 521 |  | 
|  | 522 | /* | 
|  | 523 | * Compute P_<hash>(secret, label + random)[0..dlen] | 
|  | 524 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | 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] | 526 | return( ret ); | 
|  | 527 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); | 
|  | 529 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); | 
|  | 530 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 531 |  | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 532 | for( i = 0; i < dlen; i += md_len ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 533 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 535 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); | 
|  | 536 | mbedtls_md_hmac_finish( &md_ctx, h_i ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 537 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | mbedtls_md_hmac_reset ( &md_ctx ); | 
|  | 539 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); | 
|  | 540 | mbedtls_md_hmac_finish( &md_ctx, tmp ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 541 |  | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 542 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 543 |  | 
|  | 544 | for( j = 0; j < k; j++ ) | 
|  | 545 | dstbuf[i + j]  = h_i[j]; | 
|  | 546 | } | 
|  | 547 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 548 | mbedtls_md_free( &md_ctx ); | 
| Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 549 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 550 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
|  | 551 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 552 |  | 
|  | 553 | return( 0 ); | 
|  | 554 | } | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 555 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 557 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, | 
|  | 558 | const char *label, | 
|  | 559 | const unsigned char *random, size_t rlen, | 
|  | 560 | unsigned char *dstbuf, size_t dlen ) | 
|  | 561 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 563 | label, random, rlen, dstbuf, dlen ) ); | 
|  | 564 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | #endif /* MBEDTLS_SHA256_C */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 566 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 567 | #if defined(MBEDTLS_SHA512_C) | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 568 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, | 
|  | 569 | const char *label, | 
|  | 570 | const unsigned char *random, size_t rlen, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 571 | unsigned char *dstbuf, size_t dlen ) | 
|  | 572 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, | 
| Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 574 | label, random, rlen, dstbuf, dlen ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 575 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 577 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 578 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 579 | 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] | 580 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 581 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 582 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 583 | 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] | 584 | #endif | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 585 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 587 | static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * ); | 
|  | 588 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 589 | #endif | 
|  | 590 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 591 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 592 | static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * ); | 
|  | 593 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 594 | #endif | 
|  | 595 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 597 | #if defined(MBEDTLS_SHA256_C) | 
|  | 598 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); | 
|  | 599 | static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * ); | 
|  | 600 | 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] | 601 | #endif | 
| Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 602 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 603 | #if defined(MBEDTLS_SHA512_C) | 
|  | 604 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); | 
|  | 605 | static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * ); | 
|  | 606 | 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] | 607 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 608 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 609 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 611 | { | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 612 | int ret = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 613 | unsigned char tmp[64]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 614 | unsigned char keyblk[256]; | 
|  | 615 | unsigned char *key1; | 
|  | 616 | unsigned char *key2; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 617 | unsigned char *mac_enc; | 
|  | 618 | unsigned char *mac_dec; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 619 | size_t mac_key_len; | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 620 | size_t iv_copy_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 622 | const mbedtls_md_info_t *md_info; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 623 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 624 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
|  | 625 | mbedtls_ssl_transform *transform = ssl->transform_negotiate; | 
|  | 626 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 627 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 629 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 631 | if( cipher_info == NULL ) | 
|  | 632 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 634 | transform->ciphersuite_info->cipher ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 636 | } | 
|  | 637 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 639 | if( md_info == NULL ) | 
|  | 640 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 642 | transform->ciphersuite_info->mac ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 644 | } | 
|  | 645 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 646 | /* | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 647 | * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 648 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 650 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 651 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 652 | handshake->tls_prf = ssl3_prf; | 
|  | 653 | handshake->calc_verify = ssl_calc_verify_ssl; | 
|  | 654 | handshake->calc_finished = ssl_calc_finished_ssl; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 655 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 656 | else | 
|  | 657 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 659 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 660 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 661 | handshake->tls_prf = tls1_prf; | 
|  | 662 | handshake->calc_verify = ssl_calc_verify_tls; | 
|  | 663 | handshake->calc_finished = ssl_calc_finished_tls; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 664 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 665 | else | 
|  | 666 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 668 | #if defined(MBEDTLS_SHA512_C) | 
|  | 669 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && | 
|  | 670 | transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 671 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 672 | handshake->tls_prf = tls_prf_sha384; | 
|  | 673 | handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 674 | handshake->calc_finished = ssl_calc_finished_tls_sha384; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 675 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 676 | else | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 677 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 678 | #if defined(MBEDTLS_SHA256_C) | 
|  | 679 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 680 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 681 | handshake->tls_prf = tls_prf_sha256; | 
|  | 682 | handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 683 | handshake->calc_finished = ssl_calc_finished_tls_sha256; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 684 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 685 | else | 
|  | 686 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 688 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 690 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 691 | } | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 692 |  | 
|  | 693 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 694 | * SSLv3: | 
|  | 695 | *   master = | 
|  | 696 | *     MD5( premaster + SHA1( 'A'   + premaster + randbytes ) ) + | 
|  | 697 | *     MD5( premaster + SHA1( 'BB'  + premaster + randbytes ) ) + | 
|  | 698 | *     MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) ) | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 699 | * | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 700 | * TLSv1+: | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 701 | *   master = PRF( premaster, "master secret", randbytes )[0..47] | 
|  | 702 | */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 703 | if( handshake->resume == 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 704 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 706 | handshake->pmslen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 707 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 709 | if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 710 | { | 
|  | 711 | unsigned char session_hash[48]; | 
|  | 712 | size_t hash_len; | 
|  | 713 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 714 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) ); | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 715 |  | 
|  | 716 | ssl->handshake->calc_verify( ssl, session_hash ); | 
|  | 717 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 719 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 720 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 722 | if( ssl->transform_negotiate->ciphersuite_info->mac == | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 723 | MBEDTLS_MD_SHA384 ) | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 724 | { | 
|  | 725 | hash_len = 48; | 
|  | 726 | } | 
|  | 727 | else | 
|  | 728 | #endif | 
|  | 729 | hash_len = 32; | 
|  | 730 | } | 
|  | 731 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 733 | hash_len = 36; | 
|  | 734 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | 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] | 736 |  | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 737 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, | 
|  | 738 | "extended master secret", | 
|  | 739 | session_hash, hash_len, | 
|  | 740 | session->master, 48 ); | 
|  | 741 | if( ret != 0 ) | 
|  | 742 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 743 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 744 | return( ret ); | 
|  | 745 | } | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 746 |  | 
|  | 747 | } | 
|  | 748 | else | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 749 | #endif | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 750 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, | 
|  | 751 | "master secret", | 
|  | 752 | handshake->randbytes, 64, | 
|  | 753 | session->master, 48 ); | 
|  | 754 | if( ret != 0 ) | 
|  | 755 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 756 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 757 | return( ret ); | 
|  | 758 | } | 
| Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 759 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 760 | mbedtls_platform_zeroize( handshake->premaster, | 
|  | 761 | sizeof(handshake->premaster) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 762 | } | 
|  | 763 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 764 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 765 |  | 
|  | 766 | /* | 
|  | 767 | * Swap the client and server random values. | 
|  | 768 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 769 | memcpy( tmp, handshake->randbytes, 64 ); | 
|  | 770 | memcpy( handshake->randbytes, tmp + 32, 32 ); | 
|  | 771 | memcpy( handshake->randbytes + 32, tmp, 32 ); | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 772 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 773 |  | 
|  | 774 | /* | 
|  | 775 | *  SSLv3: | 
|  | 776 | *    key block = | 
|  | 777 | *      MD5( master + SHA1( 'A'    + master + randbytes ) ) + | 
|  | 778 | *      MD5( master + SHA1( 'BB'   + master + randbytes ) ) + | 
|  | 779 | *      MD5( master + SHA1( 'CCC'  + master + randbytes ) ) + | 
|  | 780 | *      MD5( master + SHA1( 'DDDD' + master + randbytes ) ) + | 
|  | 781 | *      ... | 
|  | 782 | * | 
|  | 783 | *  TLSv1: | 
|  | 784 | *    key block = PRF( master, "key expansion", randbytes ) | 
|  | 785 | */ | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 786 | ret = handshake->tls_prf( session->master, 48, "key expansion", | 
|  | 787 | handshake->randbytes, 64, keyblk, 256 ); | 
|  | 788 | if( ret != 0 ) | 
|  | 789 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); | 
| Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 791 | return( ret ); | 
|  | 792 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 793 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 794 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", | 
|  | 795 | mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) ); | 
|  | 796 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 ); | 
|  | 797 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 ); | 
|  | 798 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 799 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 800 | mbedtls_platform_zeroize( handshake->randbytes, | 
|  | 801 | sizeof( handshake->randbytes ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 802 |  | 
|  | 803 | /* | 
|  | 804 | * Determine the appropriate key, IV and MAC length. | 
|  | 805 | */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 806 |  | 
| Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 807 | transform->keylen = cipher_info->key_bitlen / 8; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 808 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 809 | if( cipher_info->mode == MBEDTLS_MODE_GCM || | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 810 | cipher_info->mode == MBEDTLS_MODE_CCM || | 
|  | 811 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 812 | { | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 813 | size_t taglen, explicit_ivlen; | 
|  | 814 |  | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 815 | transform->maclen = 0; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 816 | mac_key_len = 0; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 817 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 818 | /* All modes haves 96-bit IVs; | 
|  | 819 | * GCM and CCM has 4 implicit and 8 explicit bytes | 
|  | 820 | * ChachaPoly has all 12 bytes implicit | 
|  | 821 | */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 822 | transform->ivlen = 12; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 823 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) | 
|  | 824 | transform->fixed_ivlen = 12; | 
|  | 825 | else | 
|  | 826 | transform->fixed_ivlen = 4; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 827 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 828 | /* All modes have 128-bit tags, except CCM_8 (ciphersuite flag) */ | 
|  | 829 | taglen = transform->ciphersuite_info->flags & | 
|  | 830 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | 
|  | 831 |  | 
|  | 832 |  | 
|  | 833 | /* Minimum length of encrypted record */ | 
|  | 834 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; | 
|  | 835 | transform->minlen = explicit_ivlen + taglen; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 836 | } | 
|  | 837 | else | 
|  | 838 | { | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 839 | /* Initialize HMAC contexts */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 840 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || | 
|  | 841 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 842 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 843 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 844 | return( ret ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 845 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 846 |  | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 847 | /* Get MAC length */ | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 848 | mac_key_len = mbedtls_md_get_size( md_info ); | 
|  | 849 | transform->maclen = mac_key_len; | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 850 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 852 | /* | 
|  | 853 | * If HMAC is to be truncated, we shall keep the leftmost bytes, | 
|  | 854 | * (rfc 6066 page 13 or rfc 2104 section 4), | 
|  | 855 | * so we only need to adjust the length here. | 
|  | 856 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 857 | if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 858 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 859 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 860 |  | 
|  | 861 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) | 
|  | 862 | /* Fall back to old, non-compliant version of the truncated | 
| Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 863 | * HMAC implementation which also truncates the key | 
|  | 864 | * (Mbed TLS versions from 1.3 to 2.6.0) */ | 
| Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 865 | mac_key_len = transform->maclen; | 
|  | 866 | #endif | 
|  | 867 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 868 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 869 |  | 
|  | 870 | /* IV length */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 871 | transform->ivlen = cipher_info->iv_size; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 872 |  | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 873 | /* Minimum length */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 875 | transform->minlen = transform->maclen; | 
|  | 876 | else | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 877 | { | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 878 | /* | 
|  | 879 | * GenericBlockCipher: | 
| Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 880 | * 1. if EtM is in use: one block plus MAC | 
|  | 881 | *    otherwise: * first multiple of blocklen greater than maclen | 
|  | 882 | * 2. IV except for SSL3 and TLS 1.0 | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 883 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 884 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 885 | if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) | 
| Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 886 | { | 
|  | 887 | transform->minlen = transform->maclen | 
|  | 888 | + cipher_info->block_size; | 
|  | 889 | } | 
|  | 890 | else | 
|  | 891 | #endif | 
|  | 892 | { | 
|  | 893 | transform->minlen = transform->maclen | 
|  | 894 | + cipher_info->block_size | 
|  | 895 | - transform->maclen % cipher_info->block_size; | 
|  | 896 | } | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 897 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 898 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) | 
|  | 899 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || | 
|  | 900 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 901 | ; /* No need to adjust minlen */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 902 | else | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 903 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 904 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 905 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || | 
|  | 906 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 907 | { | 
|  | 908 | transform->minlen += transform->ivlen; | 
|  | 909 | } | 
|  | 910 | else | 
|  | 911 | #endif | 
|  | 912 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 913 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 914 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 915 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 916 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 917 | } | 
|  | 918 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 919 | 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] | 920 | transform->keylen, transform->minlen, transform->ivlen, | 
|  | 921 | transform->maclen ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 922 |  | 
|  | 923 | /* | 
|  | 924 | * Finally setup the cipher contexts, IVs and MAC secrets. | 
|  | 925 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 926 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 927 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 928 | { | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 929 | key1 = keyblk + mac_key_len * 2; | 
|  | 930 | key2 = keyblk + mac_key_len * 2 + transform->keylen; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 931 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 932 | mac_enc = keyblk; | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 933 | mac_dec = keyblk + mac_key_len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 934 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 935 | /* | 
|  | 936 | * This is not used in TLS v1.1. | 
|  | 937 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 938 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 939 | transform->fixed_ivlen : transform->ivlen; | 
|  | 940 | memcpy( transform->iv_enc, key2 + transform->keylen,  iv_copy_len ); | 
|  | 941 | memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 942 | iv_copy_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 943 | } | 
|  | 944 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 945 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 946 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 947 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 948 | { | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 949 | key1 = keyblk + mac_key_len * 2 + transform->keylen; | 
|  | 950 | key2 = keyblk + mac_key_len * 2; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 951 |  | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 952 | mac_enc = keyblk + mac_key_len; | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 953 | mac_dec = keyblk; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 954 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 955 | /* | 
|  | 956 | * This is not used in TLS v1.1. | 
|  | 957 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 958 | iv_copy_len = ( transform->fixed_ivlen ) ? | 
|  | 959 | transform->fixed_ivlen : transform->ivlen; | 
|  | 960 | memcpy( transform->iv_dec, key1 + transform->keylen,  iv_copy_len ); | 
|  | 961 | memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len, | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 962 | iv_copy_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 963 | } | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 964 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 965 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 966 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 967 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 968 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 969 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 970 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 971 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 972 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 973 | { | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 974 | if( mac_key_len > sizeof transform->mac_enc ) | 
| Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 975 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 976 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 977 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 978 | } | 
|  | 979 |  | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 980 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); | 
|  | 981 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 982 | } | 
|  | 983 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 984 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 985 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 986 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 987 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 988 | { | 
| Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 989 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. | 
|  | 990 | For AEAD-based ciphersuites, there is nothing to do here. */ | 
|  | 991 | if( mac_key_len != 0 ) | 
|  | 992 | { | 
|  | 993 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); | 
|  | 994 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); | 
|  | 995 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 996 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 997 | else | 
|  | 998 | #endif | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 999 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1000 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1001 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1002 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1003 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1004 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 1005 | if( mbedtls_ssl_hw_record_init != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1006 | { | 
|  | 1007 | int ret = 0; | 
|  | 1008 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1009 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1010 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen, | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1012 | transform->iv_enc, transform->iv_dec, | 
|  | 1013 | iv_copy_len, | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1014 | mac_enc, mac_dec, | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1015 | mac_key_len ) ) != 0 ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1016 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); | 
|  | 1018 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1019 | } | 
|  | 1020 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1021 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1022 |  | 
| Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1023 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) | 
|  | 1024 | if( ssl->conf->f_export_keys != NULL ) | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1025 | { | 
| Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1026 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, | 
|  | 1027 | session->master, keyblk, | 
| Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1028 | mac_key_len, transform->keylen, | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1029 | iv_copy_len ); | 
|  | 1030 | } | 
|  | 1031 | #endif | 
|  | 1032 |  | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1033 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1034 | cipher_info ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1035 | { | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1036 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1037 | return( ret ); | 
|  | 1038 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1039 |  | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1040 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1041 | cipher_info ) ) != 0 ) | 
|  | 1042 | { | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1043 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1044 | return( ret ); | 
|  | 1045 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1046 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, | 
| Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1048 | cipher_info->key_bitlen, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1049 | MBEDTLS_ENCRYPT ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1050 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1051 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1052 | return( ret ); | 
|  | 1053 | } | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1054 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1055 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, | 
| Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1056 | cipher_info->key_bitlen, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | MBEDTLS_DECRYPT ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1058 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1060 | return( ret ); | 
|  | 1061 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1062 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1063 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
|  | 1064 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1065 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1066 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, | 
|  | 1067 | MBEDTLS_PADDING_NONE ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1068 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1069 | 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] | 1070 | return( ret ); | 
| Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1071 | } | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1072 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1073 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, | 
|  | 1074 | MBEDTLS_PADDING_NONE ) ) != 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_set_padding_mode", ret ); | 
| Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1077 | return( ret ); | 
|  | 1078 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1079 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1080 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1081 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1082 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1083 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1085 | // Initialize compression | 
|  | 1086 | // | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1087 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1088 | { | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1089 | if( ssl->compress_buf == NULL ) | 
|  | 1090 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1092 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1093 | if( ssl->compress_buf == NULL ) | 
|  | 1094 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1095 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1096 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1097 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1098 | } | 
|  | 1099 | } | 
|  | 1100 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1102 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1103 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); | 
|  | 1104 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1105 |  | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1106 | if( deflateInit( &transform->ctx_deflate, | 
|  | 1107 | Z_DEFAULT_COMPRESSION )   != Z_OK || | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1108 | inflateInit( &transform->ctx_inflate ) != Z_OK ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1109 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1110 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); | 
|  | 1111 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1112 | } | 
|  | 1113 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1114 | #endif /* MBEDTLS_ZLIB_SUPPORT */ | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1115 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1116 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1117 |  | 
|  | 1118 | return( 0 ); | 
|  | 1119 | } | 
|  | 1120 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1121 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 1122 | 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] | 1123 | { | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1124 | mbedtls_md5_context md5; | 
|  | 1125 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1126 | unsigned char pad_1[48]; | 
|  | 1127 | unsigned char pad_2[48]; | 
|  | 1128 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1129 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1130 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1131 | mbedtls_md5_init( &md5 ); | 
|  | 1132 | mbedtls_sha1_init( &sha1 ); | 
|  | 1133 |  | 
|  | 1134 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 1135 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1136 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1137 | memset( pad_1, 0x36, 48 ); | 
|  | 1138 | memset( pad_2, 0x5C, 48 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1139 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1140 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); | 
|  | 1141 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); | 
|  | 1142 | mbedtls_md5_finish_ret( &md5, hash ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1143 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1144 | mbedtls_md5_starts_ret( &md5 ); | 
|  | 1145 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); | 
|  | 1146 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); | 
|  | 1147 | mbedtls_md5_update_ret( &md5, hash,  16 ); | 
|  | 1148 | mbedtls_md5_finish_ret( &md5, hash ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1149 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1150 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); | 
|  | 1151 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); | 
|  | 1152 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1153 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1154 | mbedtls_sha1_starts_ret( &sha1 ); | 
|  | 1155 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); | 
|  | 1156 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); | 
|  | 1157 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); | 
|  | 1158 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1159 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1160 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); | 
|  | 1161 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1162 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1163 | mbedtls_md5_free(  &md5  ); | 
|  | 1164 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1165 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1166 | return; | 
|  | 1167 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1168 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1169 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1170 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 1171 | 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] | 1172 | { | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1173 | mbedtls_md5_context md5; | 
|  | 1174 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1175 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1176 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1177 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1178 | mbedtls_md5_init( &md5 ); | 
|  | 1179 | mbedtls_sha1_init( &sha1 ); | 
|  | 1180 |  | 
|  | 1181 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 1182 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1183 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1184 | mbedtls_md5_finish_ret( &md5,  hash ); | 
|  | 1185 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1186 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1187 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); | 
|  | 1188 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1189 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1190 | mbedtls_md5_free(  &md5  ); | 
|  | 1191 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1192 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1193 | return; | 
|  | 1194 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1195 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1196 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1197 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1198 | #if defined(MBEDTLS_SHA256_C) | 
|  | 1199 | 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] | 1200 | { | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1201 | mbedtls_sha256_context sha256; | 
| 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_sha256_init( &sha256 ); | 
|  | 1204 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1205 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1206 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1207 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1208 | mbedtls_sha256_finish_ret( &sha256, hash ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1209 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1210 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 ); | 
|  | 1211 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1212 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1213 | mbedtls_sha256_free( &sha256 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1214 |  | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1215 | return; | 
|  | 1216 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1217 | #endif /* MBEDTLS_SHA256_C */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1218 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1219 | #if defined(MBEDTLS_SHA512_C) | 
|  | 1220 | 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] | 1221 | { | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1222 | mbedtls_sha512_context sha512; | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1223 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1224 | mbedtls_sha512_init( &sha512 ); | 
|  | 1225 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1226 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1227 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1228 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1229 | mbedtls_sha512_finish_ret( &sha512, hash ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1230 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1231 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 ); | 
|  | 1232 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1233 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1234 | mbedtls_sha512_free( &sha512 ); | 
| Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1235 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1236 | return; | 
|  | 1237 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1238 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 1239 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1240 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1241 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 1242 | 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] | 1243 | { | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1244 | unsigned char *p = ssl->handshake->premaster; | 
|  | 1245 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1246 | const unsigned char *psk = ssl->conf->psk; | 
|  | 1247 | size_t psk_len = ssl->conf->psk_len; | 
|  | 1248 |  | 
|  | 1249 | /* If the psk callback was called, use its result */ | 
|  | 1250 | if( ssl->handshake->psk != NULL ) | 
|  | 1251 | { | 
|  | 1252 | psk = ssl->handshake->psk; | 
|  | 1253 | psk_len = ssl->handshake->psk_len; | 
|  | 1254 | } | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1255 |  | 
|  | 1256 | /* | 
|  | 1257 | * PMS = struct { | 
|  | 1258 | *     opaque other_secret<0..2^16-1>; | 
|  | 1259 | *     opaque psk<0..2^16-1>; | 
|  | 1260 | * }; | 
|  | 1261 | * with "other_secret" depending on the particular key exchange | 
|  | 1262 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1263 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) | 
|  | 1264 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1265 | { | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1266 | if( end - p < 2 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1267 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1268 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1269 | *(p++) = (unsigned char)( psk_len >> 8 ); | 
|  | 1270 | *(p++) = (unsigned char)( psk_len      ); | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1271 |  | 
|  | 1272 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 1273 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1274 |  | 
|  | 1275 | memset( p, 0, psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1276 | p += psk_len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1277 | } | 
|  | 1278 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1279 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ | 
|  | 1280 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) | 
|  | 1281 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1282 | { | 
|  | 1283 | /* | 
|  | 1284 | * other_secret already set by the ClientKeyExchange message, | 
|  | 1285 | * and is 48 bytes long | 
|  | 1286 | */ | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1287 | if( end - p < 2 ) | 
|  | 1288 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1289 |  | 
| Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1290 | *p++ = 0; | 
|  | 1291 | *p++ = 48; | 
|  | 1292 | p += 48; | 
|  | 1293 | } | 
|  | 1294 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1295 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ | 
|  | 1296 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) | 
|  | 1297 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1298 | { | 
| Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1299 | int ret; | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1300 | size_t len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1301 |  | 
| Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1302 | /* Write length only when we know the actual value */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1303 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1304 | p + 2, end - ( p + 2 ), &len, | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1305 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1306 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1307 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1308 | return( ret ); | 
|  | 1309 | } | 
| Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1310 | *(p++) = (unsigned char)( len >> 8 ); | 
|  | 1311 | *(p++) = (unsigned char)( len ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1312 | p += len; | 
|  | 1313 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1314 | 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] | 1315 | } | 
|  | 1316 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1317 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ | 
|  | 1318 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) | 
|  | 1319 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1320 | { | 
| Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1321 | int ret; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1322 | size_t zlen; | 
|  | 1323 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1324 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1325 | p + 2, end - ( p + 2 ), | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1326 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1327 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1328 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1329 | return( ret ); | 
|  | 1330 | } | 
|  | 1331 |  | 
|  | 1332 | *(p++) = (unsigned char)( zlen >> 8 ); | 
|  | 1333 | *(p++) = (unsigned char)( zlen      ); | 
|  | 1334 | p += zlen; | 
|  | 1335 |  | 
| Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 1336 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, | 
|  | 1337 | MBEDTLS_DEBUG_ECDH_Z ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1338 | } | 
|  | 1339 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1340 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1341 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1342 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1343 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1344 | } | 
|  | 1345 |  | 
|  | 1346 | /* opaque psk<0..2^16-1>; */ | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1347 | if( end - p < 2 ) | 
|  | 1348 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1349 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1350 | *(p++) = (unsigned char)( psk_len >> 8 ); | 
|  | 1351 | *(p++) = (unsigned char)( psk_len      ); | 
| Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1352 |  | 
|  | 1353 | if( end < p || (size_t)( end - p ) < psk_len ) | 
|  | 1354 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 1355 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1356 | memcpy( p, psk, psk_len ); | 
|  | 1357 | p += psk_len; | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1358 |  | 
|  | 1359 | ssl->handshake->pmslen = p - ssl->handshake->premaster; | 
|  | 1360 |  | 
|  | 1361 | return( 0 ); | 
|  | 1362 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1363 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ | 
| Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1364 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1366 | /* | 
|  | 1367 | * SSLv3.0 MAC functions | 
|  | 1368 | */ | 
| Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1369 | #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] | 1370 | static void ssl_mac( mbedtls_md_context_t *md_ctx, | 
|  | 1371 | const unsigned char *secret, | 
|  | 1372 | const unsigned char *buf, size_t len, | 
|  | 1373 | const unsigned char *ctr, int type, | 
| Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1374 | unsigned char out[SSL_MAC_MAX_BYTES] ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1375 | { | 
|  | 1376 | unsigned char header[11]; | 
|  | 1377 | unsigned char padding[48]; | 
| Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1378 | int padlen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1379 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); | 
|  | 1380 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1381 |  | 
| Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1382 | /* Only MD5 and SHA-1 supported */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1383 | if( md_type == MBEDTLS_MD_MD5 ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1384 | padlen = 48; | 
| Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1385 | else | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1386 | padlen = 40; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1387 |  | 
|  | 1388 | memcpy( header, ctr, 8 ); | 
|  | 1389 | header[ 8] = (unsigned char)  type; | 
|  | 1390 | header[ 9] = (unsigned char)( len >> 8 ); | 
|  | 1391 | header[10] = (unsigned char)( len      ); | 
|  | 1392 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1393 | memset( padding, 0x36, padlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1394 | mbedtls_md_starts( md_ctx ); | 
|  | 1395 | mbedtls_md_update( md_ctx, secret,  md_size ); | 
|  | 1396 | mbedtls_md_update( md_ctx, padding, padlen  ); | 
|  | 1397 | mbedtls_md_update( md_ctx, header,  11      ); | 
|  | 1398 | mbedtls_md_update( md_ctx, buf,     len     ); | 
| Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1399 | mbedtls_md_finish( md_ctx, out              ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1400 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1401 | memset( padding, 0x5C, padlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | mbedtls_md_starts( md_ctx ); | 
|  | 1403 | mbedtls_md_update( md_ctx, secret,    md_size ); | 
|  | 1404 | mbedtls_md_update( md_ctx, padding,   padlen  ); | 
| Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1405 | mbedtls_md_update( md_ctx, out,       md_size ); | 
|  | 1406 | mbedtls_md_finish( md_ctx, out                ); | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1407 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1408 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1409 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1410 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) ||     \ | 
|  | 1411 | ( defined(MBEDTLS_CIPHER_MODE_CBC) &&                                  \ | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 1412 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C)) ) | 
| Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1413 | #define SSL_SOME_MODES_USE_MAC | 
| Manuel Pégourié-Gonnard | 8e4b337 | 2014-11-17 15:06:13 +0100 | [diff] [blame] | 1414 | #endif | 
|  | 1415 |  | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1416 | /* The function below is only used in the Lucky 13 counter-measure in | 
|  | 1417 | * ssl_decrypt_buf(). These are the defines that guard the call site. */ | 
|  | 1418 | #if defined(SSL_SOME_MODES_USE_MAC) && \ | 
|  | 1419 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 1420 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 1421 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) | 
|  | 1422 | /* This function makes sure every byte in the memory region is accessed | 
|  | 1423 | * (in ascending addresses order) */ | 
|  | 1424 | static void ssl_read_memory( unsigned char *p, size_t len ) | 
|  | 1425 | { | 
|  | 1426 | unsigned char acc = 0; | 
|  | 1427 | volatile unsigned char force; | 
|  | 1428 |  | 
|  | 1429 | for( ; len != 0; p++, len-- ) | 
|  | 1430 | acc ^= *p; | 
|  | 1431 |  | 
|  | 1432 | force = acc; | 
|  | 1433 | (void) force; | 
|  | 1434 | } | 
|  | 1435 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ | 
|  | 1436 |  | 
| Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1437 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1438 | * Encryption/decryption functions | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 1439 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1440 | static int ssl_encrypt_buf( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1441 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1442 | mbedtls_cipher_mode_t mode; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1443 | int auth_done = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1444 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1445 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1446 |  | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1447 | if( ssl->session_out == NULL || ssl->transform_out == NULL ) | 
|  | 1448 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1450 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1451 | } | 
|  | 1452 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1453 | 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] | 1454 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1455 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", | 
| Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 1456 | ssl->out_msg, ssl->out_msglen ); | 
|  | 1457 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1458 | /* | 
| Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1459 | * Add MAC before if needed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1460 | */ | 
| Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1461 | #if defined(SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | if( mode == MBEDTLS_MODE_STREAM || | 
|  | 1463 | ( mode == MBEDTLS_MODE_CBC | 
|  | 1464 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 1465 | && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1466 | #endif | 
|  | 1467 | ) ) | 
| 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 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 1470 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1471 | { | 
| Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1472 | unsigned char mac[SSL_MAC_MAX_BYTES]; | 
| Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1473 |  | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1474 | ssl_mac( &ssl->transform_out->md_ctx_enc, | 
|  | 1475 | ssl->transform_out->mac_enc, | 
|  | 1476 | ssl->out_msg, ssl->out_msglen, | 
| Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1477 | ssl->out_ctr, ssl->out_msgtype, | 
|  | 1478 | mac ); | 
|  | 1479 |  | 
|  | 1480 | 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] | 1481 | } | 
|  | 1482 | else | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1483 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1484 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 1485 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1486 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1487 | { | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1488 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; | 
|  | 1489 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 ); | 
|  | 1491 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 ); | 
|  | 1492 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 ); | 
|  | 1493 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1494 | ssl->out_msg, ssl->out_msglen ); | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1495 | 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] | 1496 | mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc ); | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1497 |  | 
|  | 1498 | 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] | 1499 | } | 
|  | 1500 | else | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1501 | #endif | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1502 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1503 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1504 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1505 | } | 
|  | 1506 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1507 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1508 | ssl->out_msg + ssl->out_msglen, | 
|  | 1509 | ssl->transform_out->maclen ); | 
|  | 1510 |  | 
|  | 1511 | ssl->out_msglen += ssl->transform_out->maclen; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1512 | auth_done++; | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1513 | } | 
| Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1514 | #endif /* AEAD not the only option */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1515 |  | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1516 | /* | 
|  | 1517 | * Encrypt | 
|  | 1518 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1519 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) | 
|  | 1520 | if( mode == MBEDTLS_MODE_STREAM ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1521 | { | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1522 | int ret; | 
|  | 1523 | size_t olen = 0; | 
|  | 1524 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1525 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1526 | "including %d bytes of padding", | 
|  | 1527 | ssl->out_msglen, 0 ) ); | 
|  | 1528 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1529 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc, | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1530 | ssl->transform_out->iv_enc, | 
| Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1531 | ssl->transform_out->ivlen, | 
|  | 1532 | ssl->out_msg, ssl->out_msglen, | 
|  | 1533 | ssl->out_msg, &olen ) ) != 0 ) | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1534 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1535 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1536 | return( ret ); | 
|  | 1537 | } | 
|  | 1538 |  | 
|  | 1539 | if( ssl->out_msglen != olen ) | 
|  | 1540 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1541 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1542 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1543 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1544 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1545 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1546 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1547 | #if defined(MBEDTLS_GCM_C) || \ | 
|  | 1548 | defined(MBEDTLS_CCM_C) || \ | 
|  | 1549 | defined(MBEDTLS_CHACHAPOLY_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1550 | if( mode == MBEDTLS_MODE_GCM || | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1551 | mode == MBEDTLS_MODE_CCM || | 
|  | 1552 | mode == MBEDTLS_MODE_CHACHAPOLY ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1553 | { | 
| Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1554 | int ret; | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1555 | size_t enc_msglen, olen; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1556 | unsigned char *enc_msg; | 
|  | 1557 | unsigned char add_data[13]; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1558 | unsigned char iv[12]; | 
|  | 1559 | mbedtls_ssl_transform *transform = ssl->transform_out; | 
|  | 1560 | unsigned char taglen = transform->ciphersuite_info->flags & | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1561 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1562 | size_t explicit_ivlen = transform->ivlen - transform->fixed_ivlen; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1563 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1564 | /* | 
|  | 1565 | * Prepare additional authenticated data | 
|  | 1566 | */ | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1567 | memcpy( add_data, ssl->out_ctr, 8 ); | 
|  | 1568 | add_data[8]  = ssl->out_msgtype; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1569 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1570 | ssl->conf->transport, add_data + 9 ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1571 | add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF; | 
|  | 1572 | add_data[12] = ssl->out_msglen & 0xFF; | 
|  | 1573 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1574 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1575 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1576 | /* | 
|  | 1577 | * Generate IV | 
|  | 1578 | */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1579 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) | 
|  | 1580 | { | 
| Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 1581 | /* GCM and CCM: fixed || explicit (=seqnum) */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1582 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); | 
|  | 1583 | memcpy( iv + transform->fixed_ivlen, ssl->out_ctr, 8 ); | 
|  | 1584 | memcpy( ssl->out_iv, ssl->out_ctr, 8 ); | 
|  | 1585 |  | 
|  | 1586 | } | 
|  | 1587 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) | 
|  | 1588 | { | 
| Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 1589 | /* ChachaPoly: fixed XOR sequence number */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1590 | unsigned char i; | 
|  | 1591 |  | 
|  | 1592 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); | 
|  | 1593 |  | 
|  | 1594 | for( i = 0; i < 8; i++ ) | 
|  | 1595 | iv[i+4] ^= ssl->out_ctr[i]; | 
|  | 1596 | } | 
|  | 1597 | else | 
| Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 1598 | { | 
|  | 1599 | /* 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] | 1600 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1601 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 1602 | } | 
|  | 1603 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1604 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", | 
|  | 1605 | iv, transform->ivlen ); | 
|  | 1606 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", | 
|  | 1607 | ssl->out_iv, explicit_ivlen ); | 
| Manuel Pégourié-Gonnard | 226d5da | 2013-09-05 13:19:22 +0200 | [diff] [blame] | 1608 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1609 | /* | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1610 | * Fix message length with added IV | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1611 | */ | 
|  | 1612 | enc_msg = ssl->out_msg; | 
|  | 1613 | enc_msglen = ssl->out_msglen; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1614 | ssl->out_msglen += explicit_ivlen; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1615 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1617 | "including 0 bytes of padding", | 
|  | 1618 | ssl->out_msglen ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1619 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1620 | /* | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1621 | * Encrypt and authenticate | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1622 | */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1623 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, | 
|  | 1624 | iv, transform->ivlen, | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1625 | add_data, 13, | 
|  | 1626 | enc_msg, enc_msglen, | 
|  | 1627 | enc_msg, &olen, | 
| Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1628 | enc_msg + enc_msglen, taglen ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1629 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1630 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1631 | return( ret ); | 
|  | 1632 | } | 
|  | 1633 |  | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1634 | if( olen != enc_msglen ) | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1635 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1637 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1638 | } | 
|  | 1639 |  | 
| Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1640 | ssl->out_msglen += taglen; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1641 | auth_done++; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1642 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1643 | 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] | 1644 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1645 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1646 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ | 
|  | 1647 | #if defined(MBEDTLS_CIPHER_MODE_CBC) &&                                    \ | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 1648 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1649 | if( mode == MBEDTLS_MODE_CBC ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1650 | { | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1651 | int ret; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1652 | unsigned char *enc_msg; | 
| Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 1653 | size_t enc_msglen, padlen, olen = 0, i; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1654 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1655 | padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) % | 
|  | 1656 | ssl->transform_out->ivlen; | 
|  | 1657 | if( padlen == ssl->transform_out->ivlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1658 | padlen = 0; | 
|  | 1659 |  | 
|  | 1660 | for( i = 0; i <= padlen; i++ ) | 
|  | 1661 | ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen; | 
|  | 1662 |  | 
|  | 1663 | ssl->out_msglen += padlen + 1; | 
|  | 1664 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1665 | enc_msglen = ssl->out_msglen; | 
|  | 1666 | enc_msg = ssl->out_msg; | 
|  | 1667 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1668 | #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] | 1669 | /* | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1670 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per | 
|  | 1671 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1672 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1673 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1674 | { | 
|  | 1675 | /* | 
|  | 1676 | * Generate IV | 
|  | 1677 | */ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 1678 | 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] | 1679 | ssl->transform_out->ivlen ); | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1680 | if( ret != 0 ) | 
|  | 1681 | return( ret ); | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1682 |  | 
| Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 1683 | memcpy( ssl->out_iv, ssl->transform_out->iv_enc, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1684 | ssl->transform_out->ivlen ); | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1685 |  | 
|  | 1686 | /* | 
|  | 1687 | * Fix pointer positions and message length with added IV | 
|  | 1688 | */ | 
| Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 1689 | enc_msg = ssl->out_msg; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1690 | enc_msglen = ssl->out_msglen; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1691 | ssl->out_msglen += ssl->transform_out->ivlen; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1692 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1693 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1694 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1695 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1696 | "including %d bytes of IV and %d bytes of padding", | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1697 | ssl->out_msglen, ssl->transform_out->ivlen, | 
|  | 1698 | padlen + 1 ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1699 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1700 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc, | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1701 | ssl->transform_out->iv_enc, | 
| Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1702 | ssl->transform_out->ivlen, | 
|  | 1703 | enc_msg, enc_msglen, | 
|  | 1704 | enc_msg, &olen ) ) != 0 ) | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1705 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1706 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1707 | return( ret ); | 
|  | 1708 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1709 |  | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1710 | if( enc_msglen != olen ) | 
|  | 1711 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1712 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1713 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1714 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1715 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1716 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) | 
|  | 1717 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1718 | { | 
|  | 1719 | /* | 
|  | 1720 | * Save IV in SSL3 and TLS1 | 
|  | 1721 | */ | 
|  | 1722 | memcpy( ssl->transform_out->iv_enc, | 
|  | 1723 | ssl->transform_out->cipher_ctx_enc.iv, | 
|  | 1724 | ssl->transform_out->ivlen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1725 | } | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1726 | #endif | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1727 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1728 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1729 | if( auth_done == 0 ) | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1730 | { | 
| Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1731 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; | 
|  | 1732 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1733 | /* | 
|  | 1734 | * MAC(MAC_write_key, seq_num + | 
|  | 1735 | *     TLSCipherText.type + | 
|  | 1736 | *     TLSCipherText.version + | 
| Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1737 | *     length_of( (IV +) ENC(...) ) + | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1738 | *     IV + // except for TLS 1.0 | 
|  | 1739 | *     ENC(content + padding + padding_length)); | 
|  | 1740 | */ | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1741 | unsigned char pseudo_hdr[13]; | 
|  | 1742 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1743 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1744 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1745 | memcpy( pseudo_hdr +  0, ssl->out_ctr, 8 ); | 
|  | 1746 | memcpy( pseudo_hdr +  8, ssl->out_hdr, 3 ); | 
| Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1747 | pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF ); | 
|  | 1748 | pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen      ) & 0xFF ); | 
|  | 1749 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1750 | 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] | 1751 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1752 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 ); | 
|  | 1753 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1754 | ssl->out_iv, ssl->out_msglen ); | 
| Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1755 | 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] | 1756 | mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1757 |  | 
| Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1758 | memcpy( ssl->out_iv + ssl->out_msglen, mac, | 
|  | 1759 | ssl->transform_out->maclen ); | 
|  | 1760 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1761 | ssl->out_msglen += ssl->transform_out->maclen; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1762 | auth_done++; | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1763 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1764 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1765 | } | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1766 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1767 | #endif /* MBEDTLS_CIPHER_MODE_CBC && | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 1768 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1769 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1770 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1771 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1772 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1773 |  | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1774 | /* Make extra sure authentication was performed, exactly once */ | 
|  | 1775 | if( auth_done != 1 ) | 
|  | 1776 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1777 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1778 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1779 | } | 
|  | 1780 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1781 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1782 |  | 
|  | 1783 | return( 0 ); | 
|  | 1784 | } | 
|  | 1785 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1786 | static int ssl_decrypt_buf( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1787 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1788 | mbedtls_cipher_mode_t mode; | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1789 | int auth_done = 0; | 
| Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1790 | #if defined(SSL_SOME_MODES_USE_MAC) | 
| Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1791 | size_t padlen = 0, correct = 1; | 
|  | 1792 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1793 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1794 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); | 
| 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 | if( ssl->session_in == NULL || ssl->transform_in == NULL ) | 
|  | 1797 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1798 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1799 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1800 | } | 
|  | 1801 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1802 | 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] | 1803 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1804 | if( ssl->in_msglen < ssl->transform_in->minlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1805 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1806 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)", | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1807 | ssl->in_msglen, ssl->transform_in->minlen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1808 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1809 | } | 
|  | 1810 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1811 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) | 
|  | 1812 | if( mode == MBEDTLS_MODE_STREAM ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1813 | { | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1814 | int ret; | 
|  | 1815 | size_t olen = 0; | 
|  | 1816 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1817 | padlen = 0; | 
|  | 1818 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1819 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec, | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1820 | ssl->transform_in->iv_dec, | 
| Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1821 | ssl->transform_in->ivlen, | 
|  | 1822 | ssl->in_msg, ssl->in_msglen, | 
|  | 1823 | ssl->in_msg, &olen ) ) != 0 ) | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1824 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1825 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1826 | return( ret ); | 
|  | 1827 | } | 
|  | 1828 |  | 
|  | 1829 | if( ssl->in_msglen != olen ) | 
|  | 1830 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1831 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1832 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1833 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1834 | } | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1835 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1836 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1837 | #if defined(MBEDTLS_GCM_C) || \ | 
|  | 1838 | defined(MBEDTLS_CCM_C) || \ | 
|  | 1839 | defined(MBEDTLS_CHACHAPOLY_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1840 | if( mode == MBEDTLS_MODE_GCM || | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1841 | mode == MBEDTLS_MODE_CCM || | 
|  | 1842 | mode == MBEDTLS_MODE_CHACHAPOLY ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1843 | { | 
| Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1844 | int ret; | 
|  | 1845 | size_t dec_msglen, olen; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1846 | unsigned char *dec_msg; | 
|  | 1847 | unsigned char *dec_msg_result; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1848 | unsigned char add_data[13]; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1849 | unsigned char iv[12]; | 
|  | 1850 | mbedtls_ssl_transform *transform = ssl->transform_in; | 
|  | 1851 | unsigned char taglen = transform->ciphersuite_info->flags & | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1852 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1853 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1854 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1855 | /* | 
|  | 1856 | * Compute and update sizes | 
|  | 1857 | */ | 
| Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 1858 | if( ssl->in_msglen < explicit_iv_len + taglen ) | 
| Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1859 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1860 | 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] | 1861 | "+ taglen (%d)", ssl->in_msglen, | 
|  | 1862 | explicit_iv_len, taglen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1863 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1864 | } | 
|  | 1865 | dec_msglen = ssl->in_msglen - explicit_iv_len - taglen; | 
|  | 1866 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1867 | dec_msg = ssl->in_msg; | 
|  | 1868 | dec_msg_result = ssl->in_msg; | 
|  | 1869 | ssl->in_msglen = dec_msglen; | 
|  | 1870 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1871 | /* | 
|  | 1872 | * Prepare additional authenticated data | 
|  | 1873 | */ | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1874 | memcpy( add_data, ssl->in_ctr, 8 ); | 
|  | 1875 | add_data[8]  = ssl->in_msgtype; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1876 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1877 | ssl->conf->transport, add_data + 9 ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1878 | add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF; | 
|  | 1879 | add_data[12] = ssl->in_msglen & 0xFF; | 
|  | 1880 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1881 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data for AEAD", add_data, 13 ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1882 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1883 | /* | 
|  | 1884 | * Prepare IV | 
|  | 1885 | */ | 
|  | 1886 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) | 
|  | 1887 | { | 
| Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 1888 | /* GCM and CCM: fixed || explicit (transmitted) */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1889 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); | 
|  | 1890 | memcpy( iv + transform->fixed_ivlen, ssl->in_iv, 8 ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1891 |  | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1892 | } | 
|  | 1893 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) | 
|  | 1894 | { | 
| Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 1895 | /* ChachaPoly: fixed XOR sequence number */ | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1896 | unsigned char i; | 
|  | 1897 |  | 
|  | 1898 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); | 
|  | 1899 |  | 
|  | 1900 | for( i = 0; i < 8; i++ ) | 
|  | 1901 | iv[i+4] ^= ssl->in_ctr[i]; | 
|  | 1902 | } | 
|  | 1903 | else | 
|  | 1904 | { | 
|  | 1905 | /* Reminder if we ever add an AEAD mode with a different size */ | 
|  | 1906 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1907 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 1908 | } | 
|  | 1909 |  | 
|  | 1910 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1911 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1912 |  | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1913 | /* | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1914 | * Decrypt and authenticate | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1915 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1916 | 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] | 1917 | iv, transform->ivlen, | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1918 | add_data, 13, | 
|  | 1919 | dec_msg, dec_msglen, | 
|  | 1920 | dec_msg_result, &olen, | 
| Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1921 | dec_msg + dec_msglen, taglen ) ) != 0 ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1922 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1923 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1924 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1925 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) | 
|  | 1926 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1927 |  | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1928 | return( ret ); | 
|  | 1929 | } | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1930 | auth_done++; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1931 |  | 
| Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1932 | if( olen != dec_msglen ) | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1933 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 1935 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1936 | } | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1937 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1938 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1939 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ | 
|  | 1940 | #if defined(MBEDTLS_CIPHER_MODE_CBC) &&                                    \ | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 1941 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1942 | if( mode == MBEDTLS_MODE_CBC ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1943 | { | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1944 | /* | 
|  | 1945 | * Decrypt and check the padding | 
|  | 1946 | */ | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1947 | int ret; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1948 | unsigned char *dec_msg; | 
|  | 1949 | unsigned char *dec_msg_result; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1950 | size_t dec_msglen; | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1951 | size_t minlen = 0; | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1952 | size_t olen = 0; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1953 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1954 | /* | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1955 | * Check immediate ciphertext sanity | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1956 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1957 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 1958 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1959 | minlen += ssl->transform_in->ivlen; | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1960 | #endif | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1961 |  | 
|  | 1962 | if( ssl->in_msglen < minlen + ssl->transform_in->ivlen || | 
|  | 1963 | ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 ) | 
|  | 1964 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1965 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1966 | "+ 1 ) ( + expl IV )", ssl->in_msglen, | 
|  | 1967 | ssl->transform_in->ivlen, | 
|  | 1968 | ssl->transform_in->maclen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1969 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1970 | } | 
|  | 1971 |  | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1972 | dec_msglen = ssl->in_msglen; | 
|  | 1973 | dec_msg = ssl->in_msg; | 
|  | 1974 | dec_msg_result = ssl->in_msg; | 
|  | 1975 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1976 | /* | 
|  | 1977 | * Authenticate before decrypt if enabled | 
|  | 1978 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1979 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 1980 | 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] | 1981 | { | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1982 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; | 
| Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1983 | unsigned char pseudo_hdr[13]; | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1984 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1985 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1986 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1987 | dec_msglen -= ssl->transform_in->maclen; | 
|  | 1988 | ssl->in_msglen -= ssl->transform_in->maclen; | 
|  | 1989 |  | 
| Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1990 | memcpy( pseudo_hdr +  0, ssl->in_ctr, 8 ); | 
|  | 1991 | memcpy( pseudo_hdr +  8, ssl->in_hdr, 3 ); | 
|  | 1992 | pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF ); | 
|  | 1993 | pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen      ) & 0xFF ); | 
|  | 1994 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1995 | 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] | 1996 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1997 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 ); | 
|  | 1998 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1999 | ssl->in_iv, ssl->in_msglen ); | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2000 | 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] | 2001 | mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2002 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2003 | 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] | 2004 | ssl->transform_in->maclen ); | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2005 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2006 | ssl->transform_in->maclen ); | 
|  | 2007 |  | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2008 | if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, mac_expect, | 
|  | 2009 | ssl->transform_in->maclen ) != 0 ) | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2010 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2011 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2012 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2013 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2014 | } | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2015 | auth_done++; | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2016 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2017 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2018 |  | 
|  | 2019 | /* | 
|  | 2020 | * Check length sanity | 
|  | 2021 | */ | 
|  | 2022 | if( ssl->in_msglen % ssl->transform_in->ivlen != 0 ) | 
|  | 2023 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2024 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2025 | ssl->in_msglen, ssl->transform_in->ivlen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2026 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2027 | } | 
|  | 2028 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2029 | #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] | 2030 | /* | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2031 | * 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] | 2032 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2033 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2034 | { | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2035 | unsigned char i; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2036 | dec_msglen -= ssl->transform_in->ivlen; | 
|  | 2037 | ssl->in_msglen -= ssl->transform_in->ivlen; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2038 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2039 | for( i = 0; i < ssl->transform_in->ivlen; i++ ) | 
| Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 2040 | ssl->transform_in->iv_dec[i] = ssl->in_iv[i]; | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2041 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2042 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2043 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2044 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec, | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2045 | ssl->transform_in->iv_dec, | 
| Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 2046 | ssl->transform_in->ivlen, | 
|  | 2047 | dec_msg, dec_msglen, | 
|  | 2048 | dec_msg_result, &olen ) ) != 0 ) | 
| Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2049 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2050 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2051 | return( ret ); | 
|  | 2052 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2053 |  | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2054 | if( dec_msglen != olen ) | 
|  | 2055 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2056 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2057 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2058 | } | 
| Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2059 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2060 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) | 
|  | 2061 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2062 | { | 
|  | 2063 | /* | 
|  | 2064 | * Save IV in SSL3 and TLS1 | 
|  | 2065 | */ | 
|  | 2066 | memcpy( ssl->transform_in->iv_dec, | 
|  | 2067 | ssl->transform_in->cipher_ctx_dec.iv, | 
|  | 2068 | ssl->transform_in->ivlen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2069 | } | 
| Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2070 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2071 |  | 
|  | 2072 | padlen = 1 + ssl->in_msg[ssl->in_msglen - 1]; | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2073 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2074 | if( ssl->in_msglen < ssl->transform_in->maclen + padlen && | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2075 | auth_done == 0 ) | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2076 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2077 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
|  | 2078 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2079 | ssl->in_msglen, ssl->transform_in->maclen, padlen ) ); | 
| Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2080 | #endif | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2081 | padlen = 0; | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2082 | correct = 0; | 
|  | 2083 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2084 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2085 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 2086 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2087 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2088 | if( padlen > ssl->transform_in->ivlen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2089 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2090 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
|  | 2091 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2092 | "should be no more than %d", | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2093 | padlen, ssl->transform_in->ivlen ) ); | 
| Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2094 | #endif | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2095 | correct = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2096 | } | 
|  | 2097 | } | 
|  | 2098 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2099 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 2100 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 2101 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2102 | if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2103 | { | 
|  | 2104 | /* | 
| Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2105 | * TLSv1+: always check the padding up to the first failure | 
|  | 2106 | * and fake check up to 256 bytes of padding | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2107 | */ | 
| Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2108 | size_t pad_count = 0, real_count = 1; | 
| Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2109 | size_t padding_idx = ssl->in_msglen - padlen; | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2110 | size_t i; | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2111 |  | 
| Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2112 | /* | 
|  | 2113 | * Padding is guaranteed to be incorrect if: | 
| Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2114 | *   1. padlen > ssl->in_msglen | 
| Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2115 | * | 
| Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2116 | *   2. padding_idx > MBEDTLS_SSL_IN_CONTENT_LEN + | 
| Paul Bakker | 61885c7 | 2014-04-25 12:59:03 +0200 | [diff] [blame] | 2117 | *                     ssl->transform_in->maclen | 
| Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2118 | * | 
|  | 2119 | * In both cases we reset padding_idx to a safe value (0) to | 
|  | 2120 | * prevent out-of-buffer reads. | 
|  | 2121 | */ | 
| Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2122 | correct &= ( padlen <= ssl->in_msglen ); | 
|  | 2123 | correct &= ( padding_idx <= MBEDTLS_SSL_IN_CONTENT_LEN + | 
| Paul Bakker | 61885c7 | 2014-04-25 12:59:03 +0200 | [diff] [blame] | 2124 | ssl->transform_in->maclen ); | 
| Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2125 |  | 
|  | 2126 | padding_idx *= correct; | 
|  | 2127 |  | 
| Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2128 | for( i = 0; i < 256; i++ ) | 
| Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2129 | { | 
| Angus Gratton | b512bc1 | 2018-06-19 15:57:50 +1000 | [diff] [blame] | 2130 | real_count &= ( i < padlen ); | 
| Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2131 | pad_count += real_count * | 
|  | 2132 | ( ssl->in_msg[padding_idx + i] == padlen - 1 ); | 
|  | 2133 | } | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2134 |  | 
|  | 2135 | correct &= ( pad_count == padlen ); /* Only 1 on correct padding */ | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2136 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2137 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2138 | if( padlen > 0 && correct == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2139 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); | 
| Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2140 | #endif | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2141 | padlen &= correct * 0x1FF; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2142 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2143 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2144 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 2145 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2146 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2147 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2148 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2149 | } | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2150 |  | 
|  | 2151 | ssl->in_msglen -= padlen; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2152 | } | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2153 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2154 | #endif /* MBEDTLS_CIPHER_MODE_CBC && | 
| Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2155 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2156 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2157 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2158 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2159 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2160 |  | 
| Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2161 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2162 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2163 | ssl->in_msg, ssl->in_msglen ); | 
| Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2164 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2165 |  | 
|  | 2166 | /* | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2167 | * Authenticate if not done yet. | 
|  | 2168 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2169 | */ | 
| Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 2170 | #if defined(SSL_SOME_MODES_USE_MAC) | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2171 | if( auth_done == 0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2172 | { | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2173 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; | 
| Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2174 |  | 
| Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2175 | ssl->in_msglen -= ssl->transform_in->maclen; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2176 |  | 
| Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2177 | ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 ); | 
|  | 2178 | ssl->in_len[1] = (unsigned char)( ssl->in_msglen      ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2179 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2180 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 2181 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2182 | { | 
|  | 2183 | ssl_mac( &ssl->transform_in->md_ctx_dec, | 
|  | 2184 | ssl->transform_in->mac_dec, | 
|  | 2185 | ssl->in_msg, ssl->in_msglen, | 
| Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 2186 | ssl->in_ctr, ssl->in_msgtype, | 
|  | 2187 | mac_expect ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2188 | } | 
|  | 2189 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2190 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 2191 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 2192 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2193 | if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2194 | { | 
|  | 2195 | /* | 
|  | 2196 | * Process MAC and always update for padlen afterwards to make | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2197 | * total time independent of padlen. | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2198 | * | 
|  | 2199 | * Known timing attacks: | 
|  | 2200 | *  - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) | 
|  | 2201 | * | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2202 | * To compensate for different timings for the MAC calculation | 
|  | 2203 | * depending on how much padding was removed (which is determined | 
|  | 2204 | * by padlen), process extra_run more blocks through the hash | 
|  | 2205 | * function. | 
|  | 2206 | * | 
|  | 2207 | * The formula in the paper is | 
|  | 2208 | *   extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) | 
|  | 2209 | * where L1 is the size of the header plus the decrypted message | 
|  | 2210 | * plus CBC padding and L2 is the size of the header plus the | 
|  | 2211 | * decrypted message. This is for an underlying hash function | 
|  | 2212 | * with 64-byte blocks. | 
|  | 2213 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values | 
|  | 2214 | * correctly. We round down instead of up, so -56 is the correct | 
|  | 2215 | * value for our calculations instead of -55. | 
|  | 2216 | * | 
| Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 2217 | * Repeat the formula rather than defining a block_size variable. | 
|  | 2218 | * This avoids requiring division by a variable at runtime | 
|  | 2219 | * (which would be marginally less efficient and would require | 
|  | 2220 | * linking an extra division function in some builds). | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2221 | */ | 
|  | 2222 | size_t j, extra_run = 0; | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2223 |  | 
|  | 2224 | /* | 
|  | 2225 | * The next two sizes are the minimum and maximum values of | 
|  | 2226 | * in_msglen over all padlen values. | 
|  | 2227 | * | 
|  | 2228 | * They're independent of padlen, since we previously did | 
|  | 2229 | * in_msglen -= padlen. | 
|  | 2230 | * | 
|  | 2231 | * Note that max_len + maclen is never more than the buffer | 
|  | 2232 | * length, as we previously did in_msglen -= maclen too. | 
|  | 2233 | */ | 
|  | 2234 | const size_t max_len = ssl->in_msglen + padlen; | 
|  | 2235 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; | 
|  | 2236 |  | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2237 | switch( ssl->transform_in->ciphersuite_info->mac ) | 
|  | 2238 | { | 
| Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 2239 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ | 
|  | 2240 | defined(MBEDTLS_SHA256_C) | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2241 | case MBEDTLS_MD_MD5: | 
|  | 2242 | case MBEDTLS_MD_SHA1: | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2243 | case MBEDTLS_MD_SHA256: | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2244 | /* 8 bytes of message size, 64-byte compression blocks */ | 
|  | 2245 | extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 - | 
|  | 2246 | ( 13 + ssl->in_msglen          + 8 ) / 64; | 
|  | 2247 | break; | 
|  | 2248 | #endif | 
| Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 2249 | #if defined(MBEDTLS_SHA512_C) | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2250 | case MBEDTLS_MD_SHA384: | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2251 | /* 16 bytes of message size, 128-byte compression blocks */ | 
|  | 2252 | extra_run = ( 13 + ssl->in_msglen + padlen + 16 ) / 128 - | 
|  | 2253 | ( 13 + ssl->in_msglen          + 16 ) / 128; | 
|  | 2254 | break; | 
|  | 2255 | #endif | 
|  | 2256 | default: | 
| Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 2257 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2258 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 2259 | } | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2260 |  | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2261 | extra_run &= correct * 0xFF; | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2262 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2263 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 ); | 
|  | 2264 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 ); | 
|  | 2265 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 ); | 
|  | 2266 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg, | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2267 | ssl->in_msglen ); | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2268 | /* Make sure we access everything even when padlen > 0. This | 
|  | 2269 | * makes the synchronisation requirements for just-in-time | 
|  | 2270 | * Prime+Probe attacks much tighter and hopefully impractical. */ | 
|  | 2271 | ssl_read_memory( ssl->in_msg + ssl->in_msglen, padlen ); | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2272 | mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, mac_expect ); | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2273 |  | 
|  | 2274 | /* Call mbedtls_md_process at least once due to cache attacks | 
|  | 2275 | * that observe whether md_process() was called of not */ | 
| Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 2276 | for( j = 0; j < extra_run + 1; j++ ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2277 | mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg ); | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2278 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2279 | mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec ); | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2280 |  | 
|  | 2281 | /* Make sure we access all the memory that could contain the MAC, | 
|  | 2282 | * before we check it in the next code block. This makes the | 
|  | 2283 | * synchronisation requirements for just-in-time Prime+Probe | 
|  | 2284 | * attacks much tighter and hopefully impractical. */ | 
|  | 2285 | ssl_read_memory( ssl->in_msg + min_len, | 
|  | 2286 | max_len - min_len + ssl->transform_in->maclen ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2287 | } | 
|  | 2288 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2289 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 2290 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2291 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2292 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2293 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2294 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2295 |  | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2296 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2297 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, ssl->transform_in->maclen ); | 
|  | 2298 | MBEDTLS_SSL_DEBUG_BUF( 4, "message  mac", ssl->in_msg + ssl->in_msglen, | 
|  | 2299 | ssl->transform_in->maclen ); | 
| Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2300 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2301 |  | 
| Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2302 | if( mbedtls_ssl_safer_memcmp( ssl->in_msg + ssl->in_msglen, mac_expect, | 
|  | 2303 | ssl->transform_in->maclen ) != 0 ) | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2304 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | #if defined(MBEDTLS_SSL_DEBUG_ALL) | 
|  | 2306 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); | 
| Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2307 | #endif | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2308 | correct = 0; | 
|  | 2309 | } | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2310 | auth_done++; | 
| Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2311 | } | 
| Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 2312 |  | 
|  | 2313 | /* | 
|  | 2314 | * Finally check the correct flag | 
|  | 2315 | */ | 
|  | 2316 | if( correct == 0 ) | 
|  | 2317 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 2318 | #endif /* SSL_SOME_MODES_USE_MAC */ | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2319 |  | 
|  | 2320 | /* Make extra sure authentication was performed, exactly once */ | 
|  | 2321 | if( auth_done != 1 ) | 
|  | 2322 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2323 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2324 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2325 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2326 |  | 
|  | 2327 | if( ssl->in_msglen == 0 ) | 
|  | 2328 | { | 
| Angus Gratton | 3481792 | 2018-06-19 15:58:22 +1000 | [diff] [blame] | 2329 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 2330 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 | 
|  | 2331 | && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) | 
|  | 2332 | { | 
|  | 2333 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ | 
|  | 2334 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); | 
|  | 2335 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 2336 | } | 
|  | 2337 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 2338 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2339 | ssl->nb_zero++; | 
|  | 2340 |  | 
|  | 2341 | /* | 
|  | 2342 | * Three or more empty messages may be a DoS attack | 
|  | 2343 | * (excessive CPU consumption). | 
|  | 2344 | */ | 
|  | 2345 | if( ssl->nb_zero > 3 ) | 
|  | 2346 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2347 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2348 | "messages, possible DoS attack" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2349 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2350 | } | 
|  | 2351 | } | 
|  | 2352 | else | 
|  | 2353 | ssl->nb_zero = 0; | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2354 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2355 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2356 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2357 | { | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 2358 | ; /* in_ctr read from peer, not maintained internally */ | 
| Manuel Pégourié-Gonnard | ea22ce5 | 2014-09-24 09:46:10 +0200 | [diff] [blame] | 2359 | } | 
|  | 2360 | else | 
|  | 2361 | #endif | 
|  | 2362 | { | 
| Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2363 | unsigned char i; | 
| Manuel Pégourié-Gonnard | ea22ce5 | 2014-09-24 09:46:10 +0200 | [diff] [blame] | 2364 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) | 
|  | 2365 | if( ++ssl->in_ctr[i - 1] != 0 ) | 
|  | 2366 | break; | 
|  | 2367 |  | 
|  | 2368 | /* The loop goes to its end iff the counter is wrapping */ | 
|  | 2369 | if( i == ssl_ep_len( ssl ) ) | 
|  | 2370 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2371 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); | 
|  | 2372 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
| Manuel Pégourié-Gonnard | ea22ce5 | 2014-09-24 09:46:10 +0200 | [diff] [blame] | 2373 | } | 
| Manuel Pégourié-Gonnard | 83cdffc | 2014-03-10 21:20:29 +0100 | [diff] [blame] | 2374 | } | 
|  | 2375 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2376 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2377 |  | 
|  | 2378 | return( 0 ); | 
|  | 2379 | } | 
|  | 2380 |  | 
| Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2381 | #undef MAC_NONE | 
|  | 2382 | #undef MAC_PLAINTEXT | 
|  | 2383 | #undef MAC_CIPHERTEXT | 
|  | 2384 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2385 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2386 | /* | 
|  | 2387 | * Compression/decompression functions | 
|  | 2388 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2389 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2390 | { | 
|  | 2391 | int ret; | 
|  | 2392 | unsigned char *msg_post = ssl->out_msg; | 
| Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 2393 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2394 | size_t len_pre = ssl->out_msglen; | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2395 | unsigned char *msg_pre = ssl->compress_buf; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2396 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2397 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2398 |  | 
| Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2399 | if( len_pre == 0 ) | 
|  | 2400 | return( 0 ); | 
|  | 2401 |  | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2402 | memcpy( msg_pre, ssl->out_msg, len_pre ); | 
|  | 2403 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2404 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2405 | ssl->out_msglen ) ); | 
|  | 2406 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2407 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2408 | ssl->out_msg, ssl->out_msglen ); | 
|  | 2409 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2410 | ssl->transform_out->ctx_deflate.next_in = msg_pre; | 
|  | 2411 | ssl->transform_out->ctx_deflate.avail_in = len_pre; | 
|  | 2412 | ssl->transform_out->ctx_deflate.next_out = msg_post; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2413 | 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] | 2414 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2415 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2416 | if( ret != Z_OK ) | 
|  | 2417 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2418 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); | 
|  | 2419 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2420 | } | 
|  | 2421 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2422 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - | 
| Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 2423 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2424 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2425 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2426 | ssl->out_msglen ) ); | 
|  | 2427 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2428 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2429 | ssl->out_msg, ssl->out_msglen ); | 
|  | 2430 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2431 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2432 |  | 
|  | 2433 | return( 0 ); | 
|  | 2434 | } | 
|  | 2435 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2436 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2437 | { | 
|  | 2438 | int ret; | 
|  | 2439 | unsigned char *msg_post = ssl->in_msg; | 
| Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2440 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2441 | size_t len_pre = ssl->in_msglen; | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2442 | unsigned char *msg_pre = ssl->compress_buf; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2443 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2444 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2445 |  | 
| Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2446 | if( len_pre == 0 ) | 
|  | 2447 | return( 0 ); | 
|  | 2448 |  | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2449 | memcpy( msg_pre, ssl->in_msg, len_pre ); | 
|  | 2450 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2451 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2452 | ssl->in_msglen ) ); | 
|  | 2453 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2454 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2455 | ssl->in_msg, ssl->in_msglen ); | 
|  | 2456 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2457 | ssl->transform_in->ctx_inflate.next_in = msg_pre; | 
|  | 2458 | ssl->transform_in->ctx_inflate.avail_in = len_pre; | 
|  | 2459 | ssl->transform_in->ctx_inflate.next_out = msg_post; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2460 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - | 
| Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2461 | header_bytes; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2462 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2463 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2464 | if( ret != Z_OK ) | 
|  | 2465 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2466 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); | 
|  | 2467 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2468 | } | 
|  | 2469 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2470 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - | 
| Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2471 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2472 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2473 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2474 | ssl->in_msglen ) ); | 
|  | 2475 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2476 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2477 | ssl->in_msg, ssl->in_msglen ); | 
|  | 2478 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2479 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2480 |  | 
|  | 2481 | return( 0 ); | 
|  | 2482 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2483 | #endif /* MBEDTLS_ZLIB_SUPPORT */ | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2484 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2485 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 2486 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2487 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2488 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2489 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2490 | { | 
|  | 2491 | /* If renegotiation is not enforced, retransmit until we would reach max | 
|  | 2492 | * timeout if we were using the usual handshake doubling scheme */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2493 | if( ssl->conf->renego_max_records < 0 ) | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2494 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2495 | 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] | 2496 | unsigned char doublings = 1; | 
|  | 2497 |  | 
|  | 2498 | while( ratio != 0 ) | 
|  | 2499 | { | 
|  | 2500 | ++doublings; | 
|  | 2501 | ratio >>= 1; | 
|  | 2502 | } | 
|  | 2503 |  | 
|  | 2504 | if( ++ssl->renego_records_seen > doublings ) | 
|  | 2505 | { | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2506 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2507 | return( 0 ); | 
|  | 2508 | } | 
|  | 2509 | } | 
|  | 2510 |  | 
|  | 2511 | return( ssl_write_hello_request( ssl ) ); | 
|  | 2512 | } | 
|  | 2513 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2514 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2515 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2516 | /* | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2517 | * Fill the input message buffer by appending data to it. | 
|  | 2518 | * 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] | 2519 | * | 
|  | 2520 | * If we return 0, is it guaranteed that (at least) nb_want bytes are | 
|  | 2521 | * available (from this read and/or a previous one). Otherwise, an error code | 
|  | 2522 | * is returned (possibly EOF or WANT_READ). | 
|  | 2523 | * | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2524 | * With stream transport (TLS) on success ssl->in_left == nb_want, but | 
|  | 2525 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, | 
|  | 2526 | * since we always read a whole datagram at once. | 
|  | 2527 | * | 
| Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 2528 | * 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] | 2529 | * they're done reading a record. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2530 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2531 | 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] | 2532 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2533 | int ret; | 
|  | 2534 | size_t len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2535 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2536 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2537 |  | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2538 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) | 
|  | 2539 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2540 | 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] | 2541 | "or mbedtls_ssl_set_bio()" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2542 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2543 | } | 
|  | 2544 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2545 | 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] | 2546 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2547 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); | 
|  | 2548 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 2549 | } | 
|  | 2550 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2551 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2552 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2553 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2554 | uint32_t timeout; | 
|  | 2555 |  | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 2556 | /* Just to be sure */ | 
|  | 2557 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) | 
|  | 2558 | { | 
|  | 2559 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " | 
|  | 2560 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); | 
|  | 2561 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 2562 | } | 
|  | 2563 |  | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2564 | /* | 
|  | 2565 | * The point is, we need to always read a full datagram at once, so we | 
|  | 2566 | * sometimes read more then requested, and handle the additional data. | 
|  | 2567 | * It could be the rest of the current record (while fetching the | 
|  | 2568 | * header) and/or some other records in the same datagram. | 
|  | 2569 | */ | 
|  | 2570 |  | 
|  | 2571 | /* | 
|  | 2572 | * Move to the next record in the already read datagram if applicable | 
|  | 2573 | */ | 
|  | 2574 | if( ssl->next_record_offset != 0 ) | 
|  | 2575 | { | 
|  | 2576 | if( ssl->in_left < ssl->next_record_offset ) | 
|  | 2577 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2578 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2579 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2580 | } | 
|  | 2581 |  | 
|  | 2582 | ssl->in_left -= ssl->next_record_offset; | 
|  | 2583 |  | 
|  | 2584 | if( ssl->in_left != 0 ) | 
|  | 2585 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2586 | 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] | 2587 | ssl->next_record_offset ) ); | 
|  | 2588 | memmove( ssl->in_hdr, | 
|  | 2589 | ssl->in_hdr + ssl->next_record_offset, | 
|  | 2590 | ssl->in_left ); | 
|  | 2591 | } | 
|  | 2592 |  | 
|  | 2593 | ssl->next_record_offset = 0; | 
|  | 2594 | } | 
|  | 2595 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2596 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2597 | ssl->in_left, nb_want ) ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2598 |  | 
|  | 2599 | /* | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2600 | * Done if we already have enough data. | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2601 | */ | 
|  | 2602 | if( nb_want <= ssl->in_left) | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2603 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2604 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2605 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2606 | } | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2607 |  | 
|  | 2608 | /* | 
|  | 2609 | * A record can't be split accross datagrams. If we need to read but | 
|  | 2610 | * are not at the beginning of a new record, the caller did something | 
|  | 2611 | * wrong. | 
|  | 2612 | */ | 
|  | 2613 | if( ssl->in_left != 0 ) | 
|  | 2614 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2615 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 2616 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2617 | } | 
|  | 2618 |  | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2619 | /* | 
|  | 2620 | * Don't even try to read if time's out already. | 
|  | 2621 | * This avoids by-passing the timer when repeatedly receiving messages | 
|  | 2622 | * that will end up being dropped. | 
|  | 2623 | */ | 
|  | 2624 | if( ssl_check_timer( ssl ) != 0 ) | 
| Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 2625 | { | 
|  | 2626 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2627 | ret = MBEDTLS_ERR_SSL_TIMEOUT; | 
| Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 2628 | } | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2629 | else | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2630 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2631 | 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] | 2632 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2633 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2634 | timeout = ssl->handshake->retransmit_timeout; | 
|  | 2635 | else | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2636 | timeout = ssl->conf->read_timeout; | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2637 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2638 | 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] | 2639 |  | 
| Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 2640 | if( ssl->f_recv_timeout != NULL ) | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2641 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, | 
|  | 2642 | timeout ); | 
|  | 2643 | else | 
|  | 2644 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); | 
|  | 2645 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2647 |  | 
|  | 2648 | if( ret == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2649 | return( MBEDTLS_ERR_SSL_CONN_EOF ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2650 | } | 
|  | 2651 |  | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2652 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2653 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2655 | ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2656 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2657 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 2658 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2659 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) | 
|  | 2660 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2661 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2662 | return( MBEDTLS_ERR_SSL_TIMEOUT ); | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2663 | } | 
|  | 2664 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2665 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2666 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2667 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2668 | return( ret ); | 
|  | 2669 | } | 
|  | 2670 |  | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2671 | return( MBEDTLS_ERR_SSL_WANT_READ ); | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 2672 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2674 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2675 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2676 | { | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2677 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2678 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2679 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2680 | return( ret ); | 
|  | 2681 | } | 
|  | 2682 |  | 
| Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2683 | return( MBEDTLS_ERR_SSL_WANT_READ ); | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2684 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2685 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2686 | } | 
|  | 2687 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2688 | if( ret < 0 ) | 
|  | 2689 | return( ret ); | 
|  | 2690 |  | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2691 | ssl->in_left = ret; | 
|  | 2692 | } | 
|  | 2693 | else | 
|  | 2694 | #endif | 
|  | 2695 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2696 | 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] | 2697 | ssl->in_left, nb_want ) ); | 
|  | 2698 |  | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2699 | while( ssl->in_left < nb_want ) | 
|  | 2700 | { | 
|  | 2701 | len = nb_want - ssl->in_left; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 2702 |  | 
|  | 2703 | if( ssl_check_timer( ssl ) != 0 ) | 
|  | 2704 | ret = MBEDTLS_ERR_SSL_TIMEOUT; | 
|  | 2705 | else | 
| Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 2706 | { | 
|  | 2707 | if( ssl->f_recv_timeout != NULL ) | 
|  | 2708 | { | 
|  | 2709 | ret = ssl->f_recv_timeout( ssl->p_bio, | 
|  | 2710 | ssl->in_hdr + ssl->in_left, len, | 
|  | 2711 | ssl->conf->read_timeout ); | 
|  | 2712 | } | 
|  | 2713 | else | 
|  | 2714 | { | 
|  | 2715 | ret = ssl->f_recv( ssl->p_bio, | 
|  | 2716 | ssl->in_hdr + ssl->in_left, len ); | 
|  | 2717 | } | 
|  | 2718 | } | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2719 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2720 | 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] | 2721 | ssl->in_left, nb_want ) ); | 
|  | 2722 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2723 |  | 
|  | 2724 | if( ret == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2725 | return( MBEDTLS_ERR_SSL_CONN_EOF ); | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2726 |  | 
|  | 2727 | if( ret < 0 ) | 
|  | 2728 | return( ret ); | 
|  | 2729 |  | 
| mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 2730 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) | 
| mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 2731 | { | 
| Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 2732 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2733 | ( "f_recv returned %d bytes but only %lu were requested", | 
| mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 2734 | ret, (unsigned long)len ) ); | 
| mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 2735 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 2736 | } | 
|  | 2737 |  | 
| Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2738 | ssl->in_left += ret; | 
|  | 2739 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2740 | } | 
|  | 2741 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2742 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2743 |  | 
|  | 2744 | return( 0 ); | 
|  | 2745 | } | 
|  | 2746 |  | 
|  | 2747 | /* | 
|  | 2748 | * Flush any data not yet written | 
|  | 2749 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2750 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2751 | { | 
| Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2752 | int ret; | 
| Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2753 | unsigned char *buf; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2754 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2755 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2756 |  | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2757 | if( ssl->f_send == NULL ) | 
|  | 2758 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2759 | 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] | 2760 | "or mbedtls_ssl_set_bio()" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2761 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2762 | } | 
|  | 2763 |  | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2764 | /* Avoid incrementing counter if data is flushed */ | 
|  | 2765 | if( ssl->out_left == 0 ) | 
|  | 2766 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2767 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2768 | return( 0 ); | 
|  | 2769 | } | 
|  | 2770 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2771 | while( ssl->out_left > 0 ) | 
|  | 2772 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2773 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", | 
|  | 2774 | mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2775 |  | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2776 | buf = ssl->out_hdr - ssl->out_left; | 
| Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2777 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); | 
| Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 2778 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2779 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2780 |  | 
|  | 2781 | if( ret <= 0 ) | 
|  | 2782 | return( ret ); | 
|  | 2783 |  | 
| mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 2784 | 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] | 2785 | { | 
| Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 2786 | MBEDTLS_SSL_DEBUG_MSG( 1, | 
|  | 2787 | ( "f_send returned %d bytes but only %lu bytes were sent", | 
| mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 2788 | ret, (unsigned long)ssl->out_left ) ); | 
| mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 2789 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 2790 | } | 
|  | 2791 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2792 | ssl->out_left -= ret; | 
|  | 2793 | } | 
|  | 2794 |  | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2795 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 2796 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2797 | { | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2798 | ssl->out_hdr = ssl->out_buf; | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2799 | } | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2800 | else | 
|  | 2801 | #endif | 
|  | 2802 | { | 
|  | 2803 | ssl->out_hdr = ssl->out_buf + 8; | 
|  | 2804 | } | 
|  | 2805 | ssl_update_out_pointers( ssl, ssl->transform_out ); | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2806 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2807 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2808 |  | 
|  | 2809 | return( 0 ); | 
|  | 2810 | } | 
|  | 2811 |  | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2812 | /* | 
|  | 2813 | * Functions to handle the DTLS retransmission state machine | 
|  | 2814 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2815 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2816 | /* | 
|  | 2817 | * Append current handshake message to current outgoing flight | 
|  | 2818 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2819 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2820 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2821 | mbedtls_ssl_flight_item *msg; | 
| Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 2822 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); | 
|  | 2823 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", | 
|  | 2824 | ssl->out_msg, ssl->out_msglen ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2825 |  | 
|  | 2826 | /* Allocate space for current message */ | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2827 | 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] | 2828 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2829 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2830 | sizeof( mbedtls_ssl_flight_item ) ) ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2831 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2832 | } | 
|  | 2833 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2834 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2835 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2836 | 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] | 2837 | mbedtls_free( msg ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2838 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2839 | } | 
|  | 2840 |  | 
|  | 2841 | /* Copy current handshake message with headers */ | 
|  | 2842 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); | 
|  | 2843 | msg->len = ssl->out_msglen; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2844 | msg->type = ssl->out_msgtype; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2845 | msg->next = NULL; | 
|  | 2846 |  | 
|  | 2847 | /* Append to the current flight */ | 
|  | 2848 | if( ssl->handshake->flight == NULL ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2849 | ssl->handshake->flight = msg; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2850 | else | 
|  | 2851 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2852 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2853 | while( cur->next != NULL ) | 
|  | 2854 | cur = cur->next; | 
|  | 2855 | cur->next = msg; | 
|  | 2856 | } | 
|  | 2857 |  | 
| Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 2858 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2859 | return( 0 ); | 
|  | 2860 | } | 
|  | 2861 |  | 
|  | 2862 | /* | 
|  | 2863 | * Free the current flight of handshake messages | 
|  | 2864 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2865 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2866 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2867 | mbedtls_ssl_flight_item *cur = flight; | 
|  | 2868 | mbedtls_ssl_flight_item *next; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2869 |  | 
|  | 2870 | while( cur != NULL ) | 
|  | 2871 | { | 
|  | 2872 | next = cur->next; | 
|  | 2873 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2874 | mbedtls_free( cur->p ); | 
|  | 2875 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2876 |  | 
|  | 2877 | cur = next; | 
|  | 2878 | } | 
|  | 2879 | } | 
|  | 2880 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2881 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 2882 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 2883 | #endif | 
|  | 2884 |  | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2885 | /* | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2886 | * Swap transform_out and out_ctr with the alternative ones | 
|  | 2887 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2889 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2890 | mbedtls_ssl_transform *tmp_transform; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2891 | unsigned char tmp_out_ctr[8]; | 
|  | 2892 |  | 
|  | 2893 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) | 
|  | 2894 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2895 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2896 | return; | 
|  | 2897 | } | 
|  | 2898 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2899 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2900 |  | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2901 | /* Swap transforms */ | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2902 | tmp_transform                     = ssl->transform_out; | 
|  | 2903 | ssl->transform_out                = ssl->handshake->alt_transform_out; | 
|  | 2904 | ssl->handshake->alt_transform_out = tmp_transform; | 
|  | 2905 |  | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2906 | /* Swap epoch + sequence_number */ | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 2907 | memcpy( tmp_out_ctr,                 ssl->cur_out_ctr,            8 ); | 
|  | 2908 | 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] | 2909 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr,                 8 ); | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2910 |  | 
|  | 2911 | /* Adjust to the newly activated transform */ | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 2912 | ssl_update_out_pointers( ssl, ssl->transform_out ); | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2913 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2914 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 2915 | if( mbedtls_ssl_hw_record_activate != NULL ) | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2916 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2917 | 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] | 2918 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2919 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); | 
|  | 2920 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2921 | } | 
|  | 2922 | } | 
|  | 2923 | #endif | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2924 | } | 
|  | 2925 |  | 
|  | 2926 | /* | 
|  | 2927 | * Retransmit the current flight of messages. | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2928 | */ | 
|  | 2929 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) | 
|  | 2930 | { | 
|  | 2931 | int ret = 0; | 
|  | 2932 |  | 
|  | 2933 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); | 
|  | 2934 |  | 
|  | 2935 | ret = mbedtls_ssl_flight_transmit( ssl ); | 
|  | 2936 |  | 
|  | 2937 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); | 
|  | 2938 |  | 
|  | 2939 | return( ret ); | 
|  | 2940 | } | 
|  | 2941 |  | 
|  | 2942 | /* | 
|  | 2943 | * Transmit or retransmit the current flight of messages. | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2944 | * | 
|  | 2945 | * Need to remember the current message in case flush_output returns | 
|  | 2946 | * 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] | 2947 | * 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] | 2948 | */ | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2949 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2950 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2951 | int ret; | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2952 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2953 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2954 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2955 | { | 
| Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 2956 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2957 |  | 
|  | 2958 | ssl->handshake->cur_msg = ssl->handshake->flight; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2959 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2960 | ssl_swap_epochs( ssl ); | 
|  | 2961 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2962 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2963 | } | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2964 |  | 
|  | 2965 | while( ssl->handshake->cur_msg != NULL ) | 
|  | 2966 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2967 | size_t max_frag_len; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2968 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2969 |  | 
| Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2970 | int const is_finished = | 
|  | 2971 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 2972 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); | 
|  | 2973 |  | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2974 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? | 
|  | 2975 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; | 
|  | 2976 |  | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2977 | /* Swap epochs before sending Finished: we can't do it after | 
|  | 2978 | * sending ChangeCipherSpec, in case write returns WANT_READ. | 
|  | 2979 | * Must be done before copying, may change out_msg pointer */ | 
| Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2980 | 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] | 2981 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2982 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) ); | 
| Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2983 | ssl_swap_epochs( ssl ); | 
|  | 2984 | } | 
|  | 2985 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2986 | ret = ssl_get_remaining_payload_in_datagram( ssl ); | 
|  | 2987 | if( ret < 0 ) | 
|  | 2988 | return( ret ); | 
|  | 2989 | max_frag_len = (size_t) ret; | 
|  | 2990 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2991 | /* CCS is copied as is, while HS messages may need fragmentation */ | 
|  | 2992 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) | 
|  | 2993 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2994 | if( max_frag_len == 0 ) | 
|  | 2995 | { | 
|  | 2996 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
|  | 2997 | return( ret ); | 
|  | 2998 |  | 
|  | 2999 | continue; | 
|  | 3000 | } | 
|  | 3001 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3002 | memcpy( ssl->out_msg, cur->p, cur->len ); | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3003 | ssl->out_msglen  = cur->len; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3004 | ssl->out_msgtype = cur->type; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3005 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3006 | /* Update position inside current message */ | 
|  | 3007 | ssl->handshake->cur_msg_p += cur->len; | 
|  | 3008 | } | 
|  | 3009 | else | 
|  | 3010 | { | 
|  | 3011 | const unsigned char * const p = ssl->handshake->cur_msg_p; | 
|  | 3012 | const size_t hs_len = cur->len - 12; | 
|  | 3013 | const size_t frag_off = p - ( cur->p + 12 ); | 
|  | 3014 | const size_t rem_len = hs_len - frag_off; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3015 | size_t cur_hs_frag_len, max_hs_frag_len; | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3016 |  | 
| Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3017 | 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] | 3018 | { | 
| Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3019 | if( is_finished ) | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3020 | ssl_swap_epochs( ssl ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3021 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3022 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
|  | 3023 | return( ret ); | 
|  | 3024 |  | 
|  | 3025 | continue; | 
|  | 3026 | } | 
|  | 3027 | max_hs_frag_len = max_frag_len - 12; | 
|  | 3028 |  | 
|  | 3029 | cur_hs_frag_len = rem_len > max_hs_frag_len ? | 
|  | 3030 | max_hs_frag_len : rem_len; | 
|  | 3031 |  | 
|  | 3032 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) | 
| Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3033 | { | 
|  | 3034 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3035 | (unsigned) cur_hs_frag_len, | 
|  | 3036 | (unsigned) max_hs_frag_len ) ); | 
| Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3037 | } | 
| Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 3038 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3039 | /* Messages are stored with handshake headers as if not fragmented, | 
|  | 3040 | * copy beginning of headers then fill fragmentation fields. | 
|  | 3041 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ | 
|  | 3042 | memcpy( ssl->out_msg, cur->p, 6 ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3043 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3044 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); | 
|  | 3045 | ssl->out_msg[7] = ( ( frag_off >>  8 ) & 0xff ); | 
|  | 3046 | ssl->out_msg[8] = ( ( frag_off       ) & 0xff ); | 
|  | 3047 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3048 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); | 
|  | 3049 | ssl->out_msg[10] = ( ( cur_hs_frag_len >>  8 ) & 0xff ); | 
|  | 3050 | ssl->out_msg[11] = ( ( cur_hs_frag_len       ) & 0xff ); | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3051 |  | 
|  | 3052 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); | 
|  | 3053 |  | 
| Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 3054 | /* Copy the handshake message content and set records fields */ | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3055 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); | 
|  | 3056 | ssl->out_msglen = cur_hs_frag_len + 12; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3057 | ssl->out_msgtype = cur->type; | 
|  | 3058 |  | 
|  | 3059 | /* Update position inside current message */ | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3060 | ssl->handshake->cur_msg_p += cur_hs_frag_len; | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3061 | } | 
|  | 3062 |  | 
|  | 3063 | /* If done with the current message move to the next one if any */ | 
|  | 3064 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) | 
|  | 3065 | { | 
|  | 3066 | if( cur->next != NULL ) | 
|  | 3067 | { | 
|  | 3068 | ssl->handshake->cur_msg = cur->next; | 
|  | 3069 | ssl->handshake->cur_msg_p = cur->next->p + 12; | 
|  | 3070 | } | 
|  | 3071 | else | 
|  | 3072 | { | 
|  | 3073 | ssl->handshake->cur_msg = NULL; | 
|  | 3074 | ssl->handshake->cur_msg_p = NULL; | 
|  | 3075 | } | 
|  | 3076 | } | 
|  | 3077 |  | 
|  | 3078 | /* Actually send the message out */ | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3079 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3080 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3081 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3082 | return( ret ); | 
|  | 3083 | } | 
|  | 3084 | } | 
|  | 3085 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3086 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
|  | 3087 | return( ret ); | 
|  | 3088 |  | 
| Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3089 | /* Update state and set timer */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3090 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 3091 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; | 
| Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 3092 | else | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3093 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3094 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3095 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); | 
|  | 3096 | } | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3097 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3098 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3099 |  | 
|  | 3100 | return( 0 ); | 
|  | 3101 | } | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3102 |  | 
|  | 3103 | /* | 
|  | 3104 | * To be called when the last message of an incoming flight is received. | 
|  | 3105 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3106 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3107 | { | 
|  | 3108 | /* We won't need to resend that one any more */ | 
|  | 3109 | ssl_flight_free( ssl->handshake->flight ); | 
|  | 3110 | ssl->handshake->flight = NULL; | 
|  | 3111 | ssl->handshake->cur_msg = NULL; | 
|  | 3112 |  | 
|  | 3113 | /* The next incoming flight will start with this msg_seq */ | 
|  | 3114 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; | 
|  | 3115 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3116 | /* We don't want to remember CCS's across flight boundaries. */ | 
| Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3117 | ssl->handshake->buffering.seen_ccs = 0; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3118 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3119 | /* Clear future message buffering structure. */ | 
|  | 3120 | ssl_buffering_free( ssl ); | 
|  | 3121 |  | 
| Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3122 | /* Cancel timer */ | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3123 | ssl_set_timer( ssl, 0 ); | 
|  | 3124 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3125 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 3126 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3127 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3128 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3129 | } | 
|  | 3130 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3131 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3132 | } | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3133 |  | 
|  | 3134 | /* | 
|  | 3135 | * To be called when the last message of an outgoing flight is send. | 
|  | 3136 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3137 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3138 | { | 
| Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3139 | ssl_reset_retransmit_timeout( ssl ); | 
| Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3140 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3141 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3142 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 3143 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3144 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3145 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3146 | } | 
|  | 3147 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3148 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3149 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3150 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3151 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3152 | /* | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3153 | * Handshake layer functions | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3154 | */ | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3155 |  | 
|  | 3156 | /* | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3157 | * Write (DTLS: or queue) current handshake (including CCS) message. | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3158 | * | 
|  | 3159 | *  - fill in handshake headers | 
|  | 3160 | *  - update handshake checksum | 
|  | 3161 | *  - DTLS: save message for resending | 
|  | 3162 | *  - then pass to the record layer | 
|  | 3163 | * | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3164 | * DTLS: except for HelloRequest, messages are only queued, and will only be | 
|  | 3165 | * actually sent when calling flight_transmit() or resend(). | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3166 | * | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3167 | * Inputs: | 
|  | 3168 | *  - ssl->out_msglen: 4 + actual handshake message len | 
|  | 3169 | *      (4 is the size of handshake headers for TLS) | 
|  | 3170 | *  - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) | 
|  | 3171 | *  - ssl->out_msg + 4: the handshake message body | 
|  | 3172 | * | 
| Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 3173 | * 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] | 3174 | *   - ssl->out_msglen: the length of the record contents | 
|  | 3175 | *      (including handshake headers but excluding record headers) | 
|  | 3176 | *   - ssl->out_msg: the record contents (handshake headers + content) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3177 | */ | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3178 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3179 | { | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3180 | int ret; | 
|  | 3181 | const size_t hs_len = ssl->out_msglen - 4; | 
|  | 3182 | const unsigned char hs_type = ssl->out_msg[0]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3183 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3184 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); | 
|  | 3185 |  | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3186 | /* | 
|  | 3187 | * Sanity checks | 
|  | 3188 | */ | 
| Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3189 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE          && | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3190 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) | 
|  | 3191 | { | 
| Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3192 | /* In SSLv3, the client might send a NoCertificate alert. */ | 
|  | 3193 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 3194 | if( ! ( ssl->minor_ver      == MBEDTLS_SSL_MINOR_VERSION_0 && | 
|  | 3195 | ssl->out_msgtype    == MBEDTLS_SSL_MSG_ALERT       && | 
|  | 3196 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ) | 
|  | 3197 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ | 
|  | 3198 | { | 
|  | 3199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3200 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3201 | } | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3202 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3203 |  | 
| Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3204 | /* Whenever we send anything different from a | 
|  | 3205 | * HelloRequest we should be in a handshake - double check. */ | 
|  | 3206 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 3207 | hs_type          == MBEDTLS_SSL_HS_HELLO_REQUEST ) && | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3208 | ssl->handshake == NULL ) | 
|  | 3209 | { | 
|  | 3210 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3211 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3212 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3213 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3214 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3215 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3216 | ssl->handshake != NULL && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3217 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3218 | { | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3219 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 3220 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3221 | } | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3222 | #endif | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3223 |  | 
| Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 3224 | /* Double-check that we did not exceed the bounds | 
|  | 3225 | * of the outgoing record buffer. | 
|  | 3226 | * This should never fail as the various message | 
|  | 3227 | * writing functions must obey the bounds of the | 
|  | 3228 | * outgoing record buffer, but better be safe. | 
|  | 3229 | * | 
|  | 3230 | * Note: We deliberately do not check for the MTU or MFL here. | 
|  | 3231 | */ | 
|  | 3232 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
|  | 3233 | { | 
|  | 3234 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " | 
|  | 3235 | "size %u, maximum %u", | 
|  | 3236 | (unsigned) ssl->out_msglen, | 
|  | 3237 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | 
|  | 3238 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3239 | } | 
|  | 3240 |  | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3241 | /* | 
|  | 3242 | * Fill handshake headers | 
|  | 3243 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3244 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3245 | { | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3246 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); | 
|  | 3247 | ssl->out_msg[2] = (unsigned char)( hs_len >>  8 ); | 
|  | 3248 | ssl->out_msg[3] = (unsigned char)( hs_len       ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3249 |  | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3250 | /* | 
|  | 3251 | * DTLS has additional fields in the Handshake layer, | 
|  | 3252 | * between the length field and the actual payload: | 
|  | 3253 | *      uint16 message_seq; | 
|  | 3254 | *      uint24 fragment_offset; | 
|  | 3255 | *      uint24 fragment_length; | 
|  | 3256 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3257 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3258 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3259 | { | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3260 | /* Make room for the additional DTLS fields */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3261 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) | 
| Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 3262 | { | 
|  | 3263 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " | 
|  | 3264 | "size %u, maximum %u", | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3265 | (unsigned) ( hs_len ), | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3266 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); | 
| Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 3267 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3268 | } | 
|  | 3269 |  | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3270 | 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] | 3271 | ssl->out_msglen += 8; | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3272 |  | 
| Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3273 | /* Write message_seq and update it, except for HelloRequest */ | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3274 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) | 
| Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3275 | { | 
| Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 3276 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; | 
|  | 3277 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq      ) & 0xFF; | 
|  | 3278 | ++( ssl->handshake->out_msg_seq ); | 
| Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3279 | } | 
|  | 3280 | else | 
|  | 3281 | { | 
|  | 3282 | ssl->out_msg[4] = 0; | 
|  | 3283 | ssl->out_msg[5] = 0; | 
|  | 3284 | } | 
| Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3285 |  | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3286 | /* Handshake hashes are computed without fragmentation, | 
|  | 3287 | * 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] | 3288 | memset( ssl->out_msg + 6, 0x00, 3 ); | 
|  | 3289 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3290 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3291 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3292 |  | 
| Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 3293 | /* Update running hashes of handshake messages seen */ | 
| Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3294 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) | 
|  | 3295 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3296 | } | 
|  | 3297 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3298 | /* 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] | 3299 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3300 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3301 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 3302 | hs_type          == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3303 | { | 
|  | 3304 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) | 
|  | 3305 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3306 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3307 | return( ret ); | 
|  | 3308 | } | 
|  | 3309 | } | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3310 | else | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3311 | #endif | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3312 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3313 | 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] | 3314 | { | 
|  | 3315 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); | 
|  | 3316 | return( ret ); | 
|  | 3317 | } | 
|  | 3318 | } | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3319 |  | 
|  | 3320 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); | 
|  | 3321 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3322 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3323 | } | 
|  | 3324 |  | 
|  | 3325 | /* | 
|  | 3326 | * Record layer functions | 
|  | 3327 | */ | 
|  | 3328 |  | 
|  | 3329 | /* | 
|  | 3330 | * Write current record. | 
|  | 3331 | * | 
|  | 3332 | * Uses: | 
|  | 3333 | *  - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) | 
|  | 3334 | *  - ssl->out_msglen: length of the record content (excl headers) | 
|  | 3335 | *  - ssl->out_msg: record content | 
|  | 3336 | */ | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3337 | 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] | 3338 | { | 
|  | 3339 | int ret, done = 0; | 
|  | 3340 | size_t len = ssl->out_msglen; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3341 | uint8_t flush = force_flush; | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3342 |  | 
|  | 3343 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3344 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3346 | if( ssl->transform_out != NULL && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3347 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3348 | { | 
|  | 3349 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) | 
|  | 3350 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3351 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3352 | return( ret ); | 
|  | 3353 | } | 
|  | 3354 |  | 
|  | 3355 | len = ssl->out_msglen; | 
|  | 3356 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3357 | #endif /*MBEDTLS_ZLIB_SUPPORT */ | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3358 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3359 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 3360 | if( mbedtls_ssl_hw_record_write != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3361 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3362 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3363 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3364 | ret = mbedtls_ssl_hw_record_write( ssl ); | 
|  | 3365 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3366 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3367 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); | 
|  | 3368 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3369 | } | 
| Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3370 |  | 
|  | 3371 | if( ret == 0 ) | 
|  | 3372 | done = 1; | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3373 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3375 | if( !done ) | 
|  | 3376 | { | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3377 | unsigned i; | 
|  | 3378 | size_t protected_record_size; | 
|  | 3379 |  | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3380 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3381 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3382 | ssl->conf->transport, ssl->out_hdr + 1 ); | 
| Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3383 |  | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3384 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); | 
| Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3385 | ssl->out_len[0] = (unsigned char)( len >> 8 ); | 
|  | 3386 | ssl->out_len[1] = (unsigned char)( len      ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3387 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3388 | if( ssl->transform_out != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3389 | { | 
|  | 3390 | if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 ) | 
|  | 3391 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3392 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3393 | return( ret ); | 
|  | 3394 | } | 
|  | 3395 |  | 
|  | 3396 | len = ssl->out_msglen; | 
| Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3397 | ssl->out_len[0] = (unsigned char)( len >> 8 ); | 
|  | 3398 | ssl->out_len[1] = (unsigned char)( len      ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3399 | } | 
|  | 3400 |  | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3401 | protected_record_size = len + mbedtls_ssl_hdr_len( ssl ); | 
|  | 3402 |  | 
|  | 3403 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 3404 | /* In case of DTLS, double-check that we don't exceed | 
|  | 3405 | * the remaining space in the datagram. */ | 
|  | 3406 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 3407 | { | 
| Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 3408 | ret = ssl_get_remaining_space_in_datagram( ssl ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3409 | if( ret < 0 ) | 
|  | 3410 | return( ret ); | 
|  | 3411 |  | 
|  | 3412 | if( protected_record_size > (size_t) ret ) | 
|  | 3413 | { | 
|  | 3414 | /* Should never happen */ | 
|  | 3415 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3416 | } | 
|  | 3417 | } | 
|  | 3418 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3419 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3420 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " | 
| Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 3421 | "version = [%d:%d], msglen = %d", | 
|  | 3422 | ssl->out_hdr[0], ssl->out_hdr[1], | 
|  | 3423 | ssl->out_hdr[2], len ) ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3424 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3425 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", | 
| Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 3426 | ssl->out_hdr, protected_record_size ); | 
| Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3427 |  | 
|  | 3428 | ssl->out_left += protected_record_size; | 
|  | 3429 | ssl->out_hdr  += protected_record_size; | 
|  | 3430 | ssl_update_out_pointers( ssl, ssl->transform_out ); | 
|  | 3431 |  | 
| Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3432 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) | 
|  | 3433 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) | 
|  | 3434 | break; | 
|  | 3435 |  | 
|  | 3436 | /* The loop goes to its end iff the counter is wrapping */ | 
|  | 3437 | if( i == ssl_ep_len( ssl ) ) | 
|  | 3438 | { | 
|  | 3439 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); | 
|  | 3440 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
|  | 3441 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3442 | } | 
|  | 3443 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3444 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 3445 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 3446 | flush == SSL_DONT_FORCE_FLUSH ) | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3447 | { | 
| Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 3448 | size_t remaining; | 
|  | 3449 | ret = ssl_get_remaining_payload_in_datagram( ssl ); | 
|  | 3450 | if( ret < 0 ) | 
|  | 3451 | { | 
|  | 3452 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", | 
|  | 3453 | ret ); | 
|  | 3454 | return( ret ); | 
|  | 3455 | } | 
|  | 3456 |  | 
|  | 3457 | remaining = (size_t) ret; | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3458 | if( remaining == 0 ) | 
| Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 3459 | { | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3460 | flush = SSL_FORCE_FLUSH; | 
| Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 3461 | } | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3462 | else | 
|  | 3463 | { | 
| Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 3464 | 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] | 3465 | } | 
|  | 3466 | } | 
|  | 3467 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 3468 |  | 
|  | 3469 | if( ( flush == SSL_FORCE_FLUSH ) && | 
|  | 3470 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3471 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3472 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3473 | return( ret ); | 
|  | 3474 | } | 
|  | 3475 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3476 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3477 |  | 
|  | 3478 | return( 0 ); | 
|  | 3479 | } | 
|  | 3480 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3481 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3482 |  | 
|  | 3483 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) | 
|  | 3484 | { | 
|  | 3485 | if( ssl->in_msglen < ssl->in_hslen || | 
|  | 3486 | memcmp( ssl->in_msg + 6, "\0\0\0",        3 ) != 0 || | 
|  | 3487 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) | 
|  | 3488 | { | 
|  | 3489 | return( 1 ); | 
|  | 3490 | } | 
|  | 3491 | return( 0 ); | 
|  | 3492 | } | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3493 |  | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 3494 | 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] | 3495 | { | 
|  | 3496 | return( ( ssl->in_msg[9] << 16  ) | | 
|  | 3497 | ( ssl->in_msg[10] << 8  ) | | 
|  | 3498 | ssl->in_msg[11] ); | 
|  | 3499 | } | 
|  | 3500 |  | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 3501 | 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] | 3502 | { | 
|  | 3503 | return( ( ssl->in_msg[6] << 16 ) | | 
|  | 3504 | ( ssl->in_msg[7] << 8  ) | | 
|  | 3505 | ssl->in_msg[8] ); | 
|  | 3506 | } | 
|  | 3507 |  | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 3508 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3509 | { | 
|  | 3510 | uint32_t msg_len, frag_off, frag_len; | 
|  | 3511 |  | 
|  | 3512 | msg_len  = ssl_get_hs_total_len( ssl ); | 
|  | 3513 | frag_off = ssl_get_hs_frag_off( ssl ); | 
|  | 3514 | frag_len = ssl_get_hs_frag_len( ssl ); | 
|  | 3515 |  | 
|  | 3516 | if( frag_off > msg_len ) | 
|  | 3517 | return( -1 ); | 
|  | 3518 |  | 
|  | 3519 | if( frag_len > msg_len - frag_off ) | 
|  | 3520 | return( -1 ); | 
|  | 3521 |  | 
|  | 3522 | if( frag_len + 12 > ssl->in_msglen ) | 
|  | 3523 | return( -1 ); | 
|  | 3524 |  | 
|  | 3525 | return( 0 ); | 
|  | 3526 | } | 
|  | 3527 |  | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3528 | /* | 
|  | 3529 | * Mark bits in bitmask (used for DTLS HS reassembly) | 
|  | 3530 | */ | 
|  | 3531 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) | 
|  | 3532 | { | 
|  | 3533 | unsigned int start_bits, end_bits; | 
|  | 3534 |  | 
|  | 3535 | start_bits = 8 - ( offset % 8 ); | 
|  | 3536 | if( start_bits != 8 ) | 
|  | 3537 | { | 
|  | 3538 | size_t first_byte_idx = offset / 8; | 
|  | 3539 |  | 
| Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 3540 | /* Special case */ | 
|  | 3541 | if( len <= start_bits ) | 
|  | 3542 | { | 
|  | 3543 | for( ; len != 0; len-- ) | 
|  | 3544 | mask[first_byte_idx] |= 1 << ( start_bits - len ); | 
|  | 3545 |  | 
|  | 3546 | /* Avoid potential issues with offset or len becoming invalid */ | 
|  | 3547 | return; | 
|  | 3548 | } | 
|  | 3549 |  | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3550 | offset += start_bits; /* Now offset % 8 == 0 */ | 
|  | 3551 | len -= start_bits; | 
|  | 3552 |  | 
|  | 3553 | for( ; start_bits != 0; start_bits-- ) | 
|  | 3554 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); | 
|  | 3555 | } | 
|  | 3556 |  | 
|  | 3557 | end_bits = len % 8; | 
|  | 3558 | if( end_bits != 0 ) | 
|  | 3559 | { | 
|  | 3560 | size_t last_byte_idx = ( offset + len ) / 8; | 
|  | 3561 |  | 
|  | 3562 | len -= end_bits; /* Now len % 8 == 0 */ | 
|  | 3563 |  | 
|  | 3564 | for( ; end_bits != 0; end_bits-- ) | 
|  | 3565 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); | 
|  | 3566 | } | 
|  | 3567 |  | 
|  | 3568 | memset( mask + offset / 8, 0xFF, len / 8 ); | 
|  | 3569 | } | 
|  | 3570 |  | 
|  | 3571 | /* | 
|  | 3572 | * Check that bitmask is full | 
|  | 3573 | */ | 
|  | 3574 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) | 
|  | 3575 | { | 
|  | 3576 | size_t i; | 
|  | 3577 |  | 
|  | 3578 | for( i = 0; i < len / 8; i++ ) | 
|  | 3579 | if( mask[i] != 0xFF ) | 
|  | 3580 | return( -1 ); | 
|  | 3581 |  | 
|  | 3582 | for( i = 0; i < len % 8; i++ ) | 
|  | 3583 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) | 
|  | 3584 | return( -1 ); | 
|  | 3585 |  | 
|  | 3586 | return( 0 ); | 
|  | 3587 | } | 
|  | 3588 |  | 
| Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3589 | /* msg_len does not include the handshake header */ | 
| Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 3590 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 3591 | unsigned add_bitmap ) | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3592 | { | 
| Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3593 | size_t alloc_len; | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3594 |  | 
| Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3595 | alloc_len  = 12;                                 /* Handshake header */ | 
|  | 3596 | alloc_len += msg_len;                            /* Content buffer   */ | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3597 |  | 
| Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 3598 | if( add_bitmap ) | 
|  | 3599 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap       */ | 
| Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3600 |  | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 3601 | return( alloc_len ); | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3602 | } | 
| Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3603 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3604 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3605 |  | 
| Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 3606 | 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] | 3607 | { | 
|  | 3608 | return( ( ssl->in_msg[1] << 16 ) | | 
|  | 3609 | ( ssl->in_msg[2] << 8  ) | | 
|  | 3610 | ssl->in_msg[3] ); | 
|  | 3611 | } | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3612 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3613 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3614 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3615 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3616 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3617 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", | 
| Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3618 | ssl->in_msglen ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3619 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3620 | } | 
|  | 3621 |  | 
| Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 3622 | 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] | 3623 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3624 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3625 | " %d, type = %d, hslen = %d", | 
| Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3626 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3627 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3628 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3629 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3630 | { | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3631 | int ret; | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3632 | 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] | 3633 |  | 
| Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3634 | if( ssl_check_hs_header( ssl ) != 0 ) | 
|  | 3635 | { | 
|  | 3636 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); | 
|  | 3637 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 3638 | } | 
|  | 3639 |  | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3640 | if( ssl->handshake != NULL && | 
| Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 3641 | ( ( ssl->state   != MBEDTLS_SSL_HANDSHAKE_OVER && | 
|  | 3642 | recv_msg_seq != ssl->handshake->in_msg_seq ) || | 
|  | 3643 | ( ssl->state  == MBEDTLS_SSL_HANDSHAKE_OVER && | 
|  | 3644 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3645 | { | 
| Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 3646 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) | 
|  | 3647 | { | 
|  | 3648 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", | 
|  | 3649 | recv_msg_seq, | 
|  | 3650 | ssl->handshake->in_msg_seq ) ); | 
|  | 3651 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); | 
|  | 3652 | } | 
|  | 3653 |  | 
| Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 3654 | /* Retransmit only on last message from previous flight, to avoid | 
|  | 3655 | * too many retransmissions. | 
|  | 3656 | * Besides, No sane server ever retransmits HelloVerifyRequest */ | 
|  | 3657 | 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] | 3658 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3659 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3660 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3661 | "message_seq = %d, start_of_flight = %d", | 
|  | 3662 | recv_msg_seq, | 
|  | 3663 | ssl->handshake->in_flight_start_seq ) ); | 
|  | 3664 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3665 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3666 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3667 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3668 | return( ret ); | 
|  | 3669 | } | 
|  | 3670 | } | 
|  | 3671 | else | 
|  | 3672 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3673 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " | 
| Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3674 | "message_seq = %d, expected = %d", | 
|  | 3675 | recv_msg_seq, | 
|  | 3676 | ssl->handshake->in_msg_seq ) ); | 
|  | 3677 | } | 
|  | 3678 |  | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 3679 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3680 | } | 
|  | 3681 | /* Wait until message completion to increment in_msg_seq */ | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3682 |  | 
| Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 3683 | /* Message reassembly is handled alongside buffering of future | 
|  | 3684 | * messages; the commonality is that both handshake fragments and | 
| Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 3685 | * future messages cannot be forwarded immediately to the | 
| Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 3686 | * handshake logic layer. */ | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3687 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3688 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3689 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); | 
| Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 3690 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3691 | } | 
|  | 3692 | } | 
|  | 3693 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3694 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3695 | /* With TLS we don't handle fragmentation (for now) */ | 
|  | 3696 | if( ssl->in_msglen < ssl->in_hslen ) | 
|  | 3697 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3698 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); | 
|  | 3699 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3700 | } | 
|  | 3701 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3702 | return( 0 ); | 
|  | 3703 | } | 
|  | 3704 |  | 
|  | 3705 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) | 
|  | 3706 | { | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3707 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3708 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3709 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) | 
| Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 3710 | { | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3711 | 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] | 3712 | } | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3713 |  | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3714 | /* Handshake message is complete, increment counter */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3715 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3716 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3717 | ssl->handshake != NULL ) | 
|  | 3718 | { | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3719 | unsigned offset; | 
|  | 3720 | mbedtls_ssl_hs_buffer *hs_buf; | 
| Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3721 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3722 | /* Increment handshake sequence number */ | 
|  | 3723 | hs->in_msg_seq++; | 
|  | 3724 |  | 
|  | 3725 | /* | 
|  | 3726 | * Clear up handshake buffering and reassembly structure. | 
|  | 3727 | */ | 
|  | 3728 |  | 
|  | 3729 | /* Free first entry */ | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 3730 | ssl_buffering_free_slot( ssl, 0 ); | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3731 |  | 
|  | 3732 | /* Shift all other entries */ | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 3733 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; | 
|  | 3734 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3735 | offset++, hs_buf++ ) | 
|  | 3736 | { | 
|  | 3737 | *hs_buf = *(hs_buf + 1); | 
|  | 3738 | } | 
|  | 3739 |  | 
|  | 3740 | /* Create a fresh last entry */ | 
|  | 3741 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3742 | } | 
|  | 3743 | #endif | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3744 | } | 
|  | 3745 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3746 | /* | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3747 | * DTLS anti-replay: RFC 6347 4.1.2.6 | 
|  | 3748 | * | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3749 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). | 
|  | 3750 | * Bit n is set iff record number in_window_top - n has been seen. | 
|  | 3751 | * | 
|  | 3752 | * Usually, in_window_top is the last record number seen and the lsb of | 
|  | 3753 | * in_window is set. The only exception is the initial state (record number 0 | 
|  | 3754 | * not seen yet). | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3755 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3756 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 3757 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3758 | { | 
|  | 3759 | ssl->in_window_top = 0; | 
|  | 3760 | ssl->in_window = 0; | 
|  | 3761 | } | 
|  | 3762 |  | 
|  | 3763 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) | 
|  | 3764 | { | 
|  | 3765 | return( ( (uint64_t) buf[0] << 40 ) | | 
|  | 3766 | ( (uint64_t) buf[1] << 32 ) | | 
|  | 3767 | ( (uint64_t) buf[2] << 24 ) | | 
|  | 3768 | ( (uint64_t) buf[3] << 16 ) | | 
|  | 3769 | ( (uint64_t) buf[4] <<  8 ) | | 
|  | 3770 | ( (uint64_t) buf[5]       ) ); | 
|  | 3771 | } | 
|  | 3772 |  | 
|  | 3773 | /* | 
|  | 3774 | * Return 0 if sequence number is acceptable, -1 otherwise | 
|  | 3775 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3776 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3777 | { | 
|  | 3778 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); | 
|  | 3779 | uint64_t bit; | 
|  | 3780 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3781 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3782 | return( 0 ); | 
|  | 3783 |  | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3784 | if( rec_seqnum > ssl->in_window_top ) | 
|  | 3785 | return( 0 ); | 
|  | 3786 |  | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3787 | bit = ssl->in_window_top - rec_seqnum; | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3788 |  | 
|  | 3789 | if( bit >= 64 ) | 
|  | 3790 | return( -1 ); | 
|  | 3791 |  | 
|  | 3792 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) | 
|  | 3793 | return( -1 ); | 
|  | 3794 |  | 
|  | 3795 | return( 0 ); | 
|  | 3796 | } | 
|  | 3797 |  | 
|  | 3798 | /* | 
|  | 3799 | * Update replay window on new validated record | 
|  | 3800 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3801 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3802 | { | 
|  | 3803 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); | 
|  | 3804 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3805 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3806 | return; | 
|  | 3807 |  | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3808 | if( rec_seqnum > ssl->in_window_top ) | 
|  | 3809 | { | 
|  | 3810 | /* Update window_top and the contents of the window */ | 
|  | 3811 | uint64_t shift = rec_seqnum - ssl->in_window_top; | 
|  | 3812 |  | 
|  | 3813 | if( shift >= 64 ) | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3814 | ssl->in_window = 1; | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3815 | else | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3816 | { | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3817 | ssl->in_window <<= shift; | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3818 | ssl->in_window |= 1; | 
|  | 3819 | } | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3820 |  | 
|  | 3821 | ssl->in_window_top = rec_seqnum; | 
|  | 3822 | } | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3823 | else | 
|  | 3824 | { | 
|  | 3825 | /* Mark that number as seen in the current window */ | 
| Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3826 | uint64_t bit = ssl->in_window_top - rec_seqnum; | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3827 |  | 
|  | 3828 | if( bit < 64 ) /* Always true, but be extra sure */ | 
|  | 3829 | ssl->in_window |= (uint64_t) 1 << bit; | 
|  | 3830 | } | 
|  | 3831 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3832 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3833 |  | 
| Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3834 | #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] | 3835 | /* Forward declaration */ | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3836 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); | 
|  | 3837 |  | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3838 | /* | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3839 | * Without any SSL context, check if a datagram looks like a ClientHello with | 
|  | 3840 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. | 
| Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 3841 | * Both input and output include full DTLS headers. | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3842 | * | 
|  | 3843 | * - if cookie is valid, return 0 | 
|  | 3844 | * - if ClientHello looks superficially valid but cookie is not, | 
|  | 3845 | *   fill obuf and set olen, then | 
|  | 3846 | *   return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED | 
|  | 3847 | * - otherwise return a specific error code | 
|  | 3848 | */ | 
|  | 3849 | static int ssl_check_dtls_clihlo_cookie( | 
|  | 3850 | mbedtls_ssl_cookie_write_t *f_cookie_write, | 
|  | 3851 | mbedtls_ssl_cookie_check_t *f_cookie_check, | 
|  | 3852 | void *p_cookie, | 
|  | 3853 | const unsigned char *cli_id, size_t cli_id_len, | 
|  | 3854 | const unsigned char *in, size_t in_len, | 
|  | 3855 | unsigned char *obuf, size_t buf_len, size_t *olen ) | 
|  | 3856 | { | 
|  | 3857 | size_t sid_len, cookie_len; | 
|  | 3858 | unsigned char *p; | 
|  | 3859 |  | 
|  | 3860 | if( f_cookie_write == NULL || f_cookie_check == NULL ) | 
|  | 3861 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 3862 |  | 
|  | 3863 | /* | 
|  | 3864 | * Structure of ClientHello with record and handshake headers, | 
|  | 3865 | * and expected values. We don't need to check a lot, more checks will be | 
|  | 3866 | * done when actually parsing the ClientHello - skipping those checks | 
|  | 3867 | * avoids code duplication and does not make cookie forging any easier. | 
|  | 3868 | * | 
|  | 3869 | *  0-0  ContentType type;                  copied, must be handshake | 
|  | 3870 | *  1-2  ProtocolVersion version;           copied | 
|  | 3871 | *  3-4  uint16 epoch;                      copied, must be 0 | 
|  | 3872 | *  5-10 uint48 sequence_number;            copied | 
|  | 3873 | * 11-12 uint16 length;                     (ignored) | 
|  | 3874 | * | 
|  | 3875 | * 13-13 HandshakeType msg_type;            (ignored) | 
|  | 3876 | * 14-16 uint24 length;                     (ignored) | 
|  | 3877 | * 17-18 uint16 message_seq;                copied | 
|  | 3878 | * 19-21 uint24 fragment_offset;            copied, must be 0 | 
|  | 3879 | * 22-24 uint24 fragment_length;            (ignored) | 
|  | 3880 | * | 
|  | 3881 | * 25-26 ProtocolVersion client_version;    (ignored) | 
|  | 3882 | * 27-58 Random random;                     (ignored) | 
|  | 3883 | * 59-xx SessionID session_id;              1 byte len + sid_len content | 
|  | 3884 | * 60+   opaque cookie<0..2^8-1>;           1 byte len + content | 
|  | 3885 | *       ... | 
|  | 3886 | * | 
|  | 3887 | * Minimum length is 61 bytes. | 
|  | 3888 | */ | 
|  | 3889 | if( in_len < 61 || | 
|  | 3890 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || | 
|  | 3891 | in[3] != 0 || in[4] != 0 || | 
|  | 3892 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) | 
|  | 3893 | { | 
|  | 3894 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 3895 | } | 
|  | 3896 |  | 
|  | 3897 | sid_len = in[59]; | 
|  | 3898 | if( sid_len > in_len - 61 ) | 
|  | 3899 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 3900 |  | 
|  | 3901 | cookie_len = in[60 + sid_len]; | 
|  | 3902 | if( cookie_len > in_len - 60 ) | 
|  | 3903 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); | 
|  | 3904 |  | 
|  | 3905 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, | 
|  | 3906 | cli_id, cli_id_len ) == 0 ) | 
|  | 3907 | { | 
|  | 3908 | /* Valid cookie */ | 
|  | 3909 | return( 0 ); | 
|  | 3910 | } | 
|  | 3911 |  | 
|  | 3912 | /* | 
|  | 3913 | * If we get here, we've got an invalid cookie, let's prepare HVR. | 
|  | 3914 | * | 
|  | 3915 | *  0-0  ContentType type;                  copied | 
|  | 3916 | *  1-2  ProtocolVersion version;           copied | 
|  | 3917 | *  3-4  uint16 epoch;                      copied | 
|  | 3918 | *  5-10 uint48 sequence_number;            copied | 
|  | 3919 | * 11-12 uint16 length;                     olen - 13 | 
|  | 3920 | * | 
|  | 3921 | * 13-13 HandshakeType msg_type;            hello_verify_request | 
|  | 3922 | * 14-16 uint24 length;                     olen - 25 | 
|  | 3923 | * 17-18 uint16 message_seq;                copied | 
|  | 3924 | * 19-21 uint24 fragment_offset;            copied | 
|  | 3925 | * 22-24 uint24 fragment_length;            olen - 25 | 
|  | 3926 | * | 
|  | 3927 | * 25-26 ProtocolVersion server_version;    0xfe 0xff | 
|  | 3928 | * 27-27 opaque cookie<0..2^8-1>;           cookie_len = olen - 27, cookie | 
|  | 3929 | * | 
|  | 3930 | * Minimum length is 28. | 
|  | 3931 | */ | 
|  | 3932 | if( buf_len < 28 ) | 
|  | 3933 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | 
|  | 3934 |  | 
|  | 3935 | /* Copy most fields and adapt others */ | 
|  | 3936 | memcpy( obuf, in, 25 ); | 
|  | 3937 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; | 
|  | 3938 | obuf[25] = 0xfe; | 
|  | 3939 | obuf[26] = 0xff; | 
|  | 3940 |  | 
|  | 3941 | /* Generate and write actual cookie */ | 
|  | 3942 | p = obuf + 28; | 
|  | 3943 | if( f_cookie_write( p_cookie, | 
|  | 3944 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) | 
|  | 3945 | { | 
|  | 3946 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 3947 | } | 
|  | 3948 |  | 
|  | 3949 | *olen = p - obuf; | 
|  | 3950 |  | 
|  | 3951 | /* Go back and fill length fields */ | 
|  | 3952 | obuf[27] = (unsigned char)( *olen - 28 ); | 
|  | 3953 |  | 
|  | 3954 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); | 
|  | 3955 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >>  8 ); | 
|  | 3956 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 )       ); | 
|  | 3957 |  | 
|  | 3958 | obuf[11] = (unsigned char)( ( *olen - 13 ) >>  8 ); | 
|  | 3959 | obuf[12] = (unsigned char)( ( *olen - 13 )       ); | 
|  | 3960 |  | 
|  | 3961 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); | 
|  | 3962 | } | 
|  | 3963 |  | 
|  | 3964 | /* | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3965 | * Handle possible client reconnect with the same UDP quadruplet | 
|  | 3966 | * (RFC 6347 Section 4.2.8). | 
|  | 3967 | * | 
|  | 3968 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record | 
|  | 3969 | * that looks like a ClientHello. | 
|  | 3970 | * | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3971 | * - if the input looks like a ClientHello without cookies, | 
|  | 3972 | *   send back HelloVerifyRequest, then | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3973 | *   return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3974 | * - if the input looks like a ClientHello with a valid cookie, | 
|  | 3975 | *   reset the session of the current context, and | 
| Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 3976 | *   return MBEDTLS_ERR_SSL_CLIENT_RECONNECT | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3977 | * - if anything goes wrong, return a specific error code | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3978 | * | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3979 | * mbedtls_ssl_read_record() will ignore the record if anything else than | 
| Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 3980 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function | 
|  | 3981 | * cannot not return 0. | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3982 | */ | 
|  | 3983 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) | 
|  | 3984 | { | 
|  | 3985 | int ret; | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3986 | size_t len; | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3987 |  | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3988 | ret = ssl_check_dtls_clihlo_cookie( | 
|  | 3989 | ssl->conf->f_cookie_write, | 
|  | 3990 | ssl->conf->f_cookie_check, | 
|  | 3991 | ssl->conf->p_cookie, | 
|  | 3992 | ssl->cli_id, ssl->cli_id_len, | 
|  | 3993 | ssl->in_buf, ssl->in_left, | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3994 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3995 |  | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3996 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); | 
|  | 3997 |  | 
|  | 3998 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3999 | { | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4000 | /* 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] | 4001 | * If the error is permanent we'll catch it later, | 
|  | 4002 | * if it's not, then hopefully it'll work next time. */ | 
|  | 4003 | (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len ); | 
|  | 4004 |  | 
|  | 4005 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4006 | } | 
|  | 4007 |  | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4008 | if( ret == 0 ) | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4009 | { | 
| Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4010 | /* Got a valid cookie, partially reset context */ | 
|  | 4011 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) | 
|  | 4012 | { | 
|  | 4013 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); | 
|  | 4014 | return( ret ); | 
|  | 4015 | } | 
|  | 4016 |  | 
|  | 4017 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4018 | } | 
|  | 4019 |  | 
| Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4020 | return( ret ); | 
|  | 4021 | } | 
| Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4022 | #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] | 4023 |  | 
| Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4024 | /* | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4025 | * ContentType type; | 
|  | 4026 | * ProtocolVersion version; | 
|  | 4027 | * uint16 epoch;            // DTLS only | 
|  | 4028 | * uint48 sequence_number;  // DTLS only | 
|  | 4029 | * uint16 length; | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4030 | * | 
|  | 4031 | * 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] | 4032 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4033 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. | 
|  | 4034 | * | 
|  | 4035 | * With DTLS, mbedtls_ssl_read_record() will: | 
| Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 4036 | * 1. proceed with the record if this function returns 0 | 
|  | 4037 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD | 
|  | 4038 | * 3. return CLIENT_RECONNECT if this function return that value | 
|  | 4039 | * 4. drop the whole datagram if this function returns anything else. | 
|  | 4040 | * Point 2 is needed when the peer is resending, and we have already received | 
|  | 4041 | * 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] | 4042 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4043 | static int ssl_parse_record_header( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4044 | { | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4045 | int major_ver, minor_ver; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4046 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4047 | 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] | 4048 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4049 | ssl->in_msgtype =  ssl->in_hdr[0]; | 
| Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4050 | 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] | 4051 | 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] | 4052 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4053 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4054 | "version = [%d:%d], msglen = %d", | 
| Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4055 | ssl->in_msgtype, | 
|  | 4056 | major_ver, minor_ver, ssl->in_msglen ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4057 |  | 
| Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4058 | /* Check record type */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4059 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 4060 | ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT && | 
|  | 4061 | ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && | 
|  | 4062 | ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) | 
| Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4063 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4064 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); | 
| Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4065 |  | 
|  | 4066 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Andres Amaya Garcia | 0169253 | 2017-06-28 09:26:46 +0100 | [diff] [blame] | 4067 | /* Silently ignore invalid DTLS records as recommended by RFC 6347 | 
|  | 4068 | * Section 4.1.2.7 */ | 
| Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4069 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 4070 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4071 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 4072 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 4073 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4074 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4075 | } | 
|  | 4076 |  | 
|  | 4077 | /* Check version */ | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4078 | if( major_ver != ssl->major_ver ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4079 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4080 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); | 
|  | 4081 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4082 | } | 
|  | 4083 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4084 | if( minor_ver > ssl->conf->max_minor_ver ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4085 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4086 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); | 
|  | 4087 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4088 | } | 
|  | 4089 |  | 
| Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4090 | /* Check length against the size of our buffer */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4091 | if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN | 
| Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4092 | - (size_t)( ssl->in_msg - ssl->in_buf ) ) | 
| Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4093 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4094 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); | 
|  | 4095 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4096 | } | 
|  | 4097 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4098 | /* | 
| Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4099 | * DTLS-related tests. | 
|  | 4100 | * Check epoch before checking length constraint because | 
|  | 4101 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec | 
|  | 4102 | * message gets duplicated before the corresponding Finished message, | 
|  | 4103 | * the second ChangeCipherSpec should be discarded because it belongs | 
|  | 4104 | * to an old epoch, but not because its length is shorter than | 
|  | 4105 | * the minimum record length for packets using the new record transform. | 
|  | 4106 | * Note that these two kinds of failures are handled differently, | 
|  | 4107 | * as an unexpected record is silently skipped but an invalid | 
|  | 4108 | * record leads to the entire datagram being dropped. | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4109 | */ | 
|  | 4110 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4111 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 4112 | { | 
|  | 4113 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; | 
|  | 4114 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4115 | /* Check epoch (and sequence number) with DTLS */ | 
|  | 4116 | if( rec_epoch != ssl->in_epoch ) | 
|  | 4117 | { | 
|  | 4118 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " | 
|  | 4119 | "expected %d, received %d", | 
|  | 4120 | ssl->in_epoch, rec_epoch ) ); | 
|  | 4121 |  | 
|  | 4122 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) | 
|  | 4123 | /* | 
|  | 4124 | * Check for an epoch 0 ClientHello. We can't use in_msg here to | 
|  | 4125 | * access the first byte of record content (handshake type), as we | 
|  | 4126 | * have an active transform (possibly iv_len != 0), so use the | 
|  | 4127 | * fact that the record header len is 13 instead. | 
|  | 4128 | */ | 
|  | 4129 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && | 
|  | 4130 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && | 
|  | 4131 | rec_epoch == 0 && | 
|  | 4132 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
|  | 4133 | ssl->in_left > 13 && | 
|  | 4134 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) | 
|  | 4135 | { | 
|  | 4136 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " | 
|  | 4137 | "from the same port" ) ); | 
|  | 4138 | return( ssl_handle_possible_reconnect( ssl ) ); | 
|  | 4139 | } | 
|  | 4140 | else | 
|  | 4141 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4142 | { | 
|  | 4143 | /* Consider buffering the record. */ | 
|  | 4144 | if( rec_epoch == (unsigned int) ssl->in_epoch + 1 ) | 
|  | 4145 | { | 
|  | 4146 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); | 
|  | 4147 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); | 
|  | 4148 | } | 
|  | 4149 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4150 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4151 | } | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4152 | } | 
|  | 4153 |  | 
|  | 4154 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 4155 | /* Replay detection only works for the current epoch */ | 
|  | 4156 | if( rec_epoch == ssl->in_epoch && | 
|  | 4157 | mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) | 
|  | 4158 | { | 
|  | 4159 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); | 
|  | 4160 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); | 
|  | 4161 | } | 
|  | 4162 | #endif | 
| Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4163 |  | 
| Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4164 | /* Drop unexpected ApplicationData records, | 
|  | 4165 | * except at the beginning of renegotiations */ | 
|  | 4166 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && | 
|  | 4167 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER | 
|  | 4168 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 4169 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && | 
|  | 4170 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) | 
|  | 4171 | #endif | 
|  | 4172 | ) | 
|  | 4173 | { | 
|  | 4174 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); | 
|  | 4175 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); | 
|  | 4176 | } | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4177 | } | 
|  | 4178 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4179 |  | 
| Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4180 |  | 
|  | 4181 | /* Check length against bounds of the current transform and version */ | 
|  | 4182 | if( ssl->transform_in == NULL ) | 
|  | 4183 | { | 
|  | 4184 | if( ssl->in_msglen < 1 || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4185 | ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
| Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4186 | { | 
|  | 4187 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); | 
|  | 4188 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 4189 | } | 
|  | 4190 | } | 
|  | 4191 | else | 
|  | 4192 | { | 
|  | 4193 | if( ssl->in_msglen < ssl->transform_in->minlen ) | 
|  | 4194 | { | 
|  | 4195 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); | 
|  | 4196 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 4197 | } | 
|  | 4198 |  | 
|  | 4199 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 4200 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4201 | ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN ) | 
| Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4202 | { | 
|  | 4203 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); | 
|  | 4204 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 4205 | } | 
|  | 4206 | #endif | 
|  | 4207 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 4208 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 4209 | /* | 
|  | 4210 | * TLS encrypted messages can have up to 256 bytes of padding | 
|  | 4211 | */ | 
|  | 4212 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 && | 
|  | 4213 | ssl->in_msglen > ssl->transform_in->minlen + | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4214 | MBEDTLS_SSL_IN_CONTENT_LEN + 256 ) | 
| Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4215 | { | 
|  | 4216 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); | 
|  | 4217 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 4218 | } | 
|  | 4219 | #endif | 
|  | 4220 | } | 
|  | 4221 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4222 | return( 0 ); | 
|  | 4223 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4224 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4225 | /* | 
|  | 4226 | * If applicable, decrypt (and decompress) record content | 
|  | 4227 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4228 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4229 | { | 
|  | 4230 | int ret, done = 0; | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 4231 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4232 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", | 
|  | 4233 | ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4234 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4235 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 4236 | if( mbedtls_ssl_hw_record_read != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4237 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4238 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4239 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4240 | ret = mbedtls_ssl_hw_record_read( ssl ); | 
|  | 4241 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4242 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4243 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); | 
|  | 4244 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4245 | } | 
| Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4246 |  | 
|  | 4247 | if( ret == 0 ) | 
|  | 4248 | done = 1; | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4249 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4250 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4251 | if( !done && ssl->transform_in != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4252 | { | 
|  | 4253 | if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 ) | 
|  | 4254 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4255 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4256 | return( ret ); | 
|  | 4257 | } | 
|  | 4258 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4259 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4260 | ssl->in_msg, ssl->in_msglen ); | 
|  | 4261 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4262 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4263 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4264 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); | 
|  | 4265 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4266 | } | 
|  | 4267 | } | 
|  | 4268 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4269 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4270 | if( ssl->transform_in != NULL && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4271 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4272 | { | 
|  | 4273 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) | 
|  | 4274 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4275 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4276 | return( ret ); | 
|  | 4277 | } | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4278 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4279 | #endif /* MBEDTLS_ZLIB_SUPPORT */ | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4280 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4281 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4282 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 4283 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4284 | mbedtls_ssl_dtls_replay_update( ssl ); | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 4285 | } | 
|  | 4286 | #endif | 
|  | 4287 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4288 | return( 0 ); | 
|  | 4289 | } | 
|  | 4290 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4291 | 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] | 4292 |  | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4293 | /* | 
|  | 4294 | * Read a record. | 
|  | 4295 | * | 
| Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4296 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, | 
|  | 4297 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. | 
|  | 4298 | * | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4299 | */ | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4300 |  | 
|  | 4301 | /* Helper functions for mbedtls_ssl_read_record(). */ | 
|  | 4302 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4303 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); | 
|  | 4304 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); | 
| Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 4305 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4306 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, | 
| Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 4307 | unsigned update_hs_digest ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4308 | { | 
|  | 4309 | int ret; | 
|  | 4310 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4311 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4312 |  | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4313 | if( ssl->keep_current_message == 0 ) | 
|  | 4314 | { | 
|  | 4315 | do { | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4316 |  | 
| Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 4317 | ret = ssl_consume_current_message( ssl ); | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4318 | if( ret != 0 ) | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4319 | return( ret ); | 
| Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 4320 |  | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4321 | if( ssl_record_is_in_progress( ssl ) == 0 ) | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4322 | { | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4323 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4324 | int have_buffered = 0; | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4325 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4326 | /* We only check for buffered messages if the | 
|  | 4327 | * current datagram is fully consumed. */ | 
|  | 4328 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 4329 | ssl_next_record_is_in_datagram( ssl ) == 0 ) | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4330 | { | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4331 | if( ssl_load_buffered_message( ssl ) == 0 ) | 
|  | 4332 | have_buffered = 1; | 
|  | 4333 | } | 
|  | 4334 |  | 
|  | 4335 | if( have_buffered == 0 ) | 
|  | 4336 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4337 | { | 
|  | 4338 | ret = ssl_get_next_record( ssl ); | 
|  | 4339 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) | 
|  | 4340 | continue; | 
|  | 4341 |  | 
|  | 4342 | if( ret != 0 ) | 
|  | 4343 | { | 
| Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 4344 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4345 | return( ret ); | 
|  | 4346 | } | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4347 | } | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4348 | } | 
|  | 4349 |  | 
|  | 4350 | ret = mbedtls_ssl_handle_message_type( ssl ); | 
|  | 4351 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4352 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4353 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) | 
|  | 4354 | { | 
|  | 4355 | /* Buffer future message */ | 
|  | 4356 | ret = ssl_buffer_message( ssl ); | 
|  | 4357 | if( ret != 0 ) | 
|  | 4358 | return( ret ); | 
|  | 4359 |  | 
|  | 4360 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; | 
|  | 4361 | } | 
|  | 4362 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4363 |  | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4364 | } while( MBEDTLS_ERR_SSL_NON_FATAL           == ret  || | 
|  | 4365 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4366 |  | 
|  | 4367 | if( 0 != ret ) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4368 | { | 
| Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 4369 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4370 | return( ret ); | 
|  | 4371 | } | 
|  | 4372 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4373 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && | 
| Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 4374 | update_hs_digest == 1 ) | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4375 | { | 
|  | 4376 | mbedtls_ssl_update_handshake_status( ssl ); | 
|  | 4377 | } | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4378 | } | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4379 | else | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4380 | { | 
| Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 4381 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4382 | ssl->keep_current_message = 0; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4383 | } | 
|  | 4384 |  | 
|  | 4385 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); | 
|  | 4386 |  | 
|  | 4387 | return( 0 ); | 
|  | 4388 | } | 
|  | 4389 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4390 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 4391 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4392 | { | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4393 | if( ssl->in_left > ssl->next_record_offset ) | 
|  | 4394 | return( 1 ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4395 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4396 | return( 0 ); | 
|  | 4397 | } | 
|  | 4398 |  | 
|  | 4399 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) | 
|  | 4400 | { | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4401 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4402 | mbedtls_ssl_hs_buffer * hs_buf; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4403 | int ret = 0; | 
|  | 4404 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4405 | if( hs == NULL ) | 
|  | 4406 | return( -1 ); | 
|  | 4407 |  | 
| Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 4408 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); | 
|  | 4409 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4410 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || | 
|  | 4411 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) | 
|  | 4412 | { | 
|  | 4413 | /* Check if we have seen a ChangeCipherSpec before. | 
|  | 4414 | * If yes, synthesize a CCS record. */ | 
| Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 4415 | if( !hs->buffering.seen_ccs ) | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4416 | { | 
|  | 4417 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); | 
|  | 4418 | ret = -1; | 
| Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 4419 | goto exit; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4420 | } | 
|  | 4421 |  | 
| Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 4422 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4423 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; | 
|  | 4424 | ssl->in_msglen = 1; | 
|  | 4425 | ssl->in_msg[0] = 1; | 
|  | 4426 |  | 
|  | 4427 | /* As long as they are equal, the exact value doesn't matter. */ | 
|  | 4428 | ssl->in_left            = 0; | 
|  | 4429 | ssl->next_record_offset = 0; | 
|  | 4430 |  | 
| Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4431 | hs->buffering.seen_ccs = 0; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4432 | goto exit; | 
|  | 4433 | } | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4434 |  | 
| Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 4435 | #if defined(MBEDTLS_DEBUG_C) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4436 | /* Debug only */ | 
|  | 4437 | { | 
|  | 4438 | unsigned offset; | 
|  | 4439 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) | 
|  | 4440 | { | 
|  | 4441 | hs_buf = &hs->buffering.hs[offset]; | 
|  | 4442 | if( hs_buf->is_valid == 1 ) | 
|  | 4443 | { | 
|  | 4444 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", | 
|  | 4445 | hs->in_msg_seq + offset, | 
| Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 4446 | hs_buf->is_complete ? "fully" : "partially" ) ); | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4447 | } | 
|  | 4448 | } | 
|  | 4449 | } | 
| Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 4450 | #endif /* MBEDTLS_DEBUG_C */ | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4451 |  | 
|  | 4452 | /* Check if we have buffered and/or fully reassembled the | 
|  | 4453 | * next handshake message. */ | 
|  | 4454 | hs_buf = &hs->buffering.hs[0]; | 
|  | 4455 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) | 
|  | 4456 | { | 
|  | 4457 | /* Synthesize a record containing the buffered HS message. */ | 
|  | 4458 | size_t msg_len = ( hs_buf->data[1] << 16 ) | | 
|  | 4459 | ( hs_buf->data[2] << 8  ) | | 
|  | 4460 | hs_buf->data[3]; | 
|  | 4461 |  | 
|  | 4462 | /* Double-check that we haven't accidentally buffered | 
|  | 4463 | * a message that doesn't fit into the input buffer. */ | 
|  | 4464 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
|  | 4465 | { | 
|  | 4466 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4467 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4468 | } | 
|  | 4469 |  | 
|  | 4470 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); | 
|  | 4471 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", | 
|  | 4472 | hs_buf->data, msg_len + 12 ); | 
|  | 4473 |  | 
|  | 4474 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 4475 | ssl->in_hslen   = msg_len + 12; | 
|  | 4476 | ssl->in_msglen  = msg_len + 12; | 
|  | 4477 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); | 
|  | 4478 |  | 
|  | 4479 | ret = 0; | 
|  | 4480 | goto exit; | 
|  | 4481 | } | 
|  | 4482 | else | 
|  | 4483 | { | 
|  | 4484 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", | 
|  | 4485 | hs->in_msg_seq ) ); | 
|  | 4486 | } | 
|  | 4487 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4488 | ret = -1; | 
|  | 4489 |  | 
|  | 4490 | exit: | 
|  | 4491 |  | 
|  | 4492 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); | 
|  | 4493 | return( ret ); | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4494 | } | 
|  | 4495 |  | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4496 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, | 
|  | 4497 | size_t desired ) | 
|  | 4498 | { | 
|  | 4499 | int offset; | 
|  | 4500 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
| Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 4501 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", | 
|  | 4502 | (unsigned) desired ) ); | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4503 |  | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4504 | /* Get rid of future records epoch first, if such exist. */ | 
|  | 4505 | ssl_free_buffered_record( ssl ); | 
|  | 4506 |  | 
|  | 4507 | /* Check if we have enough space available now. */ | 
|  | 4508 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - | 
|  | 4509 | hs->buffering.total_bytes_buffered ) ) | 
|  | 4510 | { | 
| Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 4511 | 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] | 4512 | return( 0 ); | 
|  | 4513 | } | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4514 |  | 
| Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 4515 | /* We don't have enough space to buffer the next expected handshake | 
|  | 4516 | * message. Remove buffers used for future messages to gain space, | 
|  | 4517 | * starting with the most distant one. */ | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4518 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; | 
|  | 4519 | offset >= 0; offset-- ) | 
|  | 4520 | { | 
|  | 4521 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", | 
|  | 4522 | offset ) ); | 
|  | 4523 |  | 
| Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 4524 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4525 |  | 
|  | 4526 | /* Check if we have enough space available now. */ | 
|  | 4527 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - | 
|  | 4528 | hs->buffering.total_bytes_buffered ) ) | 
|  | 4529 | { | 
| Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 4530 | 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] | 4531 | return( 0 ); | 
|  | 4532 | } | 
|  | 4533 | } | 
|  | 4534 |  | 
|  | 4535 | return( -1 ); | 
|  | 4536 | } | 
|  | 4537 |  | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4538 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) | 
|  | 4539 | { | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4540 | int ret = 0; | 
|  | 4541 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 4542 |  | 
|  | 4543 | if( hs == NULL ) | 
|  | 4544 | return( 0 ); | 
|  | 4545 |  | 
|  | 4546 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); | 
|  | 4547 |  | 
|  | 4548 | switch( ssl->in_msgtype ) | 
|  | 4549 | { | 
|  | 4550 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: | 
|  | 4551 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4552 |  | 
| Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4553 | hs->buffering.seen_ccs = 1; | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4554 | break; | 
|  | 4555 |  | 
|  | 4556 | case MBEDTLS_SSL_MSG_HANDSHAKE: | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4557 | { | 
|  | 4558 | unsigned recv_msg_seq_offset; | 
|  | 4559 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; | 
|  | 4560 | mbedtls_ssl_hs_buffer *hs_buf; | 
|  | 4561 | size_t msg_len = ssl->in_hslen - 12; | 
|  | 4562 |  | 
|  | 4563 | /* We should never receive an old handshake | 
|  | 4564 | * message - double-check nonetheless. */ | 
|  | 4565 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) | 
|  | 4566 | { | 
|  | 4567 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4568 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4569 | } | 
|  | 4570 |  | 
|  | 4571 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; | 
|  | 4572 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) | 
|  | 4573 | { | 
|  | 4574 | /* Silently ignore -- message too far in the future */ | 
|  | 4575 | MBEDTLS_SSL_DEBUG_MSG( 2, | 
|  | 4576 | ( "Ignore future HS message with sequence number %u, " | 
|  | 4577 | "buffering window %u - %u", | 
|  | 4578 | recv_msg_seq, ssl->handshake->in_msg_seq, | 
|  | 4579 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); | 
|  | 4580 |  | 
|  | 4581 | goto exit; | 
|  | 4582 | } | 
|  | 4583 |  | 
|  | 4584 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", | 
|  | 4585 | recv_msg_seq, recv_msg_seq_offset ) ); | 
|  | 4586 |  | 
|  | 4587 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; | 
|  | 4588 |  | 
|  | 4589 | /* Check if the buffering for this seq nr has already commenced. */ | 
| Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 4590 | if( !hs_buf->is_valid ) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4591 | { | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4592 | size_t reassembly_buf_sz; | 
|  | 4593 |  | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4594 | hs_buf->is_fragmented = | 
|  | 4595 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); | 
|  | 4596 |  | 
|  | 4597 | /* We copy the message back into the input buffer | 
|  | 4598 | * after reassembly, so check that it's not too large. | 
|  | 4599 | * This is an implementation-specific limitation | 
|  | 4600 | * and not one from the standard, hence it is not | 
|  | 4601 | * checked in ssl_check_hs_header(). */ | 
| Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 4602 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4603 | { | 
|  | 4604 | /* Ignore message */ | 
|  | 4605 | goto exit; | 
|  | 4606 | } | 
|  | 4607 |  | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4608 | /* Check if we have enough space to buffer the message. */ | 
|  | 4609 | if( hs->buffering.total_bytes_buffered > | 
|  | 4610 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) | 
|  | 4611 | { | 
|  | 4612 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4613 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4614 | } | 
|  | 4615 |  | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4616 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, | 
|  | 4617 | hs_buf->is_fragmented ); | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4618 |  | 
|  | 4619 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - | 
|  | 4620 | hs->buffering.total_bytes_buffered ) ) | 
|  | 4621 | { | 
|  | 4622 | if( recv_msg_seq_offset > 0 ) | 
|  | 4623 | { | 
|  | 4624 | /* If we can't buffer a future message because | 
|  | 4625 | * of space limitations -- ignore. */ | 
|  | 4626 | 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", | 
|  | 4627 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, | 
|  | 4628 | (unsigned) hs->buffering.total_bytes_buffered ) ); | 
|  | 4629 | goto exit; | 
|  | 4630 | } | 
| Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 4631 | else | 
|  | 4632 | { | 
|  | 4633 | 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", | 
|  | 4634 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, | 
|  | 4635 | (unsigned) hs->buffering.total_bytes_buffered ) ); | 
|  | 4636 | } | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4637 |  | 
| Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4638 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) | 
| Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 4639 | { | 
| Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 4640 | 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", | 
|  | 4641 | (unsigned) msg_len, | 
|  | 4642 | (unsigned) reassembly_buf_sz, | 
|  | 4643 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4644 | (unsigned) hs->buffering.total_bytes_buffered ) ); | 
| Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 4645 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; | 
|  | 4646 | goto exit; | 
|  | 4647 | } | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4648 | } | 
|  | 4649 |  | 
|  | 4650 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", | 
|  | 4651 | msg_len ) ); | 
|  | 4652 |  | 
| Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4653 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); | 
|  | 4654 | if( hs_buf->data == NULL ) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4655 | { | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4656 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4657 | goto exit; | 
|  | 4658 | } | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4659 | hs_buf->data_len = reassembly_buf_sz; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4660 |  | 
|  | 4661 | /* Prepare final header: copy msg_type, length and message_seq, | 
|  | 4662 | * then add standardised fragment_offset and fragment_length */ | 
|  | 4663 | memcpy( hs_buf->data, ssl->in_msg, 6 ); | 
|  | 4664 | memset( hs_buf->data + 6, 0, 3 ); | 
|  | 4665 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); | 
|  | 4666 |  | 
|  | 4667 | hs_buf->is_valid = 1; | 
| Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4668 |  | 
|  | 4669 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4670 | } | 
|  | 4671 | else | 
|  | 4672 | { | 
|  | 4673 | /* Make sure msg_type and length are consistent */ | 
|  | 4674 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) | 
|  | 4675 | { | 
|  | 4676 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); | 
|  | 4677 | /* Ignore */ | 
|  | 4678 | goto exit; | 
|  | 4679 | } | 
|  | 4680 | } | 
|  | 4681 |  | 
| Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 4682 | if( !hs_buf->is_complete ) | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4683 | { | 
|  | 4684 | size_t frag_len, frag_off; | 
|  | 4685 | unsigned char * const msg = hs_buf->data + 12; | 
|  | 4686 |  | 
|  | 4687 | /* | 
|  | 4688 | * Check and copy current fragment | 
|  | 4689 | */ | 
|  | 4690 |  | 
|  | 4691 | /* Validation of header fields already done in | 
|  | 4692 | * mbedtls_ssl_prepare_handshake_record(). */ | 
|  | 4693 | frag_off = ssl_get_hs_frag_off( ssl ); | 
|  | 4694 | frag_len = ssl_get_hs_frag_len( ssl ); | 
|  | 4695 |  | 
|  | 4696 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", | 
|  | 4697 | frag_off, frag_len ) ); | 
|  | 4698 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); | 
|  | 4699 |  | 
|  | 4700 | if( hs_buf->is_fragmented ) | 
|  | 4701 | { | 
|  | 4702 | unsigned char * const bitmask = msg + msg_len; | 
|  | 4703 | ssl_bitmask_set( bitmask, frag_off, frag_len ); | 
|  | 4704 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, | 
|  | 4705 | msg_len ) == 0 ); | 
|  | 4706 | } | 
|  | 4707 | else | 
|  | 4708 | { | 
|  | 4709 | hs_buf->is_complete = 1; | 
|  | 4710 | } | 
|  | 4711 |  | 
|  | 4712 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", | 
|  | 4713 | hs_buf->is_complete ? "" : "not yet " ) ); | 
|  | 4714 | } | 
|  | 4715 |  | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4716 | break; | 
| Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4717 | } | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4718 |  | 
|  | 4719 | default: | 
| Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 4720 | /* We don't buffer other types of messages. */ | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4721 | break; | 
|  | 4722 | } | 
|  | 4723 |  | 
|  | 4724 | exit: | 
|  | 4725 |  | 
|  | 4726 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); | 
|  | 4727 | return( ret ); | 
| Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4728 | } | 
|  | 4729 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4730 |  | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4731 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4732 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4733 | /* | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4734 | * Consume last content-layer message and potentially | 
|  | 4735 | * update in_msglen which keeps track of the contents' | 
|  | 4736 | * consumption state. | 
|  | 4737 | * | 
|  | 4738 | * (1) Handshake messages: | 
|  | 4739 | *     Remove last handshake message, move content | 
|  | 4740 | *     and adapt in_msglen. | 
|  | 4741 | * | 
|  | 4742 | * (2) Alert messages: | 
|  | 4743 | *     Consume whole record content, in_msglen = 0. | 
|  | 4744 | * | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4745 | * (3) Change cipher spec: | 
|  | 4746 | *     Consume whole record content, in_msglen = 0. | 
|  | 4747 | * | 
|  | 4748 | * (4) Application data: | 
|  | 4749 | *     Don't do anything - the record layer provides | 
|  | 4750 | *     the application data as a stream transport | 
|  | 4751 | *     and consumes through mbedtls_ssl_read only. | 
|  | 4752 | * | 
|  | 4753 | */ | 
|  | 4754 |  | 
|  | 4755 | /* Case (1): Handshake messages */ | 
|  | 4756 | if( ssl->in_hslen != 0 ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4757 | { | 
| Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4758 | /* Hard assertion to be sure that no application data | 
|  | 4759 | * is in flight, as corrupting ssl->in_msglen during | 
|  | 4760 | * ssl->in_offt != NULL is fatal. */ | 
|  | 4761 | if( ssl->in_offt != NULL ) | 
|  | 4762 | { | 
|  | 4763 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4764 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4765 | } | 
|  | 4766 |  | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4767 | /* | 
|  | 4768 | * Get next Handshake message in the current record | 
|  | 4769 | */ | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4770 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4771 | /* Notes: | 
| Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4772 | * (1) in_hslen is not necessarily the size of the | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4773 | *     current handshake content: If DTLS handshake | 
|  | 4774 | *     fragmentation is used, that's the fragment | 
|  | 4775 | *     size instead. Using the total handshake message | 
| Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4776 | *     size here is faulty and should be changed at | 
|  | 4777 | *     some point. | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4778 | * (2) While it doesn't seem to cause problems, one | 
|  | 4779 | *     has to be very careful not to assume that in_hslen | 
|  | 4780 | *     is always <= in_msglen in a sensible communication. | 
|  | 4781 | *     Again, it's wrong for DTLS handshake fragmentation. | 
|  | 4782 | *     The following check is therefore mandatory, and | 
|  | 4783 | *     should not be treated as a silently corrected assertion. | 
| Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4784 | *     Additionally, ssl->in_hslen might be arbitrarily out of | 
|  | 4785 | *     bounds after handling a DTLS message with an unexpected | 
|  | 4786 | *     sequence number, see mbedtls_ssl_prepare_handshake_record. | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4787 | */ | 
|  | 4788 | if( ssl->in_hslen < ssl->in_msglen ) | 
|  | 4789 | { | 
|  | 4790 | ssl->in_msglen -= ssl->in_hslen; | 
|  | 4791 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, | 
|  | 4792 | ssl->in_msglen ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4793 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4794 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", | 
|  | 4795 | ssl->in_msg, ssl->in_msglen ); | 
|  | 4796 | } | 
|  | 4797 | else | 
|  | 4798 | { | 
|  | 4799 | ssl->in_msglen = 0; | 
|  | 4800 | } | 
| Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 4801 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4802 | ssl->in_hslen   = 0; | 
|  | 4803 | } | 
|  | 4804 | /* Case (4): Application data */ | 
|  | 4805 | else if( ssl->in_offt != NULL ) | 
|  | 4806 | { | 
|  | 4807 | return( 0 ); | 
|  | 4808 | } | 
|  | 4809 | /* Everything else (CCS & Alerts) */ | 
|  | 4810 | else | 
|  | 4811 | { | 
|  | 4812 | ssl->in_msglen = 0; | 
|  | 4813 | } | 
|  | 4814 |  | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4815 | return( 0 ); | 
|  | 4816 | } | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4817 |  | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4818 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) | 
|  | 4819 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4820 | if( ssl->in_msglen > 0 ) | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4821 | return( 1 ); | 
|  | 4822 |  | 
|  | 4823 | return( 0 ); | 
|  | 4824 | } | 
|  | 4825 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4826 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4827 |  | 
|  | 4828 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) | 
|  | 4829 | { | 
|  | 4830 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 4831 | if( hs == NULL ) | 
|  | 4832 | return; | 
|  | 4833 |  | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4834 | if( hs->buffering.future_record.data != NULL ) | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4835 | { | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4836 | hs->buffering.total_bytes_buffered -= | 
|  | 4837 | hs->buffering.future_record.len; | 
|  | 4838 |  | 
|  | 4839 | mbedtls_free( hs->buffering.future_record.data ); | 
|  | 4840 | hs->buffering.future_record.data = NULL; | 
|  | 4841 | } | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4842 | } | 
|  | 4843 |  | 
|  | 4844 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) | 
|  | 4845 | { | 
|  | 4846 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 4847 | unsigned char * rec; | 
|  | 4848 | size_t rec_len; | 
|  | 4849 | unsigned rec_epoch; | 
|  | 4850 |  | 
|  | 4851 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 4852 | return( 0 ); | 
|  | 4853 |  | 
|  | 4854 | if( hs == NULL ) | 
|  | 4855 | return( 0 ); | 
|  | 4856 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4857 | rec       = hs->buffering.future_record.data; | 
|  | 4858 | rec_len   = hs->buffering.future_record.len; | 
|  | 4859 | rec_epoch = hs->buffering.future_record.epoch; | 
|  | 4860 |  | 
|  | 4861 | if( rec == NULL ) | 
|  | 4862 | return( 0 ); | 
|  | 4863 |  | 
| Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4864 | /* Only consider loading future records if the | 
|  | 4865 | * input buffer is empty. */ | 
| Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 4866 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) | 
| Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4867 | return( 0 ); | 
|  | 4868 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4869 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); | 
|  | 4870 |  | 
|  | 4871 | if( rec_epoch != ssl->in_epoch ) | 
|  | 4872 | { | 
|  | 4873 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); | 
|  | 4874 | goto exit; | 
|  | 4875 | } | 
|  | 4876 |  | 
|  | 4877 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); | 
|  | 4878 |  | 
|  | 4879 | /* Double-check that the record is not too large */ | 
|  | 4880 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - | 
|  | 4881 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) | 
|  | 4882 | { | 
|  | 4883 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 4884 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
|  | 4885 | } | 
|  | 4886 |  | 
|  | 4887 | memcpy( ssl->in_hdr, rec, rec_len ); | 
|  | 4888 | ssl->in_left = rec_len; | 
|  | 4889 | ssl->next_record_offset = 0; | 
|  | 4890 |  | 
|  | 4891 | ssl_free_buffered_record( ssl ); | 
|  | 4892 |  | 
|  | 4893 | exit: | 
|  | 4894 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); | 
|  | 4895 | return( 0 ); | 
|  | 4896 | } | 
|  | 4897 |  | 
|  | 4898 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ) | 
|  | 4899 | { | 
|  | 4900 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 4901 | size_t const rec_hdr_len = 13; | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4902 | size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen; | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4903 |  | 
|  | 4904 | /* Don't buffer future records outside handshakes. */ | 
|  | 4905 | if( hs == NULL ) | 
|  | 4906 | return( 0 ); | 
|  | 4907 |  | 
|  | 4908 | /* Only buffer handshake records (we are only interested | 
|  | 4909 | * in Finished messages). */ | 
|  | 4910 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
|  | 4911 | return( 0 ); | 
|  | 4912 |  | 
|  | 4913 | /* Don't buffer more than one future epoch record. */ | 
|  | 4914 | if( hs->buffering.future_record.data != NULL ) | 
|  | 4915 | return( 0 ); | 
|  | 4916 |  | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4917 | /* Don't buffer record if there's not enough buffering space remaining. */ | 
|  | 4918 | if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - | 
|  | 4919 | hs->buffering.total_bytes_buffered ) ) | 
|  | 4920 | { | 
|  | 4921 | 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", | 
|  | 4922 | (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING, | 
|  | 4923 | (unsigned) hs->buffering.total_bytes_buffered ) ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4924 | return( 0 ); | 
|  | 4925 | } | 
|  | 4926 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4927 | /* Buffer record */ | 
|  | 4928 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", | 
|  | 4929 | ssl->in_epoch + 1 ) ); | 
|  | 4930 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr, | 
|  | 4931 | rec_hdr_len + ssl->in_msglen ); | 
|  | 4932 |  | 
|  | 4933 | /* ssl_parse_record_header() only considers records | 
|  | 4934 | * of the next epoch as candidates for buffering. */ | 
|  | 4935 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4936 | hs->buffering.future_record.len   = total_buf_sz; | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4937 |  | 
|  | 4938 | hs->buffering.future_record.data = | 
|  | 4939 | mbedtls_calloc( 1, hs->buffering.future_record.len ); | 
|  | 4940 | if( hs->buffering.future_record.data == NULL ) | 
|  | 4941 | { | 
|  | 4942 | /* If we run out of RAM trying to buffer a | 
|  | 4943 | * record from the next epoch, just ignore. */ | 
|  | 4944 | return( 0 ); | 
|  | 4945 | } | 
|  | 4946 |  | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4947 | memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz ); | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4948 |  | 
| Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4949 | hs->buffering.total_bytes_buffered += total_buf_sz; | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4950 | return( 0 ); | 
|  | 4951 | } | 
|  | 4952 |  | 
|  | 4953 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 4954 |  | 
| Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4955 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) | 
| Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4956 | { | 
|  | 4957 | int ret; | 
|  | 4958 |  | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4959 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 4960 | /* We might have buffered a future record; if so, | 
|  | 4961 | * and if the epoch matches now, load it. | 
|  | 4962 | * On success, this call will set ssl->in_left to | 
|  | 4963 | * the length of the buffered record, so that | 
|  | 4964 | * the calls to ssl_fetch_input() below will | 
|  | 4965 | * essentially be no-ops. */ | 
|  | 4966 | ret = ssl_load_buffered_record( ssl ); | 
|  | 4967 | if( ret != 0 ) | 
|  | 4968 | return( ret ); | 
|  | 4969 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4970 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4971 | 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] | 4972 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4973 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4974 | return( ret ); | 
|  | 4975 | } | 
|  | 4976 |  | 
|  | 4977 | if( ( ret = ssl_parse_record_header( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4978 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4979 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 4980 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 4981 | ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4982 | { | 
| Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4983 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) | 
|  | 4984 | { | 
|  | 4985 | ret = ssl_buffer_future_record( ssl ); | 
|  | 4986 | if( ret != 0 ) | 
|  | 4987 | return( ret ); | 
|  | 4988 |  | 
|  | 4989 | /* Fall through to handling of unexpected records */ | 
|  | 4990 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; | 
|  | 4991 | } | 
|  | 4992 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4993 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) | 
|  | 4994 | { | 
|  | 4995 | /* Skip unexpected record (but not whole datagram) */ | 
|  | 4996 | ssl->next_record_offset = ssl->in_msglen | 
|  | 4997 | + mbedtls_ssl_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4998 |  | 
| Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4999 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " | 
|  | 5000 | "(header)" ) ); | 
|  | 5001 | } | 
|  | 5002 | else | 
|  | 5003 | { | 
|  | 5004 | /* Skip invalid record and the rest of the datagram */ | 
|  | 5005 | ssl->next_record_offset = 0; | 
|  | 5006 | ssl->in_left = 0; | 
|  | 5007 |  | 
|  | 5008 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " | 
|  | 5009 | "(header)" ) ); | 
|  | 5010 | } | 
|  | 5011 |  | 
|  | 5012 | /* Get next record */ | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5013 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5014 | } | 
|  | 5015 | #endif | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5016 | return( ret ); | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5017 | } | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5018 |  | 
|  | 5019 | /* | 
|  | 5020 | * Read and optionally decrypt the message contents | 
|  | 5021 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5022 | if( ( ret = mbedtls_ssl_fetch_input( ssl, | 
|  | 5023 | mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5024 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5025 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5026 | return( ret ); | 
|  | 5027 | } | 
|  | 5028 |  | 
|  | 5029 | /* Done reading this record, get ready for the next one */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5030 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5031 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5032 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5033 | ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl ); | 
| Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5034 | if( ssl->next_record_offset < ssl->in_left ) | 
|  | 5035 | { | 
|  | 5036 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); | 
|  | 5037 | } | 
|  | 5038 | } | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5039 | else | 
|  | 5040 | #endif | 
|  | 5041 | ssl->in_left = 0; | 
|  | 5042 |  | 
|  | 5043 | if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5044 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5045 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5046 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5047 | { | 
|  | 5048 | /* Silently discard invalid records */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5049 | if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD || | 
|  | 5050 | ret == MBEDTLS_ERR_SSL_INVALID_MAC ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5051 | { | 
| Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 5052 | /* Except when waiting for Finished as a bad mac here | 
|  | 5053 | * probably means something went wrong in the handshake | 
|  | 5054 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ | 
|  | 5055 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || | 
|  | 5056 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) | 
|  | 5057 | { | 
|  | 5058 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) | 
|  | 5059 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) | 
|  | 5060 | { | 
|  | 5061 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 5062 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5063 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); | 
|  | 5064 | } | 
|  | 5065 | #endif | 
|  | 5066 | return( ret ); | 
|  | 5067 | } | 
|  | 5068 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5069 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5070 | if( ssl->conf->badmac_limit != 0 && | 
|  | 5071 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5072 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5073 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); | 
|  | 5074 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5075 | } | 
|  | 5076 | #endif | 
|  | 5077 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5078 | /* As above, invalid records cause | 
|  | 5079 | * dismissal of the whole datagram. */ | 
|  | 5080 |  | 
|  | 5081 | ssl->next_record_offset = 0; | 
|  | 5082 | ssl->in_left = 0; | 
|  | 5083 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5084 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5085 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5086 | } | 
|  | 5087 |  | 
|  | 5088 | return( ret ); | 
|  | 5089 | } | 
|  | 5090 | else | 
|  | 5091 | #endif | 
|  | 5092 | { | 
|  | 5093 | /* Error out (and send alert) on invalid records */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5094 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) | 
|  | 5095 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5096 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5097 | mbedtls_ssl_send_alert_message( ssl, | 
|  | 5098 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5099 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); | 
| Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5100 | } | 
|  | 5101 | #endif | 
|  | 5102 | return( ret ); | 
|  | 5103 | } | 
|  | 5104 | } | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5105 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5106 | return( 0 ); | 
|  | 5107 | } | 
|  | 5108 |  | 
|  | 5109 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) | 
|  | 5110 | { | 
|  | 5111 | int ret; | 
|  | 5112 |  | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5113 | /* | 
| Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5114 | * Handle particular types of records | 
|  | 5115 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5116 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5117 | { | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5118 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) | 
|  | 5119 | { | 
| Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 5120 | return( ret ); | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5121 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5122 | } | 
|  | 5123 |  | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5124 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5125 | { | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5126 | if( ssl->in_msglen != 1 ) | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5127 | { | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5128 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", | 
|  | 5129 | ssl->in_msglen ) ); | 
|  | 5130 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5131 | } | 
|  | 5132 |  | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5133 | if( ssl->in_msg[0] != 1 ) | 
|  | 5134 | { | 
|  | 5135 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", | 
|  | 5136 | ssl->in_msg[0] ) ); | 
|  | 5137 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5138 | } | 
|  | 5139 |  | 
|  | 5140 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5141 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 5142 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC    && | 
|  | 5143 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) | 
|  | 5144 | { | 
|  | 5145 | if( ssl->handshake == NULL ) | 
|  | 5146 | { | 
|  | 5147 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); | 
|  | 5148 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); | 
|  | 5149 | } | 
|  | 5150 |  | 
|  | 5151 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); | 
|  | 5152 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); | 
|  | 5153 | } | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5154 | #endif | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5155 | } | 
| Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5156 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5157 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5158 | { | 
| Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 5159 | if( ssl->in_msglen != 2 ) | 
|  | 5160 | { | 
|  | 5161 | /* Note: Standard allows for more than one 2 byte alert | 
|  | 5162 | to be packed in a single message, but Mbed TLS doesn't | 
|  | 5163 | currently support this. */ | 
|  | 5164 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", | 
|  | 5165 | ssl->in_msglen ) ); | 
|  | 5166 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); | 
|  | 5167 | } | 
|  | 5168 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5169 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5170 | ssl->in_msg[0], ssl->in_msg[1] ) ); | 
|  | 5171 |  | 
|  | 5172 | /* | 
| Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 5173 | * Ignore non-fatal alerts, except close_notify and no_renegotiation | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5174 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5175 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5176 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5177 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5178 | ssl->in_msg[1] ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5179 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5180 | } | 
|  | 5181 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5182 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && | 
|  | 5183 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5184 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5185 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); | 
|  | 5186 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5187 | } | 
| Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5188 |  | 
|  | 5189 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) | 
|  | 5190 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && | 
|  | 5191 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) | 
|  | 5192 | { | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5193 | 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] | 5194 | /* Will be handled when trying to parse ServerHello */ | 
|  | 5195 | return( 0 ); | 
|  | 5196 | } | 
|  | 5197 | #endif | 
|  | 5198 |  | 
|  | 5199 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) | 
|  | 5200 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && | 
|  | 5201 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && | 
|  | 5202 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && | 
|  | 5203 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) | 
|  | 5204 | { | 
|  | 5205 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); | 
|  | 5206 | /* Will be handled in mbedtls_ssl_parse_certificate() */ | 
|  | 5207 | return( 0 ); | 
|  | 5208 | } | 
|  | 5209 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ | 
|  | 5210 |  | 
|  | 5211 | /* Silently ignore: fetch new message */ | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5212 | return MBEDTLS_ERR_SSL_NON_FATAL; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5213 | } | 
|  | 5214 |  | 
| Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5215 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 5216 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 5217 | ssl->handshake != NULL && | 
|  | 5218 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER  ) | 
|  | 5219 | { | 
|  | 5220 | ssl_handshake_wrapup_free_hs_transform( ssl ); | 
|  | 5221 | } | 
|  | 5222 | #endif | 
|  | 5223 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5224 | return( 0 ); | 
|  | 5225 | } | 
|  | 5226 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5227 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5228 | { | 
|  | 5229 | int ret; | 
|  | 5230 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5231 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, | 
|  | 5232 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5233 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5234 | { | 
|  | 5235 | return( ret ); | 
|  | 5236 | } | 
|  | 5237 |  | 
|  | 5238 | return( 0 ); | 
|  | 5239 | } | 
|  | 5240 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5241 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5242 | unsigned char level, | 
|  | 5243 | unsigned char message ) | 
|  | 5244 | { | 
|  | 5245 | int ret; | 
|  | 5246 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5247 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 5248 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 5249 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5250 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5251 | 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] | 5252 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5253 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5254 | ssl->out_msglen = 2; | 
|  | 5255 | ssl->out_msg[0] = level; | 
|  | 5256 | ssl->out_msg[1] = message; | 
|  | 5257 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 5258 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5259 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5260 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5261 | return( ret ); | 
|  | 5262 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5263 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); | 
| Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5264 |  | 
|  | 5265 | return( 0 ); | 
|  | 5266 | } | 
|  | 5267 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5268 | /* | 
|  | 5269 | * Handshake functions | 
|  | 5270 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5271 | #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)         && \ | 
|  | 5272 | !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)     && \ | 
|  | 5273 | !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)     && \ | 
|  | 5274 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)   && \ | 
|  | 5275 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \ | 
|  | 5276 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED)    && \ | 
|  | 5277 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 5278 | /* No certificate support -> dummy functions */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5279 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5280 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5281 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5282 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5283 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5284 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5285 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 5286 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
| Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 5287 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
|  | 5288 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5289 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5290 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5291 | ssl->state++; | 
|  | 5292 | return( 0 ); | 
|  | 5293 | } | 
|  | 5294 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5295 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5296 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5297 | } | 
|  | 5298 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5299 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5300 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5301 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5302 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5303 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5304 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5305 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 5306 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
| Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 5307 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
|  | 5308 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5309 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5310 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5311 | ssl->state++; | 
|  | 5312 | return( 0 ); | 
|  | 5313 | } | 
|  | 5314 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5315 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 5316 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5317 | } | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 5318 |  | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5319 | #else | 
| Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 5320 | /* Some certificate support -> implement write and parse */ | 
|  | 5321 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5322 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5323 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5324 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5325 | size_t i, n; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5326 | const mbedtls_x509_crt *crt; | 
|  | 5327 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5328 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5329 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5330 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5331 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 5332 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
| Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 5333 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
|  | 5334 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5335 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5336 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
| Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 5337 | ssl->state++; | 
|  | 5338 | return( 0 ); | 
|  | 5339 | } | 
|  | 5340 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5341 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5342 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5343 | { | 
|  | 5344 | if( ssl->client_auth == 0 ) | 
|  | 5345 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5346 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5347 | ssl->state++; | 
|  | 5348 | return( 0 ); | 
|  | 5349 | } | 
|  | 5350 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5351 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5352 | /* | 
|  | 5353 | * If using SSLv3 and got no cert, send an Alert message | 
|  | 5354 | * (otherwise an empty Certificate message will be sent). | 
|  | 5355 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5356 | if( mbedtls_ssl_own_cert( ssl )  == NULL && | 
|  | 5357 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5358 | { | 
|  | 5359 | ssl->out_msglen  = 2; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5360 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; | 
|  | 5361 | ssl->out_msg[0]  = MBEDTLS_SSL_ALERT_LEVEL_WARNING; | 
|  | 5362 | ssl->out_msg[1]  = MBEDTLS_SSL_ALERT_MSG_NO_CERT; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5363 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5364 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5365 | goto write_msg; | 
|  | 5366 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5367 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5368 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5369 | #endif /* MBEDTLS_SSL_CLI_C */ | 
|  | 5370 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5371 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5372 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5373 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5374 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5375 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); | 
|  | 5376 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5377 | } | 
|  | 5378 | } | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 5379 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5380 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5381 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5382 |  | 
|  | 5383 | /* | 
|  | 5384 | *     0  .  0    handshake type | 
|  | 5385 | *     1  .  3    handshake length | 
|  | 5386 | *     4  .  6    length of all certs | 
|  | 5387 | *     7  .  9    length of cert. 1 | 
|  | 5388 | *    10  . n-1   peer certificate | 
|  | 5389 | *     n  . n+2   length of cert. 2 | 
|  | 5390 | *    n+3 . ...   upper level cert, etc. | 
|  | 5391 | */ | 
|  | 5392 | i = 7; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5393 | crt = mbedtls_ssl_own_cert( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5394 |  | 
| Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 5395 | while( crt != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5396 | { | 
|  | 5397 | n = crt->raw.len; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5398 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5399 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5400 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5401 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5402 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5403 | } | 
|  | 5404 |  | 
|  | 5405 | ssl->out_msg[i    ] = (unsigned char)( n >> 16 ); | 
|  | 5406 | ssl->out_msg[i + 1] = (unsigned char)( n >>  8 ); | 
|  | 5407 | ssl->out_msg[i + 2] = (unsigned char)( n       ); | 
|  | 5408 |  | 
|  | 5409 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); | 
|  | 5410 | i += n; crt = crt->next; | 
|  | 5411 | } | 
|  | 5412 |  | 
|  | 5413 | ssl->out_msg[4]  = (unsigned char)( ( i - 7 ) >> 16 ); | 
|  | 5414 | ssl->out_msg[5]  = (unsigned char)( ( i - 7 ) >>  8 ); | 
|  | 5415 | ssl->out_msg[6]  = (unsigned char)( ( i - 7 )       ); | 
|  | 5416 |  | 
|  | 5417 | ssl->out_msglen  = i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5418 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 5419 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_CERTIFICATE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5420 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 5421 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5422 | write_msg: | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 5423 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5424 |  | 
|  | 5425 | ssl->state++; | 
|  | 5426 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5427 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5428 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5429 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5430 | return( ret ); | 
|  | 5431 | } | 
|  | 5432 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5433 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5434 |  | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 5435 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5436 | } | 
|  | 5437 |  | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5438 | /* | 
|  | 5439 | * Once the certificate message is read, parse it into a cert chain and | 
|  | 5440 | * perform basic checks, but leave actual verification to the caller | 
|  | 5441 | */ | 
|  | 5442 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5443 | { | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5444 | int ret; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 5445 | size_t i, n; | 
| Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 5446 | uint8_t alert; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5447 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5448 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 5449 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5450 | /* | 
|  | 5451 | * Check if the client sent an empty certificate | 
|  | 5452 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5453 | if( ssl->conf->endpoint  == MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5454 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5455 | { | 
| Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 5456 | if( ssl->in_msglen  == 2                        && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5457 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT            && | 
|  | 5458 | ssl->in_msg[0]  == MBEDTLS_SSL_ALERT_LEVEL_WARNING  && | 
|  | 5459 | ssl->in_msg[1]  == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5460 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5461 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5462 |  | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5463 | /* The client was asked for a certificate but didn't send | 
|  | 5464 | one. The client should know what's going on, so we | 
|  | 5465 | don't send an alert. */ | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5466 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5467 | return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5468 | } | 
|  | 5469 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5470 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5471 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5472 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 5473 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5474 | if( ssl->conf->endpoint  == MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5475 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5476 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5477 | if( ssl->in_hslen   == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && | 
|  | 5478 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE    && | 
|  | 5479 | ssl->in_msg[0]  == MBEDTLS_SSL_HS_CERTIFICATE   && | 
|  | 5480 | 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] | 5481 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5482 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5483 |  | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5484 | /* The client was asked for a certificate but didn't send | 
|  | 5485 | one. The client should know what's going on, so we | 
|  | 5486 | don't send an alert. */ | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 5487 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5488 | return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5489 | } | 
|  | 5490 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5491 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 5492 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 5493 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5494 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5495 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5496 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5497 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5498 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5499 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5500 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5501 | } | 
|  | 5502 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5503 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || | 
|  | 5504 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5505 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5506 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5507 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5508 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5509 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5510 | } | 
|  | 5511 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5512 | i = mbedtls_ssl_hs_hdr_len( ssl ); | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 5513 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5514 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5515 | * Same message structure as in mbedtls_ssl_write_certificate() | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5516 | */ | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 5517 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5518 |  | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 5519 | if( ssl->in_msg[i] != 0 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5520 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5521 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5522 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5523 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5524 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5525 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5526 | } | 
|  | 5527 |  | 
| Manuel Pégourié-Gonnard | bfb355c | 2013-09-07 17:27:43 +0200 | [diff] [blame] | 5528 | /* In case we tried to reuse a session but it failed */ | 
|  | 5529 | if( ssl->session_negotiate->peer_cert != NULL ) | 
|  | 5530 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5531 | mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert ); | 
|  | 5532 | mbedtls_free( ssl->session_negotiate->peer_cert ); | 
| Manuel Pégourié-Gonnard | bfb355c | 2013-09-07 17:27:43 +0200 | [diff] [blame] | 5533 | } | 
|  | 5534 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 5535 | if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5536 | sizeof( mbedtls_x509_crt ) ) ) == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5537 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 5538 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5539 | sizeof( mbedtls_x509_crt ) ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5540 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5541 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 5542 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5543 | } | 
|  | 5544 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5545 | mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5546 |  | 
| Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 5547 | i += 3; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5548 |  | 
|  | 5549 | while( i < ssl->in_hslen ) | 
|  | 5550 | { | 
| Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 5551 | if ( i + 3 > ssl->in_hslen ) { | 
|  | 5552 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
|  | 5553 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5554 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
|  | 5555 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
|  | 5556 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5557 | if( ssl->in_msg[i] != 0 ) | 
|  | 5558 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5559 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5560 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5561 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5562 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5563 | } | 
|  | 5564 |  | 
|  | 5565 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) | 
|  | 5566 | | (unsigned int) ssl->in_msg[i + 2]; | 
|  | 5567 | i += 3; | 
|  | 5568 |  | 
|  | 5569 | if( n < 128 || i + n > ssl->in_hslen ) | 
|  | 5570 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5571 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5572 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5573 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5574 | return( MBEDTLS_ERR_SSL_BAD_HS_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 | ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert, | 
| Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 5578 | ssl->in_msg + i, n ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5579 | switch( ret ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5580 | { | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5581 | case 0: /*ok*/ | 
|  | 5582 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: | 
|  | 5583 | /* Ignore certificate with an unknown algorithm: maybe a | 
|  | 5584 | prior certificate was already trusted. */ | 
|  | 5585 | break; | 
|  | 5586 |  | 
|  | 5587 | case MBEDTLS_ERR_X509_ALLOC_FAILED: | 
|  | 5588 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; | 
|  | 5589 | goto crt_parse_der_failed; | 
|  | 5590 |  | 
|  | 5591 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: | 
|  | 5592 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 5593 | goto crt_parse_der_failed; | 
|  | 5594 |  | 
|  | 5595 | default: | 
|  | 5596 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 5597 | crt_parse_der_failed: | 
|  | 5598 | 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] | 5599 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5600 | return( ret ); | 
|  | 5601 | } | 
|  | 5602 |  | 
|  | 5603 | i += n; | 
|  | 5604 | } | 
|  | 5605 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5606 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5607 |  | 
| Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5608 | /* | 
|  | 5609 | * On client, make sure the server cert doesn't change during renego to | 
|  | 5610 | * avoid "triple handshake" attack: https://secure-resumption.com/ | 
|  | 5611 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5612 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5613 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5614 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5615 | { | 
|  | 5616 | if( ssl->session->peer_cert == NULL ) | 
|  | 5617 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5618 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5619 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5620 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5621 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5622 | } | 
|  | 5623 |  | 
|  | 5624 | if( ssl->session->peer_cert->raw.len != | 
|  | 5625 | ssl->session_negotiate->peer_cert->raw.len || | 
|  | 5626 | memcmp( ssl->session->peer_cert->raw.p, | 
|  | 5627 | ssl->session_negotiate->peer_cert->raw.p, | 
|  | 5628 | ssl->session->peer_cert->raw.len ) != 0 ) | 
|  | 5629 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5630 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5631 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5632 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5633 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); | 
| Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5634 | } | 
|  | 5635 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5636 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 5637 |  | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5638 | return( 0 ); | 
|  | 5639 | } | 
|  | 5640 |  | 
|  | 5641 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) | 
|  | 5642 | { | 
|  | 5643 | int ret; | 
|  | 5644 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = | 
|  | 5645 | ssl->transform_negotiate->ciphersuite_info; | 
|  | 5646 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 5647 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET | 
|  | 5648 | ? ssl->handshake->sni_authmode | 
|  | 5649 | : ssl->conf->authmode; | 
|  | 5650 | #else | 
|  | 5651 | const int authmode = ssl->conf->authmode; | 
|  | 5652 | #endif | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5653 | void *rs_ctx = NULL; | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5654 |  | 
|  | 5655 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); | 
|  | 5656 |  | 
|  | 5657 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || | 
|  | 5658 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || | 
|  | 5659 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || | 
|  | 5660 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) | 
|  | 5661 | { | 
|  | 5662 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
|  | 5663 | ssl->state++; | 
|  | 5664 | return( 0 ); | 
|  | 5665 | } | 
|  | 5666 |  | 
|  | 5667 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 5668 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && | 
|  | 5669 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) | 
|  | 5670 | { | 
|  | 5671 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
|  | 5672 | ssl->state++; | 
|  | 5673 | return( 0 ); | 
|  | 5674 | } | 
|  | 5675 |  | 
|  | 5676 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && | 
|  | 5677 | authmode == MBEDTLS_SSL_VERIFY_NONE ) | 
|  | 5678 | { | 
|  | 5679 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY; | 
|  | 5680 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5681 |  | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5682 | ssl->state++; | 
|  | 5683 | return( 0 ); | 
|  | 5684 | } | 
|  | 5685 | #endif | 
|  | 5686 |  | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5687 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
|  | 5688 | if( ssl->handshake->ecrs_enabled && | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 5689 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5690 | { | 
|  | 5691 | goto crt_verify; | 
|  | 5692 | } | 
|  | 5693 | #endif | 
|  | 5694 |  | 
| Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 5695 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5696 | { | 
|  | 5697 | /* mbedtls_ssl_read_record may have sent an alert already. We | 
|  | 5698 | let it decide whether to alert. */ | 
|  | 5699 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 5700 | return( ret ); | 
|  | 5701 | } | 
|  | 5702 |  | 
|  | 5703 | if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 ) | 
|  | 5704 | { | 
|  | 5705 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 5706 | if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE && | 
|  | 5707 | authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) | 
|  | 5708 | { | 
|  | 5709 | ret = 0; | 
|  | 5710 | } | 
|  | 5711 | #endif | 
|  | 5712 |  | 
|  | 5713 | ssl->state++; | 
|  | 5714 | return( ret ); | 
|  | 5715 | } | 
|  | 5716 |  | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5717 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
|  | 5718 | if( ssl->handshake->ecrs_enabled) | 
| Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 5719 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5720 |  | 
|  | 5721 | crt_verify: | 
|  | 5722 | if( ssl->handshake->ecrs_enabled) | 
|  | 5723 | rs_ctx = &ssl->handshake->ecrs_ctx; | 
|  | 5724 | #endif | 
|  | 5725 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 5726 | if( authmode != MBEDTLS_SSL_VERIFY_NONE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5727 | { | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 5728 | mbedtls_x509_crt *ca_chain; | 
|  | 5729 | mbedtls_x509_crl *ca_crl; | 
|  | 5730 |  | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5731 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 5732 | if( ssl->handshake->sni_ca_chain != NULL ) | 
|  | 5733 | { | 
|  | 5734 | ca_chain = ssl->handshake->sni_ca_chain; | 
|  | 5735 | ca_crl   = ssl->handshake->sni_ca_crl; | 
|  | 5736 | } | 
|  | 5737 | else | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 5738 | #endif | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 5739 | { | 
|  | 5740 | ca_chain = ssl->conf->ca_chain; | 
|  | 5741 | ca_crl   = ssl->conf->ca_crl; | 
|  | 5742 | } | 
|  | 5743 |  | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5744 | /* | 
|  | 5745 | * Main check: verify certificate | 
|  | 5746 | */ | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5747 | ret = mbedtls_x509_crt_verify_restartable( | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 5748 | ssl->session_negotiate->peer_cert, | 
|  | 5749 | ca_chain, ca_crl, | 
|  | 5750 | ssl->conf->cert_profile, | 
|  | 5751 | ssl->hostname, | 
|  | 5752 | &ssl->session_negotiate->verify_result, | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5753 | ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5754 |  | 
|  | 5755 | if( ret != 0 ) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5756 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5757 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5758 | } | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5759 |  | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5760 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
|  | 5761 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 5762 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); | 
| Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 5763 | #endif | 
|  | 5764 |  | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5765 | /* | 
|  | 5766 | * Secondary checks: always done, but change 'ret' only if it was 0 | 
|  | 5767 | */ | 
|  | 5768 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5769 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5770 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5771 | 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] | 5772 |  | 
|  | 5773 | /* 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] | 5774 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 5775 | 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] | 5776 | { | 
| Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5777 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; | 
|  | 5778 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5779 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5780 | if( ret == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5781 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 5782 | } | 
|  | 5783 | } | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5784 | #endif /* MBEDTLS_ECP_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5785 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5786 | if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert, | 
| Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5787 | ciphersuite_info, | 
|  | 5788 | ! ssl->conf->endpoint, | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 5789 | &ssl->session_negotiate->verify_result ) != 0 ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5790 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5791 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5792 | if( ret == 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5793 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 5794 | } | 
|  | 5795 |  | 
| Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5796 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a | 
|  | 5797 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, | 
|  | 5798 | * with details encoded in the verification flags. All other kinds | 
|  | 5799 | * of error codes, including those from the user provided f_vrfy | 
|  | 5800 | * functions, are treated as fatal and lead to a failure of | 
|  | 5801 | * ssl_parse_certificate even if verification was optional. */ | 
|  | 5802 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && | 
|  | 5803 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || | 
|  | 5804 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) | 
|  | 5805 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5806 | ret = 0; | 
| Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5807 | } | 
|  | 5808 |  | 
|  | 5809 | if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) | 
|  | 5810 | { | 
|  | 5811 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); | 
|  | 5812 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; | 
|  | 5813 | } | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5814 |  | 
|  | 5815 | if( ret != 0 ) | 
|  | 5816 | { | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5817 | uint8_t alert; | 
|  | 5818 |  | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5819 | /* The certificate may have been rejected for several reasons. | 
|  | 5820 | Pick one and send the corresponding alert. Which alert to send | 
|  | 5821 | may be a subject of debate in some cases. */ | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5822 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) | 
|  | 5823 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; | 
|  | 5824 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) | 
|  | 5825 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; | 
|  | 5826 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) | 
|  | 5827 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 5828 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) | 
|  | 5829 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 5830 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) | 
|  | 5831 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 5832 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) | 
|  | 5833 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 5834 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) | 
|  | 5835 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; | 
|  | 5836 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) | 
|  | 5837 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; | 
|  | 5838 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) | 
|  | 5839 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; | 
|  | 5840 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) | 
|  | 5841 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; | 
| Gilles Peskine | 8498cb3 | 2017-05-10 15:39:40 +0200 | [diff] [blame] | 5842 | else | 
|  | 5843 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5844 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5845 | alert ); | 
|  | 5846 | } | 
| Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 5847 |  | 
| Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 5848 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 5849 | if( ssl->session_negotiate->verify_result != 0 ) | 
|  | 5850 | { | 
|  | 5851 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", | 
|  | 5852 | ssl->session_negotiate->verify_result ) ); | 
|  | 5853 | } | 
|  | 5854 | else | 
|  | 5855 | { | 
|  | 5856 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); | 
|  | 5857 | } | 
|  | 5858 | #endif /* MBEDTLS_DEBUG_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5859 | } | 
|  | 5860 |  | 
| Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 5861 | ssl->state++; | 
|  | 5862 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5863 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5864 |  | 
|  | 5865 | return( ret ); | 
|  | 5866 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5867 | #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED | 
|  | 5868 | !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED | 
|  | 5869 | !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED | 
|  | 5870 | !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED | 
|  | 5871 | !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED | 
|  | 5872 | !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED | 
|  | 5873 | !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5874 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5875 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5876 | { | 
|  | 5877 | int ret; | 
|  | 5878 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5879 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5880 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5881 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5882 | ssl->out_msglen  = 1; | 
|  | 5883 | ssl->out_msg[0]  = 1; | 
|  | 5884 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5885 | ssl->state++; | 
|  | 5886 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5887 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5888 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5889 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5890 | return( ret ); | 
|  | 5891 | } | 
|  | 5892 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5893 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5894 |  | 
|  | 5895 | return( 0 ); | 
|  | 5896 | } | 
|  | 5897 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5898 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5899 | { | 
|  | 5900 | int ret; | 
|  | 5901 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5902 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5903 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5904 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5905 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5906 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5907 | return( ret ); | 
|  | 5908 | } | 
|  | 5909 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5910 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5911 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5912 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5913 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5914 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5915 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5916 | } | 
|  | 5917 |  | 
| Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5918 | /* CCS records are only accepted if they have length 1 and content '1', | 
|  | 5919 | * so we don't need to check this here. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5920 |  | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5921 | /* | 
|  | 5922 | * Switch to our negotiated transform and session parameters for inbound | 
|  | 5923 | * data. | 
|  | 5924 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5925 | 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] | 5926 | ssl->transform_in = ssl->transform_negotiate; | 
|  | 5927 | ssl->session_in = ssl->session_negotiate; | 
|  | 5928 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5929 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5930 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5931 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5932 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5933 | ssl_dtls_replay_reset( ssl ); | 
|  | 5934 | #endif | 
|  | 5935 |  | 
|  | 5936 | /* Increment epoch */ | 
|  | 5937 | if( ++ssl->in_epoch == 0 ) | 
|  | 5938 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5939 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5940 | /* This is highly unlikely to happen for legitimate reasons, so | 
|  | 5941 | 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] | 5942 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5943 | } | 
|  | 5944 | } | 
|  | 5945 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5946 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5947 | memset( ssl->in_ctr, 0, 8 ); | 
|  | 5948 |  | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5949 | ssl_update_in_pointers( ssl, ssl->transform_negotiate ); | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5950 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5951 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 5952 | if( mbedtls_ssl_hw_record_activate != NULL ) | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5953 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5954 | 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] | 5955 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5956 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5957 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 5958 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5959 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5960 | } | 
|  | 5961 | } | 
|  | 5962 | #endif | 
|  | 5963 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5964 | ssl->state++; | 
|  | 5965 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5966 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5967 |  | 
|  | 5968 | return( 0 ); | 
|  | 5969 | } | 
|  | 5970 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5971 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, | 
|  | 5972 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 5973 | { | 
| Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 5974 | ((void) ciphersuite_info); | 
| Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 5975 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5976 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 5977 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 5978 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5979 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 5980 | else | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 5981 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5982 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 5983 | #if defined(MBEDTLS_SHA512_C) | 
|  | 5984 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 5985 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; | 
|  | 5986 | else | 
|  | 5987 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5988 | #if defined(MBEDTLS_SHA256_C) | 
|  | 5989 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5990 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 5991 | else | 
|  | 5992 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5993 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 5994 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5995 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 5996 | return; | 
| Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 5997 | } | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 5998 | } | 
| Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 5999 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6000 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6001 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6002 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 6003 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6004 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5  ); | 
|  | 6005 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6006 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6007 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 6008 | #if defined(MBEDTLS_SHA256_C) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6009 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6010 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6011 | #if defined(MBEDTLS_SHA512_C) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6012 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6013 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6014 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6015 | } | 
|  | 6016 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6017 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6018 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6019 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6020 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 6021 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6022 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); | 
|  | 6023 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6024 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6025 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 6026 | #if defined(MBEDTLS_SHA256_C) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6027 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6028 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6029 | #if defined(MBEDTLS_SHA512_C) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6030 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); | 
| Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6031 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6032 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6033 | } | 
|  | 6034 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6035 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 6036 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 6037 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6038 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6039 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6040 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); | 
|  | 6041 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6042 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6043 | #endif | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6044 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6045 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 6046 | #if defined(MBEDTLS_SHA256_C) | 
|  | 6047 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6048 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6049 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6050 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6051 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6052 | #endif | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6053 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6054 | #if defined(MBEDTLS_SHA512_C) | 
|  | 6055 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6056 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6057 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6058 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6059 | } | 
| Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6060 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6061 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6062 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6063 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6064 | static void ssl_calc_finished_ssl( | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6065 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6066 | { | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6067 | const char *sender; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6068 | mbedtls_md5_context  md5; | 
|  | 6069 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6070 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6071 | unsigned char padbuf[48]; | 
|  | 6072 | unsigned char md5sum[16]; | 
|  | 6073 | unsigned char sha1sum[20]; | 
|  | 6074 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6075 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6076 | if( !session ) | 
|  | 6077 | session = ssl->session; | 
|  | 6078 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6079 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished ssl" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6080 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6081 | mbedtls_md5_init( &md5 ); | 
|  | 6082 | mbedtls_sha1_init( &sha1 ); | 
|  | 6083 |  | 
|  | 6084 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 6085 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6086 |  | 
|  | 6087 | /* | 
|  | 6088 | * SSLv3: | 
|  | 6089 | *   hash = | 
|  | 6090 | *      MD5( master + pad2 + | 
|  | 6091 | *          MD5( handshake + sender + master + pad1 ) ) | 
|  | 6092 | *   + SHA1( master + pad2 + | 
|  | 6093 | *         SHA1( handshake + sender + master + pad1 ) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6094 | */ | 
|  | 6095 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6096 | #if !defined(MBEDTLS_MD5_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6097 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished  md5 state", (unsigned char *) | 
|  | 6098 | md5.state, sizeof(  md5.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6099 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6100 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6101 | #if !defined(MBEDTLS_SHA1_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6102 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) | 
|  | 6103 | sha1.state, sizeof( sha1.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6104 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6105 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6106 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6107 | : "SRVR"; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6108 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6109 | memset( padbuf, 0x36, 48 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6110 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6111 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); | 
|  | 6112 | mbedtls_md5_update_ret( &md5, session->master, 48 ); | 
|  | 6113 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); | 
|  | 6114 | mbedtls_md5_finish_ret( &md5, md5sum ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6115 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6116 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); | 
|  | 6117 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); | 
|  | 6118 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); | 
|  | 6119 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6120 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6121 | memset( padbuf, 0x5C, 48 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6122 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6123 | mbedtls_md5_starts_ret( &md5 ); | 
|  | 6124 | mbedtls_md5_update_ret( &md5, session->master, 48 ); | 
|  | 6125 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); | 
|  | 6126 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); | 
|  | 6127 | mbedtls_md5_finish_ret( &md5, buf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6128 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6129 | mbedtls_sha1_starts_ret( &sha1 ); | 
|  | 6130 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); | 
|  | 6131 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); | 
|  | 6132 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); | 
|  | 6133 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6134 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6135 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6136 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6137 | mbedtls_md5_free(  &md5  ); | 
|  | 6138 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6139 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6140 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
|  | 6141 | mbedtls_platform_zeroize(  md5sum, sizeof(  md5sum ) ); | 
|  | 6142 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6143 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6144 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6145 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6146 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6147 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6148 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6149 | static void ssl_calc_finished_tls( | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6150 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6151 | { | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6152 | int len = 12; | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6153 | const char *sender; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6154 | mbedtls_md5_context  md5; | 
|  | 6155 | mbedtls_sha1_context sha1; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6156 | unsigned char padbuf[36]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6157 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6158 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6159 | if( !session ) | 
|  | 6160 | session = ssl->session; | 
|  | 6161 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6162 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6163 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6164 | mbedtls_md5_init( &md5 ); | 
|  | 6165 | mbedtls_sha1_init( &sha1 ); | 
|  | 6166 |  | 
|  | 6167 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | 
|  | 6168 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6169 |  | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6170 | /* | 
|  | 6171 | * TLSv1: | 
|  | 6172 | *   hash = PRF( master, finished_label, | 
|  | 6173 | *               MD5( handshake ) + SHA1( handshake ) )[0..11] | 
|  | 6174 | */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6175 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6176 | #if !defined(MBEDTLS_MD5_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6177 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished  md5 state", (unsigned char *) | 
|  | 6178 | md5.state, sizeof(  md5.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6179 | #endif | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6180 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6181 | #if !defined(MBEDTLS_SHA1_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6182 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) | 
|  | 6183 | sha1.state, sizeof( sha1.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6184 | #endif | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6185 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6186 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6187 | ? "client finished" | 
|  | 6188 | : "server finished"; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6189 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6190 | mbedtls_md5_finish_ret(  &md5, padbuf ); | 
|  | 6191 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6192 |  | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6193 | ssl->handshake->tls_prf( session->master, 48, sender, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6194 | padbuf, 36, buf, len ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6195 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6196 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6197 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6198 | mbedtls_md5_free(  &md5  ); | 
|  | 6199 | mbedtls_sha1_free( &sha1 ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6200 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6201 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6202 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6203 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6204 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6205 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6206 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6207 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 6208 | #if defined(MBEDTLS_SHA256_C) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6209 | static void ssl_calc_finished_tls_sha256( | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6210 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6211 | { | 
|  | 6212 | int len = 12; | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6213 | const char *sender; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6214 | mbedtls_sha256_context sha256; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6215 | unsigned char padbuf[32]; | 
|  | 6216 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6217 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6218 | if( !session ) | 
|  | 6219 | session = ssl->session; | 
|  | 6220 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6221 | mbedtls_sha256_init( &sha256 ); | 
|  | 6222 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6223 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha256" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6224 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6225 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6226 |  | 
|  | 6227 | /* | 
|  | 6228 | * TLSv1.2: | 
|  | 6229 | *   hash = PRF( master, finished_label, | 
|  | 6230 | *               Hash( handshake ) )[0.11] | 
|  | 6231 | */ | 
|  | 6232 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6233 | #if !defined(MBEDTLS_SHA256_ALT) | 
|  | 6234 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6235 | sha256.state, sizeof( sha256.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6236 | #endif | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6237 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6238 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6239 | ? "client finished" | 
|  | 6240 | : "server finished"; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6241 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6242 | mbedtls_sha256_finish_ret( &sha256, padbuf ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6243 |  | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6244 | ssl->handshake->tls_prf( session->master, 48, sender, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6245 | padbuf, 32, buf, len ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6246 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6247 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6248 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6249 | mbedtls_sha256_free( &sha256 ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6250 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6251 | mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6252 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6253 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6254 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6255 | #endif /* MBEDTLS_SHA256_C */ | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6256 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6257 | #if defined(MBEDTLS_SHA512_C) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6258 | static void ssl_calc_finished_tls_sha384( | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6259 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6260 | { | 
|  | 6261 | int len = 12; | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6262 | const char *sender; | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6263 | mbedtls_sha512_context sha512; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6264 | unsigned char padbuf[48]; | 
|  | 6265 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6266 | mbedtls_ssl_session *session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6267 | if( !session ) | 
|  | 6268 | session = ssl->session; | 
|  | 6269 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6270 | mbedtls_sha512_init( &sha512 ); | 
|  | 6271 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6272 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha384" ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6273 |  | 
| Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6274 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6275 |  | 
|  | 6276 | /* | 
|  | 6277 | * TLSv1.2: | 
|  | 6278 | *   hash = PRF( master, finished_label, | 
|  | 6279 | *               Hash( handshake ) )[0.11] | 
|  | 6280 | */ | 
|  | 6281 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6282 | #if !defined(MBEDTLS_SHA512_ALT) | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6283 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) | 
|  | 6284 | sha512.state, sizeof( sha512.state ) ); | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6285 | #endif | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6286 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6287 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6288 | ? "client finished" | 
|  | 6289 | : "server finished"; | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6290 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6291 | mbedtls_sha512_finish_ret( &sha512, padbuf ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6292 |  | 
| Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6293 | ssl->handshake->tls_prf( session->master, 48, sender, | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6294 | padbuf, 48, buf, len ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6295 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6296 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6297 |  | 
| Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6298 | mbedtls_sha512_free( &sha512 ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6299 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6300 | mbedtls_platform_zeroize(  padbuf, sizeof( padbuf ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6301 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6302 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6303 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6304 | #endif /* MBEDTLS_SHA512_C */ | 
|  | 6305 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6306 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6307 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6308 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6309 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6310 |  | 
|  | 6311 | /* | 
|  | 6312 | * Free our handshake params | 
|  | 6313 | */ | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6314 | mbedtls_ssl_handshake_free( ssl ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6315 | mbedtls_free( ssl->handshake ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6316 | ssl->handshake = NULL; | 
|  | 6317 |  | 
|  | 6318 | /* | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6319 | * Free the previous transform and swith in the current one | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6320 | */ | 
|  | 6321 | if( ssl->transform ) | 
|  | 6322 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6323 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 6324 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6325 | } | 
|  | 6326 | ssl->transform = ssl->transform_negotiate; | 
|  | 6327 | ssl->transform_negotiate = NULL; | 
|  | 6328 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6329 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6330 | } | 
|  | 6331 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6332 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6333 | { | 
|  | 6334 | int resume = ssl->handshake->resume; | 
|  | 6335 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6336 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6337 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6338 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6339 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6340 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6341 | ssl->renego_status =  MBEDTLS_SSL_RENEGOTIATION_DONE; | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6342 | ssl->renego_records_seen = 0; | 
|  | 6343 | } | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 6344 | #endif | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6345 |  | 
|  | 6346 | /* | 
|  | 6347 | * Free the previous session and switch in the current one | 
|  | 6348 | */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6349 | if( ssl->session ) | 
|  | 6350 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6351 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 6352 | /* RFC 7366 3.1: keep the EtM state */ | 
|  | 6353 | ssl->session_negotiate->encrypt_then_mac = | 
|  | 6354 | ssl->session->encrypt_then_mac; | 
|  | 6355 | #endif | 
|  | 6356 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6357 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 6358 | mbedtls_free( ssl->session ); | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6359 | } | 
|  | 6360 | ssl->session = ssl->session_negotiate; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6361 | ssl->session_negotiate = NULL; | 
|  | 6362 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6363 | /* | 
|  | 6364 | * Add cache entry | 
|  | 6365 | */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6366 | if( ssl->conf->f_set_cache != NULL && | 
| Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 6367 | ssl->session->id_len != 0 && | 
| Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 6368 | resume == 0 ) | 
|  | 6369 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 6370 | 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] | 6371 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); | 
| Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 6372 | } | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6373 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6374 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6375 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6376 | ssl->handshake->flight != NULL ) | 
|  | 6377 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 6378 | /* Cancel handshake timer */ | 
|  | 6379 | ssl_set_timer( ssl, 0 ); | 
|  | 6380 |  | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6381 | /* Keep last flight around in case we need to resend it: | 
|  | 6382 | * we need the handshake and transform structures for that */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6383 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6384 | } | 
|  | 6385 | else | 
|  | 6386 | #endif | 
|  | 6387 | ssl_handshake_wrapup_free_hs_transform( ssl ); | 
|  | 6388 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6389 | ssl->state++; | 
|  | 6390 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6391 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6392 | } | 
|  | 6393 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6394 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6395 | { | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6396 | int ret, hash_len; | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6397 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6398 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6399 |  | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 6400 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); | 
| Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 6401 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6402 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6403 |  | 
| Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 6404 | /* | 
|  | 6405 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites | 
|  | 6406 | * may define some other value. Currently (early 2016), no defined | 
|  | 6407 | * ciphersuite does this (and this is unlikely to change as activity has | 
|  | 6408 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. | 
|  | 6409 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6410 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6411 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6412 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6413 | ssl->verify_data_len = hash_len; | 
|  | 6414 | 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] | 6415 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6416 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6417 | ssl->out_msglen  = 4 + hash_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6418 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 6419 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_FINISHED; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6420 |  | 
|  | 6421 | /* | 
|  | 6422 | * In case of session resuming, invert the client and server | 
|  | 6423 | * ChangeCipherSpec messages order. | 
|  | 6424 | */ | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6425 | if( ssl->handshake->resume != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6426 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6427 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6428 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6429 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6430 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6431 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6432 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6433 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6434 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6435 | } | 
|  | 6436 | else | 
|  | 6437 | ssl->state++; | 
|  | 6438 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6439 | /* | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 6440 | * Switch to our negotiated transform and session parameters for outbound | 
|  | 6441 | * data. | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6442 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6443 | 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] | 6444 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6445 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6446 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6447 | { | 
|  | 6448 | unsigned char i; | 
|  | 6449 |  | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6450 | /* Remember current epoch settings for resending */ | 
|  | 6451 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6452 | 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] | 6453 |  | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6454 | /* Set sequence_number to zero */ | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6455 | memset( ssl->cur_out_ctr + 2, 0, 6 ); | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6456 |  | 
|  | 6457 | /* Increment epoch */ | 
|  | 6458 | for( i = 2; i > 0; i-- ) | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6459 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6460 | break; | 
|  | 6461 |  | 
|  | 6462 | /* The loop goes to its end iff the counter is wrapping */ | 
|  | 6463 | if( i == 0 ) | 
|  | 6464 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6465 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); | 
|  | 6466 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6467 | } | 
|  | 6468 | } | 
|  | 6469 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6470 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6471 | memset( ssl->cur_out_ctr, 0, 8 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6472 |  | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6473 | ssl->transform_out = ssl->transform_negotiate; | 
|  | 6474 | ssl->session_out = ssl->session_negotiate; | 
|  | 6475 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6476 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 6477 | if( mbedtls_ssl_hw_record_activate != NULL ) | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 6478 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6479 | 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] | 6480 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6481 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); | 
|  | 6482 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 6483 | } | 
|  | 6484 | } | 
|  | 6485 | #endif | 
|  | 6486 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6487 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6488 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6489 | mbedtls_ssl_send_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 6490 | #endif | 
|  | 6491 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6492 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6493 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6494 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6495 | return( ret ); | 
|  | 6496 | } | 
|  | 6497 |  | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 6498 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6499 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 6500 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
|  | 6501 | { | 
|  | 6502 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); | 
|  | 6503 | return( ret ); | 
|  | 6504 | } | 
|  | 6505 | #endif | 
|  | 6506 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6507 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6508 |  | 
|  | 6509 | return( 0 ); | 
|  | 6510 | } | 
|  | 6511 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6512 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 6513 | #define SSL_MAX_HASH_LEN 36 | 
|  | 6514 | #else | 
|  | 6515 | #define SSL_MAX_HASH_LEN 12 | 
|  | 6516 | #endif | 
|  | 6517 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6518 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6519 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6520 | int ret; | 
| Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 6521 | unsigned int hash_len; | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 6522 | unsigned char buf[SSL_MAX_HASH_LEN]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6523 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6524 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6525 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6526 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6527 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 6528 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6529 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6530 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6531 | return( ret ); | 
|  | 6532 | } | 
|  | 6533 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6534 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6535 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6536 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6537 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6538 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6539 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6540 | } | 
|  | 6541 |  | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 6542 | /* 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] | 6543 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 6544 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 6545 | hash_len = 36; | 
|  | 6546 | else | 
|  | 6547 | #endif | 
|  | 6548 | hash_len = 12; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6549 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6550 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || | 
|  | 6551 | ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6552 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6553 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6554 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6555 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6556 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6557 | } | 
|  | 6558 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6559 | 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] | 6560 | buf, hash_len ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6561 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6562 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); | 
| Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6563 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 6564 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6565 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6566 | } | 
|  | 6567 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6568 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6569 | ssl->verify_data_len = hash_len; | 
|  | 6570 | memcpy( ssl->peer_verify_data, buf, hash_len ); | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 6571 | #endif | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6572 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6573 | if( ssl->handshake->resume != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6574 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6575 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6576 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6577 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6578 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6579 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6580 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6581 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; | 
| Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6582 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6583 | } | 
|  | 6584 | else | 
|  | 6585 | ssl->state++; | 
|  | 6586 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6587 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6588 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6589 | mbedtls_ssl_recv_flight_completed( ssl ); | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6590 | #endif | 
|  | 6591 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6592 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6593 |  | 
|  | 6594 | return( 0 ); | 
|  | 6595 | } | 
|  | 6596 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6597 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6598 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6599 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6600 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6601 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 6602 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 6603 | mbedtls_md5_init(   &handshake->fin_md5  ); | 
|  | 6604 | mbedtls_sha1_init(   &handshake->fin_sha1 ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6605 | mbedtls_md5_starts_ret( &handshake->fin_md5  ); | 
|  | 6606 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6607 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6608 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 6609 | #if defined(MBEDTLS_SHA256_C) | 
|  | 6610 | mbedtls_sha256_init(   &handshake->fin_sha256    ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6611 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6612 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6613 | #if defined(MBEDTLS_SHA512_C) | 
|  | 6614 | mbedtls_sha512_init(   &handshake->fin_sha512    ); | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6615 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6616 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6617 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6618 |  | 
|  | 6619 | handshake->update_checksum = ssl_update_checksum_start; | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6620 |  | 
|  | 6621 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 6622 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 6623 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); | 
|  | 6624 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6625 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6626 | #if defined(MBEDTLS_DHM_C) | 
|  | 6627 | mbedtls_dhm_init( &handshake->dhm_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6628 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6629 | #if defined(MBEDTLS_ECDH_C) | 
|  | 6630 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6631 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 6632 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 6633 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 6634 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 6635 | handshake->ecjpake_cache = NULL; | 
|  | 6636 | handshake->ecjpake_cache_len = 0; | 
|  | 6637 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 6638 | #endif | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 6639 |  | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 6640 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 6641 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 6642 | #endif | 
|  | 6643 |  | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 6644 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 6645 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; | 
|  | 6646 | #endif | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6647 | } | 
|  | 6648 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6649 | static void ssl_transform_init( mbedtls_ssl_transform *transform ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6650 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6651 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 6652 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6653 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); | 
|  | 6654 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 6655 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6656 | mbedtls_md_init( &transform->md_ctx_enc ); | 
|  | 6657 | mbedtls_md_init( &transform->md_ctx_dec ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6658 | } | 
|  | 6659 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6660 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6661 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6662 | memset( session, 0, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6663 | } | 
|  | 6664 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6665 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6666 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6667 | /* Clear old handshake information if present */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6668 | if( ssl->transform_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6669 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6670 | if( ssl->session_negotiate ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6671 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6672 | if( ssl->handshake ) | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6673 | mbedtls_ssl_handshake_free( ssl ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6674 |  | 
|  | 6675 | /* | 
|  | 6676 | * Either the pointers are now NULL or cleared properly and can be freed. | 
|  | 6677 | * Now allocate missing structures. | 
|  | 6678 | */ | 
|  | 6679 | if( ssl->transform_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6680 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 6681 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6682 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6683 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6684 | if( ssl->session_negotiate == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6685 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 6686 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6687 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6688 |  | 
| Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 6689 | if( ssl->handshake == NULL ) | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6690 | { | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 6691 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 6692 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6693 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6694 | /* All pointers should exist and can be directly freed without issue */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6695 | if( ssl->handshake == NULL || | 
|  | 6696 | ssl->transform_negotiate == NULL || | 
|  | 6697 | ssl->session_negotiate == NULL ) | 
|  | 6698 | { | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 6699 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6700 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6701 | mbedtls_free( ssl->handshake ); | 
|  | 6702 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 6703 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6704 |  | 
|  | 6705 | ssl->handshake = NULL; | 
|  | 6706 | ssl->transform_negotiate = NULL; | 
|  | 6707 | ssl->session_negotiate = NULL; | 
|  | 6708 |  | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 6709 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6710 | } | 
|  | 6711 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6712 | /* Initialize structures */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6713 | mbedtls_ssl_session_init( ssl->session_negotiate ); | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6714 | ssl_transform_init( ssl->transform_negotiate ); | 
| Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 6715 | ssl_handshake_params_init( ssl->handshake ); | 
|  | 6716 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6717 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 6718 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6719 | { | 
|  | 6720 | ssl->handshake->alt_transform_out = ssl->transform_out; | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6721 |  | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 6722 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 6723 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; | 
|  | 6724 | else | 
|  | 6725 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 6726 |  | 
|  | 6727 | ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 6728 | } | 
| Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 6729 | #endif | 
|  | 6730 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6731 | return( 0 ); | 
|  | 6732 | } | 
|  | 6733 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6734 | #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] | 6735 | /* Dummy cookie callbacks for defaults */ | 
|  | 6736 | static int ssl_cookie_write_dummy( void *ctx, | 
|  | 6737 | unsigned char **p, unsigned char *end, | 
|  | 6738 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 6739 | { | 
|  | 6740 | ((void) ctx); | 
|  | 6741 | ((void) p); | 
|  | 6742 | ((void) end); | 
|  | 6743 | ((void) cli_id); | 
|  | 6744 | ((void) cli_id_len); | 
|  | 6745 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6746 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 6747 | } | 
|  | 6748 |  | 
|  | 6749 | static int ssl_cookie_check_dummy( void *ctx, | 
|  | 6750 | const unsigned char *cookie, size_t cookie_len, | 
|  | 6751 | const unsigned char *cli_id, size_t cli_id_len ) | 
|  | 6752 | { | 
|  | 6753 | ((void) ctx); | 
|  | 6754 | ((void) cookie); | 
|  | 6755 | ((void) cookie_len); | 
|  | 6756 | ((void) cli_id); | 
|  | 6757 | ((void) cli_id_len); | 
|  | 6758 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6759 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 6760 | } | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6761 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 6762 |  | 
| Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 6763 | /* Once ssl->out_hdr as the address of the beginning of the | 
|  | 6764 | * next outgoing record is set, deduce the other pointers. | 
|  | 6765 | * | 
|  | 6766 | * Note: For TLS, we save the implicit record sequence number | 
|  | 6767 | *       (entering MAC computation) in the 8 bytes before ssl->out_hdr, | 
|  | 6768 | *       and the caller has to make sure there's space for this. | 
|  | 6769 | */ | 
|  | 6770 |  | 
|  | 6771 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, | 
|  | 6772 | mbedtls_ssl_transform *transform ) | 
|  | 6773 | { | 
|  | 6774 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6775 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6776 | { | 
|  | 6777 | ssl->out_ctr = ssl->out_hdr +  3; | 
|  | 6778 | ssl->out_len = ssl->out_hdr + 11; | 
|  | 6779 | ssl->out_iv  = ssl->out_hdr + 13; | 
|  | 6780 | } | 
|  | 6781 | else | 
|  | 6782 | #endif | 
|  | 6783 | { | 
|  | 6784 | ssl->out_ctr = ssl->out_hdr - 8; | 
|  | 6785 | ssl->out_len = ssl->out_hdr + 3; | 
|  | 6786 | ssl->out_iv  = ssl->out_hdr + 5; | 
|  | 6787 | } | 
|  | 6788 |  | 
|  | 6789 | /* Adjust out_msg to make space for explicit IV, if used. */ | 
|  | 6790 | if( transform != NULL && | 
|  | 6791 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) | 
|  | 6792 | { | 
|  | 6793 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; | 
|  | 6794 | } | 
|  | 6795 | else | 
|  | 6796 | ssl->out_msg = ssl->out_iv; | 
|  | 6797 | } | 
|  | 6798 |  | 
|  | 6799 | /* Once ssl->in_hdr as the address of the beginning of the | 
|  | 6800 | * next incoming record is set, deduce the other pointers. | 
|  | 6801 | * | 
|  | 6802 | * Note: For TLS, we save the implicit record sequence number | 
|  | 6803 | *       (entering MAC computation) in the 8 bytes before ssl->in_hdr, | 
|  | 6804 | *       and the caller has to make sure there's space for this. | 
|  | 6805 | */ | 
|  | 6806 |  | 
|  | 6807 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl, | 
|  | 6808 | mbedtls_ssl_transform *transform ) | 
|  | 6809 | { | 
|  | 6810 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6811 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6812 | { | 
|  | 6813 | ssl->in_ctr = ssl->in_hdr +  3; | 
|  | 6814 | ssl->in_len = ssl->in_hdr + 11; | 
|  | 6815 | ssl->in_iv  = ssl->in_hdr + 13; | 
|  | 6816 | } | 
|  | 6817 | else | 
|  | 6818 | #endif | 
|  | 6819 | { | 
|  | 6820 | ssl->in_ctr = ssl->in_hdr - 8; | 
|  | 6821 | ssl->in_len = ssl->in_hdr + 3; | 
|  | 6822 | ssl->in_iv  = ssl->in_hdr + 5; | 
|  | 6823 | } | 
|  | 6824 |  | 
|  | 6825 | /* Offset in_msg from in_iv to allow space for explicit IV, if used. */ | 
|  | 6826 | if( transform != NULL && | 
|  | 6827 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) | 
|  | 6828 | { | 
|  | 6829 | ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen; | 
|  | 6830 | } | 
|  | 6831 | else | 
|  | 6832 | ssl->in_msg = ssl->in_iv; | 
|  | 6833 | } | 
|  | 6834 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6835 | /* | 
|  | 6836 | * Initialize an SSL context | 
|  | 6837 | */ | 
| Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 6838 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) | 
|  | 6839 | { | 
|  | 6840 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); | 
|  | 6841 | } | 
|  | 6842 |  | 
|  | 6843 | /* | 
|  | 6844 | * Setup an SSL context | 
|  | 6845 | */ | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 6846 |  | 
|  | 6847 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) | 
|  | 6848 | { | 
|  | 6849 | /* Set the incoming and outgoing record pointers. */ | 
|  | 6850 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 6851 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 6852 | { | 
|  | 6853 | ssl->out_hdr = ssl->out_buf; | 
|  | 6854 | ssl->in_hdr  = ssl->in_buf; | 
|  | 6855 | } | 
|  | 6856 | else | 
|  | 6857 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 6858 | { | 
|  | 6859 | ssl->out_hdr = ssl->out_buf + 8; | 
|  | 6860 | ssl->in_hdr  = ssl->in_buf  + 8; | 
|  | 6861 | } | 
|  | 6862 |  | 
|  | 6863 | /* Derive other internal pointers. */ | 
|  | 6864 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); | 
|  | 6865 | ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ ); | 
|  | 6866 | } | 
|  | 6867 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 6868 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 6869 | const mbedtls_ssl_config *conf ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6870 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6871 | int ret; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6872 |  | 
| Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 6873 | ssl->conf = conf; | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 6874 |  | 
|  | 6875 | /* | 
| Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 6876 | * Prepare base structures | 
| Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 6877 | */ | 
| k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 6878 |  | 
|  | 6879 | /* Set to NULL in case of an error condition */ | 
|  | 6880 | ssl->out_buf = NULL; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 6881 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6882 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); | 
|  | 6883 | if( ssl->in_buf == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6884 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6885 | 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] | 6886 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 6887 | goto error; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6888 | } | 
|  | 6889 |  | 
|  | 6890 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
|  | 6891 | if( ssl->out_buf == NULL ) | 
|  | 6892 | { | 
|  | 6893 | 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] | 6894 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 6895 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6896 | } | 
|  | 6897 |  | 
| Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 6898 | ssl_reset_in_out_pointers( ssl ); | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6899 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6900 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 6901 | goto error; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6902 |  | 
|  | 6903 | return( 0 ); | 
| k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 6904 |  | 
|  | 6905 | error: | 
|  | 6906 | mbedtls_free( ssl->in_buf ); | 
|  | 6907 | mbedtls_free( ssl->out_buf ); | 
|  | 6908 |  | 
|  | 6909 | ssl->conf = NULL; | 
|  | 6910 |  | 
|  | 6911 | ssl->in_buf = NULL; | 
|  | 6912 | ssl->out_buf = NULL; | 
|  | 6913 |  | 
|  | 6914 | ssl->in_hdr = NULL; | 
|  | 6915 | ssl->in_ctr = NULL; | 
|  | 6916 | ssl->in_len = NULL; | 
|  | 6917 | ssl->in_iv = NULL; | 
|  | 6918 | ssl->in_msg = NULL; | 
|  | 6919 |  | 
|  | 6920 | ssl->out_hdr = NULL; | 
|  | 6921 | ssl->out_ctr = NULL; | 
|  | 6922 | ssl->out_len = NULL; | 
|  | 6923 | ssl->out_iv = NULL; | 
|  | 6924 | ssl->out_msg = NULL; | 
|  | 6925 |  | 
| k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 6926 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6927 | } | 
|  | 6928 |  | 
|  | 6929 | /* | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6930 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 6931 | * all application-set variables, function pointers and data. | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 6932 | * | 
|  | 6933 | * If partial is non-zero, keep data in the input buffer and client ID. | 
|  | 6934 | * (Use when a DTLS client reconnects from the same port.) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6935 | */ | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 6936 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6937 | { | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6938 | int ret; | 
|  | 6939 |  | 
| Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 6940 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) ||     \ | 
|  | 6941 | !defined(MBEDTLS_SSL_SRV_C) | 
|  | 6942 | ((void) partial); | 
|  | 6943 | #endif | 
|  | 6944 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6945 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 6946 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 6947 | /* Cancel any possibly running timer */ | 
|  | 6948 | ssl_set_timer( ssl, 0 ); | 
|  | 6949 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6950 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 6951 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 6952 | ssl->renego_records_seen = 0; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6953 |  | 
|  | 6954 | ssl->verify_data_len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6955 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); | 
|  | 6956 | 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] | 6957 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6958 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6959 |  | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6960 | ssl->in_offt = NULL; | 
| Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 6961 | ssl_reset_in_out_pointers( ssl ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6962 |  | 
|  | 6963 | ssl->in_msgtype = 0; | 
|  | 6964 | ssl->in_msglen = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6965 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 6966 | ssl->next_record_offset = 0; | 
| Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6967 | ssl->in_epoch = 0; | 
| Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 6968 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6969 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 6970 | ssl_dtls_replay_reset( ssl ); | 
|  | 6971 | #endif | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6972 |  | 
|  | 6973 | ssl->in_hslen = 0; | 
|  | 6974 | ssl->nb_zero = 0; | 
| Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 6975 |  | 
|  | 6976 | ssl->keep_current_message = 0; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6977 |  | 
|  | 6978 | ssl->out_msgtype = 0; | 
|  | 6979 | ssl->out_msglen = 0; | 
|  | 6980 | ssl->out_left = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6981 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
|  | 6982 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 6983 | ssl->split_done = 0; | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6984 | #endif | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6985 |  | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 6986 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); | 
|  | 6987 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6988 | ssl->transform_in = NULL; | 
|  | 6989 | ssl->transform_out = NULL; | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 6990 |  | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 6991 | ssl->session_in = NULL; | 
|  | 6992 | ssl->session_out = NULL; | 
|  | 6993 |  | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6994 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 6995 |  | 
|  | 6996 | #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] | 6997 | if( partial == 0 ) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 6998 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ | 
|  | 6999 | { | 
|  | 7000 | ssl->in_left = 0; | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7001 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7002 | } | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7003 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7004 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 7005 | if( mbedtls_ssl_hw_record_reset != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7006 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7007 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); | 
|  | 7008 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7009 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7010 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); | 
|  | 7011 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7012 | } | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7013 | } | 
|  | 7014 | #endif | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7015 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7016 | if( ssl->transform ) | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7017 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7018 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 7019 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7020 | ssl->transform = NULL; | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7021 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7022 |  | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 7023 | if( ssl->session ) | 
|  | 7024 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7025 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 7026 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 7027 | ssl->session = NULL; | 
|  | 7028 | } | 
|  | 7029 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7030 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7031 | ssl->alpn_chosen = NULL; | 
|  | 7032 | #endif | 
|  | 7033 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 7034 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7035 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7036 | if( partial == 0 ) | 
| Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7037 | #endif | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7038 | { | 
|  | 7039 | mbedtls_free( ssl->cli_id ); | 
|  | 7040 | ssl->cli_id = NULL; | 
|  | 7041 | ssl->cli_id_len = 0; | 
|  | 7042 | } | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 7043 | #endif | 
|  | 7044 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7045 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 7046 | return( ret ); | 
| Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7047 |  | 
|  | 7048 | return( 0 ); | 
| Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7049 | } | 
|  | 7050 |  | 
| Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 7051 | /* | 
| Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7052 | * Reset an initialized and used SSL context for re-use while retaining | 
|  | 7053 | * all application-set variables, function pointers and data. | 
|  | 7054 | */ | 
|  | 7055 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) | 
|  | 7056 | { | 
|  | 7057 | return( ssl_session_reset_int( ssl, 0 ) ); | 
|  | 7058 | } | 
|  | 7059 |  | 
|  | 7060 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7061 | * SSL set accessors | 
|  | 7062 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7063 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7064 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7065 | conf->endpoint   = endpoint; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7066 | } | 
|  | 7067 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7068 | 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] | 7069 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7070 | conf->transport = transport; | 
| Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 7071 | } | 
|  | 7072 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7073 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7074 | 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] | 7075 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7076 | conf->anti_replay = mode; | 
| Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 7077 | } | 
|  | 7078 | #endif | 
|  | 7079 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7080 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7081 | 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] | 7082 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7083 | conf->badmac_limit = limit; | 
| Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 7084 | } | 
|  | 7085 | #endif | 
|  | 7086 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7087 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 7088 |  | 
| Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 7089 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, | 
|  | 7090 | unsigned allow_packing ) | 
| Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 7091 | { | 
|  | 7092 | ssl->disable_datagram_packing = !allow_packing; | 
|  | 7093 | } | 
|  | 7094 |  | 
|  | 7095 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, | 
|  | 7096 | uint32_t min, uint32_t max ) | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 7097 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7098 | conf->hs_timeout_min = min; | 
|  | 7099 | conf->hs_timeout_max = max; | 
| Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 7100 | } | 
|  | 7101 | #endif | 
|  | 7102 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7103 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7104 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7105 | conf->authmode   = authmode; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7106 | } | 
|  | 7107 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7108 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7109 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7110 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7111 | void *p_vrfy ) | 
|  | 7112 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7113 | conf->f_vrfy      = f_vrfy; | 
|  | 7114 | conf->p_vrfy      = p_vrfy; | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7115 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7116 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7117 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7118 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 7119 | int (*f_rng)(void *, unsigned char *, size_t), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7120 | void *p_rng ) | 
|  | 7121 | { | 
| Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 7122 | conf->f_rng      = f_rng; | 
|  | 7123 | conf->p_rng      = p_rng; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7124 | } | 
|  | 7125 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7126 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 7127 | void (*f_dbg)(void *, int, const char *, int, const char *), | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7128 | void  *p_dbg ) | 
|  | 7129 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7130 | conf->f_dbg      = f_dbg; | 
|  | 7131 | conf->p_dbg      = p_dbg; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7132 | } | 
|  | 7133 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7134 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7135 | void *p_bio, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 7136 | mbedtls_ssl_send_t *f_send, | 
|  | 7137 | mbedtls_ssl_recv_t *f_recv, | 
|  | 7138 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7139 | { | 
|  | 7140 | ssl->p_bio          = p_bio; | 
|  | 7141 | ssl->f_send         = f_send; | 
|  | 7142 | ssl->f_recv         = f_recv; | 
|  | 7143 | ssl->f_recv_timeout = f_recv_timeout; | 
| Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 7144 | } | 
|  | 7145 |  | 
| Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 7146 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 7147 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) | 
|  | 7148 | { | 
|  | 7149 | ssl->mtu = mtu; | 
|  | 7150 | } | 
|  | 7151 | #endif | 
|  | 7152 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7153 | 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] | 7154 | { | 
|  | 7155 | conf->read_timeout   = timeout; | 
| Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7156 | } | 
|  | 7157 |  | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7158 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, | 
|  | 7159 | void *p_timer, | 
| Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 7160 | mbedtls_ssl_set_timer_t *f_set_timer, | 
|  | 7161 | mbedtls_ssl_get_timer_t *f_get_timer ) | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7162 | { | 
|  | 7163 | ssl->p_timer        = p_timer; | 
|  | 7164 | ssl->f_set_timer    = f_set_timer; | 
|  | 7165 | ssl->f_get_timer    = f_get_timer; | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 7166 |  | 
|  | 7167 | /* Make sure we start with no timer running */ | 
|  | 7168 | ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7169 | } | 
|  | 7170 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7171 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7172 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7173 | void *p_cache, | 
|  | 7174 | int (*f_get_cache)(void *, mbedtls_ssl_session *), | 
|  | 7175 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7176 | { | 
| Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7177 | conf->p_cache = p_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7178 | conf->f_get_cache = f_get_cache; | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7179 | conf->f_set_cache = f_set_cache; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7180 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7181 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7182 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7183 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 7184 | 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] | 7185 | { | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7186 | int ret; | 
|  | 7187 |  | 
|  | 7188 | if( ssl == NULL || | 
|  | 7189 | session == NULL || | 
|  | 7190 | ssl->session_negotiate == NULL || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7191 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7192 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7193 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7194 | } | 
|  | 7195 |  | 
|  | 7196 | if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 ) | 
|  | 7197 | return( ret ); | 
|  | 7198 |  | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7199 | ssl->handshake->resume = 1; | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7200 |  | 
|  | 7201 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7202 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7203 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7204 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7205 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7206 | const int *ciphersuites ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7207 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7208 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; | 
|  | 7209 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; | 
|  | 7210 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; | 
|  | 7211 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 7212 | } | 
|  | 7213 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7214 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7215 | const int *ciphersuites, | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 7216 | int major, int minor ) | 
|  | 7217 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7218 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) | 
| Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 7219 | return; | 
|  | 7220 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7221 | 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] | 7222 | return; | 
|  | 7223 |  | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7224 | conf->ciphersuite_list[minor] = ciphersuites; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7225 | } | 
|  | 7226 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7227 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 7228 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, | 
| Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 7229 | const mbedtls_x509_crt_profile *profile ) | 
| Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 7230 | { | 
|  | 7231 | conf->cert_profile = profile; | 
|  | 7232 | } | 
|  | 7233 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7234 | /* Append a new keycert entry to a (possibly empty) list */ | 
|  | 7235 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, | 
|  | 7236 | mbedtls_x509_crt *cert, | 
|  | 7237 | mbedtls_pk_context *key ) | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7238 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7239 | mbedtls_ssl_key_cert *new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7240 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7241 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); | 
|  | 7242 | if( new_cert == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 7243 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7244 |  | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7245 | new_cert->cert = cert; | 
|  | 7246 | new_cert->key  = key; | 
|  | 7247 | new_cert->next = NULL; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7248 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7249 | /* Update head is the list was null, else add to the end */ | 
|  | 7250 | if( *head == NULL ) | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 7251 | { | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7252 | *head = new_cert; | 
| Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 7253 | } | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7254 | else | 
|  | 7255 | { | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7256 | mbedtls_ssl_key_cert *cur = *head; | 
|  | 7257 | while( cur->next != NULL ) | 
|  | 7258 | cur = cur->next; | 
| niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 7259 | cur->next = new_cert; | 
| Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 7260 | } | 
|  | 7261 |  | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7262 | return( 0 ); | 
|  | 7263 | } | 
|  | 7264 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7265 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 7266 | mbedtls_x509_crt *own_cert, | 
|  | 7267 | mbedtls_pk_context *pk_key ) | 
|  | 7268 | { | 
| Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 7269 | 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] | 7270 | } | 
|  | 7271 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7272 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 7273 | mbedtls_x509_crt *ca_chain, | 
|  | 7274 | mbedtls_x509_crl *ca_crl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7275 | { | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 7276 | conf->ca_chain   = ca_chain; | 
|  | 7277 | conf->ca_crl     = ca_crl; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7278 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7279 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 7280 |  | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 7281 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
|  | 7282 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, | 
|  | 7283 | mbedtls_x509_crt *own_cert, | 
|  | 7284 | mbedtls_pk_context *pk_key ) | 
|  | 7285 | { | 
|  | 7286 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, | 
|  | 7287 | own_cert, pk_key ) ); | 
|  | 7288 | } | 
| Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 7289 |  | 
|  | 7290 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, | 
|  | 7291 | mbedtls_x509_crt *ca_chain, | 
|  | 7292 | mbedtls_x509_crl *ca_crl ) | 
|  | 7293 | { | 
|  | 7294 | ssl->handshake->sni_ca_chain   = ca_chain; | 
|  | 7295 | ssl->handshake->sni_ca_crl     = ca_crl; | 
|  | 7296 | } | 
| Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7297 |  | 
|  | 7298 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, | 
|  | 7299 | int authmode ) | 
|  | 7300 | { | 
|  | 7301 | ssl->handshake->sni_authmode = authmode; | 
|  | 7302 | } | 
| Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 7303 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
|  | 7304 |  | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 7305 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 7306 | /* | 
|  | 7307 | * Set EC J-PAKE password for current handshake | 
|  | 7308 | */ | 
|  | 7309 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, | 
|  | 7310 | const unsigned char *pw, | 
|  | 7311 | size_t pw_len ) | 
|  | 7312 | { | 
|  | 7313 | mbedtls_ecjpake_role role; | 
|  | 7314 |  | 
| Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 7315 | if( ssl->handshake == NULL || ssl->conf == NULL ) | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 7316 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7317 |  | 
|  | 7318 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 7319 | role = MBEDTLS_ECJPAKE_SERVER; | 
|  | 7320 | else | 
|  | 7321 | role = MBEDTLS_ECJPAKE_CLIENT; | 
|  | 7322 |  | 
|  | 7323 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, | 
|  | 7324 | role, | 
|  | 7325 | MBEDTLS_MD_SHA256, | 
|  | 7326 | MBEDTLS_ECP_DP_SECP256R1, | 
|  | 7327 | pw, pw_len ) ); | 
|  | 7328 | } | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 7329 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 7330 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7331 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7332 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 7333 | const unsigned char *psk, size_t psk_len, | 
|  | 7334 | const unsigned char *psk_identity, size_t psk_identity_len ) | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 7335 | { | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7336 | if( psk == NULL || psk_identity == NULL ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7337 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7338 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7339 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 7340 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 7341 |  | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 7342 | /* Identity len will be encoded on two bytes */ | 
|  | 7343 | if( ( psk_identity_len >> 16 ) != 0 || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7344 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) | 
| Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 7345 | { | 
|  | 7346 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7347 | } | 
|  | 7348 |  | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7349 | if( conf->psk != NULL ) | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7350 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7351 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7352 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7353 | mbedtls_free( conf->psk ); | 
| Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 7354 | conf->psk = NULL; | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7355 | conf->psk_len = 0; | 
|  | 7356 | } | 
|  | 7357 | if( conf->psk_identity != NULL ) | 
|  | 7358 | { | 
|  | 7359 | mbedtls_free( conf->psk_identity ); | 
| Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 7360 | conf->psk_identity = NULL; | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7361 | conf->psk_identity_len = 0; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7362 | } | 
|  | 7363 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7364 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL || | 
|  | 7365 | ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL ) | 
| Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 7366 | { | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7367 | mbedtls_free( conf->psk ); | 
| Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 7368 | mbedtls_free( conf->psk_identity ); | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7369 | conf->psk = NULL; | 
| Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 7370 | conf->psk_identity = NULL; | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 7371 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 7372 | } | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7373 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7374 | conf->psk_len = psk_len; | 
|  | 7375 | conf->psk_identity_len = psk_identity_len; | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7376 |  | 
| Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 7377 | memcpy( conf->psk, psk, conf->psk_len ); | 
|  | 7378 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7379 |  | 
|  | 7380 | return( 0 ); | 
|  | 7381 | } | 
|  | 7382 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 7383 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, | 
|  | 7384 | const unsigned char *psk, size_t psk_len ) | 
|  | 7385 | { | 
|  | 7386 | if( psk == NULL || ssl->handshake == NULL ) | 
|  | 7387 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7388 |  | 
|  | 7389 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) | 
|  | 7390 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7391 |  | 
|  | 7392 | if( ssl->handshake->psk != NULL ) | 
| Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 7393 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7394 | mbedtls_platform_zeroize( ssl->handshake->psk, | 
|  | 7395 | ssl->handshake->psk_len ); | 
| Simon Butcher | 5b8d1d6 | 2015-10-04 22:06:51 +0100 | [diff] [blame] | 7396 | mbedtls_free( ssl->handshake->psk ); | 
| Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 7397 | ssl->handshake->psk_len = 0; | 
| Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 7398 | } | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 7399 |  | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7400 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 7401 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 7402 |  | 
|  | 7403 | ssl->handshake->psk_len = psk_len; | 
|  | 7404 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); | 
|  | 7405 |  | 
|  | 7406 | return( 0 ); | 
|  | 7407 | } | 
|  | 7408 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7409 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7410 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, | 
| Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 7411 | size_t), | 
|  | 7412 | void *p_psk ) | 
|  | 7413 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7414 | conf->f_psk = f_psk; | 
|  | 7415 | conf->p_psk = p_psk; | 
| Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 7416 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7417 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ | 
| Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 7418 |  | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 7419 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 7420 |  | 
|  | 7421 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7422 | 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] | 7423 | { | 
|  | 7424 | int ret; | 
|  | 7425 |  | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 7426 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || | 
|  | 7427 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) | 
|  | 7428 | { | 
|  | 7429 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 7430 | mbedtls_mpi_free( &conf->dhm_G ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7431 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 7432 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7433 |  | 
|  | 7434 | return( 0 ); | 
|  | 7435 | } | 
| Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 7436 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7437 |  | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 7438 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, | 
|  | 7439 | const unsigned char *dhm_P, size_t P_len, | 
|  | 7440 | const unsigned char *dhm_G, size_t G_len ) | 
|  | 7441 | { | 
|  | 7442 | int ret; | 
|  | 7443 |  | 
|  | 7444 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || | 
|  | 7445 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) | 
|  | 7446 | { | 
|  | 7447 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 7448 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 7449 | return( ret ); | 
|  | 7450 | } | 
|  | 7451 |  | 
|  | 7452 | return( 0 ); | 
|  | 7453 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7454 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7455 | 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] | 7456 | { | 
|  | 7457 | int ret; | 
|  | 7458 |  | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 7459 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || | 
|  | 7460 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) | 
|  | 7461 | { | 
|  | 7462 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 7463 | mbedtls_mpi_free( &conf->dhm_G ); | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 7464 | return( ret ); | 
| Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 7465 | } | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 7466 |  | 
|  | 7467 | return( 0 ); | 
|  | 7468 | } | 
| Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 7469 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 7470 |  | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 7471 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 7472 | /* | 
|  | 7473 | * Set the minimum length for Diffie-Hellman parameters | 
|  | 7474 | */ | 
|  | 7475 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, | 
|  | 7476 | unsigned int bitlen ) | 
|  | 7477 | { | 
|  | 7478 | conf->dhm_min_bitlen = bitlen; | 
|  | 7479 | } | 
|  | 7480 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ | 
|  | 7481 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 7482 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 7483 | /* | 
|  | 7484 | * Set allowed/preferred hashes for handshake signatures | 
|  | 7485 | */ | 
|  | 7486 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, | 
|  | 7487 | const int *hashes ) | 
|  | 7488 | { | 
|  | 7489 | conf->sig_hashes = hashes; | 
|  | 7490 | } | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7491 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 7492 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7493 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 7494 | /* | 
|  | 7495 | * Set the allowed elliptic curves | 
|  | 7496 | */ | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7497 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7498 | const mbedtls_ecp_group_id *curve_list ) | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 7499 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7500 | conf->curve_list = curve_list; | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 7501 | } | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7502 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 7503 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 7504 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7505 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7506 | { | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7507 | /* Initialize to suppress unnecessary compiler warning */ | 
|  | 7508 | size_t hostname_len = 0; | 
|  | 7509 |  | 
|  | 7510 | /* Check if new hostname is valid before | 
|  | 7511 | * making any change to current one */ | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7512 | if( hostname != NULL ) | 
|  | 7513 | { | 
|  | 7514 | hostname_len = strlen( hostname ); | 
|  | 7515 |  | 
|  | 7516 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) | 
|  | 7517 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 7518 | } | 
|  | 7519 |  | 
|  | 7520 | /* Now it's clear that we will overwrite the old hostname, | 
|  | 7521 | * so we can free it safely */ | 
|  | 7522 |  | 
|  | 7523 | if( ssl->hostname != NULL ) | 
|  | 7524 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7525 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7526 | mbedtls_free( ssl->hostname ); | 
|  | 7527 | } | 
|  | 7528 |  | 
|  | 7529 | /* Passing NULL as hostname shall clear the old one */ | 
| Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 7530 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7531 | if( hostname == NULL ) | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7532 | { | 
|  | 7533 | ssl->hostname = NULL; | 
|  | 7534 | } | 
|  | 7535 | else | 
|  | 7536 | { | 
|  | 7537 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7538 | if( ssl->hostname == NULL ) | 
|  | 7539 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 7540 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7541 | memcpy( ssl->hostname, hostname, hostname_len ); | 
| Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 7542 |  | 
| Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 7543 | ssl->hostname[hostname_len] = '\0'; | 
|  | 7544 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7545 |  | 
|  | 7546 | return( 0 ); | 
|  | 7547 | } | 
| Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 7548 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7549 |  | 
| Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 7550 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7551 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7552 | int (*f_sni)(void *, mbedtls_ssl_context *, | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 7553 | const unsigned char *, size_t), | 
|  | 7554 | void *p_sni ) | 
|  | 7555 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7556 | conf->f_sni = f_sni; | 
|  | 7557 | conf->p_sni = p_sni; | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 7558 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7559 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ | 
| Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 7560 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7561 | #if defined(MBEDTLS_SSL_ALPN) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7562 | 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] | 7563 | { | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 7564 | size_t cur_len, tot_len; | 
|  | 7565 | const char **p; | 
|  | 7566 |  | 
|  | 7567 | /* | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 7568 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings | 
|  | 7569 | * MUST NOT be truncated." | 
|  | 7570 | * We check lengths now rather than later. | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 7571 | */ | 
|  | 7572 | tot_len = 0; | 
|  | 7573 | for( p = protos; *p != NULL; p++ ) | 
|  | 7574 | { | 
|  | 7575 | cur_len = strlen( *p ); | 
|  | 7576 | tot_len += cur_len; | 
|  | 7577 |  | 
|  | 7578 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7579 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 7580 | } | 
|  | 7581 |  | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7582 | conf->alpn_list = protos; | 
| Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 7583 |  | 
|  | 7584 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7585 | } | 
|  | 7586 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7587 | 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] | 7588 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 7589 | return( ssl->alpn_chosen ); | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7590 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7591 | #endif /* MBEDTLS_SSL_ALPN */ | 
| Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7592 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7593 | 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] | 7594 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7595 | conf->max_major_ver = major; | 
|  | 7596 | conf->max_minor_ver = minor; | 
| Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 7597 | } | 
|  | 7598 |  | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7599 | 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] | 7600 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7601 | conf->min_major_ver = major; | 
|  | 7602 | conf->min_minor_ver = minor; | 
| Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 7603 | } | 
|  | 7604 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7605 | #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] | 7606 | 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] | 7607 | { | 
| Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 7608 | conf->fallback = fallback; | 
| Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 7609 | } | 
|  | 7610 | #endif | 
|  | 7611 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 7612 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 7613 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, | 
|  | 7614 | char cert_req_ca_list ) | 
|  | 7615 | { | 
|  | 7616 | conf->cert_req_ca_list = cert_req_ca_list; | 
|  | 7617 | } | 
|  | 7618 | #endif | 
|  | 7619 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7620 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7621 | 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] | 7622 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7623 | conf->encrypt_then_mac = etm; | 
| Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 7624 | } | 
|  | 7625 | #endif | 
|  | 7626 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7627 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7628 | 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] | 7629 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7630 | conf->extended_ms = ems; | 
| Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 7631 | } | 
|  | 7632 | #endif | 
|  | 7633 |  | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 7634 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7635 | 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] | 7636 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7637 | conf->arc4_disabled = arc4; | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 7638 | } | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 7639 | #endif | 
| Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 7640 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7641 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7642 | 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] | 7643 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7644 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7645 | 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] | 7646 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7647 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 7648 | } | 
|  | 7649 |  | 
| Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 7650 | conf->mfl_code = mfl_code; | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 7651 |  | 
|  | 7652 | return( 0 ); | 
|  | 7653 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7654 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
| Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 7655 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7656 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) | 
| Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7657 | 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] | 7658 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7659 | conf->trunc_hmac = truncate; | 
| Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 7660 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7661 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ | 
| Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 7662 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7663 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7664 | 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] | 7665 | { | 
| Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 7666 | conf->cbc_record_splitting = split; | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 7667 | } | 
|  | 7668 | #endif | 
|  | 7669 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7670 | 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] | 7671 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7672 | conf->allow_legacy_renegotiation = allow_legacy; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7673 | } | 
|  | 7674 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7675 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7676 | 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] | 7677 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7678 | conf->disable_renegotiation = renegotiation; | 
| Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7679 | } | 
|  | 7680 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7681 | 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] | 7682 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7683 | conf->renego_max_records = max_records; | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 7684 | } | 
|  | 7685 |  | 
| Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7686 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 7687 | const unsigned char period[8] ) | 
|  | 7688 | { | 
| Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7689 | memcpy( conf->renego_period, period, 8 ); | 
| Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 7690 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7691 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7692 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7693 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 7694 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 7695 | 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] | 7696 | { | 
| Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 7697 | conf->session_tickets = use_tickets; | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 7698 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 7699 | #endif | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 7700 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 7701 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 7702 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, | 
|  | 7703 | mbedtls_ssl_ticket_write_t *f_ticket_write, | 
|  | 7704 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, | 
|  | 7705 | void *p_ticket ) | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 7706 | { | 
| Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 7707 | conf->f_ticket_write = f_ticket_write; | 
|  | 7708 | conf->f_ticket_parse = f_ticket_parse; | 
|  | 7709 | conf->p_ticket       = p_ticket; | 
| Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 7710 | } | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 7711 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7712 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ | 
| Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 7713 |  | 
| Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 7714 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) | 
|  | 7715 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, | 
|  | 7716 | mbedtls_ssl_export_keys_t *f_export_keys, | 
|  | 7717 | void *p_export_keys ) | 
|  | 7718 | { | 
|  | 7719 | conf->f_export_keys = f_export_keys; | 
|  | 7720 | conf->p_export_keys = p_export_keys; | 
|  | 7721 | } | 
|  | 7722 | #endif | 
|  | 7723 |  | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 7724 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 7725 | void mbedtls_ssl_conf_async_private_cb( | 
|  | 7726 | mbedtls_ssl_config *conf, | 
|  | 7727 | mbedtls_ssl_async_sign_t *f_async_sign, | 
|  | 7728 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, | 
|  | 7729 | mbedtls_ssl_async_resume_t *f_async_resume, | 
|  | 7730 | mbedtls_ssl_async_cancel_t *f_async_cancel, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 7731 | void *async_config_data ) | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 7732 | { | 
|  | 7733 | conf->f_async_sign_start = f_async_sign; | 
|  | 7734 | conf->f_async_decrypt_start = f_async_decrypt; | 
|  | 7735 | conf->f_async_resume = f_async_resume; | 
|  | 7736 | conf->f_async_cancel = f_async_cancel; | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 7737 | conf->p_async_config_data = async_config_data; | 
|  | 7738 | } | 
|  | 7739 |  | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 7740 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) | 
|  | 7741 | { | 
|  | 7742 | return( conf->p_async_config_data ); | 
|  | 7743 | } | 
|  | 7744 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 7745 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 7746 | { | 
|  | 7747 | if( ssl->handshake == NULL ) | 
|  | 7748 | return( NULL ); | 
|  | 7749 | else | 
|  | 7750 | return( ssl->handshake->user_async_ctx ); | 
|  | 7751 | } | 
|  | 7752 |  | 
| Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 7753 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 7754 | void *ctx ) | 
|  | 7755 | { | 
|  | 7756 | if( ssl->handshake != NULL ) | 
|  | 7757 | ssl->handshake->user_async_ctx = ctx; | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 7758 | } | 
| Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 7759 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
| Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 7760 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7761 | /* | 
|  | 7762 | * SSL get accessors | 
|  | 7763 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7764 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7765 | { | 
|  | 7766 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); | 
|  | 7767 | } | 
|  | 7768 |  | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 7769 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) | 
|  | 7770 | { | 
|  | 7771 | /* | 
|  | 7772 | * Case A: We're currently holding back | 
|  | 7773 | * a message for further processing. | 
|  | 7774 | */ | 
|  | 7775 |  | 
|  | 7776 | if( ssl->keep_current_message == 1 ) | 
|  | 7777 | { | 
| Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 7778 | 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] | 7779 | return( 1 ); | 
|  | 7780 | } | 
|  | 7781 |  | 
|  | 7782 | /* | 
|  | 7783 | * Case B: Further records are pending in the current datagram. | 
|  | 7784 | */ | 
|  | 7785 |  | 
|  | 7786 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 7787 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 7788 | ssl->in_left > ssl->next_record_offset ) | 
|  | 7789 | { | 
| Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 7790 | 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] | 7791 | return( 1 ); | 
|  | 7792 | } | 
|  | 7793 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 7794 |  | 
|  | 7795 | /* | 
|  | 7796 | * Case C: A handshake message is being processed. | 
|  | 7797 | */ | 
|  | 7798 |  | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 7799 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) | 
|  | 7800 | { | 
| Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 7801 | 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] | 7802 | return( 1 ); | 
|  | 7803 | } | 
|  | 7804 |  | 
|  | 7805 | /* | 
|  | 7806 | * Case D: An application data message is being processed | 
|  | 7807 | */ | 
|  | 7808 | if( ssl->in_offt != NULL ) | 
|  | 7809 | { | 
| Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 7810 | 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] | 7811 | return( 1 ); | 
|  | 7812 | } | 
|  | 7813 |  | 
|  | 7814 | /* | 
|  | 7815 | * In all other cases, the rest of the message can be dropped. | 
| Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 7816 | * As in ssl_get_next_record, this needs to be adapted if | 
| Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 7817 | * we implement support for multiple alerts in single records. | 
|  | 7818 | */ | 
|  | 7819 |  | 
|  | 7820 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); | 
|  | 7821 | return( 0 ); | 
|  | 7822 | } | 
|  | 7823 |  | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7824 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7825 | { | 
| Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 7826 | if( ssl->session != NULL ) | 
|  | 7827 | return( ssl->session->verify_result ); | 
|  | 7828 |  | 
|  | 7829 | if( ssl->session_negotiate != NULL ) | 
|  | 7830 | return( ssl->session_negotiate->verify_result ); | 
|  | 7831 |  | 
| Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 7832 | return( 0xFFFFFFFF ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7833 | } | 
|  | 7834 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7835 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 7836 | { | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 7837 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 7838 | return( NULL ); | 
| Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 7839 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7840 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 7841 | } | 
|  | 7842 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7843 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7844 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7845 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7846 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 7847 | { | 
|  | 7848 | switch( ssl->minor_ver ) | 
|  | 7849 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7850 | case MBEDTLS_SSL_MINOR_VERSION_2: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 7851 | return( "DTLSv1.0" ); | 
|  | 7852 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7853 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 7854 | return( "DTLSv1.2" ); | 
|  | 7855 |  | 
|  | 7856 | default: | 
|  | 7857 | return( "unknown (DTLS)" ); | 
|  | 7858 | } | 
|  | 7859 | } | 
|  | 7860 | #endif | 
|  | 7861 |  | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7862 | switch( ssl->minor_ver ) | 
|  | 7863 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7864 | case MBEDTLS_SSL_MINOR_VERSION_0: | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7865 | return( "SSLv3.0" ); | 
|  | 7866 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7867 | case MBEDTLS_SSL_MINOR_VERSION_1: | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7868 | return( "TLSv1.0" ); | 
|  | 7869 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7870 | case MBEDTLS_SSL_MINOR_VERSION_2: | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7871 | return( "TLSv1.1" ); | 
|  | 7872 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7873 | case MBEDTLS_SSL_MINOR_VERSION_3: | 
| Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7874 | return( "TLSv1.2" ); | 
|  | 7875 |  | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7876 | default: | 
| Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 7877 | return( "unknown" ); | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7878 | } | 
| Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 7879 | } | 
|  | 7880 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7881 | 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] | 7882 | { | 
| Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 7883 | size_t transform_expansion = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7884 | const mbedtls_ssl_transform *transform = ssl->transform_out; | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 7885 | unsigned block_size; | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 7886 |  | 
| Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 7887 | if( transform == NULL ) | 
|  | 7888 | return( (int) mbedtls_ssl_hdr_len( ssl ) ); | 
|  | 7889 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7890 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
|  | 7891 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) | 
|  | 7892 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 7893 | #endif | 
|  | 7894 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7895 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 7896 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7897 | case MBEDTLS_MODE_GCM: | 
|  | 7898 | case MBEDTLS_MODE_CCM: | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 7899 | case MBEDTLS_MODE_CHACHAPOLY: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7900 | case MBEDTLS_MODE_STREAM: | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 7901 | transform_expansion = transform->minlen; | 
|  | 7902 | break; | 
|  | 7903 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7904 | case MBEDTLS_MODE_CBC: | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 7905 |  | 
|  | 7906 | block_size = mbedtls_cipher_get_block_size( | 
|  | 7907 | &transform->cipher_ctx_enc ); | 
|  | 7908 |  | 
| Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 7909 | /* Expansion due to the addition of the MAC. */ | 
|  | 7910 | transform_expansion += transform->maclen; | 
|  | 7911 |  | 
|  | 7912 | /* Expansion due to the addition of CBC padding; | 
|  | 7913 | * Theoretically up to 256 bytes, but we never use | 
|  | 7914 | * more than the block size of the underlying cipher. */ | 
|  | 7915 | transform_expansion += block_size; | 
|  | 7916 |  | 
|  | 7917 | /* For TLS 1.1 or higher, an explicit IV is added | 
|  | 7918 | * after the record header. */ | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 7919 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 7920 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) | 
| Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 7921 | transform_expansion += block_size; | 
| Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 7922 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 7923 |  | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 7924 | break; | 
|  | 7925 |  | 
|  | 7926 | default: | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 7927 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7928 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 7929 | } | 
|  | 7930 |  | 
| Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 7931 | return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) ); | 
| Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 7932 | } | 
|  | 7933 |  | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 7934 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 7935 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) | 
|  | 7936 | { | 
|  | 7937 | size_t max_len; | 
|  | 7938 |  | 
|  | 7939 | /* | 
|  | 7940 | * Assume mfl_code is correct since it was checked when set | 
|  | 7941 | */ | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7942 | 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] | 7943 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 7944 | /* Check if a smaller max length was negotiated */ | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 7945 | if( ssl->session_out != NULL && | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7946 | 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] | 7947 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7948 | 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] | 7949 | } | 
|  | 7950 |  | 
| Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 7951 | /* During a handshake, use the value being negotiated */ | 
|  | 7952 | if( ssl->session_negotiate != NULL && | 
|  | 7953 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) | 
|  | 7954 | { | 
|  | 7955 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); | 
|  | 7956 | } | 
|  | 7957 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 7958 | return( max_len ); | 
| Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 7959 | } | 
|  | 7960 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ | 
|  | 7961 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 7962 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 7963 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) | 
|  | 7964 | { | 
| Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 7965 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ | 
|  | 7966 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
|  | 7967 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || | 
|  | 7968 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) | 
|  | 7969 | return ( 0 ); | 
|  | 7970 |  | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 7971 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) | 
|  | 7972 | return( ssl->mtu ); | 
|  | 7973 |  | 
|  | 7974 | if( ssl->mtu == 0 ) | 
|  | 7975 | return( ssl->handshake->mtu ); | 
|  | 7976 |  | 
|  | 7977 | return( ssl->mtu < ssl->handshake->mtu ? | 
|  | 7978 | ssl->mtu : ssl->handshake->mtu ); | 
|  | 7979 | } | 
|  | 7980 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 7981 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 7982 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) | 
|  | 7983 | { | 
|  | 7984 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; | 
|  | 7985 |  | 
| Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 7986 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ | 
|  | 7987 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 7988 | (void) ssl; | 
|  | 7989 | #endif | 
|  | 7990 |  | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 7991 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) | 
|  | 7992 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); | 
|  | 7993 |  | 
|  | 7994 | if( max_len > mfl ) | 
|  | 7995 | max_len = mfl; | 
|  | 7996 | #endif | 
|  | 7997 |  | 
|  | 7998 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 7999 | if( ssl_get_current_mtu( ssl ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8000 | { | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8001 | const size_t mtu = ssl_get_current_mtu( ssl ); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8002 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); | 
|  | 8003 | const size_t overhead = (size_t) ret; | 
|  | 8004 |  | 
|  | 8005 | if( ret < 0 ) | 
|  | 8006 | return( ret ); | 
|  | 8007 |  | 
|  | 8008 | if( mtu <= overhead ) | 
|  | 8009 | { | 
|  | 8010 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); | 
|  | 8011 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); | 
|  | 8012 | } | 
|  | 8013 |  | 
|  | 8014 | if( max_len > mtu - overhead ) | 
|  | 8015 | max_len = mtu - overhead; | 
|  | 8016 | } | 
| Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8017 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8018 |  | 
| Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 8019 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) &&        \ | 
|  | 8020 | !defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 8021 | ((void) ssl); | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8022 | #endif | 
|  | 8023 |  | 
|  | 8024 | return( (int) max_len ); | 
|  | 8025 | } | 
|  | 8026 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8027 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 8028 | 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] | 8029 | { | 
|  | 8030 | if( ssl == NULL || ssl->session == NULL ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8031 | return( NULL ); | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8032 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8033 | return( ssl->session->peer_cert ); | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8034 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8035 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8036 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8037 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 8038 | 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] | 8039 | { | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8040 | if( ssl == NULL || | 
|  | 8041 | dst == NULL || | 
|  | 8042 | ssl->session == NULL || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8043 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8044 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8045 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8046 | } | 
|  | 8047 |  | 
| Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8048 | return( ssl_session_copy( dst, ssl->session ) ); | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8049 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8050 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8051 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8052 | /* | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8053 | * Perform a single step of the SSL handshake | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8054 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8055 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8056 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8057 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8058 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8059 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 8060 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8061 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8062 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8063 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8064 | ret = mbedtls_ssl_handshake_client_step( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8065 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8066 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8067 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8068 | ret = mbedtls_ssl_handshake_server_step( ssl ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8069 | #endif | 
|  | 8070 |  | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8071 | return( ret ); | 
|  | 8072 | } | 
|  | 8073 |  | 
|  | 8074 | /* | 
|  | 8075 | * Perform the SSL handshake | 
|  | 8076 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8077 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8078 | { | 
|  | 8079 | int ret = 0; | 
|  | 8080 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8081 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 8082 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8083 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8084 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8085 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8086 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8087 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8088 | ret = mbedtls_ssl_handshake_step( ssl ); | 
| Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 8089 |  | 
|  | 8090 | if( ret != 0 ) | 
|  | 8091 | break; | 
|  | 8092 | } | 
|  | 8093 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8094 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8095 |  | 
|  | 8096 | return( ret ); | 
|  | 8097 | } | 
|  | 8098 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8099 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 8100 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8101 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8102 | * Write HelloRequest to request renegotiation on server | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8103 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8104 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8105 | { | 
|  | 8106 | int ret; | 
|  | 8107 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8108 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8109 |  | 
|  | 8110 | ssl->out_msglen  = 4; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8111 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; | 
|  | 8112 | ssl->out_msg[0]  = MBEDTLS_SSL_HS_HELLO_REQUEST; | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8113 |  | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8114 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8115 | { | 
| Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 8116 | 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] | 8117 | return( ret ); | 
|  | 8118 | } | 
|  | 8119 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8120 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8121 |  | 
|  | 8122 | return( 0 ); | 
|  | 8123 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8124 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8125 |  | 
|  | 8126 | /* | 
|  | 8127 | * Actually renegotiate current connection, triggered by either: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8128 | * - any side: calling mbedtls_ssl_renegotiate(), | 
|  | 8129 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), | 
|  | 8130 | * - 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] | 8131 | *   the initial handshake is completed. | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8132 | * 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] | 8133 | * 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] | 8134 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8135 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8136 | { | 
|  | 8137 | int ret; | 
|  | 8138 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8139 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8140 |  | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8141 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) | 
|  | 8142 | return( ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8143 |  | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 8144 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and | 
|  | 8145 | * the ServerHello will have message_seq = 1" */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8146 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8147 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8148 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 8149 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8150 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 8151 | ssl->handshake->out_msg_seq = 1; | 
|  | 8152 | else | 
|  | 8153 | ssl->handshake->in_msg_seq = 1; | 
| Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 8154 | } | 
|  | 8155 | #endif | 
|  | 8156 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8157 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; | 
|  | 8158 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8159 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8160 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8161 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8162 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8163 | return( ret ); | 
|  | 8164 | } | 
|  | 8165 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8166 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8167 |  | 
|  | 8168 | return( 0 ); | 
|  | 8169 | } | 
|  | 8170 |  | 
|  | 8171 | /* | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8172 | * Renegotiate current connection on client, | 
|  | 8173 | * or request renegotiation on server | 
|  | 8174 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8175 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8176 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8177 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8178 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8179 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 8180 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8181 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8182 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8183 | /* On server, just send the request */ | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8184 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8185 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8186 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 8187 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8188 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8189 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 8190 |  | 
|  | 8191 | /* Did we already try/start sending HelloRequest? */ | 
|  | 8192 | if( ssl->out_left != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8193 | return( mbedtls_ssl_flush_output( ssl ) ); | 
| Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 8194 |  | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8195 | return( ssl_write_hello_request( ssl ) ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8196 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8197 | #endif /* MBEDTLS_SSL_SRV_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8198 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8199 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8200 | /* | 
|  | 8201 | * On client, either start the renegotiation process or, | 
|  | 8202 | * if already in progress, continue the handshake | 
|  | 8203 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8204 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8205 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8206 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
|  | 8207 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8208 |  | 
|  | 8209 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) | 
|  | 8210 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8211 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8212 | return( ret ); | 
|  | 8213 | } | 
|  | 8214 | } | 
|  | 8215 | else | 
|  | 8216 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8217 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8218 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8219 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8220 | return( ret ); | 
|  | 8221 | } | 
|  | 8222 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8223 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 8224 |  | 
| Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 8225 | return( ret ); | 
| Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 8226 | } | 
|  | 8227 |  | 
|  | 8228 | /* | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8229 | * Check record counters and renegotiate if they're above the limit. | 
|  | 8230 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8231 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8232 | { | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 8233 | size_t ep_len = ssl_ep_len( ssl ); | 
|  | 8234 | int in_ctr_cmp; | 
|  | 8235 | int out_ctr_cmp; | 
|  | 8236 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8237 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || | 
|  | 8238 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8239 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8240 | { | 
|  | 8241 | return( 0 ); | 
|  | 8242 | } | 
|  | 8243 |  | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 8244 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, | 
|  | 8245 | ssl->conf->renego_period + ep_len, 8 - ep_len ); | 
| Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8246 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 8247 | ssl->conf->renego_period + ep_len, 8 - ep_len ); | 
|  | 8248 |  | 
|  | 8249 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8250 | { | 
|  | 8251 | return( 0 ); | 
|  | 8252 | } | 
|  | 8253 |  | 
| Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 8254 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8255 | return( mbedtls_ssl_renegotiate( ssl ) ); | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8256 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8257 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8258 |  | 
|  | 8259 | /* | 
|  | 8260 | * Receive application data decrypted from the SSL layer | 
|  | 8261 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8262 | 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] | 8263 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8264 | int ret; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 8265 | size_t n; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8266 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8267 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 8268 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8269 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8270 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8271 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8272 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8273 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8274 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8275 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8276 | return( ret ); | 
|  | 8277 |  | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8278 | if( ssl->handshake != NULL && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8279 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8280 | { | 
| Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 8281 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8282 | return( ret ); | 
|  | 8283 | } | 
| Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 8284 | } | 
|  | 8285 | #endif | 
|  | 8286 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8287 | /* | 
|  | 8288 | * Check if renegotiation is necessary and/or handshake is | 
|  | 8289 | * in process. If yes, perform/continue, and fall through | 
|  | 8290 | * if an unexpected packet is received while the client | 
|  | 8291 | * is waiting for the ServerHello. | 
|  | 8292 | * | 
|  | 8293 | * (There is no equivalent to the last condition on | 
|  | 8294 | *  the server-side as it is not treated as within | 
|  | 8295 | *  a handshake while waiting for the ClientHello | 
|  | 8296 | *  after a renegotiation request.) | 
|  | 8297 | */ | 
|  | 8298 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8299 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8300 | ret = ssl_check_ctr_renegotiate( ssl ); | 
|  | 8301 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && | 
|  | 8302 | ret != 0 ) | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8303 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8304 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); | 
| Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 8305 | return( ret ); | 
|  | 8306 | } | 
|  | 8307 | #endif | 
|  | 8308 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8309 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8310 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8311 | ret = mbedtls_ssl_handshake( ssl ); | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8312 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && | 
|  | 8313 | ret != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8314 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8315 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8316 | return( ret ); | 
|  | 8317 | } | 
|  | 8318 | } | 
|  | 8319 |  | 
| Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 8320 | /* Loop as long as no application data record is available */ | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 8321 | while( ssl->in_offt == NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8322 | { | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8323 | /* Start timer if not already running */ | 
| Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 8324 | if( ssl->f_get_timer != NULL && | 
|  | 8325 | ssl->f_get_timer( ssl->p_timer ) == -1 ) | 
|  | 8326 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8327 | ssl_set_timer( ssl, ssl->conf->read_timeout ); | 
| Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 8328 | } | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8329 |  | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 8330 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8331 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8332 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) | 
|  | 8333 | return( 0 ); | 
| Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 8334 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8335 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
|  | 8336 | return( ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8337 | } | 
|  | 8338 |  | 
|  | 8339 | if( ssl->in_msglen  == 0 && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8340 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8341 | { | 
|  | 8342 | /* | 
|  | 8343 | * OpenSSL sends empty messages to randomize the IV | 
|  | 8344 | */ | 
| Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 8345 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8346 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8347 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) | 
| Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 8348 | return( 0 ); | 
|  | 8349 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8350 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8351 | return( ret ); | 
|  | 8352 | } | 
|  | 8353 | } | 
|  | 8354 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8355 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8356 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8358 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8359 | /* | 
|  | 8360 | * - For client-side, expect SERVER_HELLO_REQUEST. | 
|  | 8361 | * - For server-side, expect CLIENT_HELLO. | 
|  | 8362 | * - Fail (TLS) or silently drop record (DTLS) in other cases. | 
|  | 8363 | */ | 
|  | 8364 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8365 | #if defined(MBEDTLS_SSL_CLI_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8366 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8367 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8368 | ssl->in_hslen  != mbedtls_ssl_hs_hdr_len( ssl ) ) ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8369 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8370 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8371 |  | 
|  | 8372 | /* With DTLS, drop the packet (probably from last handshake) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8373 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8374 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 8375 | { | 
|  | 8376 | continue; | 
|  | 8377 | } | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8378 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8379 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8380 | } | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8381 | #endif /* MBEDTLS_SSL_CLI_C */ | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8382 |  | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8383 | #if defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8384 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8385 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8386 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8387 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8388 |  | 
|  | 8389 | /* With DTLS, drop the packet (probably from last handshake) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8390 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8391 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 8392 | { | 
|  | 8393 | continue; | 
|  | 8394 | } | 
| Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 8395 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8396 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8397 | } | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8398 | #endif /* MBEDTLS_SSL_SRV_C */ | 
|  | 8399 |  | 
| Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 8400 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8401 | /* Determine whether renegotiation attempt should be accepted */ | 
| Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 8402 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || | 
|  | 8403 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && | 
|  | 8404 | ssl->conf->allow_legacy_renegotiation == | 
|  | 8405 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) | 
|  | 8406 | { | 
|  | 8407 | /* | 
|  | 8408 | * Accept renegotiation request | 
|  | 8409 | */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8410 |  | 
| Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 8411 | /* DTLS clients need to know renego is server-initiated */ | 
|  | 8412 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 8413 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && | 
|  | 8414 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 8415 | { | 
|  | 8416 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; | 
|  | 8417 | } | 
|  | 8418 | #endif | 
|  | 8419 | ret = ssl_start_renegotiation( ssl ); | 
|  | 8420 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && | 
|  | 8421 | ret != 0 ) | 
|  | 8422 | { | 
|  | 8423 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); | 
|  | 8424 | return( ret ); | 
|  | 8425 | } | 
|  | 8426 | } | 
|  | 8427 | else | 
| Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 8428 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8429 | { | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8430 | /* | 
|  | 8431 | * Refuse renegotiation | 
|  | 8432 | */ | 
|  | 8433 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8434 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8435 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8436 | #if defined(MBEDTLS_SSL_PROTO_SSL3) | 
|  | 8437 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8438 | { | 
| Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 8439 | /* SSLv3 does not have a "no_renegotiation" warning, so | 
|  | 8440 | we send a fatal alert and abort the connection. */ | 
|  | 8441 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 8442 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); | 
|  | 8443 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 8444 | } | 
|  | 8445 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8446 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ | 
|  | 8447 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 8448 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 8449 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 8450 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8451 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, | 
|  | 8452 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, | 
|  | 8453 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) | 
| Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 8454 | { | 
|  | 8455 | return( ret ); | 
|  | 8456 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8457 | } | 
| Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 8458 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8459 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || | 
|  | 8460 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 8461 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8462 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); | 
|  | 8463 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); | 
| Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 8464 | } | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8465 | } | 
| Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 8466 |  | 
| Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 8467 | /* At this point, we don't know whether the renegotiation has been | 
|  | 8468 | * completed or not. The cases to consider are the following: | 
|  | 8469 | * 1) The renegotiation is complete. In this case, no new record | 
|  | 8470 | *    has been read yet. | 
|  | 8471 | * 2) The renegotiation is incomplete because the client received | 
|  | 8472 | *    an application data record while awaiting the ServerHello. | 
|  | 8473 | * 3) The renegotiation is incomplete because the client received | 
|  | 8474 | *    a non-handshake, non-application data message while awaiting | 
|  | 8475 | *    the ServerHello. | 
|  | 8476 | * In each of these case, looping will be the proper action: | 
|  | 8477 | * - For 1), the next iteration will read a new record and check | 
|  | 8478 | *   if it's application data. | 
|  | 8479 | * - For 2), the loop condition isn't satisfied as application data | 
|  | 8480 | *   is present, hence continue is the same as break | 
|  | 8481 | * - For 3), the loop condition is satisfied and read_record | 
|  | 8482 | *   will re-deliver the message that was held back by the client | 
|  | 8483 | *   when expecting the ServerHello. | 
|  | 8484 | */ | 
|  | 8485 | continue; | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8486 | } | 
| Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 8487 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8488 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 8489 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8490 | if( ssl->conf->renego_max_records >= 0 ) | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 8491 | { | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8492 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 8493 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8494 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 8495 | "but not honored by client" ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8496 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 8497 | } | 
| Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 8498 | } | 
| Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 8499 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8500 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ | 
| Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 8501 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8502 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ | 
|  | 8503 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) | 
| Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 8504 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8505 | 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] | 8506 | return( MBEDTLS_ERR_SSL_WANT_READ ); | 
| Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 8507 | } | 
|  | 8508 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8509 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8510 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8511 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); | 
|  | 8512 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8513 | } | 
|  | 8514 |  | 
|  | 8515 | ssl->in_offt = ssl->in_msg; | 
| Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 8516 |  | 
| Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 8517 | /* We're going to return something now, cancel timer, | 
|  | 8518 | * except if handshake (renegotiation) is in progress */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8519 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 8520 | ssl_set_timer( ssl, 0 ); | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8521 |  | 
| Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8522 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8523 | /* If we requested renego but received AppData, resend HelloRequest. | 
|  | 8524 | * Do it now, after setting in_offt, to avoid taking this branch | 
|  | 8525 | * again if ssl_write_hello_request() returns WANT_WRITE */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8526 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8527 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8528 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8529 | { | 
| Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 8530 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8531 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8532 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); | 
| Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 8533 | return( ret ); | 
|  | 8534 | } | 
|  | 8535 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8536 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8537 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8538 | } | 
|  | 8539 |  | 
|  | 8540 | n = ( len < ssl->in_msglen ) | 
|  | 8541 | ? len : ssl->in_msglen; | 
|  | 8542 |  | 
|  | 8543 | memcpy( buf, ssl->in_offt, n ); | 
|  | 8544 | ssl->in_msglen -= n; | 
|  | 8545 |  | 
|  | 8546 | if( ssl->in_msglen == 0 ) | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8547 | { | 
|  | 8548 | /* all bytes consumed */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8549 | ssl->in_offt = NULL; | 
| Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 8550 | ssl->keep_current_message = 0; | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8551 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8552 | else | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8553 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8554 | /* more data available */ | 
|  | 8555 | ssl->in_offt += n; | 
| Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 8556 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8557 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8558 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8559 |  | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 8560 | return( (int) n ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8561 | } | 
|  | 8562 |  | 
|  | 8563 | /* | 
| Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 8564 | * Send application data to be encrypted by the SSL layer, taking care of max | 
|  | 8565 | * fragment length and buffer size. | 
|  | 8566 | * | 
|  | 8567 | * According to RFC 5246 Section 6.2.1: | 
|  | 8568 | * | 
|  | 8569 | *      Zero-length fragments of Application data MAY be sent as they are | 
|  | 8570 | *      potentially useful as a traffic analysis countermeasure. | 
|  | 8571 | * | 
|  | 8572 | * Therefore, it is possible that the input message length is 0 and the | 
|  | 8573 | * corresponding return code is 0 on success. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8574 | */ | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8575 | static int ssl_write_real( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8576 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8577 | { | 
| Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8578 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); | 
|  | 8579 | const size_t max_len = (size_t) ret; | 
|  | 8580 |  | 
|  | 8581 | if( ret < 0 ) | 
|  | 8582 | { | 
|  | 8583 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); | 
|  | 8584 | return( ret ); | 
|  | 8585 | } | 
|  | 8586 |  | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8587 | if( len > max_len ) | 
|  | 8588 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8589 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8590 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8591 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8592 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8593 | "maximum fragment length: %d > %d", | 
|  | 8594 | len, max_len ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8595 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8596 | } | 
|  | 8597 | else | 
|  | 8598 | #endif | 
|  | 8599 | len = max_len; | 
|  | 8600 | } | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8601 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8602 | if( ssl->out_left != 0 ) | 
|  | 8603 | { | 
| Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 8604 | /* | 
|  | 8605 | * The user has previously tried to send the data and | 
|  | 8606 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially | 
|  | 8607 | * written. In this case, we expect the high-level write function | 
|  | 8608 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters | 
|  | 8609 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8610 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8611 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8612 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8613 | return( ret ); | 
|  | 8614 | } | 
|  | 8615 | } | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8616 | else | 
| Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 8617 | { | 
| Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 8618 | /* | 
|  | 8619 | * The user is trying to send a message the first time, so we need to | 
|  | 8620 | * copy the data into the internal buffers and setup the data structure | 
|  | 8621 | * to keep track of partial writes | 
|  | 8622 | */ | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8623 | ssl->out_msglen  = len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8624 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8625 | memcpy( ssl->out_msg, buf, len ); | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8626 |  | 
| Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 8627 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8628 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8629 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); | 
| Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 8630 | return( ret ); | 
|  | 8631 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8632 | } | 
|  | 8633 |  | 
| Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 8634 | return( (int) len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8635 | } | 
|  | 8636 |  | 
|  | 8637 | /* | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8638 | * Write application data, doing 1/n-1 splitting if necessary. | 
|  | 8639 | * | 
|  | 8640 | * 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] | 8641 | * then the caller will call us again with the same arguments, so | 
| Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 8642 | * remember whether we already did the split or not. | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8643 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8644 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8645 | static int ssl_write_split( mbedtls_ssl_context *ssl, | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8646 | const unsigned char *buf, size_t len ) | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8647 | { | 
|  | 8648 | int ret; | 
|  | 8649 |  | 
| Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 8650 | if( ssl->conf->cbc_record_splitting == | 
|  | 8651 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || | 
| Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8652 | len <= 1 || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8653 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || | 
|  | 8654 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) | 
|  | 8655 | != MBEDTLS_MODE_CBC ) | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8656 | { | 
|  | 8657 | return( ssl_write_real( ssl, buf, len ) ); | 
|  | 8658 | } | 
|  | 8659 |  | 
|  | 8660 | if( ssl->split_done == 0 ) | 
|  | 8661 | { | 
| Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 8662 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8663 | return( ret ); | 
| Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 8664 | ssl->split_done = 1; | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8665 | } | 
|  | 8666 |  | 
| Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 8667 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) | 
|  | 8668 | return( ret ); | 
|  | 8669 | ssl->split_done = 0; | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8670 |  | 
|  | 8671 | return( ret + 1 ); | 
|  | 8672 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8673 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ | 
| Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8674 |  | 
|  | 8675 | /* | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8676 | * Write application data (public-facing wrapper) | 
|  | 8677 | */ | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8678 | 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] | 8679 | { | 
|  | 8680 | int ret; | 
|  | 8681 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8682 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8683 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8684 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 8685 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8686 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8687 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8688 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) | 
|  | 8689 | { | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8690 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8691 | return( ret ); | 
|  | 8692 | } | 
|  | 8693 | #endif | 
|  | 8694 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8695 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8696 | { | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8697 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8698 | { | 
| Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 8699 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8700 | return( ret ); | 
|  | 8701 | } | 
|  | 8702 | } | 
|  | 8703 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8704 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8705 | ret = ssl_write_split( ssl, buf, len ); | 
|  | 8706 | #else | 
|  | 8707 | ret = ssl_write_real( ssl, buf, len ); | 
|  | 8708 | #endif | 
|  | 8709 |  | 
| Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 8710 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); | 
| Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 8711 |  | 
|  | 8712 | return( ret ); | 
|  | 8713 | } | 
|  | 8714 |  | 
|  | 8715 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8716 | * Notify the peer that the connection is being closed | 
|  | 8717 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8718 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8719 | { | 
|  | 8720 | int ret; | 
|  | 8721 |  | 
| Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 8722 | if( ssl == NULL || ssl->conf == NULL ) | 
|  | 8723 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | 
|  | 8724 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8725 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8726 |  | 
| Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 8727 | if( ssl->out_left != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8728 | return( mbedtls_ssl_flush_output( ssl ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8729 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8730 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8731 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8732 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, | 
|  | 8733 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, | 
|  | 8734 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8735 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8736 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8737 | return( ret ); | 
|  | 8738 | } | 
|  | 8739 | } | 
|  | 8740 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8741 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8742 |  | 
| Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 8743 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8744 | } | 
|  | 8745 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8746 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8747 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8748 | if( transform == NULL ) | 
|  | 8749 | return; | 
|  | 8750 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8751 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8752 | deflateEnd( &transform->ctx_deflate ); | 
|  | 8753 | inflateEnd( &transform->ctx_inflate ); | 
|  | 8754 | #endif | 
|  | 8755 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8756 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); | 
|  | 8757 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); | 
| Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 8758 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8759 | mbedtls_md_free( &transform->md_ctx_enc ); | 
|  | 8760 | mbedtls_md_free( &transform->md_ctx_dec ); | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 8761 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8762 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8763 | } | 
|  | 8764 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8765 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 8766 | 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] | 8767 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8768 | mbedtls_ssl_key_cert *cur = key_cert, *next; | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 8769 |  | 
|  | 8770 | while( cur != NULL ) | 
|  | 8771 | { | 
|  | 8772 | next = cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8773 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 8774 | cur = next; | 
|  | 8775 | } | 
|  | 8776 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8777 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 8778 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8779 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 8780 |  | 
|  | 8781 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) | 
|  | 8782 | { | 
|  | 8783 | unsigned offset; | 
|  | 8784 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 8785 |  | 
|  | 8786 | if( hs == NULL ) | 
|  | 8787 | return; | 
|  | 8788 |  | 
| Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 8789 | ssl_free_buffered_record( ssl ); | 
|  | 8790 |  | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8791 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 8792 | ssl_buffering_free_slot( ssl, offset ); | 
|  | 8793 | } | 
|  | 8794 |  | 
|  | 8795 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, | 
|  | 8796 | uint8_t slot ) | 
|  | 8797 | { | 
|  | 8798 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; | 
|  | 8799 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; | 
| Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 8800 |  | 
|  | 8801 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) | 
|  | 8802 | return; | 
|  | 8803 |  | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 8804 | if( hs_buf->is_valid == 1 ) | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8805 | { | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 8806 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; | 
| Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 8807 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); | 
| Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 8808 | mbedtls_free( hs_buf->data ); | 
|  | 8809 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8810 | } | 
|  | 8811 | } | 
|  | 8812 |  | 
|  | 8813 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ | 
|  | 8814 |  | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8815 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8816 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8817 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; | 
|  | 8818 |  | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8819 | if( handshake == NULL ) | 
|  | 8820 | return; | 
|  | 8821 |  | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8822 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) | 
|  | 8823 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) | 
|  | 8824 | { | 
| Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 8825 | ssl->conf->f_async_cancel( ssl ); | 
| Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8826 | handshake->async_in_progress = 0; | 
|  | 8827 | } | 
|  | 8828 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ | 
|  | 8829 |  | 
| Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 8830 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 8831 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 8832 | mbedtls_md5_free(    &handshake->fin_md5  ); | 
|  | 8833 | mbedtls_sha1_free(   &handshake->fin_sha1 ); | 
|  | 8834 | #endif | 
|  | 8835 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 8836 | #if defined(MBEDTLS_SHA256_C) | 
|  | 8837 | mbedtls_sha256_free(   &handshake->fin_sha256    ); | 
|  | 8838 | #endif | 
|  | 8839 | #if defined(MBEDTLS_SHA512_C) | 
|  | 8840 | mbedtls_sha512_free(   &handshake->fin_sha512    ); | 
|  | 8841 | #endif | 
|  | 8842 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 8843 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8844 | #if defined(MBEDTLS_DHM_C) | 
|  | 8845 | mbedtls_dhm_free( &handshake->dhm_ctx ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8846 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8847 | #if defined(MBEDTLS_ECDH_C) | 
|  | 8848 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 8849 | #endif | 
| Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8850 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8851 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); | 
| Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 8852 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 8853 | mbedtls_free( handshake->ecjpake_cache ); | 
|  | 8854 | handshake->ecjpake_cache = NULL; | 
|  | 8855 | handshake->ecjpake_cache_len = 0; | 
|  | 8856 | #endif | 
| Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 8857 | #endif | 
| Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 8858 |  | 
| Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 8859 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ | 
|  | 8860 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 8861 | /* explicit void pointer cast for buggy MS compiler */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8862 | mbedtls_free( (void *) handshake->curves ); | 
| Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 8863 | #endif | 
|  | 8864 |  | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8865 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 8866 | if( handshake->psk != NULL ) | 
|  | 8867 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8868 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); | 
| Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8869 | mbedtls_free( handshake->psk ); | 
|  | 8870 | } | 
|  | 8871 | #endif | 
|  | 8872 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8873 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ | 
|  | 8874 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 8875 | /* | 
|  | 8876 | * Free only the linked list wrapper, not the keys themselves | 
|  | 8877 | * since the belong to the SNI callback | 
|  | 8878 | */ | 
|  | 8879 | if( handshake->sni_key_cert != NULL ) | 
|  | 8880 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8881 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 8882 |  | 
|  | 8883 | while( cur != NULL ) | 
|  | 8884 | { | 
|  | 8885 | next = cur->next; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8886 | mbedtls_free( cur ); | 
| Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 8887 | cur = next; | 
|  | 8888 | } | 
|  | 8889 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8890 | #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] | 8891 |  | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8892 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 8893 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); | 
| Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 8894 | #endif | 
|  | 8895 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8896 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 8897 | mbedtls_free( handshake->verify_cookie ); | 
| Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 8898 | ssl_flight_free( handshake->flight ); | 
| Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 8899 | ssl_buffering_free( ssl ); | 
| Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 8900 | #endif | 
|  | 8901 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8902 | mbedtls_platform_zeroize( handshake, | 
|  | 8903 | sizeof( mbedtls_ssl_handshake_params ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8904 | } | 
|  | 8905 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8906 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8907 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8908 | if( session == NULL ) | 
|  | 8909 | return; | 
|  | 8910 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8911 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8912 | if( session->peer_cert != NULL ) | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8913 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8914 | mbedtls_x509_crt_free( session->peer_cert ); | 
|  | 8915 | mbedtls_free( session->peer_cert ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8916 | } | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 8917 | #endif | 
| Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8918 |  | 
| Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8919 | #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] | 8920 | mbedtls_free( session->ticket ); | 
| Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 8921 | #endif | 
| Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 8922 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8923 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8924 | } | 
|  | 8925 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8926 | /* | 
|  | 8927 | * Free an SSL context | 
|  | 8928 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8929 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8930 | { | 
| Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8931 | if( ssl == NULL ) | 
|  | 8932 | return; | 
|  | 8933 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8934 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8935 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 8936 | if( ssl->out_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8937 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8938 | 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] | 8939 | mbedtls_free( ssl->out_buf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8940 | } | 
|  | 8941 |  | 
| Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 8942 | if( ssl->in_buf != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8943 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8944 | 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] | 8945 | mbedtls_free( ssl->in_buf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8946 | } | 
|  | 8947 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8948 | #if defined(MBEDTLS_ZLIB_SUPPORT) | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 8949 | if( ssl->compress_buf != NULL ) | 
|  | 8950 | { | 
| Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8951 | 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] | 8952 | mbedtls_free( ssl->compress_buf ); | 
| Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 8953 | } | 
|  | 8954 | #endif | 
|  | 8955 |  | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8956 | if( ssl->transform ) | 
|  | 8957 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8958 | mbedtls_ssl_transform_free( ssl->transform ); | 
|  | 8959 | mbedtls_free( ssl->transform ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8960 | } | 
|  | 8961 |  | 
|  | 8962 | if( ssl->handshake ) | 
|  | 8963 | { | 
| Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 8964 | mbedtls_ssl_handshake_free( ssl ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8965 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); | 
|  | 8966 | mbedtls_ssl_session_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8967 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8968 | mbedtls_free( ssl->handshake ); | 
|  | 8969 | mbedtls_free( ssl->transform_negotiate ); | 
|  | 8970 | mbedtls_free( ssl->session_negotiate ); | 
| Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8971 | } | 
|  | 8972 |  | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8973 | if( ssl->session ) | 
|  | 8974 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8975 | mbedtls_ssl_session_free( ssl->session ); | 
|  | 8976 | mbedtls_free( ssl->session ); | 
| Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8977 | } | 
|  | 8978 |  | 
| Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 8979 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 8980 | if( ssl->hostname != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8981 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8982 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8983 | mbedtls_free( ssl->hostname ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8984 | } | 
| Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 8985 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8986 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8987 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) | 
|  | 8988 | if( mbedtls_ssl_hw_record_finish != NULL ) | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8989 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8990 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); | 
|  | 8991 | mbedtls_ssl_hw_record_finish( ssl ); | 
| Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8992 | } | 
|  | 8993 | #endif | 
|  | 8994 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8995 | #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] | 8996 | mbedtls_free( ssl->cli_id ); | 
| Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8997 | #endif | 
|  | 8998 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8999 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); | 
| Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 9000 |  | 
| Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 9001 | /* Actually clear after last debug message */ | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9002 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9003 | } | 
|  | 9004 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9005 | /* | 
|  | 9006 | * Initialze mbedtls_ssl_config | 
|  | 9007 | */ | 
|  | 9008 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) | 
|  | 9009 | { | 
|  | 9010 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); | 
|  | 9011 | } | 
|  | 9012 |  | 
| Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 9013 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 9014 | static int ssl_preset_default_hashes[] = { | 
|  | 9015 | #if defined(MBEDTLS_SHA512_C) | 
|  | 9016 | MBEDTLS_MD_SHA512, | 
|  | 9017 | MBEDTLS_MD_SHA384, | 
|  | 9018 | #endif | 
|  | 9019 | #if defined(MBEDTLS_SHA256_C) | 
|  | 9020 | MBEDTLS_MD_SHA256, | 
|  | 9021 | MBEDTLS_MD_SHA224, | 
|  | 9022 | #endif | 
| Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 9023 | #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] | 9024 | MBEDTLS_MD_SHA1, | 
|  | 9025 | #endif | 
|  | 9026 | MBEDTLS_MD_NONE | 
|  | 9027 | }; | 
| Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 9028 | #endif | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 9029 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9030 | static int ssl_preset_suiteb_ciphersuites[] = { | 
|  | 9031 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | 
|  | 9032 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | 
|  | 9033 | 0 | 
|  | 9034 | }; | 
|  | 9035 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9036 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9037 | static int ssl_preset_suiteb_hashes[] = { | 
|  | 9038 | MBEDTLS_MD_SHA256, | 
|  | 9039 | MBEDTLS_MD_SHA384, | 
|  | 9040 | MBEDTLS_MD_NONE | 
|  | 9041 | }; | 
|  | 9042 | #endif | 
|  | 9043 |  | 
|  | 9044 | #if defined(MBEDTLS_ECP_C) | 
|  | 9045 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { | 
|  | 9046 | MBEDTLS_ECP_DP_SECP256R1, | 
|  | 9047 | MBEDTLS_ECP_DP_SECP384R1, | 
|  | 9048 | MBEDTLS_ECP_DP_NONE | 
|  | 9049 | }; | 
|  | 9050 | #endif | 
|  | 9051 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9052 | /* | 
| Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 9053 | * Load default in mbedtls_ssl_config | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9054 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 9055 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9056 | int endpoint, int transport, int preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9057 | { | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 9058 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9059 | int ret; | 
| Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 9060 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9061 |  | 
| Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 9062 | /* Use the functions here so that they are covered in tests, | 
|  | 9063 | * but otherwise access member directly for efficiency */ | 
|  | 9064 | mbedtls_ssl_conf_endpoint( conf, endpoint ); | 
|  | 9065 | mbedtls_ssl_conf_transport( conf, transport ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9066 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9067 | /* | 
|  | 9068 | * Things that are common to all presets | 
|  | 9069 | */ | 
| Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 9070 | #if defined(MBEDTLS_SSL_CLI_C) | 
|  | 9071 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) | 
|  | 9072 | { | 
|  | 9073 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; | 
|  | 9074 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) | 
|  | 9075 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; | 
|  | 9076 | #endif | 
|  | 9077 | } | 
|  | 9078 | #endif | 
|  | 9079 |  | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9080 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9081 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; | 
| Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9082 | #endif | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9083 |  | 
|  | 9084 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) | 
|  | 9085 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; | 
|  | 9086 | #endif | 
|  | 9087 |  | 
|  | 9088 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) | 
|  | 9089 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; | 
|  | 9090 | #endif | 
|  | 9091 |  | 
| Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9092 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) | 
|  | 9093 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; | 
|  | 9094 | #endif | 
|  | 9095 |  | 
| Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 9096 | #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] | 9097 | conf->f_cookie_write = ssl_cookie_write_dummy; | 
|  | 9098 | conf->f_cookie_check = ssl_cookie_check_dummy; | 
|  | 9099 | #endif | 
|  | 9100 |  | 
|  | 9101 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) | 
|  | 9102 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; | 
|  | 9103 | #endif | 
|  | 9104 |  | 
| Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 9105 | #if defined(MBEDTLS_SSL_SRV_C) | 
|  | 9106 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; | 
|  | 9107 | #endif | 
|  | 9108 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9109 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 9110 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; | 
|  | 9111 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; | 
|  | 9112 | #endif | 
|  | 9113 |  | 
|  | 9114 | #if defined(MBEDTLS_SSL_RENEGOTIATION) | 
|  | 9115 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; | 
| Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9116 | memset( conf->renego_period,     0x00, 2 ); | 
|  | 9117 | memset( conf->renego_period + 2, 0xFF, 6 ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9118 | #endif | 
|  | 9119 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9120 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) | 
|  | 9121 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) | 
|  | 9122 | { | 
| Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 9123 | const unsigned char dhm_p[] = | 
|  | 9124 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; | 
|  | 9125 | const unsigned char dhm_g[] = | 
|  | 9126 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; | 
|  | 9127 |  | 
| Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 9128 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, | 
|  | 9129 | dhm_p, sizeof( dhm_p ), | 
|  | 9130 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9131 | { | 
|  | 9132 | return( ret ); | 
|  | 9133 | } | 
|  | 9134 | } | 
| Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 9135 | #endif | 
|  | 9136 |  | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9137 | /* | 
|  | 9138 | * Preset-specific defaults | 
|  | 9139 | */ | 
|  | 9140 | switch( preset ) | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9141 | { | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9142 | /* | 
|  | 9143 | * NSA Suite B | 
|  | 9144 | */ | 
|  | 9145 | case MBEDTLS_SSL_PRESET_SUITEB: | 
|  | 9146 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; | 
|  | 9147 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ | 
|  | 9148 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
|  | 9149 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
|  | 9150 |  | 
|  | 9151 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = | 
|  | 9152 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = | 
|  | 9153 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = | 
|  | 9154 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = | 
|  | 9155 | ssl_preset_suiteb_ciphersuites; | 
|  | 9156 |  | 
|  | 9157 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 9158 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9159 | #endif | 
|  | 9160 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9161 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9162 | conf->sig_hashes = ssl_preset_suiteb_hashes; | 
|  | 9163 | #endif | 
|  | 9164 |  | 
|  | 9165 | #if defined(MBEDTLS_ECP_C) | 
|  | 9166 | conf->curve_list = ssl_preset_suiteb_curves; | 
|  | 9167 | #endif | 
| Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 9168 | break; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9169 |  | 
|  | 9170 | /* | 
|  | 9171 | * Default | 
|  | 9172 | */ | 
|  | 9173 | default: | 
| Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 9174 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > | 
|  | 9175 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? | 
|  | 9176 | MBEDTLS_SSL_MIN_MAJOR_VERSION : | 
|  | 9177 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; | 
|  | 9178 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > | 
|  | 9179 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? | 
|  | 9180 | MBEDTLS_SSL_MIN_MINOR_VERSION : | 
|  | 9181 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9182 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; | 
|  | 9183 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; | 
|  | 9184 |  | 
|  | 9185 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 9186 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
|  | 9187 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; | 
|  | 9188 | #endif | 
|  | 9189 |  | 
|  | 9190 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = | 
|  | 9191 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = | 
|  | 9192 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = | 
|  | 9193 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = | 
|  | 9194 | mbedtls_ssl_list_ciphersuites(); | 
|  | 9195 |  | 
|  | 9196 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 9197 | conf->cert_profile = &mbedtls_x509_crt_profile_default; | 
|  | 9198 | #endif | 
|  | 9199 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9200 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 9201 | conf->sig_hashes = ssl_preset_default_hashes; | 
| Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 9202 | #endif | 
|  | 9203 |  | 
|  | 9204 | #if defined(MBEDTLS_ECP_C) | 
|  | 9205 | conf->curve_list = mbedtls_ecp_grp_id_list(); | 
|  | 9206 | #endif | 
|  | 9207 |  | 
|  | 9208 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) | 
|  | 9209 | conf->dhm_min_bitlen = 1024; | 
|  | 9210 | #endif | 
|  | 9211 | } | 
|  | 9212 |  | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9213 | return( 0 ); | 
|  | 9214 | } | 
|  | 9215 |  | 
|  | 9216 | /* | 
|  | 9217 | * Free mbedtls_ssl_config | 
|  | 9218 | */ | 
|  | 9219 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) | 
|  | 9220 | { | 
|  | 9221 | #if defined(MBEDTLS_DHM_C) | 
|  | 9222 | mbedtls_mpi_free( &conf->dhm_P ); | 
|  | 9223 | mbedtls_mpi_free( &conf->dhm_G ); | 
|  | 9224 | #endif | 
|  | 9225 |  | 
|  | 9226 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) | 
|  | 9227 | if( conf->psk != NULL ) | 
|  | 9228 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9229 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9230 | mbedtls_free( conf->psk ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 9231 | conf->psk = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9232 | conf->psk_len = 0; | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 9233 | } | 
|  | 9234 |  | 
|  | 9235 | if( conf->psk_identity != NULL ) | 
|  | 9236 | { | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9237 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); | 
| junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 9238 | mbedtls_free( conf->psk_identity ); | 
| Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 9239 | conf->psk_identity = NULL; | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9240 | conf->psk_identity_len = 0; | 
|  | 9241 | } | 
|  | 9242 | #endif | 
|  | 9243 |  | 
|  | 9244 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 9245 | ssl_key_cert_free( conf->key_cert ); | 
|  | 9246 | #endif | 
|  | 9247 |  | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 9248 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); | 
| Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 9249 | } | 
|  | 9250 |  | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 9251 | #if defined(MBEDTLS_PK_C) && \ | 
|  | 9252 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9253 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9254 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9255 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9256 | 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] | 9257 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9258 | #if defined(MBEDTLS_RSA_C) | 
|  | 9259 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) | 
|  | 9260 | return( MBEDTLS_SSL_SIG_RSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9261 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9262 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 9263 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) | 
|  | 9264 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9265 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9266 | return( MBEDTLS_SSL_SIG_ANON ); | 
| Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 9267 | } | 
|  | 9268 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 9269 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) | 
|  | 9270 | { | 
|  | 9271 | switch( type ) { | 
|  | 9272 | case MBEDTLS_PK_RSA: | 
|  | 9273 | return( MBEDTLS_SSL_SIG_RSA ); | 
|  | 9274 | case MBEDTLS_PK_ECDSA: | 
|  | 9275 | case MBEDTLS_PK_ECKEY: | 
|  | 9276 | return( MBEDTLS_SSL_SIG_ECDSA ); | 
|  | 9277 | default: | 
|  | 9278 | return( MBEDTLS_SSL_SIG_ANON ); | 
|  | 9279 | } | 
|  | 9280 | } | 
|  | 9281 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9282 | 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] | 9283 | { | 
|  | 9284 | switch( sig ) | 
|  | 9285 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9286 | #if defined(MBEDTLS_RSA_C) | 
|  | 9287 | case MBEDTLS_SSL_SIG_RSA: | 
|  | 9288 | return( MBEDTLS_PK_RSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9289 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9290 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 9291 | case MBEDTLS_SSL_SIG_ECDSA: | 
|  | 9292 | return( MBEDTLS_PK_ECDSA ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9293 | #endif | 
|  | 9294 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9295 | return( MBEDTLS_PK_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9296 | } | 
|  | 9297 | } | 
| Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 9298 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9299 |  | 
| Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 9300 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ | 
|  | 9301 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
|  | 9302 |  | 
|  | 9303 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ | 
|  | 9304 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 9305 | mbedtls_pk_type_t sig_alg ) | 
|  | 9306 | { | 
|  | 9307 | switch( sig_alg ) | 
|  | 9308 | { | 
|  | 9309 | case MBEDTLS_PK_RSA: | 
|  | 9310 | return( set->rsa ); | 
|  | 9311 | case MBEDTLS_PK_ECDSA: | 
|  | 9312 | return( set->ecdsa ); | 
|  | 9313 | default: | 
|  | 9314 | return( MBEDTLS_MD_NONE ); | 
|  | 9315 | } | 
|  | 9316 | } | 
|  | 9317 |  | 
|  | 9318 | /* Add a signature-hash-pair to a signature-hash set */ | 
|  | 9319 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 9320 | mbedtls_pk_type_t sig_alg, | 
|  | 9321 | mbedtls_md_type_t md_alg ) | 
|  | 9322 | { | 
|  | 9323 | switch( sig_alg ) | 
|  | 9324 | { | 
|  | 9325 | case MBEDTLS_PK_RSA: | 
|  | 9326 | if( set->rsa == MBEDTLS_MD_NONE ) | 
|  | 9327 | set->rsa = md_alg; | 
|  | 9328 | break; | 
|  | 9329 |  | 
|  | 9330 | case MBEDTLS_PK_ECDSA: | 
|  | 9331 | if( set->ecdsa == MBEDTLS_MD_NONE ) | 
|  | 9332 | set->ecdsa = md_alg; | 
|  | 9333 | break; | 
|  | 9334 |  | 
|  | 9335 | default: | 
|  | 9336 | break; | 
|  | 9337 | } | 
|  | 9338 | } | 
|  | 9339 |  | 
|  | 9340 | /* Allow exactly one hash algorithm for each signature. */ | 
|  | 9341 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, | 
|  | 9342 | mbedtls_md_type_t md_alg ) | 
|  | 9343 | { | 
|  | 9344 | set->rsa   = md_alg; | 
|  | 9345 | set->ecdsa = md_alg; | 
|  | 9346 | } | 
|  | 9347 |  | 
|  | 9348 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && | 
|  | 9349 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
|  | 9350 |  | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 9351 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9352 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX | 
| Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 9353 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9354 | 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] | 9355 | { | 
|  | 9356 | switch( hash ) | 
|  | 9357 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9358 | #if defined(MBEDTLS_MD5_C) | 
|  | 9359 | case MBEDTLS_SSL_HASH_MD5: | 
|  | 9360 | return( MBEDTLS_MD_MD5 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9361 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9362 | #if defined(MBEDTLS_SHA1_C) | 
|  | 9363 | case MBEDTLS_SSL_HASH_SHA1: | 
|  | 9364 | return( MBEDTLS_MD_SHA1 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9365 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9366 | #if defined(MBEDTLS_SHA256_C) | 
|  | 9367 | case MBEDTLS_SSL_HASH_SHA224: | 
|  | 9368 | return( MBEDTLS_MD_SHA224 ); | 
|  | 9369 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 9370 | return( MBEDTLS_MD_SHA256 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9371 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9372 | #if defined(MBEDTLS_SHA512_C) | 
|  | 9373 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 9374 | return( MBEDTLS_MD_SHA384 ); | 
|  | 9375 | case MBEDTLS_SSL_HASH_SHA512: | 
|  | 9376 | return( MBEDTLS_MD_SHA512 ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9377 | #endif | 
|  | 9378 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9379 | return( MBEDTLS_MD_NONE ); | 
| Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 9380 | } | 
|  | 9381 | } | 
|  | 9382 |  | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9383 | /* | 
|  | 9384 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX | 
|  | 9385 | */ | 
|  | 9386 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) | 
|  | 9387 | { | 
|  | 9388 | switch( md ) | 
|  | 9389 | { | 
|  | 9390 | #if defined(MBEDTLS_MD5_C) | 
|  | 9391 | case MBEDTLS_MD_MD5: | 
|  | 9392 | return( MBEDTLS_SSL_HASH_MD5 ); | 
|  | 9393 | #endif | 
|  | 9394 | #if defined(MBEDTLS_SHA1_C) | 
|  | 9395 | case MBEDTLS_MD_SHA1: | 
|  | 9396 | return( MBEDTLS_SSL_HASH_SHA1 ); | 
|  | 9397 | #endif | 
|  | 9398 | #if defined(MBEDTLS_SHA256_C) | 
|  | 9399 | case MBEDTLS_MD_SHA224: | 
|  | 9400 | return( MBEDTLS_SSL_HASH_SHA224 ); | 
|  | 9401 | case MBEDTLS_MD_SHA256: | 
|  | 9402 | return( MBEDTLS_SSL_HASH_SHA256 ); | 
|  | 9403 | #endif | 
|  | 9404 | #if defined(MBEDTLS_SHA512_C) | 
|  | 9405 | case MBEDTLS_MD_SHA384: | 
|  | 9406 | return( MBEDTLS_SSL_HASH_SHA384 ); | 
|  | 9407 | case MBEDTLS_MD_SHA512: | 
|  | 9408 | return( MBEDTLS_SSL_HASH_SHA512 ); | 
|  | 9409 | #endif | 
|  | 9410 | default: | 
|  | 9411 | return( MBEDTLS_SSL_HASH_NONE ); | 
|  | 9412 | } | 
|  | 9413 | } | 
|  | 9414 |  | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 9415 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 9416 | /* | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9417 | * 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] | 9418 | * 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] | 9419 | */ | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 9420 | 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] | 9421 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9422 | const mbedtls_ecp_group_id *gid; | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 9423 |  | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 9424 | if( ssl->conf->curve_list == NULL ) | 
|  | 9425 | return( -1 ); | 
|  | 9426 |  | 
| Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9427 | 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] | 9428 | if( *gid == grp_id ) | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 9429 | return( 0 ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 9430 |  | 
| Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 9431 | return( -1 ); | 
| Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 9432 | } | 
| Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 9433 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9434 |  | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9435 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9436 | /* | 
|  | 9437 | * Check if a hash proposed by the peer is in our list. | 
|  | 9438 | * Return 0 if we're willing to use it, -1 otherwise. | 
|  | 9439 | */ | 
|  | 9440 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, | 
|  | 9441 | mbedtls_md_type_t md ) | 
|  | 9442 | { | 
|  | 9443 | const int *cur; | 
|  | 9444 |  | 
|  | 9445 | if( ssl->conf->sig_hashes == NULL ) | 
|  | 9446 | return( -1 ); | 
|  | 9447 |  | 
|  | 9448 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) | 
|  | 9449 | if( *cur == (int) md ) | 
|  | 9450 | return( 0 ); | 
|  | 9451 |  | 
|  | 9452 | return( -1 ); | 
|  | 9453 | } | 
| Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 9454 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ | 
| Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 9455 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9456 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 9457 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, | 
|  | 9458 | const mbedtls_ssl_ciphersuite_t *ciphersuite, | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9459 | int cert_endpoint, | 
| Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9460 | uint32_t *flags ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9461 | { | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9462 | int ret = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9463 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9464 | int usage = 0; | 
|  | 9465 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9466 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9467 | const char *ext_oid; | 
|  | 9468 | size_t ext_len; | 
|  | 9469 | #endif | 
|  | 9470 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9471 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) &&          \ | 
|  | 9472 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9473 | ((void) cert); | 
|  | 9474 | ((void) cert_endpoint); | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9475 | ((void) flags); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9476 | #endif | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9477 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9478 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) | 
|  | 9479 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9480 | { | 
|  | 9481 | /* Server part of the key exchange */ | 
|  | 9482 | switch( ciphersuite->key_exchange ) | 
|  | 9483 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9484 | case MBEDTLS_KEY_EXCHANGE_RSA: | 
|  | 9485 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 9486 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9487 | break; | 
|  | 9488 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9489 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: | 
|  | 9490 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: | 
|  | 9491 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: | 
|  | 9492 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9493 | break; | 
|  | 9494 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9495 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: | 
|  | 9496 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 9497 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9498 | break; | 
|  | 9499 |  | 
|  | 9500 | /* 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] | 9501 | case MBEDTLS_KEY_EXCHANGE_NONE: | 
|  | 9502 | case MBEDTLS_KEY_EXCHANGE_PSK: | 
|  | 9503 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: | 
|  | 9504 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: | 
| Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 9505 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9506 | usage = 0; | 
|  | 9507 | } | 
|  | 9508 | } | 
|  | 9509 | else | 
|  | 9510 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9511 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ | 
|  | 9512 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9513 | } | 
|  | 9514 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9515 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9516 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 9517 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9518 | ret = -1; | 
|  | 9519 | } | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9520 | #else | 
|  | 9521 | ((void) ciphersuite); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9522 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9523 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9524 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) | 
|  | 9525 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9526 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9527 | ext_oid = MBEDTLS_OID_SERVER_AUTH; | 
|  | 9528 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9529 | } | 
|  | 9530 | else | 
|  | 9531 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9532 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; | 
|  | 9533 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9534 | } | 
|  | 9535 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9536 | 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] | 9537 | { | 
| Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 9538 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9539 | ret = -1; | 
|  | 9540 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9541 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ | 
| Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 9542 |  | 
| Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 9543 | return( ret ); | 
| Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 9544 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9545 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 9546 |  | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9547 | /* | 
|  | 9548 | * Convert version numbers to/from wire format | 
|  | 9549 | * and, for DTLS, to/from TLS equivalent. | 
|  | 9550 | * | 
|  | 9551 | * For TLS this is the identity. | 
| Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9552 | * 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] | 9553 | * 1.0 <-> 3.2      (DTLS 1.0 is based on TLS 1.1) | 
|  | 9554 | * 1.x <-> 3.x+1    for x != 0 (DTLS 1.2 based on TLS 1.2) | 
|  | 9555 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9556 | 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] | 9557 | unsigned char ver[2] ) | 
|  | 9558 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9559 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 9560 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9561 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9562 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9563 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ | 
|  | 9564 |  | 
|  | 9565 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); | 
|  | 9566 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); | 
|  | 9567 | } | 
| Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 9568 | else | 
|  | 9569 | #else | 
|  | 9570 | ((void) transport); | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9571 | #endif | 
| Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 9572 | { | 
|  | 9573 | ver[0] = (unsigned char) major; | 
|  | 9574 | ver[1] = (unsigned char) minor; | 
|  | 9575 | } | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9576 | } | 
|  | 9577 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9578 | 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] | 9579 | const unsigned char ver[2] ) | 
|  | 9580 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9581 | #if defined(MBEDTLS_SSL_PROTO_DTLS) | 
|  | 9582 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9583 | { | 
|  | 9584 | *major = 255 - ver[0] + 2; | 
|  | 9585 | *minor = 255 - ver[1] + 1; | 
|  | 9586 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9587 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9588 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ | 
|  | 9589 | } | 
| Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 9590 | else | 
|  | 9591 | #else | 
|  | 9592 | ((void) transport); | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9593 | #endif | 
| Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 9594 | { | 
|  | 9595 | *major = ver[0]; | 
|  | 9596 | *minor = ver[1]; | 
|  | 9597 | } | 
| Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 9598 | } | 
|  | 9599 |  | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 9600 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) | 
|  | 9601 | { | 
|  | 9602 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 9603 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) | 
|  | 9604 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; | 
|  | 9605 |  | 
|  | 9606 | switch( md ) | 
|  | 9607 | { | 
|  | 9608 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 9609 | #if defined(MBEDTLS_MD5_C) | 
|  | 9610 | case MBEDTLS_SSL_HASH_MD5: | 
| Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 9611 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; | 
| Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 9612 | #endif | 
|  | 9613 | #if defined(MBEDTLS_SHA1_C) | 
|  | 9614 | case MBEDTLS_SSL_HASH_SHA1: | 
|  | 9615 | ssl->handshake->calc_verify = ssl_calc_verify_tls; | 
|  | 9616 | break; | 
|  | 9617 | #endif | 
|  | 9618 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 9619 | #if defined(MBEDTLS_SHA512_C) | 
|  | 9620 | case MBEDTLS_SSL_HASH_SHA384: | 
|  | 9621 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; | 
|  | 9622 | break; | 
|  | 9623 | #endif | 
|  | 9624 | #if defined(MBEDTLS_SHA256_C) | 
|  | 9625 | case MBEDTLS_SSL_HASH_SHA256: | 
|  | 9626 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; | 
|  | 9627 | break; | 
|  | 9628 | #endif | 
|  | 9629 | default: | 
|  | 9630 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; | 
|  | 9631 | } | 
|  | 9632 |  | 
|  | 9633 | return 0; | 
|  | 9634 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 9635 | (void) ssl; | 
|  | 9636 | (void) md; | 
|  | 9637 |  | 
|  | 9638 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; | 
|  | 9639 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 9640 | } | 
|  | 9641 |  | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9642 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ | 
|  | 9643 | defined(MBEDTLS_SSL_PROTO_TLS1_1) | 
|  | 9644 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, | 
|  | 9645 | unsigned char *output, | 
|  | 9646 | unsigned char *data, size_t data_len ) | 
|  | 9647 | { | 
|  | 9648 | int ret = 0; | 
|  | 9649 | mbedtls_md5_context mbedtls_md5; | 
|  | 9650 | mbedtls_sha1_context mbedtls_sha1; | 
|  | 9651 |  | 
|  | 9652 | mbedtls_md5_init( &mbedtls_md5 ); | 
|  | 9653 | mbedtls_sha1_init( &mbedtls_sha1 ); | 
|  | 9654 |  | 
|  | 9655 | /* | 
|  | 9656 | * digitally-signed struct { | 
|  | 9657 | *     opaque md5_hash[16]; | 
|  | 9658 | *     opaque sha_hash[20]; | 
|  | 9659 | * }; | 
|  | 9660 | * | 
|  | 9661 | * md5_hash | 
|  | 9662 | *     MD5(ClientHello.random + ServerHello.random | 
|  | 9663 | *                            + ServerParams); | 
|  | 9664 | * sha_hash | 
|  | 9665 | *     SHA(ClientHello.random + ServerHello.random | 
|  | 9666 | *                            + ServerParams); | 
|  | 9667 | */ | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9668 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9669 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9670 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9671 | goto exit; | 
|  | 9672 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9673 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9674 | ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 9675 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9676 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9677 | goto exit; | 
|  | 9678 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9679 | 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] | 9680 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9681 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9682 | goto exit; | 
|  | 9683 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9684 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9685 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9686 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9687 | goto exit; | 
|  | 9688 | } | 
|  | 9689 |  | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9690 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9691 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9692 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9693 | goto exit; | 
|  | 9694 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9695 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9696 | ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 9697 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9698 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9699 | goto exit; | 
|  | 9700 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9701 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9702 | data_len ) ) != 0 ) | 
|  | 9703 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9704 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9705 | goto exit; | 
|  | 9706 | } | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9707 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9708 | output + 16 ) ) != 0 ) | 
|  | 9709 | { | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 9710 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9711 | goto exit; | 
|  | 9712 | } | 
|  | 9713 |  | 
|  | 9714 | exit: | 
|  | 9715 | mbedtls_md5_free( &mbedtls_md5 ); | 
|  | 9716 | mbedtls_sha1_free( &mbedtls_sha1 ); | 
|  | 9717 |  | 
|  | 9718 | if( ret != 0 ) | 
|  | 9719 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 9720 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 9721 |  | 
|  | 9722 | return( ret ); | 
|  | 9723 |  | 
|  | 9724 | } | 
|  | 9725 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ | 
|  | 9726 | MBEDTLS_SSL_PROTO_TLS1_1 */ | 
|  | 9727 |  | 
|  | 9728 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | 
|  | 9729 | defined(MBEDTLS_SSL_PROTO_TLS1_2) | 
|  | 9730 | 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] | 9731 | unsigned char *hash, size_t *hashlen, | 
|  | 9732 | unsigned char *data, size_t data_len, | 
|  | 9733 | mbedtls_md_type_t md_alg ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9734 | { | 
|  | 9735 | int ret = 0; | 
|  | 9736 | mbedtls_md_context_t ctx; | 
|  | 9737 | 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] | 9738 | *hashlen = mbedtls_md_get_size( md_info ); | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9739 |  | 
|  | 9740 | mbedtls_md_init( &ctx ); | 
|  | 9741 |  | 
|  | 9742 | /* | 
|  | 9743 | * digitally-signed struct { | 
|  | 9744 | *     opaque client_random[32]; | 
|  | 9745 | *     opaque server_random[32]; | 
|  | 9746 | *     ServerDHParams params; | 
|  | 9747 | * }; | 
|  | 9748 | */ | 
|  | 9749 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) | 
|  | 9750 | { | 
|  | 9751 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); | 
|  | 9752 | goto exit; | 
|  | 9753 | } | 
|  | 9754 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) | 
|  | 9755 | { | 
|  | 9756 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); | 
|  | 9757 | goto exit; | 
|  | 9758 | } | 
|  | 9759 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) | 
|  | 9760 | { | 
|  | 9761 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 9762 | goto exit; | 
|  | 9763 | } | 
|  | 9764 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) | 
|  | 9765 | { | 
|  | 9766 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); | 
|  | 9767 | goto exit; | 
|  | 9768 | } | 
| Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 9769 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) | 
| Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 9770 | { | 
|  | 9771 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); | 
|  | 9772 | goto exit; | 
|  | 9773 | } | 
|  | 9774 |  | 
|  | 9775 | exit: | 
|  | 9776 | mbedtls_md_free( &ctx ); | 
|  | 9777 |  | 
|  | 9778 | if( ret != 0 ) | 
|  | 9779 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | 
|  | 9780 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | 
|  | 9781 |  | 
|  | 9782 | return( ret ); | 
|  | 9783 | } | 
|  | 9784 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | 
|  | 9785 | MBEDTLS_SSL_PROTO_TLS1_2 */ | 
|  | 9786 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9787 | #endif /* MBEDTLS_SSL_TLS_C */ |