Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 shared functions |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | /* |
| 22 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 23 | * and became an IETF standard in 1999. |
| 24 | * |
| 25 | * http://wp.netscape.com/eng/ssl3/ |
| 26 | * http://www.ietf.org/rfc/rfc2246.txt |
| 27 | * http://www.ietf.org/rfc/rfc4346.txt |
| 28 | */ |
| 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #if defined(MBEDTLS_PLATFORM_C) |
| 39 | #include "mbedtls/platform.h" |
| 40 | #else |
| 41 | #include <stdlib.h> |
| 42 | #define mbedtls_calloc calloc |
| 43 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 44 | #endif |
| 45 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/debug.h" |
| 47 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 48 | #include "mbedtls/ssl_internal.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 49 | #include "mbedtls/platform_util.h" |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 50 | #include "mbedtls/version.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 51 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 52 | #include <string.h> |
| 53 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 54 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 56 | #endif |
| 57 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 58 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 59 | static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 61 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 63 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 64 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 65 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 66 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 67 | |
| 68 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 69 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
| 70 | return( 2 ); |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 71 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 72 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 73 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 74 | return( 0 ); |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 75 | #endif |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Start a timer. |
| 80 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 81 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 83 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 84 | if( ssl->f_set_timer == NULL ) |
| 85 | return; |
| 86 | |
| 87 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 88 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Return -1 is timer is expired, 0 if it isn't. |
| 93 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 95 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 96 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 97 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 98 | |
| 99 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 100 | { |
| 101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 102 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 103 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 104 | |
| 105 | return( 0 ); |
| 106 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 107 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 108 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 109 | mbedtls_ssl_transform *transform ); |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 110 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 111 | |
| 112 | #define SSL_DONT_FORCE_FLUSH 0 |
| 113 | #define SSL_FORCE_FLUSH 1 |
| 114 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 115 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 116 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 117 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 118 | /* Top-level Connection ID API */ |
| 119 | |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 120 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 121 | size_t len, |
| 122 | int ignore_other_cid ) |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 123 | { |
| 124 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 125 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 126 | |
Hanno Becker | 791ec6b | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 127 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 128 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 129 | { |
| 130 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 131 | } |
| 132 | |
| 133 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 134 | conf->cid_len = len; |
| 135 | return( 0 ); |
| 136 | } |
| 137 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 138 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 139 | int enable, |
| 140 | unsigned char const *own_cid, |
| 141 | size_t own_cid_len ) |
| 142 | { |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 143 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 144 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 145 | |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 146 | ssl->negotiate_cid = enable; |
| 147 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 148 | { |
| 149 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 150 | return( 0 ); |
| 151 | } |
| 152 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 153 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 154 | |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 155 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 156 | { |
Hanno Becker | eec2be9 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 157 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 158 | (unsigned) own_cid_len, |
| 159 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 160 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 161 | } |
| 162 | |
| 163 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b4a5606 | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 164 | /* Truncation is not an issue here because |
| 165 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 166 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | 0748986 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 167 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 168 | return( 0 ); |
| 169 | } |
| 170 | |
| 171 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 172 | int *enabled, |
| 173 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 174 | size_t *peer_cid_len ) |
| 175 | { |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 176 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 177 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 178 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) || |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 179 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 180 | { |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 181 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 78c4302 | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 182 | } |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 183 | |
Hanno Becker | cb063f5 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 184 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 185 | * were used, but client and server requested the empty CID. |
| 186 | * This is indistinguishable from not using the CID extension |
| 187 | * in the first place. */ |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 188 | if( ssl->transform_in->in_cid_len == 0 && |
| 189 | ssl->transform_in->out_cid_len == 0 ) |
| 190 | { |
| 191 | return( 0 ); |
| 192 | } |
| 193 | |
Hanno Becker | 633d604 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 194 | if( peer_cid_len != NULL ) |
| 195 | { |
| 196 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 197 | if( peer_cid != NULL ) |
| 198 | { |
| 199 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 200 | ssl->transform_in->out_cid_len ); |
| 201 | } |
| 202 | } |
Hanno Becker | 2de89fa | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 203 | |
| 204 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 205 | |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 206 | return( 0 ); |
| 207 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 208 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b9e7dea | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 209 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 210 | /* Forward declarations for functions related to message buffering. */ |
| 211 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 212 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 213 | uint8_t slot ); |
| 214 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 215 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 216 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 217 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
| 218 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 219 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 220 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 221 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 222 | 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] | 223 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 224 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 225 | |
| 226 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 227 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 228 | |
| 229 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 230 | } |
| 231 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 232 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 233 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 234 | size_t const bytes_written = ssl->out_left; |
| 235 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 236 | |
| 237 | /* Double-check that the write-index hasn't gone |
| 238 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 239 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 240 | { |
| 241 | /* Should never happen... */ |
| 242 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 243 | } |
| 244 | |
| 245 | return( (int) ( mtu - bytes_written ) ); |
| 246 | } |
| 247 | |
| 248 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 249 | { |
| 250 | int ret; |
| 251 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 252 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 253 | |
| 254 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 255 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 256 | |
| 257 | if( max_len > mfl ) |
| 258 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 259 | |
| 260 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 261 | * only limits the maximum record payload size, so in theory |
| 262 | * we would be allowed to pack multiple records of payload size |
| 263 | * MFL into a single datagram. However, this would mean that there's |
| 264 | * no way to explicitly communicate MTU restrictions to the peer. |
| 265 | * |
| 266 | * The following reduction of max_len makes sure that we never |
| 267 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 268 | */ |
| 269 | if( max_len <= ssl->out_left ) |
| 270 | return( 0 ); |
| 271 | |
| 272 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 273 | #endif |
| 274 | |
| 275 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 276 | if( ret < 0 ) |
| 277 | return( ret ); |
| 278 | remaining = (size_t) ret; |
| 279 | |
| 280 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 281 | if( ret < 0 ) |
| 282 | return( ret ); |
| 283 | expansion = (size_t) ret; |
| 284 | |
| 285 | if( remaining <= expansion ) |
| 286 | return( 0 ); |
| 287 | |
| 288 | remaining -= expansion; |
| 289 | if( remaining >= max_len ) |
| 290 | remaining = max_len; |
| 291 | |
| 292 | return( (int) remaining ); |
| 293 | } |
| 294 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 295 | /* |
| 296 | * Double the retransmit timeout value, within the allowed range, |
| 297 | * returning -1 if the maximum value has already been reached. |
| 298 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 300 | { |
| 301 | uint32_t new_timeout; |
| 302 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 303 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 304 | return( -1 ); |
| 305 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 306 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 307 | * in the following way: after the initial transmission and a first |
| 308 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 309 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 310 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 311 | * on most non-IP stacks too. */ |
| 312 | if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 313 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 314 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 315 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 316 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 317 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 318 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 319 | |
| 320 | /* Avoid arithmetic overflow and range overflow */ |
| 321 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 322 | new_timeout > ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 323 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 324 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | 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] | 329 | ssl->handshake->retransmit_timeout ) ); |
| 330 | |
| 331 | return( 0 ); |
| 332 | } |
| 333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 334 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 335 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 336 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | 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] | 338 | ssl->handshake->retransmit_timeout ) ); |
| 339 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 342 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 343 | /* |
| 344 | * Convert max_fragment_length codes to length. |
| 345 | * RFC 6066 says: |
| 346 | * enum{ |
| 347 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 348 | * } MaxFragmentLength; |
| 349 | * and we add 0 -> extension unused |
| 350 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 351 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 352 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 353 | switch( mfl ) |
| 354 | { |
| 355 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 356 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 357 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 358 | return 512; |
| 359 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 360 | return 1024; |
| 361 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 362 | return 2048; |
| 363 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 364 | return 4096; |
| 365 | default: |
| 366 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 367 | } |
| 368 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 369 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 370 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 371 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 372 | 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] | 373 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | mbedtls_ssl_session_free( dst ); |
| 375 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 378 | if( src->peer_cert != NULL ) |
| 379 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 380 | int ret; |
| 381 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 382 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 383 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 384 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 385 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 386 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | 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] | 389 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 390 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 392 | dst->peer_cert = NULL; |
| 393 | return( ret ); |
| 394 | } |
| 395 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 397 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 398 | #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] | 399 | if( src->ticket != NULL ) |
| 400 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 401 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 402 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 403 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 404 | |
| 405 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 406 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 407 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 408 | |
| 409 | return( 0 ); |
| 410 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 411 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 412 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 414 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 415 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 416 | size_t keylen, |
| 417 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 418 | size_t ivlen, |
| 419 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 420 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 422 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 423 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 424 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 425 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 426 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 427 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 428 | /* |
| 429 | * Key material generation |
| 430 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 431 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 432 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 433 | const char *label, |
| 434 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 435 | unsigned char *dstbuf, size_t dlen ) |
| 436 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 437 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 438 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 439 | mbedtls_md5_context md5; |
| 440 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 441 | unsigned char padding[16]; |
| 442 | unsigned char sha1sum[20]; |
| 443 | ((void)label); |
| 444 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 445 | mbedtls_md5_init( &md5 ); |
| 446 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 447 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 448 | /* |
| 449 | * SSLv3: |
| 450 | * block = |
| 451 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 452 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 453 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 454 | * ... |
| 455 | */ |
| 456 | for( i = 0; i < dlen / 16; i++ ) |
| 457 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 458 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 459 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 460 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 461 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 462 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 463 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 464 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 465 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 466 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 467 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 468 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 469 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 470 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 471 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 472 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 473 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 474 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 475 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 476 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 477 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 478 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 481 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 482 | mbedtls_md5_free( &md5 ); |
| 483 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 484 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 485 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 486 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 487 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 488 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 489 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +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) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 493 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 494 | const char *label, |
| 495 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 496 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 498 | size_t nb, hs; |
| 499 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 500 | const unsigned char *S1, *S2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 501 | unsigned char tmp[128]; |
| 502 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 508 | |
| 509 | if( sizeof( tmp ) < 20 + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 511 | |
| 512 | hs = ( slen + 1 ) / 2; |
| 513 | S1 = secret; |
| 514 | S2 = secret + slen - hs; |
| 515 | |
| 516 | nb = strlen( label ); |
| 517 | memcpy( tmp + 20, label, nb ); |
| 518 | memcpy( tmp + 20 + nb, random, rlen ); |
| 519 | nb += rlen; |
| 520 | |
| 521 | /* |
| 522 | * First compute P_md5(secret,label+random)[0..dlen] |
| 523 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
| 525 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | 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] | 528 | return( ret ); |
| 529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 531 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 532 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 533 | |
| 534 | for( i = 0; i < dlen; i += 16 ) |
| 535 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 537 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 538 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 540 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 541 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 542 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 543 | |
| 544 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 545 | |
| 546 | for( j = 0; j < k; j++ ) |
| 547 | dstbuf[i + j] = h_i[j]; |
| 548 | } |
| 549 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 550 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 551 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 552 | /* |
| 553 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 554 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 555 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
| 556 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | 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] | 559 | return( ret ); |
| 560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 561 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 562 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 563 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 564 | |
| 565 | for( i = 0; i < dlen; i += 20 ) |
| 566 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 567 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 568 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 569 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 572 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 573 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 574 | |
| 575 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 576 | |
| 577 | for( j = 0; j < k; j++ ) |
| 578 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 579 | } |
| 580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 581 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 582 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 583 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 584 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 585 | |
| 586 | return( 0 ); |
| 587 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 591 | 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] | 592 | const unsigned char *secret, size_t slen, |
| 593 | const char *label, |
| 594 | const unsigned char *random, size_t rlen, |
| 595 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 596 | { |
| 597 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 598 | size_t i, j, k, md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 599 | unsigned char tmp[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 601 | const mbedtls_md_info_t *md_info; |
| 602 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 603 | int ret; |
| 604 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 606 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 608 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 611 | |
| 612 | if( sizeof( tmp ) < md_len + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 614 | |
| 615 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 616 | memcpy( tmp + md_len, label, nb ); |
| 617 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 618 | nb += rlen; |
| 619 | |
| 620 | /* |
| 621 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 622 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | 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] | 624 | return( ret ); |
| 625 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 627 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 628 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 629 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 630 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 633 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 634 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 636 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 637 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 638 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 639 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 640 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 641 | |
| 642 | for( j = 0; j < k; j++ ) |
| 643 | dstbuf[i + j] = h_i[j]; |
| 644 | } |
| 645 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 646 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 647 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 648 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 649 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 650 | |
| 651 | return( 0 ); |
| 652 | } |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 655 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 656 | const char *label, |
| 657 | const unsigned char *random, size_t rlen, |
| 658 | unsigned char *dstbuf, size_t dlen ) |
| 659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 660 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 661 | label, random, rlen, dstbuf, dlen ) ); |
| 662 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 666 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 667 | const char *label, |
| 668 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 669 | unsigned char *dstbuf, size_t dlen ) |
| 670 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 672 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 673 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | #endif /* MBEDTLS_SHA512_C */ |
| 675 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | 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] | 678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 680 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 681 | 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] | 682 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 683 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 685 | static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 687 | #endif |
| 688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 690 | static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 692 | #endif |
| 693 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 695 | #if defined(MBEDTLS_SHA256_C) |
| 696 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 697 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | 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] | 699 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | #if defined(MBEDTLS_SHA512_C) |
| 702 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 703 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | 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] | 705 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 706 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 707 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 708 | /* Type for the TLS PRF */ |
| 709 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 710 | const unsigned char *, size_t, |
| 711 | unsigned char *, size_t); |
| 712 | |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 713 | /* |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 714 | * Populate a transform structure with session keys and all the other |
| 715 | * necessary information. |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 716 | * |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 717 | * Parameters: |
| 718 | * - [in/out]: transform: structure to populate |
| 719 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 720 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 721 | * - [in] ciphersuite |
| 722 | * - [in] master |
| 723 | * - [in] encrypt_then_mac |
| 724 | * - [in] trunc_hmac |
| 725 | * - [in] compression |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 726 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 727 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 728 | * - [in] minor_ver: SSL/TLS minor version |
| 729 | * - [in] endpoint: client or server |
| 730 | * - [in] ssl: optionally used for: |
| 731 | * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context |
| 732 | * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys |
| 733 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 734 | */ |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 735 | static int ssl_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 736 | int ciphersuite, |
| 737 | const unsigned char master[48], |
| 738 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 739 | int encrypt_then_mac, |
| 740 | #endif |
| 741 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 742 | int trunc_hmac, |
| 743 | #endif |
| 744 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 745 | int compression, |
| 746 | #endif |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 747 | ssl_tls_prf_t tls_prf, |
| 748 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 749 | int minor_ver, |
| 750 | unsigned endpoint, |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 751 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 752 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 753 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 754 | unsigned char keyblk[256]; |
| 755 | unsigned char *key1; |
| 756 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 757 | unsigned char *mac_enc; |
| 758 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 759 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 760 | size_t iv_copy_len; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 761 | unsigned keylen; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 762 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | const mbedtls_cipher_info_t *cipher_info; |
| 764 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 765 | |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 766 | #if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \ |
| 767 | !defined(MBEDTLS_SSL_EXPORT_KEYS) && \ |
| 768 | !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 769 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 770 | (void) ssl; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 771 | #endif |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 772 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 773 | /* Copy info about negotiated version and extensions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 774 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 775 | transform->encrypt_then_mac = encrypt_then_mac; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 776 | #endif |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 777 | transform->minor_ver = minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 778 | |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 779 | /* |
| 780 | * Get various info structures |
| 781 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 782 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 783 | if( ciphersuite_info == NULL ) |
| 784 | { |
| 785 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 786 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 787 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 788 | } |
| 789 | |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 790 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 791 | if( cipher_info == NULL ) |
| 792 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 793 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 794 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 795 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 798 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 799 | if( md_info == NULL ) |
| 800 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 801 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 802 | ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 806 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 807 | /* Copy own and peer's CID if the use of the CID |
| 808 | * extension has been negotiated. */ |
| 809 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 810 | { |
| 811 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | d91dc37 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 812 | |
Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 813 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 4932f9f | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 814 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 8013b27 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 815 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 816 | transform->in_cid_len ); |
Hanno Becker | e582d12 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 817 | |
| 818 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 819 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 820 | ssl->handshake->peer_cid_len ); |
| 821 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 822 | transform->out_cid_len ); |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 823 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 824 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | dd0afca | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 825 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 826 | /* |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 827 | * Compute key block using the PRF |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 828 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 829 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 830 | if( ret != 0 ) |
| 831 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 832 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 833 | return( ret ); |
| 834 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 835 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 837 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 838 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 839 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 840 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 841 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 842 | /* |
| 843 | * Determine the appropriate key, IV and MAC length. |
| 844 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 845 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 846 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 847 | |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 848 | #if defined(MBEDTLS_GCM_C) || \ |
| 849 | defined(MBEDTLS_CCM_C) || \ |
| 850 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 852 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 853 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 854 | { |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 855 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 856 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 857 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 858 | mac_key_len = 0; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 859 | transform->taglen = |
| 860 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 861 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 862 | /* All modes haves 96-bit IVs; |
| 863 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 864 | * ChachaPoly has all 12 bytes implicit |
| 865 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 866 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 867 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 868 | transform->fixed_ivlen = 12; |
| 869 | else |
| 870 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 871 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 872 | /* Minimum length of encrypted record */ |
| 873 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 874 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 875 | } |
| 876 | else |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 877 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 878 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 879 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 880 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 881 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 882 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 883 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 884 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 887 | return( ret ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 888 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 889 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 890 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 891 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 892 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 893 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 894 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 895 | /* |
| 896 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 897 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 898 | * so we only need to adjust the length here. |
| 899 | */ |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 900 | if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 901 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 902 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 903 | |
| 904 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 905 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 906 | * HMAC implementation which also truncates the key |
| 907 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 908 | mac_key_len = transform->maclen; |
| 909 | #endif |
| 910 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 911 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 912 | |
| 913 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 914 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 915 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 916 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 917 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 918 | transform->minlen = transform->maclen; |
| 919 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 920 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 921 | /* |
| 922 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 923 | * 1. if EtM is in use: one block plus MAC |
| 924 | * otherwise: * first multiple of blocklen greater than maclen |
| 925 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 926 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 928 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 929 | { |
| 930 | transform->minlen = transform->maclen |
| 931 | + cipher_info->block_size; |
| 932 | } |
| 933 | else |
| 934 | #endif |
| 935 | { |
| 936 | transform->minlen = transform->maclen |
| 937 | + cipher_info->block_size |
| 938 | - transform->maclen % cipher_info->block_size; |
| 939 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 940 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 941 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 942 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 943 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 944 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 945 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 946 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 947 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 948 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 949 | minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 950 | { |
| 951 | transform->minlen += transform->ivlen; |
| 952 | } |
| 953 | else |
| 954 | #endif |
| 955 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 956 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 957 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 958 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 959 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 960 | } |
Hanno Becker | f122944 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 961 | else |
| 962 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 963 | { |
| 964 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 965 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 966 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 967 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 968 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 969 | (unsigned) keylen, |
| 970 | (unsigned) transform->minlen, |
| 971 | (unsigned) transform->ivlen, |
| 972 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 973 | |
| 974 | /* |
| 975 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 976 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 977 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 978 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 979 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 980 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 981 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 982 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 983 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 984 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 985 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 986 | /* |
| 987 | * This is not used in TLS v1.1. |
| 988 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 989 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 990 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 991 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 992 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 993 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 994 | } |
| 995 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 996 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 997 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 998 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 999 | { |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1000 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1001 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1002 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1003 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1004 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1005 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1006 | /* |
| 1007 | * This is not used in TLS v1.1. |
| 1008 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1009 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1010 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1011 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1012 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1013 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1014 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1015 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1017 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1018 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1019 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1020 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1021 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1022 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1023 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1024 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1025 | { |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1026 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1027 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1029 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1030 | } |
| 1031 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1032 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1033 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1034 | } |
| 1035 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1036 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1037 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1038 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1039 | if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1040 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1041 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1042 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1043 | if( mac_key_len != 0 ) |
| 1044 | { |
| 1045 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1046 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1047 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1048 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1049 | else |
| 1050 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1052 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1053 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1054 | } |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1055 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1056 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1058 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1059 | { |
| 1060 | int ret = 0; |
| 1061 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1062 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1063 | |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1064 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1065 | transform->iv_enc, transform->iv_dec, |
| 1066 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1067 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1068 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1069 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1070 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
| 1071 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1074 | #else |
| 1075 | ((void) mac_dec); |
| 1076 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1078 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1079 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1080 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1081 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1082 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1083 | master, keyblk, |
Hanno Becker | e7f2df0 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1084 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1085 | iv_copy_len ); |
| 1086 | } |
| 1087 | #endif |
| 1088 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1089 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1090 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1091 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1092 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1093 | return( ret ); |
| 1094 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1095 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1096 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1097 | cipher_info ) ) != 0 ) |
| 1098 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1099 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1100 | return( ret ); |
| 1101 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1103 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1104 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1108 | return( ret ); |
| 1109 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1111 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1112 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1113 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1114 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1115 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1116 | return( ret ); |
| 1117 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1118 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1119 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1120 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1121 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1122 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1123 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1124 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1125 | 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] | 1126 | return( ret ); |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1127 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1129 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1130 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1131 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1132 | 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] | 1133 | return( ret ); |
| 1134 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1135 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1136 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1137 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1138 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1139 | |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1140 | /* Initialize Zlib contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1141 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1142 | if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1143 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1144 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1145 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1146 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1147 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1148 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1149 | if( deflateInit( &transform->ctx_deflate, |
| 1150 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1151 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1152 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1153 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
| 1154 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1157 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1158 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1159 | return( 0 ); |
| 1160 | } |
| 1161 | |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1162 | /* |
Manuel Pégourié-Gonnard | 42c814f | 2019-05-20 10:10:17 +0200 | [diff] [blame] | 1163 | * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1164 | * |
| 1165 | * Inputs: |
| 1166 | * - SSL/TLS minor version |
| 1167 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1168 | * |
Manuel Pégourié-Gonnard | cf31216 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1169 | * Outputs: |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1170 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1171 | */ |
| 1172 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1173 | int minor_ver, |
| 1174 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1175 | { |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1176 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C) |
| 1177 | (void) hash; |
| 1178 | #endif |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1179 | |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1180 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1181 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1182 | { |
| 1183 | handshake->tls_prf = ssl3_prf; |
| 1184 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1185 | handshake->calc_finished = ssl_calc_finished_ssl; |
| 1186 | } |
| 1187 | else |
| 1188 | #endif |
| 1189 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1190 | if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1191 | { |
| 1192 | handshake->tls_prf = tls1_prf; |
| 1193 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1194 | handshake->calc_finished = ssl_calc_finished_tls; |
| 1195 | } |
| 1196 | else |
| 1197 | #endif |
| 1198 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1199 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1200 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1201 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1202 | { |
| 1203 | handshake->tls_prf = tls_prf_sha384; |
| 1204 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1205 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1206 | } |
| 1207 | else |
| 1208 | #endif |
| 1209 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1210 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1211 | { |
| 1212 | handshake->tls_prf = tls_prf_sha256; |
| 1213 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1214 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1215 | } |
| 1216 | else |
| 1217 | #endif |
| 1218 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1219 | { |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1220 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1221 | } |
| 1222 | |
| 1223 | return( 0 ); |
| 1224 | } |
| 1225 | |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1226 | /* |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1227 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1228 | * |
| 1229 | * Parameters: |
| 1230 | * [in/out] handshake |
| 1231 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 1232 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1233 | * [out] master |
| 1234 | * [in] ssl: optionally used for debugging and calc_verify |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1235 | */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1236 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1237 | unsigned char *master, |
Manuel Pégourié-Gonnard | ed3b7a9 | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1238 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1239 | { |
| 1240 | int ret; |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1241 | |
| 1242 | #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1243 | ssl = NULL; /* make sure we don't use it except for debug and EMS */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1244 | (void) ssl; |
| 1245 | #endif |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1246 | |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1247 | if( handshake->resume != 0 ) |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1248 | { |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1249 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 1250 | return( 0 ); |
| 1251 | } |
| 1252 | |
| 1253 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, |
| 1254 | handshake->pmslen ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1255 | |
| 1256 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1257 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1258 | { |
| 1259 | unsigned char session_hash[48]; |
| 1260 | size_t hash_len; |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1261 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1262 | handshake->calc_verify( ssl, session_hash, &hash_len ); |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1263 | |
Manuel Pégourié-Gonnard | 9c5bcc9 | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1264 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1265 | session_hash, hash_len ); |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1266 | |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1267 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1268 | "extended master secret", |
| 1269 | session_hash, hash_len, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1270 | master, 48 ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1271 | } |
| 1272 | else |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1273 | #endif |
Manuel Pégourié-Gonnard | bcf258e | 2019-05-03 11:46:27 +0200 | [diff] [blame] | 1274 | { |
| 1275 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1276 | "master secret", |
| 1277 | handshake->randbytes, 64, |
| 1278 | master, 48 ); |
| 1279 | } |
Manuel Pégourié-Gonnard | dafe522 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1280 | if( ret != 0 ) |
| 1281 | { |
| 1282 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1283 | return( ret ); |
| 1284 | } |
| 1285 | |
| 1286 | mbedtls_platform_zeroize( handshake->premaster, |
| 1287 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1288 | |
| 1289 | return( 0 ); |
| 1290 | } |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1291 | |
Manuel Pégourié-Gonnard | 5ed5e90 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1292 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1293 | { |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1294 | int ret; |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1295 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1296 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1297 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1298 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1299 | |
| 1300 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1301 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1302 | ssl->minor_ver, |
| 1303 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1304 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1305 | { |
| 1306 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1307 | return( ret ); |
Manuel Pégourié-Gonnard | aa3c701 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1308 | } |
Manuel Pégourié-Gonnard | 52aa520 | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1309 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1310 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1311 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | c28c889 | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1312 | ssl->session_negotiate->master, |
| 1313 | ssl ); |
Manuel Pégourié-Gonnard | 7edd587 | 2019-05-03 09:05:41 +0200 | [diff] [blame] | 1314 | if( ret != 0 ) |
| 1315 | { |
| 1316 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1317 | return( ret ); |
| 1318 | } |
| 1319 | |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1320 | /* Swap the client and server random values: |
| 1321 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1322 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1323 | { |
| 1324 | unsigned char tmp[64]; |
| 1325 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1326 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1327 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1328 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1329 | } |
| 1330 | |
| 1331 | /* Populate transform structure */ |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1332 | ret = ssl_populate_transform( ssl->transform_negotiate, |
Manuel Pégourié-Gonnard | 84ef8bd | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 1333 | ssl->session_negotiate->ciphersuite, |
| 1334 | ssl->session_negotiate->master, |
| 1335 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1336 | ssl->session_negotiate->encrypt_then_mac, |
| 1337 | #endif |
| 1338 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1339 | ssl->session_negotiate->trunc_hmac, |
| 1340 | #endif |
| 1341 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1342 | ssl->session_negotiate->compression, |
| 1343 | #endif |
Manuel Pégourié-Gonnard | 0bcfbc3 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 1344 | ssl->handshake->tls_prf, |
| 1345 | ssl->handshake->randbytes, |
Manuel Pégourié-Gonnard | 1d10a98 | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 1346 | ssl->minor_ver, |
| 1347 | ssl->conf->endpoint, |
Manuel Pégourié-Gonnard | 12a3f44 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 1348 | ssl ); |
Manuel Pégourié-Gonnard | 707728d | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1349 | if( ret != 0 ) |
| 1350 | { |
| 1351 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret ); |
| 1352 | return( ret ); |
| 1353 | } |
| 1354 | |
| 1355 | /* We no longer need Server/ClientHello.random values */ |
| 1356 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1357 | sizeof( ssl->handshake->randbytes ) ); |
| 1358 | |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1359 | /* Allocate compression buffer */ |
| 1360 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 1361 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE && |
| 1362 | ssl->compress_buf == NULL ) |
| 1363 | { |
| 1364 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
| 1365 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
| 1366 | if( ssl->compress_buf == NULL ) |
| 1367 | { |
| 1368 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | 762d011 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 1369 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | a1abb26 | 2019-05-06 12:44:24 +0200 | [diff] [blame] | 1370 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1371 | } |
| 1372 | } |
| 1373 | #endif |
| 1374 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1375 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1376 | |
| 1377 | return( 0 ); |
| 1378 | } |
| 1379 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1380 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1381 | void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl, |
| 1382 | unsigned char hash[36], |
| 1383 | size_t *hlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1384 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1385 | mbedtls_md5_context md5; |
| 1386 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1387 | unsigned char pad_1[48]; |
| 1388 | unsigned char pad_2[48]; |
| 1389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1390 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1391 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1392 | mbedtls_md5_init( &md5 ); |
| 1393 | mbedtls_sha1_init( &sha1 ); |
| 1394 | |
| 1395 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1396 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1397 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1398 | memset( pad_1, 0x36, 48 ); |
| 1399 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1400 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1401 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1402 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1403 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1404 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1405 | mbedtls_md5_starts_ret( &md5 ); |
| 1406 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1407 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1408 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1409 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1410 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1411 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1412 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1413 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1414 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1415 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1416 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1417 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1418 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1419 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1420 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1421 | *hlen = 36; |
| 1422 | |
| 1423 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1424 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1425 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1426 | mbedtls_md5_free( &md5 ); |
| 1427 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1428 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1429 | return; |
| 1430 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1431 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1432 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1433 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1434 | void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl, |
| 1435 | unsigned char hash[36], |
| 1436 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1437 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1438 | mbedtls_md5_context md5; |
| 1439 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1441 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1442 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1443 | mbedtls_md5_init( &md5 ); |
| 1444 | mbedtls_sha1_init( &sha1 ); |
| 1445 | |
| 1446 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1447 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1448 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1449 | mbedtls_md5_finish_ret( &md5, hash ); |
| 1450 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1451 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1452 | *hlen = 36; |
| 1453 | |
| 1454 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1455 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1456 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1457 | mbedtls_md5_free( &md5 ); |
| 1458 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1459 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1460 | return; |
| 1461 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1464 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1465 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1466 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 1467 | unsigned char hash[32], |
| 1468 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1469 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1470 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1471 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1472 | mbedtls_sha256_init( &sha256 ); |
| 1473 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1474 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1475 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1476 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1477 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1478 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1479 | *hlen = 32; |
| 1480 | |
| 1481 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1482 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1483 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1484 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1485 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1486 | return; |
| 1487 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1491 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 1492 | unsigned char hash[48], |
| 1493 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1494 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1495 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1496 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1497 | mbedtls_sha512_init( &sha512 ); |
| 1498 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1499 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1500 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1501 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1502 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1503 | |
Manuel Pégourié-Gonnard | a575975 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1504 | *hlen = 48; |
| 1505 | |
| 1506 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1507 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1508 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1509 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1510 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1511 | return; |
| 1512 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1513 | #endif /* MBEDTLS_SHA512_C */ |
| 1514 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1516 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1517 | 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] | 1518 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1519 | unsigned char *p = ssl->handshake->premaster; |
| 1520 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1521 | const unsigned char *psk = ssl->conf->psk; |
| 1522 | size_t psk_len = ssl->conf->psk_len; |
| 1523 | |
| 1524 | /* If the psk callback was called, use its result */ |
| 1525 | if( ssl->handshake->psk != NULL ) |
| 1526 | { |
| 1527 | psk = ssl->handshake->psk; |
| 1528 | psk_len = ssl->handshake->psk_len; |
| 1529 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1530 | |
| 1531 | /* |
| 1532 | * PMS = struct { |
| 1533 | * opaque other_secret<0..2^16-1>; |
| 1534 | * opaque psk<0..2^16-1>; |
| 1535 | * }; |
| 1536 | * with "other_secret" depending on the particular key exchange |
| 1537 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1538 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1539 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1540 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1541 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1543 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1544 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1545 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1546 | |
| 1547 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1548 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1549 | |
| 1550 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1551 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1552 | } |
| 1553 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1554 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1555 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1556 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1557 | { |
| 1558 | /* |
| 1559 | * other_secret already set by the ClientKeyExchange message, |
| 1560 | * and is 48 bytes long |
| 1561 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1562 | if( end - p < 2 ) |
| 1563 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1564 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1565 | *p++ = 0; |
| 1566 | *p++ = 48; |
| 1567 | p += 48; |
| 1568 | } |
| 1569 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1570 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1571 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1572 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1573 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1574 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1575 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1576 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1577 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1578 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1579 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1580 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1581 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1582 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1583 | return( ret ); |
| 1584 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1585 | *(p++) = (unsigned char)( len >> 8 ); |
| 1586 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1587 | p += len; |
| 1588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1589 | 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] | 1590 | } |
| 1591 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1592 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1593 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1594 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1595 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1596 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1597 | size_t zlen; |
| 1598 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1599 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1600 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1601 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1602 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1603 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1604 | return( ret ); |
| 1605 | } |
| 1606 | |
| 1607 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 1608 | *(p++) = (unsigned char)( zlen ); |
| 1609 | p += zlen; |
| 1610 | |
Janos Follath | 3fbdada | 2018-08-15 10:26:53 +0100 | [diff] [blame] | 1611 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1612 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1613 | } |
| 1614 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1615 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1616 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1617 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1618 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1622 | if( end - p < 2 ) |
| 1623 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1624 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1625 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1626 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1627 | |
| 1628 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1629 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1630 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1631 | memcpy( p, psk, psk_len ); |
| 1632 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1633 | |
| 1634 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1635 | |
| 1636 | return( 0 ); |
| 1637 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1638 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1641 | /* |
| 1642 | * SSLv3.0 MAC functions |
| 1643 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1644 | #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] | 1645 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 1646 | const unsigned char *secret, |
| 1647 | const unsigned char *buf, size_t len, |
| 1648 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1649 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1650 | { |
| 1651 | unsigned char header[11]; |
| 1652 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1653 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1654 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 1655 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1656 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1657 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1658 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1659 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1660 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1661 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1662 | |
| 1663 | memcpy( header, ctr, 8 ); |
| 1664 | header[ 8] = (unsigned char) type; |
| 1665 | header[ 9] = (unsigned char)( len >> 8 ); |
| 1666 | header[10] = (unsigned char)( len ); |
| 1667 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1668 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | mbedtls_md_starts( md_ctx ); |
| 1670 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1671 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 1672 | mbedtls_md_update( md_ctx, header, 11 ); |
| 1673 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1674 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1675 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1676 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1677 | mbedtls_md_starts( md_ctx ); |
| 1678 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1679 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1680 | mbedtls_md_update( md_ctx, out, md_size ); |
| 1681 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1682 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1683 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1684 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1685 | /* The function below is only used in the Lucky 13 counter-measure in |
Hanno Becker | 30d02cd | 2018-10-18 15:43:13 +0100 | [diff] [blame] | 1686 | * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1687 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1688 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 1689 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1690 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 1691 | /* This function makes sure every byte in the memory region is accessed |
| 1692 | * (in ascending addresses order) */ |
| 1693 | static void ssl_read_memory( unsigned char *p, size_t len ) |
| 1694 | { |
| 1695 | unsigned char acc = 0; |
| 1696 | volatile unsigned char force; |
| 1697 | |
| 1698 | for( ; len != 0; p++, len-- ) |
| 1699 | acc ^= *p; |
| 1700 | |
| 1701 | force = acc; |
| 1702 | (void) force; |
| 1703 | } |
| 1704 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 1705 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1706 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1707 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 1708 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1709 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1710 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8969369 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 1711 | /* This functions transforms a DTLS plaintext fragment and a record content |
| 1712 | * type into an instance of the DTLSInnerPlaintext structure: |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1713 | * |
| 1714 | * struct { |
| 1715 | * opaque content[DTLSPlaintext.length]; |
| 1716 | * ContentType real_type; |
| 1717 | * uint8 zeros[length_of_padding]; |
| 1718 | * } DTLSInnerPlaintext; |
| 1719 | * |
| 1720 | * Input: |
| 1721 | * - `content`: The beginning of the buffer holding the |
| 1722 | * plaintext to be wrapped. |
| 1723 | * - `*content_size`: The length of the plaintext in Bytes. |
| 1724 | * - `max_len`: The number of Bytes available starting from |
| 1725 | * `content`. This must be `>= *content_size`. |
| 1726 | * - `rec_type`: The desired record content type. |
| 1727 | * |
| 1728 | * Output: |
| 1729 | * - `content`: The beginning of the resulting DTLSInnerPlaintext structure. |
| 1730 | * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. |
| 1731 | * |
| 1732 | * Returns: |
| 1733 | * - `0` on success. |
| 1734 | * - A negative error code if `max_len` didn't offer enough space |
| 1735 | * for the expansion. |
| 1736 | */ |
| 1737 | static int ssl_cid_build_inner_plaintext( unsigned char *content, |
| 1738 | size_t *content_size, |
| 1739 | size_t remaining, |
| 1740 | uint8_t rec_type ) |
| 1741 | { |
| 1742 | size_t len = *content_size; |
Hanno Becker | 7842609 | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 1743 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - |
| 1744 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % |
| 1745 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1746 | |
| 1747 | /* Write real content type */ |
| 1748 | if( remaining == 0 ) |
| 1749 | return( -1 ); |
| 1750 | content[ len ] = rec_type; |
| 1751 | len++; |
| 1752 | remaining--; |
| 1753 | |
| 1754 | if( remaining < pad ) |
| 1755 | return( -1 ); |
| 1756 | memset( content + len, 0, pad ); |
| 1757 | len += pad; |
| 1758 | remaining -= pad; |
| 1759 | |
| 1760 | *content_size = len; |
| 1761 | return( 0 ); |
| 1762 | } |
| 1763 | |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 1764 | /* This function parses a DTLSInnerPlaintext structure. |
| 1765 | * See ssl_cid_build_inner_plaintext() for details. */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1766 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, |
| 1767 | size_t *content_size, |
| 1768 | uint8_t *rec_type ) |
| 1769 | { |
| 1770 | size_t remaining = *content_size; |
| 1771 | |
| 1772 | /* Determine length of padding by skipping zeroes from the back. */ |
| 1773 | do |
| 1774 | { |
| 1775 | if( remaining == 0 ) |
| 1776 | return( -1 ); |
| 1777 | remaining--; |
| 1778 | } while( content[ remaining ] == 0 ); |
| 1779 | |
| 1780 | *content_size = remaining; |
| 1781 | *rec_type = content[ remaining ]; |
| 1782 | |
| 1783 | return( 0 ); |
| 1784 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1785 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1786 | |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 1787 | /* `add_data` must have size 13 Bytes if the CID extension is disabled, |
Hanno Becker | acadb0a | 2019-05-08 18:15:21 +0100 | [diff] [blame] | 1788 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1789 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1790 | size_t *add_data_len, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1791 | mbedtls_record *rec ) |
| 1792 | { |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 1793 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1794 | * |
| 1795 | * additional_data = seq_num + TLSCompressed.type + |
| 1796 | * TLSCompressed.version + TLSCompressed.length; |
| 1797 | * |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 1798 | * For the CID extension, this is extended as follows |
| 1799 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 1800 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1801 | * |
| 1802 | * additional_data = seq_num + DTLSPlaintext.type + |
| 1803 | * DTLSPlaintext.version + |
Hanno Becker | 99abf51 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 1804 | * cid + |
| 1805 | * cid_length + |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1806 | * length_of_DTLSInnerPlaintext; |
| 1807 | */ |
| 1808 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1809 | memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); |
| 1810 | add_data[8] = rec->type; |
Hanno Becker | 24ce1eb | 2019-05-20 15:01:46 +0100 | [diff] [blame] | 1811 | memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1812 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1813 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 1814 | if( rec->cid_len != 0 ) |
| 1815 | { |
| 1816 | memcpy( add_data + 11, rec->cid, rec->cid_len ); |
| 1817 | add_data[11 + rec->cid_len + 0] = rec->cid_len; |
| 1818 | add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF; |
| 1819 | add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF; |
| 1820 | *add_data_len = 13 + 1 + rec->cid_len; |
| 1821 | } |
| 1822 | else |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1823 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 1f02f05 | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 1824 | { |
| 1825 | add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF; |
| 1826 | add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF; |
| 1827 | *add_data_len = 13; |
| 1828 | } |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1831 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 1832 | mbedtls_ssl_transform *transform, |
| 1833 | mbedtls_record *rec, |
| 1834 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1835 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1836 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1837 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1838 | int auth_done = 0; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1839 | unsigned char * data; |
Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 1840 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1841 | size_t add_data_len; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1842 | size_t post_avail; |
| 1843 | |
| 1844 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1845 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1846 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1847 | ((void) ssl); |
| 1848 | #endif |
| 1849 | |
| 1850 | /* The PRNG is used for dynamic IV generation that's used |
| 1851 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
| 1852 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 1853 | ( defined(MBEDTLS_AES_C) || \ |
| 1854 | defined(MBEDTLS_ARIA_C) || \ |
| 1855 | defined(MBEDTLS_CAMELLIA_C) ) && \ |
| 1856 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) |
| 1857 | ((void) f_rng); |
| 1858 | ((void) p_rng); |
| 1859 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1860 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1861 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1862 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1863 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1864 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1865 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 1866 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1867 | } |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1868 | if( rec == NULL |
| 1869 | || rec->buf == NULL |
| 1870 | || rec->buf_len < rec->data_offset |
| 1871 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1872 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1873 | || rec->cid_len != 0 |
| 1874 | #endif |
| 1875 | ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1876 | { |
| 1877 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1878 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1881 | data = rec->buf + rec->data_offset; |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1882 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1883 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1884 | data, rec->data_len ); |
| 1885 | |
| 1886 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 1887 | |
| 1888 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 1889 | { |
| 1890 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", |
| 1891 | (unsigned) rec->data_len, |
| 1892 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 1893 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1894 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 1895 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1896 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1897 | /* |
| 1898 | * Add CID information |
| 1899 | */ |
| 1900 | rec->cid_len = transform->out_cid_len; |
| 1901 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 1902 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1903 | |
| 1904 | if( rec->cid_len != 0 ) |
| 1905 | { |
| 1906 | /* |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 1907 | * Wrap plaintext into DTLSInnerPlaintext structure. |
| 1908 | * See ssl_cid_build_inner_plaintext() for more information. |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1909 | * |
Hanno Becker | 7dc2577 | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 1910 | * Note that this changes `rec->data_len`, and hence |
| 1911 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1912 | */ |
| 1913 | if( ssl_cid_build_inner_plaintext( data, |
| 1914 | &rec->data_len, |
| 1915 | post_avail, |
| 1916 | rec->type ) != 0 ) |
| 1917 | { |
| 1918 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 1919 | } |
| 1920 | |
| 1921 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 1922 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1923 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1924 | |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1925 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 1926 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1927 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1928 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1929 | */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1930 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1931 | if( mode == MBEDTLS_MODE_STREAM || |
| 1932 | ( mode == MBEDTLS_MODE_CBC |
| 1933 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1934 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1935 | #endif |
| 1936 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1937 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1938 | if( post_avail < transform->maclen ) |
| 1939 | { |
| 1940 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 1941 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 1942 | } |
| 1943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1944 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1945 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1946 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1947 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1948 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, |
| 1949 | data, rec->data_len, rec->ctr, rec->type, mac ); |
| 1950 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1951 | } |
| 1952 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1953 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1954 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1955 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1956 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1957 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1958 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 1959 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1960 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1961 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1962 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1963 | add_data_len ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1964 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 1965 | data, rec->data_len ); |
| 1966 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 1967 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
| 1968 | |
| 1969 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1970 | } |
| 1971 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1972 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1973 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1974 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1975 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1976 | } |
| 1977 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1978 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 1979 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1980 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1981 | rec->data_len += transform->maclen; |
| 1982 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1983 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1984 | } |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1985 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1986 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1987 | /* |
| 1988 | * Encrypt |
| 1989 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1990 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 1991 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1992 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1993 | int ret; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1994 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1995 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1996 | "including %d bytes of padding", |
| 1997 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1998 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1999 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2000 | transform->iv_enc, transform->ivlen, |
| 2001 | data, rec->data_len, |
| 2002 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2003 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2004 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2005 | return( ret ); |
| 2006 | } |
| 2007 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2008 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2009 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2010 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2011 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2012 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2013 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2014 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2015 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2016 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2017 | #if defined(MBEDTLS_GCM_C) || \ |
| 2018 | defined(MBEDTLS_CCM_C) || \ |
| 2019 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2020 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2021 | mode == MBEDTLS_MODE_CCM || |
| 2022 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2023 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2024 | int ret; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2025 | unsigned char iv[12]; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2026 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2027 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2028 | /* Check that there's space for both the authentication tag |
| 2029 | * and the explicit IV before and after the record content. */ |
| 2030 | if( post_avail < transform->taglen || |
| 2031 | rec->data_offset < explicit_iv_len ) |
| 2032 | { |
| 2033 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2034 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2035 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2036 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2037 | /* |
| 2038 | * Generate IV |
| 2039 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2040 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2041 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2042 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2043 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2044 | memcpy( iv + transform->fixed_ivlen, rec->ctr, |
| 2045 | explicit_iv_len ); |
| 2046 | /* Prefix record content with explicit IV. */ |
| 2047 | memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2048 | } |
| 2049 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2050 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2051 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2052 | unsigned char i; |
| 2053 | |
| 2054 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 2055 | |
| 2056 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2057 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2058 | } |
| 2059 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2060 | { |
| 2061 | /* 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] | 2062 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2063 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2064 | } |
| 2065 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2066 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 0888581 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2067 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2068 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 2069 | iv, transform->ivlen ); |
| 2070 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2071 | data - explicit_iv_len, explicit_iv_len ); |
| 2072 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2073 | add_data, add_data_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2074 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2075 | "including 0 bytes of padding", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2076 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2077 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2078 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2079 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2080 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2081 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2082 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2083 | iv, transform->ivlen, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2084 | add_data, add_data_len, /* add data */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2085 | data, rec->data_len, /* source */ |
| 2086 | data, &rec->data_len, /* destination */ |
| 2087 | data + rec->data_len, transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2088 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2089 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2090 | return( ret ); |
| 2091 | } |
| 2092 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2093 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
| 2094 | data + rec->data_len, transform->taglen ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2095 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2096 | rec->data_len += transform->taglen + explicit_iv_len; |
| 2097 | rec->data_offset -= explicit_iv_len; |
| 2098 | post_avail -= transform->taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2099 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2100 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2101 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2102 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2103 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2104 | ( 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] | 2105 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2106 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2107 | int ret; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2108 | size_t padlen, i; |
| 2109 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2110 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2111 | /* Currently we're always using minimal padding |
| 2112 | * (up to 255 bytes would be allowed). */ |
| 2113 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 2114 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2115 | padlen = 0; |
| 2116 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2117 | /* Check there's enough space in the buffer for the padding. */ |
| 2118 | if( post_avail < padlen + 1 ) |
| 2119 | { |
| 2120 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2121 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2122 | } |
| 2123 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2124 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2125 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2126 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2127 | rec->data_len += padlen + 1; |
| 2128 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2130 | #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] | 2131 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2132 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 2133 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2134 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2135 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2136 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2137 | if( f_rng == NULL ) |
| 2138 | { |
| 2139 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 2140 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2141 | } |
| 2142 | |
| 2143 | if( rec->data_offset < transform->ivlen ) |
| 2144 | { |
| 2145 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2146 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2147 | } |
| 2148 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2149 | /* |
| 2150 | * Generate IV |
| 2151 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2152 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2153 | if( ret != 0 ) |
| 2154 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2155 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2156 | memcpy( data - transform->ivlen, transform->iv_enc, |
| 2157 | transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2158 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2159 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2160 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2162 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2163 | "including %d bytes of IV and %d bytes of padding", |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2164 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2165 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2166 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2167 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2168 | transform->iv_enc, |
| 2169 | transform->ivlen, |
| 2170 | data, rec->data_len, |
| 2171 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2172 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2173 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2174 | return( ret ); |
| 2175 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2176 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2177 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2178 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2179 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2180 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2181 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2183 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2184 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2185 | { |
| 2186 | /* |
| 2187 | * Save IV in SSL3 and TLS1 |
| 2188 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2189 | memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 2190 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2191 | } |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2192 | else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2193 | #endif |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2194 | { |
| 2195 | data -= transform->ivlen; |
| 2196 | rec->data_offset -= transform->ivlen; |
| 2197 | rec->data_len += transform->ivlen; |
| 2198 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2199 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2200 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2201 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2202 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2203 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2204 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2205 | /* |
| 2206 | * MAC(MAC_write_key, seq_num + |
| 2207 | * TLSCipherText.type + |
| 2208 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2209 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2210 | * IV + // except for TLS 1.0 |
| 2211 | * ENC(content + padding + padding_length)); |
| 2212 | */ |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2213 | |
| 2214 | if( post_avail < transform->maclen) |
| 2215 | { |
| 2216 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2217 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2218 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2219 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2220 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2222 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2223 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2224 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2225 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2226 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2227 | add_data_len ); |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2228 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2229 | data, rec->data_len ); |
| 2230 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2231 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2232 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2233 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2234 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2235 | rec->data_len += transform->maclen; |
| 2236 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2237 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2238 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2239 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2240 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2241 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2242 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2243 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2244 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2245 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2246 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2247 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2248 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2249 | /* Make extra sure authentication was performed, exactly once */ |
| 2250 | if( auth_done != 1 ) |
| 2251 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2252 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2253 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2254 | } |
| 2255 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2256 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2257 | |
| 2258 | return( 0 ); |
| 2259 | } |
| 2260 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2261 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl, |
| 2262 | mbedtls_ssl_transform *transform, |
| 2263 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2264 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2265 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2266 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2267 | int ret, auth_done = 0; |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2268 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2269 | size_t padlen = 0, correct = 1; |
| 2270 | #endif |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2271 | unsigned char* data; |
Hanno Becker | 28a0c4e | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2272 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2273 | size_t add_data_len; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2274 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2275 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | 86e48c2 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 2276 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2277 | ((void) ssl); |
| 2278 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2279 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2280 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2281 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2282 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2283 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) ); |
| 2284 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2285 | } |
| 2286 | if( rec == NULL || |
| 2287 | rec->buf == NULL || |
| 2288 | rec->buf_len < rec->data_offset || |
| 2289 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 2290 | { |
| 2291 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2292 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2293 | } |
| 2294 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2295 | data = rec->buf + rec->data_offset; |
| 2296 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2297 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2298 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2299 | /* |
| 2300 | * Match record's CID with incoming CID. |
| 2301 | */ |
Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2302 | if( rec->cid_len != transform->in_cid_len || |
| 2303 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 2304 | { |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2305 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | abd7c89 | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2306 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2307 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2309 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2310 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2311 | { |
| 2312 | padlen = 0; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2313 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2314 | transform->iv_dec, |
| 2315 | transform->ivlen, |
| 2316 | data, rec->data_len, |
| 2317 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2318 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2319 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2320 | return( ret ); |
| 2321 | } |
| 2322 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2323 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2324 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2325 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2326 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2327 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2328 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2329 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2330 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2331 | #if defined(MBEDTLS_GCM_C) || \ |
| 2332 | defined(MBEDTLS_CCM_C) || \ |
| 2333 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2334 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2335 | mode == MBEDTLS_MODE_CCM || |
| 2336 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2337 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2338 | unsigned char iv[12]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2339 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2340 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2341 | /* |
| 2342 | * Compute and update sizes |
| 2343 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2344 | if( rec->data_len < explicit_iv_len + transform->taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2345 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2346 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2347 | "+ taglen (%d)", rec->data_len, |
| 2348 | explicit_iv_len, transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2349 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2350 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2351 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2352 | /* |
| 2353 | * Prepare IV |
| 2354 | */ |
| 2355 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2356 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2357 | /* GCM and CCM: fixed || explicit (transmitted) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2358 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2359 | memcpy( iv + transform->fixed_ivlen, data, 8 ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2360 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2361 | } |
| 2362 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2363 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2364 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2365 | unsigned char i; |
| 2366 | |
| 2367 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2368 | |
| 2369 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2370 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2371 | } |
| 2372 | else |
| 2373 | { |
| 2374 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2375 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2376 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2377 | } |
| 2378 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2379 | data += explicit_iv_len; |
| 2380 | rec->data_offset += explicit_iv_len; |
| 2381 | rec->data_len -= explicit_iv_len + transform->taglen; |
| 2382 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2383 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2384 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2385 | add_data, add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2386 | |
| 2387 | memcpy( transform->iv_dec + transform->fixed_ivlen, |
| 2388 | data - explicit_iv_len, explicit_iv_len ); |
| 2389 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2390 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2391 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2392 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2393 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2394 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2395 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2396 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2397 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2398 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, |
| 2399 | iv, transform->ivlen, |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2400 | add_data, add_data_len, |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2401 | data, rec->data_len, |
| 2402 | data, &olen, |
| 2403 | data + rec->data_len, |
| 2404 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2406 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2408 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2409 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2410 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2411 | return( ret ); |
| 2412 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2413 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2414 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2415 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2416 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2417 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2418 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2419 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2420 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2421 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2423 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2424 | ( 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] | 2425 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2426 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2427 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2428 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2429 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2430 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2431 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2432 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2433 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 2434 | { |
| 2435 | /* The ciphertext is prefixed with the CBC IV. */ |
| 2436 | minlen += transform->ivlen; |
| 2437 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2438 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2439 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2440 | /* Size considerations: |
| 2441 | * |
| 2442 | * - The CBC cipher text must not be empty and hence |
| 2443 | * at least of size transform->ivlen. |
| 2444 | * |
| 2445 | * Together with the potential IV-prefix, this explains |
| 2446 | * the first of the two checks below. |
| 2447 | * |
| 2448 | * - The record must contain a MAC, either in plain or |
| 2449 | * encrypted, depending on whether Encrypt-then-MAC |
| 2450 | * is used or not. |
| 2451 | * - If it is, the message contains the IV-prefix, |
| 2452 | * the CBC ciphertext, and the MAC. |
| 2453 | * - If it is not, the padded plaintext, and hence |
| 2454 | * the CBC ciphertext, has at least length maclen + 1 |
| 2455 | * because there is at least the padding length byte. |
| 2456 | * |
| 2457 | * As the CBC ciphertext is not empty, both cases give the |
| 2458 | * lower bound minlen + maclen + 1 on the record size, which |
| 2459 | * we test for in the second check below. |
| 2460 | */ |
| 2461 | if( rec->data_len < minlen + transform->ivlen || |
| 2462 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2463 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2464 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2465 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 2466 | transform->ivlen, |
| 2467 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2468 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2469 | } |
| 2470 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2471 | /* |
| 2472 | * Authenticate before decrypt if enabled |
| 2473 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2474 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2475 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2476 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2477 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2479 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2480 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2481 | /* Safe due to the check data_len >= minlen + maclen + 1 above. */ |
| 2482 | rec->data_len -= transform->maclen; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2483 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2484 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2485 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2486 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 2487 | add_data_len ); |
| 2488 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 2489 | add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2490 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 2491 | data, rec->data_len ); |
| 2492 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 2493 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2494 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2495 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 2496 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2497 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2498 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2499 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2500 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 2501 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2503 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2504 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2505 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2506 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2507 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2508 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2509 | |
| 2510 | /* |
| 2511 | * Check length sanity |
| 2512 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2513 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2514 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2515 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2516 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2517 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2520 | #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] | 2521 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2522 | * 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] | 2523 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2524 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2525 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2526 | /* This is safe because data_len >= minlen + maclen + 1 initially, |
| 2527 | * and at this point we have at most subtracted maclen (note that |
| 2528 | * minlen == transform->ivlen here). */ |
| 2529 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2530 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2531 | data += transform->ivlen; |
| 2532 | rec->data_offset += transform->ivlen; |
| 2533 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2534 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2535 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2536 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2537 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2538 | transform->iv_dec, transform->ivlen, |
| 2539 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2540 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2541 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2542 | return( ret ); |
| 2543 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2544 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2545 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2546 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2547 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2548 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2549 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2550 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2551 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2552 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2553 | { |
| 2554 | /* |
| 2555 | * Save IV in SSL3 and TLS1 |
| 2556 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2557 | memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 2558 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2559 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2560 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2561 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2562 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 2563 | * subtracted at most minlen and maclen up to this point, |
| 2564 | * data_len > 0. */ |
| 2565 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2566 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2567 | if( auth_done == 1 ) |
| 2568 | { |
| 2569 | correct *= ( rec->data_len >= padlen + 1 ); |
| 2570 | padlen *= ( rec->data_len >= padlen + 1 ); |
| 2571 | } |
| 2572 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2573 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2574 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2575 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 2576 | { |
| 2577 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
| 2578 | rec->data_len, |
| 2579 | transform->maclen, |
| 2580 | padlen + 1 ) ); |
| 2581 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2582 | #endif |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2583 | |
| 2584 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
| 2585 | padlen *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2586 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2587 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2588 | padlen++; |
| 2589 | |
| 2590 | /* Regardless of the validity of the padding, |
| 2591 | * we have data_len >= padlen here. */ |
| 2592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2593 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2594 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2595 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2596 | if( padlen > transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2597 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2598 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2599 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2600 | "should be no more than %d", |
| 2601 | padlen, transform->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2602 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2603 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2607 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2608 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2609 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2610 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2611 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2612 | /* The padding check involves a series of up to 256 |
| 2613 | * consecutive memory reads at the end of the record |
| 2614 | * plaintext buffer. In order to hide the length and |
| 2615 | * validity of the padding, always perform exactly |
| 2616 | * `min(256,plaintext_len)` reads (but take into account |
| 2617 | * only the last `padlen` bytes for the padding check). */ |
| 2618 | size_t pad_count = 0; |
| 2619 | size_t real_count = 0; |
| 2620 | volatile unsigned char* const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2621 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2622 | /* Index of first padding byte; it has been ensured above |
| 2623 | * that the subtraction is safe. */ |
| 2624 | size_t const padding_idx = rec->data_len - padlen; |
| 2625 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 2626 | size_t const start_idx = rec->data_len - num_checks; |
| 2627 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2628 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2629 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2630 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2631 | real_count |= ( idx >= padding_idx ); |
| 2632 | pad_count += real_count * ( check[idx] == padlen - 1 ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2633 | } |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2634 | correct &= ( pad_count == padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2636 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2637 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2638 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2639 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2640 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2641 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2642 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2643 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2644 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2645 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2647 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2648 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2649 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2650 | /* If the padding was found to be invalid, padlen == 0 |
| 2651 | * and the subtraction is safe. If the padding was found valid, |
| 2652 | * padlen hasn't been changed and the previous assertion |
| 2653 | * data_len >= padlen still holds. */ |
| 2654 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2655 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2656 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2657 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2658 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2659 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2660 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2661 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2662 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2663 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2664 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2665 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2666 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2667 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2668 | |
| 2669 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2670 | * Authenticate if not done yet. |
| 2671 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2672 | */ |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2673 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2674 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2675 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2676 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2677 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2678 | /* If the initial value of padlen was such that |
| 2679 | * data_len < maclen + padlen + 1, then padlen |
| 2680 | * got reset to 1, and the initial check |
| 2681 | * data_len >= minlen + maclen + 1 |
| 2682 | * guarantees that at this point we still |
| 2683 | * have at least data_len >= maclen. |
| 2684 | * |
| 2685 | * If the initial value of padlen was such that |
| 2686 | * data_len >= maclen + padlen + 1, then we have |
| 2687 | * subtracted either padlen + 1 (if the padding was correct) |
| 2688 | * or 0 (if the padding was incorrect) since then, |
| 2689 | * hence data_len >= maclen in any case. |
| 2690 | */ |
| 2691 | rec->data_len -= transform->maclen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2692 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2693 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2694 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2695 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2696 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2697 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2698 | ssl_mac( &transform->md_ctx_dec, |
| 2699 | transform->mac_dec, |
| 2700 | data, rec->data_len, |
| 2701 | rec->ctr, rec->type, |
| 2702 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2703 | } |
| 2704 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2705 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2706 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2707 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2708 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2709 | { |
| 2710 | /* |
| 2711 | * Process MAC and always update for padlen afterwards to make |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2712 | * total time independent of padlen. |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2713 | * |
| 2714 | * Known timing attacks: |
| 2715 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 2716 | * |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2717 | * To compensate for different timings for the MAC calculation |
| 2718 | * depending on how much padding was removed (which is determined |
| 2719 | * by padlen), process extra_run more blocks through the hash |
| 2720 | * function. |
| 2721 | * |
| 2722 | * The formula in the paper is |
| 2723 | * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) |
| 2724 | * where L1 is the size of the header plus the decrypted message |
| 2725 | * plus CBC padding and L2 is the size of the header plus the |
| 2726 | * decrypted message. This is for an underlying hash function |
| 2727 | * with 64-byte blocks. |
| 2728 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values |
| 2729 | * correctly. We round down instead of up, so -56 is the correct |
| 2730 | * value for our calculations instead of -55. |
| 2731 | * |
Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 2732 | * Repeat the formula rather than defining a block_size variable. |
| 2733 | * This avoids requiring division by a variable at runtime |
| 2734 | * (which would be marginally less efficient and would require |
| 2735 | * linking an extra division function in some builds). |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2736 | */ |
| 2737 | size_t j, extra_run = 0; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2738 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2739 | |
| 2740 | /* |
| 2741 | * The next two sizes are the minimum and maximum values of |
| 2742 | * in_msglen over all padlen values. |
| 2743 | * |
| 2744 | * They're independent of padlen, since we previously did |
| 2745 | * in_msglen -= padlen. |
| 2746 | * |
| 2747 | * Note that max_len + maclen is never more than the buffer |
| 2748 | * length, as we previously did in_msglen -= maclen too. |
| 2749 | */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2750 | const size_t max_len = rec->data_len + padlen; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2751 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 2752 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2753 | memset( tmp, 0, sizeof( tmp ) ); |
| 2754 | |
| 2755 | switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2756 | { |
Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 2757 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ |
| 2758 | defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2759 | case MBEDTLS_MD_MD5: |
| 2760 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2761 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2762 | /* 8 bytes of message size, 64-byte compression blocks */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2763 | extra_run = |
| 2764 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - |
| 2765 | ( add_data_len + rec->data_len + 8 ) / 64; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2766 | break; |
| 2767 | #endif |
Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 2768 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2769 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2770 | /* 16 bytes of message size, 128-byte compression blocks */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2771 | extra_run = |
| 2772 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - |
| 2773 | ( add_data_len + rec->data_len + 16 ) / 128; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2774 | break; |
| 2775 | #endif |
| 2776 | default: |
Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 2777 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2778 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2779 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2780 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2781 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2782 | |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2783 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 2784 | add_data_len ); |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2785 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, |
| 2786 | rec->data_len ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2787 | /* Make sure we access everything even when padlen > 0. This |
| 2788 | * makes the synchronisation requirements for just-in-time |
| 2789 | * Prime+Probe attacks much tighter and hopefully impractical. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2790 | ssl_read_memory( data + rec->data_len, padlen ); |
| 2791 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2792 | |
| 2793 | /* Call mbedtls_md_process at least once due to cache attacks |
| 2794 | * that observe whether md_process() was called of not */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 2795 | for( j = 0; j < extra_run + 1; j++ ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2796 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2797 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2798 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2799 | |
| 2800 | /* Make sure we access all the memory that could contain the MAC, |
| 2801 | * before we check it in the next code block. This makes the |
| 2802 | * synchronisation requirements for just-in-time Prime+Probe |
| 2803 | * attacks much tighter and hopefully impractical. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2804 | ssl_read_memory( data + min_len, |
| 2805 | max_len - min_len + transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2806 | } |
| 2807 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2808 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2809 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2810 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2811 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2812 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2813 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2814 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2815 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2816 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
| 2817 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2818 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2819 | |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2820 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 2821 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2822 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2823 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2824 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2825 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2826 | correct = 0; |
| 2827 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2828 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2829 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 2830 | |
| 2831 | /* |
| 2832 | * Finally check the correct flag |
| 2833 | */ |
| 2834 | if( correct == 0 ) |
| 2835 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | 5cc04d5 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2836 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2837 | |
| 2838 | /* Make extra sure authentication was performed, exactly once */ |
| 2839 | if( auth_done != 1 ) |
| 2840 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2841 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2842 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2843 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2844 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2845 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2846 | if( rec->cid_len != 0 ) |
| 2847 | { |
| 2848 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, |
| 2849 | &rec->type ); |
| 2850 | if( ret != 0 ) |
| 2851 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 2852 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2853 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 92c930f | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2854 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2855 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2856 | |
| 2857 | return( 0 ); |
| 2858 | } |
| 2859 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2860 | #undef MAC_NONE |
| 2861 | #undef MAC_PLAINTEXT |
| 2862 | #undef MAC_CIPHERTEXT |
| 2863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2864 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2865 | /* |
| 2866 | * Compression/decompression functions |
| 2867 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2868 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2869 | { |
| 2870 | int ret; |
| 2871 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 2872 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2873 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2874 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2875 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2876 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2877 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2878 | if( len_pre == 0 ) |
| 2879 | return( 0 ); |
| 2880 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2881 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 2882 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2883 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2884 | ssl->out_msglen ) ); |
| 2885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2886 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2887 | ssl->out_msg, ssl->out_msglen ); |
| 2888 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2889 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 2890 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 2891 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2892 | 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] | 2893 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2894 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2895 | if( ret != Z_OK ) |
| 2896 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2897 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 2898 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2901 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 2902 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2903 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2904 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2905 | ssl->out_msglen ) ); |
| 2906 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2907 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2908 | ssl->out_msg, ssl->out_msglen ); |
| 2909 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2910 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2911 | |
| 2912 | return( 0 ); |
| 2913 | } |
| 2914 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2915 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2916 | { |
| 2917 | int ret; |
| 2918 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2919 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2920 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2921 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2922 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2923 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2924 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2925 | if( len_pre == 0 ) |
| 2926 | return( 0 ); |
| 2927 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2928 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 2929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2930 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2931 | ssl->in_msglen ) ); |
| 2932 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2933 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2934 | ssl->in_msg, ssl->in_msglen ); |
| 2935 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2936 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 2937 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 2938 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2939 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2940 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2941 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2942 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2943 | if( ret != Z_OK ) |
| 2944 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2945 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 2946 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2949 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 2950 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2953 | ssl->in_msglen ) ); |
| 2954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2955 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2956 | ssl->in_msg, ssl->in_msglen ); |
| 2957 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2958 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2959 | |
| 2960 | return( 0 ); |
| 2961 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2962 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2963 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2964 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2965 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2966 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2967 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2968 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2969 | { |
| 2970 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 2971 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2972 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2973 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2974 | 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] | 2975 | unsigned char doublings = 1; |
| 2976 | |
| 2977 | while( ratio != 0 ) |
| 2978 | { |
| 2979 | ++doublings; |
| 2980 | ratio >>= 1; |
| 2981 | } |
| 2982 | |
| 2983 | if( ++ssl->renego_records_seen > doublings ) |
| 2984 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 2985 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2986 | return( 0 ); |
| 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | return( ssl_write_hello_request( ssl ) ); |
| 2991 | } |
| 2992 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2993 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2994 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2995 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2996 | * Fill the input message buffer by appending data to it. |
| 2997 | * 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] | 2998 | * |
| 2999 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 3000 | * available (from this read and/or a previous one). Otherwise, an error code |
| 3001 | * is returned (possibly EOF or WANT_READ). |
| 3002 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3003 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 3004 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 3005 | * since we always read a whole datagram at once. |
| 3006 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3007 | * 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] | 3008 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3009 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3010 | 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] | 3011 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3012 | int ret; |
| 3013 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3014 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3015 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3016 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3017 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 3018 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3019 | 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] | 3020 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3021 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3022 | } |
| 3023 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3024 | 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] | 3025 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3026 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 3027 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3028 | } |
| 3029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3030 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3031 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3032 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3033 | uint32_t timeout; |
| 3034 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3035 | /* Just to be sure */ |
| 3036 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) |
| 3037 | { |
| 3038 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3039 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3040 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3041 | } |
| 3042 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3043 | /* |
| 3044 | * The point is, we need to always read a full datagram at once, so we |
| 3045 | * sometimes read more then requested, and handle the additional data. |
| 3046 | * It could be the rest of the current record (while fetching the |
| 3047 | * header) and/or some other records in the same datagram. |
| 3048 | */ |
| 3049 | |
| 3050 | /* |
| 3051 | * Move to the next record in the already read datagram if applicable |
| 3052 | */ |
| 3053 | if( ssl->next_record_offset != 0 ) |
| 3054 | { |
| 3055 | if( ssl->in_left < ssl->next_record_offset ) |
| 3056 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3057 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3058 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | ssl->in_left -= ssl->next_record_offset; |
| 3062 | |
| 3063 | if( ssl->in_left != 0 ) |
| 3064 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3065 | 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] | 3066 | ssl->next_record_offset ) ); |
| 3067 | memmove( ssl->in_hdr, |
| 3068 | ssl->in_hdr + ssl->next_record_offset, |
| 3069 | ssl->in_left ); |
| 3070 | } |
| 3071 | |
| 3072 | ssl->next_record_offset = 0; |
| 3073 | } |
| 3074 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3075 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3076 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3077 | |
| 3078 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3079 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3080 | */ |
| 3081 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3082 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3083 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3084 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3085 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3086 | |
| 3087 | /* |
| 3088 | * A record can't be split accross datagrams. If we need to read but |
| 3089 | * are not at the beginning of a new record, the caller did something |
| 3090 | * wrong. |
| 3091 | */ |
| 3092 | if( ssl->in_left != 0 ) |
| 3093 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3094 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3095 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3096 | } |
| 3097 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3098 | /* |
| 3099 | * Don't even try to read if time's out already. |
| 3100 | * This avoids by-passing the timer when repeatedly receiving messages |
| 3101 | * that will end up being dropped. |
| 3102 | */ |
| 3103 | if( ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3104 | { |
| 3105 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3106 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3107 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3108 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3109 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3110 | 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] | 3111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3112 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3113 | timeout = ssl->handshake->retransmit_timeout; |
| 3114 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3115 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3117 | 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] | 3118 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3119 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3120 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 3121 | timeout ); |
| 3122 | else |
| 3123 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 3124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3125 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3126 | |
| 3127 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3128 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3129 | } |
| 3130 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3131 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3133 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3134 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3135 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3136 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3137 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3138 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 3139 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3140 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3141 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3142 | } |
| 3143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3144 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3145 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3146 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3147 | return( ret ); |
| 3148 | } |
| 3149 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3150 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3151 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3152 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3153 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3154 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3155 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3156 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3157 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3158 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3159 | return( ret ); |
| 3160 | } |
| 3161 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3162 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3163 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3164 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3165 | } |
| 3166 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3167 | if( ret < 0 ) |
| 3168 | return( ret ); |
| 3169 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3170 | ssl->in_left = ret; |
| 3171 | } |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3172 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 3173 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3174 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3175 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3176 | 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] | 3177 | ssl->in_left, nb_want ) ); |
| 3178 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3179 | while( ssl->in_left < nb_want ) |
| 3180 | { |
| 3181 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3182 | |
| 3183 | if( ssl_check_timer( ssl ) != 0 ) |
| 3184 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 3185 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3186 | { |
| 3187 | if( ssl->f_recv_timeout != NULL ) |
| 3188 | { |
| 3189 | ret = ssl->f_recv_timeout( ssl->p_bio, |
| 3190 | ssl->in_hdr + ssl->in_left, len, |
| 3191 | ssl->conf->read_timeout ); |
| 3192 | } |
| 3193 | else |
| 3194 | { |
| 3195 | ret = ssl->f_recv( ssl->p_bio, |
| 3196 | ssl->in_hdr + ssl->in_left, len ); |
| 3197 | } |
| 3198 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3199 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3200 | 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] | 3201 | ssl->in_left, nb_want ) ); |
| 3202 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3203 | |
| 3204 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3205 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3206 | |
| 3207 | if( ret < 0 ) |
| 3208 | return( ret ); |
| 3209 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3210 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3211 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3212 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3213 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3214 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3215 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3216 | } |
| 3217 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3218 | ssl->in_left += ret; |
| 3219 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3220 | } |
Manuel Pégourié-Gonnard | 25838b7 | 2019-03-19 10:23:56 +0100 | [diff] [blame] | 3221 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3223 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3224 | |
| 3225 | return( 0 ); |
| 3226 | } |
| 3227 | |
| 3228 | /* |
| 3229 | * Flush any data not yet written |
| 3230 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3231 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3232 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3233 | int ret; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3234 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3236 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3237 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3238 | if( ssl->f_send == NULL ) |
| 3239 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3240 | 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] | 3241 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3242 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3243 | } |
| 3244 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3245 | /* Avoid incrementing counter if data is flushed */ |
| 3246 | if( ssl->out_left == 0 ) |
| 3247 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3248 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3249 | return( 0 ); |
| 3250 | } |
| 3251 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3252 | while( ssl->out_left > 0 ) |
| 3253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3254 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3255 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3256 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3257 | buf = ssl->out_hdr - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3258 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 3259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3260 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3261 | |
| 3262 | if( ret <= 0 ) |
| 3263 | return( ret ); |
| 3264 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3265 | 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] | 3266 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3267 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3268 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3269 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3270 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3271 | } |
| 3272 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3273 | ssl->out_left -= ret; |
| 3274 | } |
| 3275 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3276 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3277 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3278 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3279 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3280 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3281 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3282 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3283 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3284 | { |
| 3285 | ssl->out_hdr = ssl->out_buf + 8; |
| 3286 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 3287 | #endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3288 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3290 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3291 | |
| 3292 | return( 0 ); |
| 3293 | } |
| 3294 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3295 | /* |
| 3296 | * Functions to handle the DTLS retransmission state machine |
| 3297 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3298 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3299 | /* |
| 3300 | * Append current handshake message to current outgoing flight |
| 3301 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3302 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3304 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3305 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 3306 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 3307 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3308 | |
| 3309 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3310 | 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] | 3311 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3312 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3313 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3314 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3315 | } |
| 3316 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3317 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3318 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3319 | 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] | 3320 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3321 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | /* Copy current handshake message with headers */ |
| 3325 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 3326 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3327 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3328 | msg->next = NULL; |
| 3329 | |
| 3330 | /* Append to the current flight */ |
| 3331 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3332 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3333 | else |
| 3334 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3335 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3336 | while( cur->next != NULL ) |
| 3337 | cur = cur->next; |
| 3338 | cur->next = msg; |
| 3339 | } |
| 3340 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3341 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3342 | return( 0 ); |
| 3343 | } |
| 3344 | |
| 3345 | /* |
| 3346 | * Free the current flight of handshake messages |
| 3347 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3348 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3349 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3350 | mbedtls_ssl_flight_item *cur = flight; |
| 3351 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3352 | |
| 3353 | while( cur != NULL ) |
| 3354 | { |
| 3355 | next = cur->next; |
| 3356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3357 | mbedtls_free( cur->p ); |
| 3358 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3359 | |
| 3360 | cur = next; |
| 3361 | } |
| 3362 | } |
| 3363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3364 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3365 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3366 | #endif |
| 3367 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3368 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3369 | * Swap transform_out and out_ctr with the alternative ones |
| 3370 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3371 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3372 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3373 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3374 | unsigned char tmp_out_ctr[8]; |
| 3375 | |
| 3376 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 3377 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3378 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3379 | return; |
| 3380 | } |
| 3381 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3382 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3383 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3384 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3385 | tmp_transform = ssl->transform_out; |
| 3386 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 3387 | ssl->handshake->alt_transform_out = tmp_transform; |
| 3388 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3389 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3390 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3391 | 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] | 3392 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3393 | |
| 3394 | /* Adjust to the newly activated transform */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 3395 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3396 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3397 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3398 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3399 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3400 | 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] | 3401 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3402 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3403 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3404 | } |
| 3405 | } |
| 3406 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | /* |
| 3410 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3411 | */ |
| 3412 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3413 | { |
| 3414 | int ret = 0; |
| 3415 | |
| 3416 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3417 | |
| 3418 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3419 | |
| 3420 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3421 | |
| 3422 | return( ret ); |
| 3423 | } |
| 3424 | |
| 3425 | /* |
| 3426 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3427 | * |
| 3428 | * Need to remember the current message in case flush_output returns |
| 3429 | * 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] | 3430 | * 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] | 3431 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3432 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3433 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3434 | int ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3435 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3436 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3437 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3438 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3439 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3440 | |
| 3441 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3442 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3443 | ssl_swap_epochs( ssl ); |
| 3444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3445 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3446 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3447 | |
| 3448 | while( ssl->handshake->cur_msg != NULL ) |
| 3449 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3450 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3451 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3452 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3453 | int const is_finished = |
| 3454 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3455 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 3456 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3457 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 3458 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 3459 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3460 | /* Swap epochs before sending Finished: we can't do it after |
| 3461 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 3462 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3463 | 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] | 3464 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3465 | 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] | 3466 | ssl_swap_epochs( ssl ); |
| 3467 | } |
| 3468 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3469 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3470 | if( ret < 0 ) |
| 3471 | return( ret ); |
| 3472 | max_frag_len = (size_t) ret; |
| 3473 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3474 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 3475 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3476 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3477 | if( max_frag_len == 0 ) |
| 3478 | { |
| 3479 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3480 | return( ret ); |
| 3481 | |
| 3482 | continue; |
| 3483 | } |
| 3484 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3485 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3486 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3487 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3488 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3489 | /* Update position inside current message */ |
| 3490 | ssl->handshake->cur_msg_p += cur->len; |
| 3491 | } |
| 3492 | else |
| 3493 | { |
| 3494 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 3495 | const size_t hs_len = cur->len - 12; |
| 3496 | const size_t frag_off = p - ( cur->p + 12 ); |
| 3497 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3498 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3499 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3500 | 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] | 3501 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3502 | if( is_finished ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3503 | ssl_swap_epochs( ssl ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3504 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3505 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3506 | return( ret ); |
| 3507 | |
| 3508 | continue; |
| 3509 | } |
| 3510 | max_hs_frag_len = max_frag_len - 12; |
| 3511 | |
| 3512 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 3513 | max_hs_frag_len : rem_len; |
| 3514 | |
| 3515 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3516 | { |
| 3517 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3518 | (unsigned) cur_hs_frag_len, |
| 3519 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3520 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 3521 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3522 | /* Messages are stored with handshake headers as if not fragmented, |
| 3523 | * copy beginning of headers then fill fragmentation fields. |
| 3524 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 3525 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3526 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3527 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 3528 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 3529 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 3530 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3531 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 3532 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 3533 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3534 | |
| 3535 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 3536 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 3537 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3538 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 3539 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3540 | ssl->out_msgtype = cur->type; |
| 3541 | |
| 3542 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3543 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | /* If done with the current message move to the next one if any */ |
| 3547 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 3548 | { |
| 3549 | if( cur->next != NULL ) |
| 3550 | { |
| 3551 | ssl->handshake->cur_msg = cur->next; |
| 3552 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 3553 | } |
| 3554 | else |
| 3555 | { |
| 3556 | ssl->handshake->cur_msg = NULL; |
| 3557 | ssl->handshake->cur_msg_p = NULL; |
| 3558 | } |
| 3559 | } |
| 3560 | |
| 3561 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3562 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3563 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3564 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3565 | return( ret ); |
| 3566 | } |
| 3567 | } |
| 3568 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3569 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3570 | return( ret ); |
| 3571 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3572 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3573 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 3574 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 3575 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3576 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3577 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3578 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 3579 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3580 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3581 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3582 | |
| 3583 | return( 0 ); |
| 3584 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3585 | |
| 3586 | /* |
| 3587 | * To be called when the last message of an incoming flight is received. |
| 3588 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3589 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3590 | { |
| 3591 | /* We won't need to resend that one any more */ |
| 3592 | ssl_flight_free( ssl->handshake->flight ); |
| 3593 | ssl->handshake->flight = NULL; |
| 3594 | ssl->handshake->cur_msg = NULL; |
| 3595 | |
| 3596 | /* The next incoming flight will start with this msg_seq */ |
| 3597 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 3598 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3599 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3600 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3601 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3602 | /* Clear future message buffering structure. */ |
| 3603 | ssl_buffering_free( ssl ); |
| 3604 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3605 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3606 | ssl_set_timer( ssl, 0 ); |
| 3607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3608 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3609 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3610 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3611 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3612 | } |
| 3613 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3614 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3615 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3616 | |
| 3617 | /* |
| 3618 | * To be called when the last message of an outgoing flight is send. |
| 3619 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3620 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3621 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3622 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3623 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3625 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3626 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3628 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3629 | } |
| 3630 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3631 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3632 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3634 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3635 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3636 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3637 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3638 | |
| 3639 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3640 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3641 | * |
| 3642 | * - fill in handshake headers |
| 3643 | * - update handshake checksum |
| 3644 | * - DTLS: save message for resending |
| 3645 | * - then pass to the record layer |
| 3646 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3647 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 3648 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3649 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3650 | * Inputs: |
| 3651 | * - ssl->out_msglen: 4 + actual handshake message len |
| 3652 | * (4 is the size of handshake headers for TLS) |
| 3653 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 3654 | * - ssl->out_msg + 4: the handshake message body |
| 3655 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 3656 | * 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] | 3657 | * - ssl->out_msglen: the length of the record contents |
| 3658 | * (including handshake headers but excluding record headers) |
| 3659 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3660 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3661 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3662 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3663 | int ret; |
| 3664 | const size_t hs_len = ssl->out_msglen - 4; |
| 3665 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3666 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3667 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 3668 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3669 | /* |
| 3670 | * Sanity checks |
| 3671 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3672 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3673 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3674 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3675 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 3676 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
| 3677 | if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 3678 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 3679 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ) |
| 3680 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 3681 | { |
| 3682 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3683 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3684 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3685 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3686 | |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3687 | /* Whenever we send anything different from a |
| 3688 | * HelloRequest we should be in a handshake - double check. */ |
| 3689 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3690 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3691 | ssl->handshake == NULL ) |
| 3692 | { |
| 3693 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3694 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3695 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3697 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3698 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3699 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3700 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3701 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3702 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3703 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3704 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3705 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3706 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 3707 | /* Double-check that we did not exceed the bounds |
| 3708 | * of the outgoing record buffer. |
| 3709 | * This should never fail as the various message |
| 3710 | * writing functions must obey the bounds of the |
| 3711 | * outgoing record buffer, but better be safe. |
| 3712 | * |
| 3713 | * Note: We deliberately do not check for the MTU or MFL here. |
| 3714 | */ |
| 3715 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 3716 | { |
| 3717 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 3718 | "size %u, maximum %u", |
| 3719 | (unsigned) ssl->out_msglen, |
| 3720 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 3721 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3722 | } |
| 3723 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3724 | /* |
| 3725 | * Fill handshake headers |
| 3726 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3727 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3728 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3729 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 3730 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 3731 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3732 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3733 | /* |
| 3734 | * DTLS has additional fields in the Handshake layer, |
| 3735 | * between the length field and the actual payload: |
| 3736 | * uint16 message_seq; |
| 3737 | * uint24 fragment_offset; |
| 3738 | * uint24 fragment_length; |
| 3739 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3740 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3741 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3742 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3743 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3744 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 3745 | { |
| 3746 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 3747 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3748 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3749 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 3750 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3751 | } |
| 3752 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3753 | 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] | 3754 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3755 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3756 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3757 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3758 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 3759 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 3760 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 3761 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 3762 | } |
| 3763 | else |
| 3764 | { |
| 3765 | ssl->out_msg[4] = 0; |
| 3766 | ssl->out_msg[5] = 0; |
| 3767 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 3768 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3769 | /* Handshake hashes are computed without fragmentation, |
| 3770 | * 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] | 3771 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 3772 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3773 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3774 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3775 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 3776 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3777 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 3778 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3779 | } |
| 3780 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3781 | /* 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] | 3782 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3783 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | f6d6e30 | 2018-11-07 11:57:51 +0000 | [diff] [blame] | 3784 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3785 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3786 | { |
| 3787 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 3788 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3789 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3790 | return( ret ); |
| 3791 | } |
| 3792 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3793 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3794 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3795 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3796 | 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] | 3797 | { |
| 3798 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 3799 | return( ret ); |
| 3800 | } |
| 3801 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3802 | |
| 3803 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 3804 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3805 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | /* |
| 3809 | * Record layer functions |
| 3810 | */ |
| 3811 | |
| 3812 | /* |
| 3813 | * Write current record. |
| 3814 | * |
| 3815 | * Uses: |
| 3816 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 3817 | * - ssl->out_msglen: length of the record content (excl headers) |
| 3818 | * - ssl->out_msg: record content |
| 3819 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3820 | 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] | 3821 | { |
| 3822 | int ret, done = 0; |
| 3823 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3824 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3825 | |
| 3826 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3828 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3829 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3830 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3831 | { |
| 3832 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 3833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3834 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3835 | return( ret ); |
| 3836 | } |
| 3837 | |
| 3838 | len = ssl->out_msglen; |
| 3839 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3840 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3842 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3843 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3845 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3846 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3847 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 3848 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3849 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3850 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 3851 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3852 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3853 | |
| 3854 | if( ret == 0 ) |
| 3855 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3856 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3857 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3858 | if( !done ) |
| 3859 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3860 | unsigned i; |
| 3861 | size_t protected_record_size; |
| 3862 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3863 | /* Skip writing the record content type to after the encryption, |
| 3864 | * as it may change when using the CID extension. */ |
| 3865 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3866 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3867 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3868 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3869 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3870 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 3871 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3872 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3873 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3874 | { |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 3875 | mbedtls_record rec; |
| 3876 | |
| 3877 | rec.buf = ssl->out_iv; |
| 3878 | rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - |
| 3879 | ( ssl->out_iv - ssl->out_buf ); |
| 3880 | rec.data_len = ssl->out_msglen; |
| 3881 | rec.data_offset = ssl->out_msg - rec.buf; |
| 3882 | |
| 3883 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
| 3884 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 3885 | ssl->conf->transport, rec.ver ); |
| 3886 | rec.type = ssl->out_msgtype; |
| 3887 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3888 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 505089d | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 3889 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3890 | rec.cid_len = 0; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3891 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | e83efe6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3892 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3893 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 3894 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3895 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3896 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3897 | return( ret ); |
| 3898 | } |
| 3899 | |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 3900 | if( rec.data_offset != 0 ) |
| 3901 | { |
| 3902 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3903 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3904 | } |
| 3905 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3906 | /* Update the record content type and CID. */ |
| 3907 | ssl->out_msgtype = rec.type; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3908 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 3909 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3910 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | c5aee96 | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 3911 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 3307b53 | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 3912 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 3913 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3916 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3917 | |
| 3918 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3919 | /* In case of DTLS, double-check that we don't exceed |
| 3920 | * the remaining space in the datagram. */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3921 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3922 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 3923 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3924 | if( ret < 0 ) |
| 3925 | return( ret ); |
| 3926 | |
| 3927 | if( protected_record_size > (size_t) ret ) |
| 3928 | { |
| 3929 | /* Should never happen */ |
| 3930 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3931 | } |
| 3932 | } |
| 3933 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3934 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3935 | /* Now write the potentially updated record content type. */ |
| 3936 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 3937 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3938 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 3939 | "version = [%d:%d], msglen = %d", |
| 3940 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 3941 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3942 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3943 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 3944 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3945 | |
| 3946 | ssl->out_left += protected_record_size; |
| 3947 | ssl->out_hdr += protected_record_size; |
| 3948 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
| 3949 | |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3950 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 3951 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 3952 | break; |
| 3953 | |
| 3954 | /* The loop goes to its end iff the counter is wrapping */ |
| 3955 | if( i == ssl_ep_len( ssl ) ) |
| 3956 | { |
| 3957 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 3958 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 3959 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3962 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 3963 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 3964 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3965 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 3966 | size_t remaining; |
| 3967 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3968 | if( ret < 0 ) |
| 3969 | { |
| 3970 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 3971 | ret ); |
| 3972 | return( ret ); |
| 3973 | } |
| 3974 | |
| 3975 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3976 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 3977 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3978 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 3979 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3980 | else |
| 3981 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 3982 | 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] | 3983 | } |
| 3984 | } |
| 3985 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3986 | |
| 3987 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 3988 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3989 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3990 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3991 | return( ret ); |
| 3992 | } |
| 3993 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3994 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3995 | |
| 3996 | return( 0 ); |
| 3997 | } |
| 3998 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3999 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4000 | |
| 4001 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 4002 | { |
| 4003 | if( ssl->in_msglen < ssl->in_hslen || |
| 4004 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 4005 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 4006 | { |
| 4007 | return( 1 ); |
| 4008 | } |
| 4009 | return( 0 ); |
| 4010 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4011 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4012 | 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] | 4013 | { |
| 4014 | return( ( ssl->in_msg[9] << 16 ) | |
| 4015 | ( ssl->in_msg[10] << 8 ) | |
| 4016 | ssl->in_msg[11] ); |
| 4017 | } |
| 4018 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4019 | 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] | 4020 | { |
| 4021 | return( ( ssl->in_msg[6] << 16 ) | |
| 4022 | ( ssl->in_msg[7] << 8 ) | |
| 4023 | ssl->in_msg[8] ); |
| 4024 | } |
| 4025 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4026 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4027 | { |
| 4028 | uint32_t msg_len, frag_off, frag_len; |
| 4029 | |
| 4030 | msg_len = ssl_get_hs_total_len( ssl ); |
| 4031 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4032 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4033 | |
| 4034 | if( frag_off > msg_len ) |
| 4035 | return( -1 ); |
| 4036 | |
| 4037 | if( frag_len > msg_len - frag_off ) |
| 4038 | return( -1 ); |
| 4039 | |
| 4040 | if( frag_len + 12 > ssl->in_msglen ) |
| 4041 | return( -1 ); |
| 4042 | |
| 4043 | return( 0 ); |
| 4044 | } |
| 4045 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4046 | /* |
| 4047 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 4048 | */ |
| 4049 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 4050 | { |
| 4051 | unsigned int start_bits, end_bits; |
| 4052 | |
| 4053 | start_bits = 8 - ( offset % 8 ); |
| 4054 | if( start_bits != 8 ) |
| 4055 | { |
| 4056 | size_t first_byte_idx = offset / 8; |
| 4057 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4058 | /* Special case */ |
| 4059 | if( len <= start_bits ) |
| 4060 | { |
| 4061 | for( ; len != 0; len-- ) |
| 4062 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 4063 | |
| 4064 | /* Avoid potential issues with offset or len becoming invalid */ |
| 4065 | return; |
| 4066 | } |
| 4067 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4068 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 4069 | len -= start_bits; |
| 4070 | |
| 4071 | for( ; start_bits != 0; start_bits-- ) |
| 4072 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 4073 | } |
| 4074 | |
| 4075 | end_bits = len % 8; |
| 4076 | if( end_bits != 0 ) |
| 4077 | { |
| 4078 | size_t last_byte_idx = ( offset + len ) / 8; |
| 4079 | |
| 4080 | len -= end_bits; /* Now len % 8 == 0 */ |
| 4081 | |
| 4082 | for( ; end_bits != 0; end_bits-- ) |
| 4083 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 4084 | } |
| 4085 | |
| 4086 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 4087 | } |
| 4088 | |
| 4089 | /* |
| 4090 | * Check that bitmask is full |
| 4091 | */ |
| 4092 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 4093 | { |
| 4094 | size_t i; |
| 4095 | |
| 4096 | for( i = 0; i < len / 8; i++ ) |
| 4097 | if( mask[i] != 0xFF ) |
| 4098 | return( -1 ); |
| 4099 | |
| 4100 | for( i = 0; i < len % 8; i++ ) |
| 4101 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 4102 | return( -1 ); |
| 4103 | |
| 4104 | return( 0 ); |
| 4105 | } |
| 4106 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4107 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4108 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4109 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4110 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4111 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4112 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4113 | alloc_len = 12; /* Handshake header */ |
| 4114 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4115 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4116 | if( add_bitmap ) |
| 4117 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4118 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4119 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4120 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4121 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4122 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4123 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4124 | 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] | 4125 | { |
| 4126 | return( ( ssl->in_msg[1] << 16 ) | |
| 4127 | ( ssl->in_msg[2] << 8 ) | |
| 4128 | ssl->in_msg[3] ); |
| 4129 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4130 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4131 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4133 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4134 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4135 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4136 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4137 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4138 | } |
| 4139 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4140 | 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] | 4141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4142 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4143 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4144 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4145 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4146 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4147 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4148 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4149 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4150 | 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] | 4151 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4152 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 4153 | { |
| 4154 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 4155 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4156 | } |
| 4157 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4158 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4159 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4160 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 4161 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4162 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4163 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4164 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 4165 | { |
| 4166 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 4167 | recv_msg_seq, |
| 4168 | ssl->handshake->in_msg_seq ) ); |
| 4169 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4170 | } |
| 4171 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4172 | /* Retransmit only on last message from previous flight, to avoid |
| 4173 | * too many retransmissions. |
| 4174 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 4175 | 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] | 4176 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4177 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4178 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4179 | "message_seq = %d, start_of_flight = %d", |
| 4180 | recv_msg_seq, |
| 4181 | ssl->handshake->in_flight_start_seq ) ); |
| 4182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4183 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4184 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4185 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4186 | return( ret ); |
| 4187 | } |
| 4188 | } |
| 4189 | else |
| 4190 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4191 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4192 | "message_seq = %d, expected = %d", |
| 4193 | recv_msg_seq, |
| 4194 | ssl->handshake->in_msg_seq ) ); |
| 4195 | } |
| 4196 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4197 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4198 | } |
| 4199 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4200 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4201 | /* Message reassembly is handled alongside buffering of future |
| 4202 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4203 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4204 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4205 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4206 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4207 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4208 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4209 | } |
| 4210 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4211 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4212 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4213 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4214 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4215 | /* With TLS we don't handle fragmentation (for now) */ |
| 4216 | if( ssl->in_msglen < ssl->in_hslen ) |
| 4217 | { |
| 4218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 4219 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4220 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4221 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 4222 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4223 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4224 | return( 0 ); |
| 4225 | } |
| 4226 | |
| 4227 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 4228 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4229 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4230 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4231 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4232 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4233 | 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] | 4234 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4235 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4236 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4237 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4238 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4239 | ssl->handshake != NULL ) |
| 4240 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4241 | unsigned offset; |
| 4242 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4243 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4244 | /* Increment handshake sequence number */ |
| 4245 | hs->in_msg_seq++; |
| 4246 | |
| 4247 | /* |
| 4248 | * Clear up handshake buffering and reassembly structure. |
| 4249 | */ |
| 4250 | |
| 4251 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4252 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4253 | |
| 4254 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4255 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 4256 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4257 | offset++, hs_buf++ ) |
| 4258 | { |
| 4259 | *hs_buf = *(hs_buf + 1); |
| 4260 | } |
| 4261 | |
| 4262 | /* Create a fresh last entry */ |
| 4263 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4264 | } |
| 4265 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4266 | } |
| 4267 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4268 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4269 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 4270 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4271 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 4272 | * Bit n is set iff record number in_window_top - n has been seen. |
| 4273 | * |
| 4274 | * Usually, in_window_top is the last record number seen and the lsb of |
| 4275 | * in_window is set. The only exception is the initial state (record number 0 |
| 4276 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4277 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4278 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4279 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4280 | { |
| 4281 | ssl->in_window_top = 0; |
| 4282 | ssl->in_window = 0; |
| 4283 | } |
| 4284 | |
| 4285 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 4286 | { |
| 4287 | return( ( (uint64_t) buf[0] << 40 ) | |
| 4288 | ( (uint64_t) buf[1] << 32 ) | |
| 4289 | ( (uint64_t) buf[2] << 24 ) | |
| 4290 | ( (uint64_t) buf[3] << 16 ) | |
| 4291 | ( (uint64_t) buf[4] << 8 ) | |
| 4292 | ( (uint64_t) buf[5] ) ); |
| 4293 | } |
| 4294 | |
| 4295 | /* |
| 4296 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 4297 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4298 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4299 | { |
| 4300 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4301 | uint64_t bit; |
| 4302 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4303 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4304 | return( 0 ); |
| 4305 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4306 | if( rec_seqnum > ssl->in_window_top ) |
| 4307 | return( 0 ); |
| 4308 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4309 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4310 | |
| 4311 | if( bit >= 64 ) |
| 4312 | return( -1 ); |
| 4313 | |
| 4314 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 4315 | return( -1 ); |
| 4316 | |
| 4317 | return( 0 ); |
| 4318 | } |
| 4319 | |
| 4320 | /* |
| 4321 | * Update replay window on new validated record |
| 4322 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4323 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4324 | { |
| 4325 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4326 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4327 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4328 | return; |
| 4329 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4330 | if( rec_seqnum > ssl->in_window_top ) |
| 4331 | { |
| 4332 | /* Update window_top and the contents of the window */ |
| 4333 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 4334 | |
| 4335 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4336 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4337 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4338 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4339 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4340 | ssl->in_window |= 1; |
| 4341 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4342 | |
| 4343 | ssl->in_window_top = rec_seqnum; |
| 4344 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4345 | else |
| 4346 | { |
| 4347 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4348 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4349 | |
| 4350 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 4351 | ssl->in_window |= (uint64_t) 1 << bit; |
| 4352 | } |
| 4353 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4354 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4355 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4356 | #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] | 4357 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4358 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); |
| 4359 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4360 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4361 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 4362 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 4363 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4364 | * |
| 4365 | * - if cookie is valid, return 0 |
| 4366 | * - if ClientHello looks superficially valid but cookie is not, |
| 4367 | * fill obuf and set olen, then |
| 4368 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 4369 | * - otherwise return a specific error code |
| 4370 | */ |
| 4371 | static int ssl_check_dtls_clihlo_cookie( |
| 4372 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 4373 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 4374 | void *p_cookie, |
| 4375 | const unsigned char *cli_id, size_t cli_id_len, |
| 4376 | const unsigned char *in, size_t in_len, |
| 4377 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 4378 | { |
| 4379 | size_t sid_len, cookie_len; |
| 4380 | unsigned char *p; |
| 4381 | |
| 4382 | if( f_cookie_write == NULL || f_cookie_check == NULL ) |
| 4383 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4384 | |
| 4385 | /* |
| 4386 | * Structure of ClientHello with record and handshake headers, |
| 4387 | * and expected values. We don't need to check a lot, more checks will be |
| 4388 | * done when actually parsing the ClientHello - skipping those checks |
| 4389 | * avoids code duplication and does not make cookie forging any easier. |
| 4390 | * |
| 4391 | * 0-0 ContentType type; copied, must be handshake |
| 4392 | * 1-2 ProtocolVersion version; copied |
| 4393 | * 3-4 uint16 epoch; copied, must be 0 |
| 4394 | * 5-10 uint48 sequence_number; copied |
| 4395 | * 11-12 uint16 length; (ignored) |
| 4396 | * |
| 4397 | * 13-13 HandshakeType msg_type; (ignored) |
| 4398 | * 14-16 uint24 length; (ignored) |
| 4399 | * 17-18 uint16 message_seq; copied |
| 4400 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 4401 | * 22-24 uint24 fragment_length; (ignored) |
| 4402 | * |
| 4403 | * 25-26 ProtocolVersion client_version; (ignored) |
| 4404 | * 27-58 Random random; (ignored) |
| 4405 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 4406 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 4407 | * ... |
| 4408 | * |
| 4409 | * Minimum length is 61 bytes. |
| 4410 | */ |
| 4411 | if( in_len < 61 || |
| 4412 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4413 | in[3] != 0 || in[4] != 0 || |
| 4414 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 4415 | { |
| 4416 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4417 | } |
| 4418 | |
| 4419 | sid_len = in[59]; |
| 4420 | if( sid_len > in_len - 61 ) |
| 4421 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4422 | |
| 4423 | cookie_len = in[60 + sid_len]; |
| 4424 | if( cookie_len > in_len - 60 ) |
| 4425 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4426 | |
| 4427 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 4428 | cli_id, cli_id_len ) == 0 ) |
| 4429 | { |
| 4430 | /* Valid cookie */ |
| 4431 | return( 0 ); |
| 4432 | } |
| 4433 | |
| 4434 | /* |
| 4435 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 4436 | * |
| 4437 | * 0-0 ContentType type; copied |
| 4438 | * 1-2 ProtocolVersion version; copied |
| 4439 | * 3-4 uint16 epoch; copied |
| 4440 | * 5-10 uint48 sequence_number; copied |
| 4441 | * 11-12 uint16 length; olen - 13 |
| 4442 | * |
| 4443 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 4444 | * 14-16 uint24 length; olen - 25 |
| 4445 | * 17-18 uint16 message_seq; copied |
| 4446 | * 19-21 uint24 fragment_offset; copied |
| 4447 | * 22-24 uint24 fragment_length; olen - 25 |
| 4448 | * |
| 4449 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 4450 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 4451 | * |
| 4452 | * Minimum length is 28. |
| 4453 | */ |
| 4454 | if( buf_len < 28 ) |
| 4455 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4456 | |
| 4457 | /* Copy most fields and adapt others */ |
| 4458 | memcpy( obuf, in, 25 ); |
| 4459 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 4460 | obuf[25] = 0xfe; |
| 4461 | obuf[26] = 0xff; |
| 4462 | |
| 4463 | /* Generate and write actual cookie */ |
| 4464 | p = obuf + 28; |
| 4465 | if( f_cookie_write( p_cookie, |
| 4466 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 4467 | { |
| 4468 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4469 | } |
| 4470 | |
| 4471 | *olen = p - obuf; |
| 4472 | |
| 4473 | /* Go back and fill length fields */ |
| 4474 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 4475 | |
| 4476 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 4477 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 4478 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 4479 | |
| 4480 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 4481 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 4482 | |
| 4483 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 4484 | } |
| 4485 | |
| 4486 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4487 | * Handle possible client reconnect with the same UDP quadruplet |
| 4488 | * (RFC 6347 Section 4.2.8). |
| 4489 | * |
| 4490 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 4491 | * that looks like a ClientHello. |
| 4492 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4493 | * - if the input looks like a ClientHello without cookies, |
| 4494 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4495 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4496 | * - if the input looks like a ClientHello with a valid cookie, |
| 4497 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 4498 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4499 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4500 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4501 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 4502 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 4503 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4504 | */ |
| 4505 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 4506 | { |
| 4507 | int ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4508 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4509 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4510 | ret = ssl_check_dtls_clihlo_cookie( |
| 4511 | ssl->conf->f_cookie_write, |
| 4512 | ssl->conf->f_cookie_check, |
| 4513 | ssl->conf->p_cookie, |
| 4514 | ssl->cli_id, ssl->cli_id_len, |
| 4515 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4516 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4517 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4518 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 4519 | |
| 4520 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4521 | { |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4522 | /* 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] | 4523 | * If the error is permanent we'll catch it later, |
| 4524 | * if it's not, then hopefully it'll work next time. */ |
| 4525 | (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len ); |
| 4526 | |
| 4527 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4528 | } |
| 4529 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4530 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4531 | { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4532 | /* Got a valid cookie, partially reset context */ |
| 4533 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
| 4534 | { |
| 4535 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 4536 | return( ret ); |
| 4537 | } |
| 4538 | |
| 4539 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4540 | } |
| 4541 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4542 | return( ret ); |
| 4543 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4544 | #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] | 4545 | |
Hanno Becker | 46483f1 | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4546 | static int ssl_check_record_type( uint8_t record_type ) |
| 4547 | { |
| 4548 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4549 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 4550 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 4551 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 4552 | { |
| 4553 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4554 | } |
| 4555 | |
| 4556 | return( 0 ); |
| 4557 | } |
| 4558 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4559 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4560 | * ContentType type; |
| 4561 | * ProtocolVersion version; |
| 4562 | * uint16 epoch; // DTLS only |
| 4563 | * uint48 sequence_number; // DTLS only |
| 4564 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4565 | * |
| 4566 | * 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] | 4567 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4568 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 4569 | * |
| 4570 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 4571 | * 1. proceed with the record if this function returns 0 |
| 4572 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 4573 | * 3. return CLIENT_RECONNECT if this function return that value |
| 4574 | * 4. drop the whole datagram if this function returns anything else. |
| 4575 | * Point 2 is needed when the peer is resending, and we have already received |
| 4576 | * 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] | 4577 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4578 | static int ssl_parse_record_header( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4579 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4580 | int major_ver, minor_ver; |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4581 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4582 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4583 | /* Parse and validate record content type and version */ |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 4584 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4585 | ssl->in_msgtype = ssl->in_hdr[0]; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4586 | 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] | 4587 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4588 | /* Check record type */ |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4589 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4590 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4591 | ssl->in_msgtype == MBEDTLS_SSL_MSG_CID && |
| 4592 | ssl->conf->cid_len != 0 ) |
| 4593 | { |
| 4594 | /* Shift pointers to account for record header including CID |
| 4595 | * struct { |
| 4596 | * ContentType special_type = tls12_cid; |
| 4597 | * ProtocolVersion version; |
| 4598 | * uint16 epoch; |
| 4599 | * uint48 sequence_number; |
Hanno Becker | 3b2bf5b | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 4600 | * opaque cid[cid_length]; // Additional field compared to |
| 4601 | * // default DTLS record format |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4602 | * uint16 length; |
| 4603 | * opaque enc_content[DTLSCiphertext.length]; |
| 4604 | * } DTLSCiphertext; |
| 4605 | */ |
| 4606 | |
| 4607 | /* So far, we only support static CID lengths |
| 4608 | * fixed in the configuration. */ |
| 4609 | ssl->in_len = ssl->in_cid + ssl->conf->cid_len; |
| 4610 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 4611 | } |
| 4612 | else |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4613 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 46483f1 | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4614 | if( ssl_check_record_type( ssl->in_msgtype ) ) |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4615 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4616 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4617 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4618 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Andres Amaya Garcia | 0169253 | 2017-06-28 09:26:46 +0100 | [diff] [blame] | 4619 | /* Silently ignore invalid DTLS records as recommended by RFC 6347 |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4620 | * Section 4.1.2.7, that is, send alert only with TLS */ |
| 4621 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
| 4622 | { |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4623 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4624 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4625 | } |
| 4626 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4628 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | /* Check version */ |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4632 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4633 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4634 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 4635 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4638 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4639 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4640 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 4641 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4644 | /* Now that the total length of the record header is known, ensure |
| 4645 | * that the current datagram is large enough to hold it. |
| 4646 | * This would fail, for example, if we received a datagram of |
| 4647 | * size 13 + n Bytes where n is less than the size of incoming CIDs. */ |
| 4648 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 4649 | if( ret != 0 ) |
| 4650 | { |
| 4651 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 4652 | return( ret ); |
| 4653 | } |
| 4654 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 4655 | |
| 4656 | /* Parse and validate record length |
| 4657 | * This must happen after the CID parsing because |
| 4658 | * its position in the record header depends on |
| 4659 | * the presence of a CID. */ |
| 4660 | |
| 4661 | ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4662 | if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4663 | - (size_t)( ssl->in_msg - ssl->in_buf ) ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4664 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4665 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4666 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4667 | } |
| 4668 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4669 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Hanno Becker | d8f7c4a | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 4670 | "version = [%d:%d], msglen = %d", |
| 4671 | ssl->in_msgtype, |
| 4672 | major_ver, minor_ver, ssl->in_msglen ) ); |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4673 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4674 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4675 | * DTLS-related tests. |
| 4676 | * Check epoch before checking length constraint because |
| 4677 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 4678 | * message gets duplicated before the corresponding Finished message, |
| 4679 | * the second ChangeCipherSpec should be discarded because it belongs |
| 4680 | * to an old epoch, but not because its length is shorter than |
| 4681 | * the minimum record length for packets using the new record transform. |
| 4682 | * Note that these two kinds of failures are handled differently, |
| 4683 | * as an unexpected record is silently skipped but an invalid |
| 4684 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4685 | */ |
| 4686 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4687 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4688 | { |
| 4689 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 4690 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4691 | /* Check epoch (and sequence number) with DTLS */ |
| 4692 | if( rec_epoch != ssl->in_epoch ) |
| 4693 | { |
| 4694 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 4695 | "expected %d, received %d", |
| 4696 | ssl->in_epoch, rec_epoch ) ); |
| 4697 | |
| 4698 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 4699 | /* |
| 4700 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 4701 | * access the first byte of record content (handshake type), as we |
| 4702 | * have an active transform (possibly iv_len != 0), so use the |
| 4703 | * fact that the record header len is 13 instead. |
| 4704 | */ |
| 4705 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 4706 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4707 | rec_epoch == 0 && |
| 4708 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4709 | ssl->in_left > 13 && |
| 4710 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 4711 | { |
| 4712 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 4713 | "from the same port" ) ); |
| 4714 | return( ssl_handle_possible_reconnect( ssl ) ); |
| 4715 | } |
| 4716 | else |
| 4717 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4718 | { |
| 4719 | /* Consider buffering the record. */ |
| 4720 | if( rec_epoch == (unsigned int) ssl->in_epoch + 1 ) |
| 4721 | { |
| 4722 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 4723 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4724 | } |
| 4725 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4726 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4727 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4731 | /* Replay detection only works for the current epoch */ |
| 4732 | if( rec_epoch == ssl->in_epoch && |
| 4733 | mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
| 4734 | { |
| 4735 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 4736 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 4737 | } |
| 4738 | #endif |
| 4739 | } |
| 4740 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4741 | |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4742 | |
| 4743 | /* Check length against bounds of the current transform and version */ |
| 4744 | if( ssl->transform_in == NULL ) |
| 4745 | { |
| 4746 | if( ssl->in_msglen < 1 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4747 | ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4748 | { |
| 4749 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4750 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4751 | } |
| 4752 | } |
| 4753 | else |
| 4754 | { |
| 4755 | if( ssl->in_msglen < ssl->transform_in->minlen ) |
| 4756 | { |
| 4757 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4758 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4759 | } |
| 4760 | |
| 4761 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 4762 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4763 | ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4764 | { |
| 4765 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4766 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4767 | } |
| 4768 | #endif |
| 4769 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 4770 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4771 | /* |
| 4772 | * TLS encrypted messages can have up to 256 bytes of padding |
| 4773 | */ |
| 4774 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 && |
| 4775 | ssl->in_msglen > ssl->transform_in->minlen + |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4776 | MBEDTLS_SSL_IN_CONTENT_LEN + 256 ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4777 | { |
| 4778 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4779 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4780 | } |
| 4781 | #endif |
| 4782 | } |
| 4783 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4784 | return( 0 ); |
| 4785 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4786 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4787 | /* |
| 4788 | * If applicable, decrypt (and decompress) record content |
| 4789 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4790 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4791 | { |
| 4792 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 4793 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4794 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4795 | ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4797 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4798 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4799 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4800 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4801 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4802 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 4803 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4804 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4805 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 4806 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4807 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4808 | |
| 4809 | if( ret == 0 ) |
| 4810 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4811 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4812 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4813 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4814 | { |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4815 | mbedtls_record rec; |
| 4816 | |
| 4817 | rec.buf = ssl->in_iv; |
| 4818 | rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN |
| 4819 | - ( ssl->in_iv - ssl->in_buf ); |
| 4820 | rec.data_len = ssl->in_msglen; |
| 4821 | rec.data_offset = 0; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4822 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | 7ba3568 | 2019-05-09 15:54:28 +0100 | [diff] [blame] | 4823 | rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid ); |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4824 | memcpy( rec.cid, ssl->in_cid, rec.cid_len ); |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4825 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4826 | |
| 4827 | memcpy( &rec.ctr[0], ssl->in_ctr, 8 ); |
| 4828 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 4829 | ssl->conf->transport, rec.ver ); |
| 4830 | rec.type = ssl->in_msgtype; |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4831 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
| 4832 | &rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4834 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4835 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4836 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4837 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
| 4838 | ssl->conf->ignore_unexpected_cid |
| 4839 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 4840 | { |
Hanno Becker | 675c4d6 | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 4841 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 4842 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | e8eff9a | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4843 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4844 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 687e0fb | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 4845 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4846 | return( ret ); |
| 4847 | } |
| 4848 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4849 | if( ssl->in_msgtype != rec.type ) |
Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 4850 | { |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4851 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
| 4852 | ssl->in_msgtype, rec.type ) ); |
Hanno Becker | 93012fe | 2018-08-07 14:30:18 +0100 | [diff] [blame] | 4853 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4854 | |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4855 | /* The record content type may change during decryption, |
| 4856 | * so re-read it. */ |
| 4857 | ssl->in_msgtype = rec.type; |
| 4858 | /* Also update the input buffer, because unfortunately |
| 4859 | * the server-side ssl_parse_client_hello() reparses the |
| 4860 | * record header when receiving a ClientHello initiating |
| 4861 | * a renegotiation. */ |
| 4862 | ssl->in_hdr[0] = rec.type; |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4863 | ssl->in_msg = rec.buf + rec.data_offset; |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4864 | ssl->in_msglen = rec.data_len; |
| 4865 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4866 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 4867 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4868 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
| 4869 | ssl->in_msg, ssl->in_msglen ); |
| 4870 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4871 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4872 | /* We have already checked the record content type |
| 4873 | * in ssl_parse_record_header(), failing or silently |
| 4874 | * dropping the record in the case of an unknown type. |
| 4875 | * |
| 4876 | * Since with the use of CIDs, the record content type |
| 4877 | * might change during decryption, re-check the record |
| 4878 | * content type, but treat a failure as fatal this time. */ |
| 4879 | if( ssl_check_record_type( ssl->in_msgtype ) ) |
| 4880 | { |
| 4881 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 4882 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4883 | } |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4884 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | ff3e9c2 | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4885 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4886 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4887 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4888 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4889 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4890 | } |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4891 | else if( ssl->in_msglen == 0 ) |
| 4892 | { |
| 4893 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4894 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
| 4895 | && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 4896 | { |
| 4897 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 4898 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 4899 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4900 | } |
| 4901 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4902 | |
| 4903 | ssl->nb_zero++; |
| 4904 | |
| 4905 | /* |
| 4906 | * Three or more empty messages may be a DoS attack |
| 4907 | * (excessive CPU consumption). |
| 4908 | */ |
| 4909 | if( ssl->nb_zero > 3 ) |
| 4910 | { |
| 4911 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 70463db | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 4912 | "messages, possible DoS attack" ) ); |
| 4913 | /* Treat the records as if they were not properly authenticated, |
| 4914 | * thereby failing the connection if we see more than allowed |
| 4915 | * by the configured bad MAC threshold. */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4916 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 4917 | } |
| 4918 | } |
| 4919 | else |
| 4920 | ssl->nb_zero = 0; |
| 4921 | |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4922 | /* Only needed for TLS, as with DTLS in_ctr is read from the header */ |
| 4923 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 4924 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4925 | { |
| 4926 | unsigned i; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4927 | for( i = 8; i > 0; i-- ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4928 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 4929 | break; |
| 4930 | |
Manuel Pégourié-Gonnard | 8794a42 | 2019-06-11 10:04:57 +0200 | [diff] [blame] | 4931 | /* The loop goes to its end only if the counter is wrapping around */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4932 | if( i == 0 ) |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4933 | { |
| 4934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 4935 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4936 | } |
| 4937 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 4938 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 4c6876b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4939 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4942 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4943 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4944 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4945 | { |
| 4946 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 4947 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4948 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4949 | return( ret ); |
| 4950 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4951 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4952 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4953 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4954 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 4955 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 4956 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4957 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 4958 | } |
| 4959 | #endif |
| 4960 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4961 | return( 0 ); |
| 4962 | } |
| 4963 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4964 | 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] | 4965 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4966 | /* |
| 4967 | * Read a record. |
| 4968 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4969 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 4970 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 4971 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4972 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4973 | |
| 4974 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 4975 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4976 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 4977 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 4978 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4979 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 4980 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4981 | { |
| 4982 | int ret; |
| 4983 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4984 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4985 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4986 | if( ssl->keep_current_message == 0 ) |
| 4987 | { |
| 4988 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4989 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 4990 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4991 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4992 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 4993 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4994 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4995 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4996 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4997 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4998 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4999 | /* We only check for buffered messages if the |
| 5000 | * current datagram is fully consumed. */ |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5001 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5002 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5003 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5004 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 5005 | have_buffered = 1; |
| 5006 | } |
| 5007 | |
| 5008 | if( have_buffered == 0 ) |
| 5009 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5010 | { |
| 5011 | ret = ssl_get_next_record( ssl ); |
| 5012 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 5013 | continue; |
| 5014 | |
| 5015 | if( ret != 0 ) |
| 5016 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5017 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5018 | return( ret ); |
| 5019 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5020 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 5024 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5025 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5026 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5027 | { |
| 5028 | /* Buffer future message */ |
| 5029 | ret = ssl_buffer_message( ssl ); |
| 5030 | if( ret != 0 ) |
| 5031 | return( ret ); |
| 5032 | |
| 5033 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 5034 | } |
| 5035 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5036 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5037 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 5038 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5039 | |
| 5040 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5041 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5042 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5043 | return( ret ); |
| 5044 | } |
| 5045 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5046 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5047 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5048 | { |
| 5049 | mbedtls_ssl_update_handshake_status( ssl ); |
| 5050 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5051 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5052 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5053 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5054 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5055 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 5059 | |
| 5060 | return( 0 ); |
| 5061 | } |
| 5062 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5063 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5064 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5065 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5066 | if( ssl->in_left > ssl->next_record_offset ) |
| 5067 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5068 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5069 | return( 0 ); |
| 5070 | } |
| 5071 | |
| 5072 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 5073 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5074 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5075 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5076 | int ret = 0; |
| 5077 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5078 | if( hs == NULL ) |
| 5079 | return( -1 ); |
| 5080 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5081 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 5082 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5083 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 5084 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5085 | { |
| 5086 | /* Check if we have seen a ChangeCipherSpec before. |
| 5087 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5088 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5089 | { |
| 5090 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 5091 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5092 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5093 | } |
| 5094 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5095 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5096 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 5097 | ssl->in_msglen = 1; |
| 5098 | ssl->in_msg[0] = 1; |
| 5099 | |
| 5100 | /* As long as they are equal, the exact value doesn't matter. */ |
| 5101 | ssl->in_left = 0; |
| 5102 | ssl->next_record_offset = 0; |
| 5103 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5104 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5105 | goto exit; |
| 5106 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5107 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5108 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5109 | /* Debug only */ |
| 5110 | { |
| 5111 | unsigned offset; |
| 5112 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 5113 | { |
| 5114 | hs_buf = &hs->buffering.hs[offset]; |
| 5115 | if( hs_buf->is_valid == 1 ) |
| 5116 | { |
| 5117 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 5118 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5119 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5120 | } |
| 5121 | } |
| 5122 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5123 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5124 | |
| 5125 | /* Check if we have buffered and/or fully reassembled the |
| 5126 | * next handshake message. */ |
| 5127 | hs_buf = &hs->buffering.hs[0]; |
| 5128 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 5129 | { |
| 5130 | /* Synthesize a record containing the buffered HS message. */ |
| 5131 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 5132 | ( hs_buf->data[2] << 8 ) | |
| 5133 | hs_buf->data[3]; |
| 5134 | |
| 5135 | /* Double-check that we haven't accidentally buffered |
| 5136 | * a message that doesn't fit into the input buffer. */ |
| 5137 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5138 | { |
| 5139 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5140 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5141 | } |
| 5142 | |
| 5143 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 5144 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 5145 | hs_buf->data, msg_len + 12 ); |
| 5146 | |
| 5147 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5148 | ssl->in_hslen = msg_len + 12; |
| 5149 | ssl->in_msglen = msg_len + 12; |
| 5150 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 5151 | |
| 5152 | ret = 0; |
| 5153 | goto exit; |
| 5154 | } |
| 5155 | else |
| 5156 | { |
| 5157 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 5158 | hs->in_msg_seq ) ); |
| 5159 | } |
| 5160 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5161 | ret = -1; |
| 5162 | |
| 5163 | exit: |
| 5164 | |
| 5165 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 5166 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5167 | } |
| 5168 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5169 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 5170 | size_t desired ) |
| 5171 | { |
| 5172 | int offset; |
| 5173 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5174 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 5175 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5176 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5177 | /* Get rid of future records epoch first, if such exist. */ |
| 5178 | ssl_free_buffered_record( ssl ); |
| 5179 | |
| 5180 | /* Check if we have enough space available now. */ |
| 5181 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5182 | hs->buffering.total_bytes_buffered ) ) |
| 5183 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5184 | 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] | 5185 | return( 0 ); |
| 5186 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5187 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5188 | /* We don't have enough space to buffer the next expected handshake |
| 5189 | * message. Remove buffers used for future messages to gain space, |
| 5190 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5191 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 5192 | offset >= 0; offset-- ) |
| 5193 | { |
| 5194 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 5195 | offset ) ); |
| 5196 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5197 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5198 | |
| 5199 | /* Check if we have enough space available now. */ |
| 5200 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5201 | hs->buffering.total_bytes_buffered ) ) |
| 5202 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5203 | 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] | 5204 | return( 0 ); |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | return( -1 ); |
| 5209 | } |
| 5210 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5211 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 5212 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5213 | int ret = 0; |
| 5214 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5215 | |
| 5216 | if( hs == NULL ) |
| 5217 | return( 0 ); |
| 5218 | |
| 5219 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 5220 | |
| 5221 | switch( ssl->in_msgtype ) |
| 5222 | { |
| 5223 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 5224 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5225 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5226 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5227 | break; |
| 5228 | |
| 5229 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5230 | { |
| 5231 | unsigned recv_msg_seq_offset; |
| 5232 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 5233 | mbedtls_ssl_hs_buffer *hs_buf; |
| 5234 | size_t msg_len = ssl->in_hslen - 12; |
| 5235 | |
| 5236 | /* We should never receive an old handshake |
| 5237 | * message - double-check nonetheless. */ |
| 5238 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 5239 | { |
| 5240 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5241 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5242 | } |
| 5243 | |
| 5244 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 5245 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5246 | { |
| 5247 | /* Silently ignore -- message too far in the future */ |
| 5248 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 5249 | ( "Ignore future HS message with sequence number %u, " |
| 5250 | "buffering window %u - %u", |
| 5251 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 5252 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 5253 | |
| 5254 | goto exit; |
| 5255 | } |
| 5256 | |
| 5257 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 5258 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 5259 | |
| 5260 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 5261 | |
| 5262 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5263 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5264 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5265 | size_t reassembly_buf_sz; |
| 5266 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5267 | hs_buf->is_fragmented = |
| 5268 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 5269 | |
| 5270 | /* We copy the message back into the input buffer |
| 5271 | * after reassembly, so check that it's not too large. |
| 5272 | * This is an implementation-specific limitation |
| 5273 | * and not one from the standard, hence it is not |
| 5274 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 5275 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5276 | { |
| 5277 | /* Ignore message */ |
| 5278 | goto exit; |
| 5279 | } |
| 5280 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5281 | /* Check if we have enough space to buffer the message. */ |
| 5282 | if( hs->buffering.total_bytes_buffered > |
| 5283 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 5284 | { |
| 5285 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5286 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5287 | } |
| 5288 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5289 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 5290 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5291 | |
| 5292 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5293 | hs->buffering.total_bytes_buffered ) ) |
| 5294 | { |
| 5295 | if( recv_msg_seq_offset > 0 ) |
| 5296 | { |
| 5297 | /* If we can't buffer a future message because |
| 5298 | * of space limitations -- ignore. */ |
| 5299 | 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", |
| 5300 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5301 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5302 | goto exit; |
| 5303 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 5304 | else |
| 5305 | { |
| 5306 | 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", |
| 5307 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5308 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5309 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5310 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5311 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5312 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5313 | 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", |
| 5314 | (unsigned) msg_len, |
| 5315 | (unsigned) reassembly_buf_sz, |
| 5316 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5317 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5318 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5319 | goto exit; |
| 5320 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 5324 | msg_len ) ); |
| 5325 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5326 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 5327 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5328 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5329 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5330 | goto exit; |
| 5331 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5332 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5333 | |
| 5334 | /* Prepare final header: copy msg_type, length and message_seq, |
| 5335 | * then add standardised fragment_offset and fragment_length */ |
| 5336 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 5337 | memset( hs_buf->data + 6, 0, 3 ); |
| 5338 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 5339 | |
| 5340 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5341 | |
| 5342 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5343 | } |
| 5344 | else |
| 5345 | { |
| 5346 | /* Make sure msg_type and length are consistent */ |
| 5347 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 5348 | { |
| 5349 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 5350 | /* Ignore */ |
| 5351 | goto exit; |
| 5352 | } |
| 5353 | } |
| 5354 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5355 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5356 | { |
| 5357 | size_t frag_len, frag_off; |
| 5358 | unsigned char * const msg = hs_buf->data + 12; |
| 5359 | |
| 5360 | /* |
| 5361 | * Check and copy current fragment |
| 5362 | */ |
| 5363 | |
| 5364 | /* Validation of header fields already done in |
| 5365 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 5366 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 5367 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 5368 | |
| 5369 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 5370 | frag_off, frag_len ) ); |
| 5371 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 5372 | |
| 5373 | if( hs_buf->is_fragmented ) |
| 5374 | { |
| 5375 | unsigned char * const bitmask = msg + msg_len; |
| 5376 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 5377 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 5378 | msg_len ) == 0 ); |
| 5379 | } |
| 5380 | else |
| 5381 | { |
| 5382 | hs_buf->is_complete = 1; |
| 5383 | } |
| 5384 | |
| 5385 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 5386 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 5387 | } |
| 5388 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5389 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5390 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5391 | |
| 5392 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 5393 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5394 | break; |
| 5395 | } |
| 5396 | |
| 5397 | exit: |
| 5398 | |
| 5399 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 5400 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5401 | } |
| 5402 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5403 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5404 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5405 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5406 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5407 | * Consume last content-layer message and potentially |
| 5408 | * update in_msglen which keeps track of the contents' |
| 5409 | * consumption state. |
| 5410 | * |
| 5411 | * (1) Handshake messages: |
| 5412 | * Remove last handshake message, move content |
| 5413 | * and adapt in_msglen. |
| 5414 | * |
| 5415 | * (2) Alert messages: |
| 5416 | * Consume whole record content, in_msglen = 0. |
| 5417 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5418 | * (3) Change cipher spec: |
| 5419 | * Consume whole record content, in_msglen = 0. |
| 5420 | * |
| 5421 | * (4) Application data: |
| 5422 | * Don't do anything - the record layer provides |
| 5423 | * the application data as a stream transport |
| 5424 | * and consumes through mbedtls_ssl_read only. |
| 5425 | * |
| 5426 | */ |
| 5427 | |
| 5428 | /* Case (1): Handshake messages */ |
| 5429 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5430 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5431 | /* Hard assertion to be sure that no application data |
| 5432 | * is in flight, as corrupting ssl->in_msglen during |
| 5433 | * ssl->in_offt != NULL is fatal. */ |
| 5434 | if( ssl->in_offt != NULL ) |
| 5435 | { |
| 5436 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5437 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5438 | } |
| 5439 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5440 | /* |
| 5441 | * Get next Handshake message in the current record |
| 5442 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5443 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5444 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5445 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5446 | * current handshake content: If DTLS handshake |
| 5447 | * fragmentation is used, that's the fragment |
| 5448 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5449 | * size here is faulty and should be changed at |
| 5450 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5451 | * (2) While it doesn't seem to cause problems, one |
| 5452 | * has to be very careful not to assume that in_hslen |
| 5453 | * is always <= in_msglen in a sensible communication. |
| 5454 | * Again, it's wrong for DTLS handshake fragmentation. |
| 5455 | * The following check is therefore mandatory, and |
| 5456 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5457 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 5458 | * bounds after handling a DTLS message with an unexpected |
| 5459 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5460 | */ |
| 5461 | if( ssl->in_hslen < ssl->in_msglen ) |
| 5462 | { |
| 5463 | ssl->in_msglen -= ssl->in_hslen; |
| 5464 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 5465 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5466 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5467 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 5468 | ssl->in_msg, ssl->in_msglen ); |
| 5469 | } |
| 5470 | else |
| 5471 | { |
| 5472 | ssl->in_msglen = 0; |
| 5473 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 5474 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5475 | ssl->in_hslen = 0; |
| 5476 | } |
| 5477 | /* Case (4): Application data */ |
| 5478 | else if( ssl->in_offt != NULL ) |
| 5479 | { |
| 5480 | return( 0 ); |
| 5481 | } |
| 5482 | /* Everything else (CCS & Alerts) */ |
| 5483 | else |
| 5484 | { |
| 5485 | ssl->in_msglen = 0; |
| 5486 | } |
| 5487 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5488 | return( 0 ); |
| 5489 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5490 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5491 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 5492 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5493 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5494 | return( 1 ); |
| 5495 | |
| 5496 | return( 0 ); |
| 5497 | } |
| 5498 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5499 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5500 | |
| 5501 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 5502 | { |
| 5503 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5504 | if( hs == NULL ) |
| 5505 | return; |
| 5506 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5507 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5508 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5509 | hs->buffering.total_bytes_buffered -= |
| 5510 | hs->buffering.future_record.len; |
| 5511 | |
| 5512 | mbedtls_free( hs->buffering.future_record.data ); |
| 5513 | hs->buffering.future_record.data = NULL; |
| 5514 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 5518 | { |
| 5519 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5520 | unsigned char * rec; |
| 5521 | size_t rec_len; |
| 5522 | unsigned rec_epoch; |
| 5523 | |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5524 | if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5525 | return( 0 ); |
| 5526 | |
| 5527 | if( hs == NULL ) |
| 5528 | return( 0 ); |
| 5529 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5530 | rec = hs->buffering.future_record.data; |
| 5531 | rec_len = hs->buffering.future_record.len; |
| 5532 | rec_epoch = hs->buffering.future_record.epoch; |
| 5533 | |
| 5534 | if( rec == NULL ) |
| 5535 | return( 0 ); |
| 5536 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5537 | /* Only consider loading future records if the |
| 5538 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5539 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5540 | return( 0 ); |
| 5541 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5542 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 5543 | |
| 5544 | if( rec_epoch != ssl->in_epoch ) |
| 5545 | { |
| 5546 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 5547 | goto exit; |
| 5548 | } |
| 5549 | |
| 5550 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 5551 | |
| 5552 | /* Double-check that the record is not too large */ |
| 5553 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 5554 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 5555 | { |
| 5556 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5557 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5558 | } |
| 5559 | |
| 5560 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 5561 | ssl->in_left = rec_len; |
| 5562 | ssl->next_record_offset = 0; |
| 5563 | |
| 5564 | ssl_free_buffered_record( ssl ); |
| 5565 | |
| 5566 | exit: |
| 5567 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 5568 | return( 0 ); |
| 5569 | } |
| 5570 | |
| 5571 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ) |
| 5572 | { |
| 5573 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5574 | size_t const rec_hdr_len = 13; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5575 | size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5576 | |
| 5577 | /* Don't buffer future records outside handshakes. */ |
| 5578 | if( hs == NULL ) |
| 5579 | return( 0 ); |
| 5580 | |
| 5581 | /* Only buffer handshake records (we are only interested |
| 5582 | * in Finished messages). */ |
| 5583 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 5584 | return( 0 ); |
| 5585 | |
| 5586 | /* Don't buffer more than one future epoch record. */ |
| 5587 | if( hs->buffering.future_record.data != NULL ) |
| 5588 | return( 0 ); |
| 5589 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5590 | /* Don't buffer record if there's not enough buffering space remaining. */ |
| 5591 | if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5592 | hs->buffering.total_bytes_buffered ) ) |
| 5593 | { |
| 5594 | 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", |
| 5595 | (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5596 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5597 | return( 0 ); |
| 5598 | } |
| 5599 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5600 | /* Buffer record */ |
| 5601 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 5602 | ssl->in_epoch + 1 ) ); |
| 5603 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr, |
| 5604 | rec_hdr_len + ssl->in_msglen ); |
| 5605 | |
| 5606 | /* ssl_parse_record_header() only considers records |
| 5607 | * of the next epoch as candidates for buffering. */ |
| 5608 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5609 | hs->buffering.future_record.len = total_buf_sz; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5610 | |
| 5611 | hs->buffering.future_record.data = |
| 5612 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 5613 | if( hs->buffering.future_record.data == NULL ) |
| 5614 | { |
| 5615 | /* If we run out of RAM trying to buffer a |
| 5616 | * record from the next epoch, just ignore. */ |
| 5617 | return( 0 ); |
| 5618 | } |
| 5619 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5620 | memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5621 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5622 | hs->buffering.total_bytes_buffered += total_buf_sz; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5623 | return( 0 ); |
| 5624 | } |
| 5625 | |
| 5626 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5627 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5628 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5629 | { |
| 5630 | int ret; |
| 5631 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5632 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5633 | /* We might have buffered a future record; if so, |
| 5634 | * and if the epoch matches now, load it. |
| 5635 | * On success, this call will set ssl->in_left to |
| 5636 | * the length of the buffered record, so that |
| 5637 | * the calls to ssl_fetch_input() below will |
| 5638 | * essentially be no-ops. */ |
| 5639 | ret = ssl_load_buffered_record( ssl ); |
| 5640 | if( ret != 0 ) |
| 5641 | return( ret ); |
| 5642 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5643 | |
Hanno Becker | 8b09b73 | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5644 | /* Reset in pointers to default state for TLS/DTLS records, |
| 5645 | * assuming no CID and no offset between record content and |
| 5646 | * record plaintext. */ |
| 5647 | ssl_update_in_pointers( ssl ); |
| 5648 | |
| 5649 | /* Ensure that we have enough space available for the default form |
| 5650 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 5651 | * with no space for CIDs counted in). */ |
| 5652 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 5653 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5654 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5655 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5656 | return( ret ); |
| 5657 | } |
| 5658 | |
| 5659 | if( ( ret = ssl_parse_record_header( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5660 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5661 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5662 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 5663 | ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5664 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5665 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5666 | { |
| 5667 | ret = ssl_buffer_future_record( ssl ); |
| 5668 | if( ret != 0 ) |
| 5669 | return( ret ); |
| 5670 | |
| 5671 | /* Fall through to handling of unexpected records */ |
| 5672 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 5673 | } |
| 5674 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5675 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 5676 | { |
| 5677 | /* Skip unexpected record (but not whole datagram) */ |
| 5678 | ssl->next_record_offset = ssl->in_msglen |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5679 | + mbedtls_ssl_in_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5680 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5681 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 5682 | "(header)" ) ); |
| 5683 | } |
| 5684 | else |
| 5685 | { |
| 5686 | /* Skip invalid record and the rest of the datagram */ |
| 5687 | ssl->next_record_offset = 0; |
| 5688 | ssl->in_left = 0; |
| 5689 | |
| 5690 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 5691 | "(header)" ) ); |
| 5692 | } |
| 5693 | |
| 5694 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5695 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5696 | } |
| 5697 | #endif |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5698 | return( ret ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5699 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5700 | |
| 5701 | /* |
| 5702 | * Read and optionally decrypt the message contents |
| 5703 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5704 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5705 | mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5706 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5707 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5708 | return( ret ); |
| 5709 | } |
| 5710 | |
| 5711 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5712 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5713 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5714 | { |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5715 | ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl ); |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5716 | if( ssl->next_record_offset < ssl->in_left ) |
| 5717 | { |
| 5718 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 5719 | } |
| 5720 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5721 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5722 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5723 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 5724 | { |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5725 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5726 | } |
| 5727 | #endif |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5728 | |
| 5729 | if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5730 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5731 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5732 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5733 | { |
| 5734 | /* Silently discard invalid records */ |
Hanno Becker | 16e9ae2 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 5735 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5736 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 5737 | /* Except when waiting for Finished as a bad mac here |
| 5738 | * probably means something went wrong in the handshake |
| 5739 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 5740 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 5741 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 5742 | { |
| 5743 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 5744 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 5745 | { |
| 5746 | mbedtls_ssl_send_alert_message( ssl, |
| 5747 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5748 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 5749 | } |
| 5750 | #endif |
| 5751 | return( ret ); |
| 5752 | } |
| 5753 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5754 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5755 | if( ssl->conf->badmac_limit != 0 && |
| 5756 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5758 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 5759 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5760 | } |
| 5761 | #endif |
| 5762 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5763 | /* As above, invalid records cause |
| 5764 | * dismissal of the whole datagram. */ |
| 5765 | |
| 5766 | ssl->next_record_offset = 0; |
| 5767 | ssl->in_left = 0; |
| 5768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5769 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5770 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5771 | } |
| 5772 | |
| 5773 | return( ret ); |
| 5774 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5775 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 5776 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5777 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5778 | { |
| 5779 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5780 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 5781 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5782 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5783 | mbedtls_ssl_send_alert_message( ssl, |
| 5784 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5785 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5786 | } |
| 5787 | #endif |
| 5788 | return( ret ); |
| 5789 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 5790 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5791 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5792 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5793 | return( 0 ); |
| 5794 | } |
| 5795 | |
| 5796 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 5797 | { |
| 5798 | int ret; |
| 5799 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5800 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5801 | * Handle particular types of records |
| 5802 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5803 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5804 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5805 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 5806 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 5807 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5808 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5809 | } |
| 5810 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5811 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5812 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5813 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5814 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5815 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 5816 | ssl->in_msglen ) ); |
| 5817 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5818 | } |
| 5819 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5820 | if( ssl->in_msg[0] != 1 ) |
| 5821 | { |
| 5822 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 5823 | ssl->in_msg[0] ) ); |
| 5824 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5825 | } |
| 5826 | |
| 5827 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5828 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5829 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 5830 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5831 | { |
| 5832 | if( ssl->handshake == NULL ) |
| 5833 | { |
| 5834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 5835 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5836 | } |
| 5837 | |
| 5838 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 5839 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 5840 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5841 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5842 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5843 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5844 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5845 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 5846 | if( ssl->in_msglen != 2 ) |
| 5847 | { |
| 5848 | /* Note: Standard allows for more than one 2 byte alert |
| 5849 | to be packed in a single message, but Mbed TLS doesn't |
| 5850 | currently support this. */ |
| 5851 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 5852 | ssl->in_msglen ) ); |
| 5853 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5854 | } |
| 5855 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5856 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5857 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 5858 | |
| 5859 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 5860 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5861 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5862 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5863 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5864 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5865 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5866 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5867 | } |
| 5868 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5869 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5870 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5871 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5872 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 5873 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5874 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5875 | |
| 5876 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 5877 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5878 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 5879 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5880 | 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] | 5881 | /* Will be handled when trying to parse ServerHello */ |
| 5882 | return( 0 ); |
| 5883 | } |
| 5884 | #endif |
| 5885 | |
| 5886 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
| 5887 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 5888 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5889 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5890 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 5891 | { |
| 5892 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 5893 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 5894 | return( 0 ); |
| 5895 | } |
| 5896 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 5897 | |
| 5898 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5899 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5900 | } |
| 5901 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5902 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 5903 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5904 | { |
Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 5905 | /* Drop unexpected ApplicationData records, |
| 5906 | * except at the beginning of renegotiations */ |
| 5907 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 5908 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 5909 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5910 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 5911 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5912 | #endif |
Hanno Becker | 74dd3a7 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 5913 | ) |
| 5914 | { |
| 5915 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 5916 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 5917 | } |
| 5918 | |
| 5919 | if( ssl->handshake != NULL && |
| 5920 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5921 | { |
| 5922 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 5923 | } |
| 5924 | } |
Hanno Becker | f65ad82 | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 5925 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5926 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5927 | return( 0 ); |
| 5928 | } |
| 5929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5930 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5931 | { |
| 5932 | int ret; |
| 5933 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5934 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 5935 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5936 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5937 | { |
| 5938 | return( ret ); |
| 5939 | } |
| 5940 | |
| 5941 | return( 0 ); |
| 5942 | } |
| 5943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5944 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5945 | unsigned char level, |
| 5946 | unsigned char message ) |
| 5947 | { |
| 5948 | int ret; |
| 5949 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5950 | if( ssl == NULL || ssl->conf == NULL ) |
| 5951 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5952 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5953 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5954 | 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] | 5955 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5956 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5957 | ssl->out_msglen = 2; |
| 5958 | ssl->out_msg[0] = level; |
| 5959 | ssl->out_msg[1] = message; |
| 5960 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 5961 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5962 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5963 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5964 | return( ret ); |
| 5965 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5966 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5967 | |
| 5968 | return( 0 ); |
| 5969 | } |
| 5970 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5971 | /* |
| 5972 | * Handshake functions |
| 5973 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5974 | #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \ |
| 5975 | !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \ |
| 5976 | !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \ |
| 5977 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ |
| 5978 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \ |
| 5979 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \ |
| 5980 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 5981 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5982 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5983 | { |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 5984 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5985 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5986 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5988 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 5989 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 5990 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 5991 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5992 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5994 | ssl->state++; |
| 5995 | return( 0 ); |
| 5996 | } |
| 5997 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5998 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5999 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6000 | } |
| 6001 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6002 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6003 | { |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6004 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6005 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6006 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6008 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 6009 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 6010 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 6011 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6012 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6013 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6014 | ssl->state++; |
| 6015 | return( 0 ); |
| 6016 | } |
| 6017 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6018 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6019 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6020 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6021 | |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6022 | #else |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6023 | /* Some certificate support -> implement write and parse */ |
| 6024 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6025 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6026 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6027 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6028 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6029 | const mbedtls_x509_crt *crt; |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6030 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6031 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6032 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6033 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6034 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 6035 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
Manuel Pégourié-Gonnard | 25dbeb0 | 2015-09-16 17:30:03 +0200 | [diff] [blame] | 6036 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 6037 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6038 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6039 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6040 | ssl->state++; |
| 6041 | return( 0 ); |
| 6042 | } |
| 6043 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6044 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6045 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6046 | { |
| 6047 | if( ssl->client_auth == 0 ) |
| 6048 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6049 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6050 | ssl->state++; |
| 6051 | return( 0 ); |
| 6052 | } |
| 6053 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6054 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6055 | /* |
| 6056 | * If using SSLv3 and got no cert, send an Alert message |
| 6057 | * (otherwise an empty Certificate message will be sent). |
| 6058 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6059 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 6060 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6061 | { |
| 6062 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6063 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 6064 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 6065 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6066 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6067 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6068 | goto write_msg; |
| 6069 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6070 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6071 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6072 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6073 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6074 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6076 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6077 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6078 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 6079 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6080 | } |
| 6081 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6082 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6083 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6084 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6085 | |
| 6086 | /* |
| 6087 | * 0 . 0 handshake type |
| 6088 | * 1 . 3 handshake length |
| 6089 | * 4 . 6 length of all certs |
| 6090 | * 7 . 9 length of cert. 1 |
| 6091 | * 10 . n-1 peer certificate |
| 6092 | * n . n+2 length of cert. 2 |
| 6093 | * n+3 . ... upper level cert, etc. |
| 6094 | */ |
| 6095 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6096 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6097 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6098 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6099 | { |
| 6100 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6101 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6104 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6105 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 6109 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 6110 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 6111 | |
| 6112 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6113 | i += n; crt = crt->next; |
| 6114 | } |
| 6115 | |
| 6116 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 6117 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 6118 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 6119 | |
| 6120 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6121 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6122 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6123 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 6124 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6125 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6126 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6127 | |
| 6128 | ssl->state++; |
| 6129 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6130 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6131 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6132 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6133 | return( ret ); |
| 6134 | } |
| 6135 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6136 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6137 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6138 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6139 | } |
| 6140 | |
Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6141 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6142 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6143 | unsigned char *crt_buf, |
| 6144 | size_t crt_buf_len ) |
| 6145 | { |
| 6146 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6147 | |
| 6148 | if( peer_crt == NULL ) |
| 6149 | return( -1 ); |
| 6150 | |
| 6151 | if( peer_crt->raw.len != crt_buf_len ) |
| 6152 | return( -1 ); |
| 6153 | |
Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6154 | return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6155 | } |
Hanno Becker | 285ff0c | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6156 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6157 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6158 | /* |
| 6159 | * Once the certificate message is read, parse it into a cert chain and |
| 6160 | * perform basic checks, but leave actual verification to the caller |
| 6161 | */ |
| 6162 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6163 | { |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6164 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6165 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 6166 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6168 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6169 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6170 | /* |
| 6171 | * Check if the client sent an empty certificate |
| 6172 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6173 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6174 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6175 | { |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 6176 | if( ssl->in_msglen == 2 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6177 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 6178 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6179 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6180 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6181 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6182 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6183 | /* The client was asked for a certificate but didn't send |
| 6184 | one. The client should know what's going on, so we |
| 6185 | don't send an alert. */ |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6186 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6187 | return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6188 | } |
| 6189 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6190 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6192 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 6193 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6194 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6195 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6197 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6198 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6199 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6200 | 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] | 6201 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6202 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6203 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6204 | /* The client was asked for a certificate but didn't send |
| 6205 | one. The client should know what's going on, so we |
| 6206 | don't send an alert. */ |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 6207 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6208 | return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6209 | } |
| 6210 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6211 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 6212 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6213 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6214 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6215 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6216 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6217 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6218 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6219 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6220 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6223 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 6224 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6225 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6226 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6227 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6228 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6229 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6230 | } |
| 6231 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6232 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6233 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6234 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6235 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6236 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6237 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6238 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6239 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6240 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6242 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6243 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6244 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6245 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6246 | } |
| 6247 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6248 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6249 | i += 3; |
| 6250 | |
Manuel Pégourié-Gonnard | bfb355c | 2013-09-07 17:27:43 +0200 | [diff] [blame] | 6251 | /* In case we tried to reuse a session but it failed */ |
| 6252 | if( ssl->session_negotiate->peer_cert != NULL ) |
| 6253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6254 | mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert ); |
| 6255 | mbedtls_free( ssl->session_negotiate->peer_cert ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6256 | ssl->session_negotiate->peer_cert = NULL; |
Manuel Pégourié-Gonnard | bfb355c | 2013-09-07 17:27:43 +0200 | [diff] [blame] | 6257 | } |
| 6258 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6259 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6260 | while( i < ssl->in_hslen ) |
| 6261 | { |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6262 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6263 | if ( i + 3 > ssl->in_hslen ) { |
| 6264 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6265 | mbedtls_ssl_send_alert_message( ssl, |
| 6266 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6267 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6268 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6269 | } |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6270 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6271 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6272 | if( ssl->in_msg[i] != 0 ) |
| 6273 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6274 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6275 | mbedtls_ssl_send_alert_message( ssl, |
| 6276 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6277 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6278 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6279 | } |
| 6280 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6281 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6282 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6283 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6284 | i += 3; |
| 6285 | |
| 6286 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6287 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6288 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6289 | mbedtls_ssl_send_alert_message( ssl, |
| 6290 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6291 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6292 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6293 | } |
| 6294 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6295 | /* Check if we're handling the first CRT in the chain. */ |
| 6296 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 6297 | { |
Hanno Becker | 68b856d | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6298 | /* During client-side renegotiation, check that the server's |
| 6299 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6300 | * mitigating the triple handshake attack. On success, reuse |
| 6301 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6302 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6303 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6304 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6305 | { |
| 6306 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6307 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6308 | &ssl->in_msg[i], |
| 6309 | n ) != 0 ) |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6310 | { |
| 6311 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6312 | mbedtls_ssl_send_alert_message( ssl, |
| 6313 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6314 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6315 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6316 | } |
| 6317 | |
Hanno Becker | 1332f35 | 2019-02-05 15:06:15 +0000 | [diff] [blame] | 6318 | /* Now we can safely free the original chain. */ |
| 6319 | mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert ); |
| 6320 | mbedtls_free( ssl->session_negotiate->peer_cert ); |
| 6321 | ssl->session_negotiate->peer_cert = NULL; |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6322 | |
Hanno Becker | 1332f35 | 2019-02-05 15:06:15 +0000 | [diff] [blame] | 6323 | /* Intentional fallthrough. */ |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6324 | } |
| 6325 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6326 | |
| 6327 | /* Outside of client-side renegotiation, create a fresh X.509 CRT |
| 6328 | * instance to parse the end-CRT into. */ |
| 6329 | |
| 6330 | ssl->session_negotiate->peer_cert = |
| 6331 | mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6332 | if( ssl->session_negotiate->peer_cert == NULL ) |
| 6333 | { |
| 6334 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6335 | sizeof( mbedtls_x509_crt ) ) ); |
Hanno Becker | fd39919 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6336 | mbedtls_ssl_send_alert_message( ssl, |
| 6337 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6338 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6339 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6340 | } |
| 6341 | |
| 6342 | mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert ); |
| 6343 | |
| 6344 | /* Intentional fall through */ |
| 6345 | } |
| 6346 | |
| 6347 | /* Parse the next certificate in the chain. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6348 | ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert, |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6349 | ssl->in_msg + i, n ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6350 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6351 | { |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6352 | case 0: /*ok*/ |
| 6353 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6354 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6355 | prior certificate was already trusted. */ |
| 6356 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6357 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6358 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6359 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6360 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6361 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6362 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6363 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6364 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6365 | |
Hanno Becker | 33c3dc8 | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6366 | default: |
| 6367 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6368 | crt_parse_der_failed: |
| 6369 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6370 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6371 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | i += n; |
| 6375 | } |
| 6376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6377 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6378 | return( 0 ); |
| 6379 | } |
| 6380 | |
| 6381 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6382 | { |
| 6383 | int ret; |
| 6384 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
Hanno Becker | 8759e16 | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6385 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6386 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6387 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6388 | ? ssl->handshake->sni_authmode |
| 6389 | : ssl->conf->authmode; |
| 6390 | #else |
| 6391 | const int authmode = ssl->conf->authmode; |
| 6392 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6393 | void *rs_ctx = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6394 | |
| 6395 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6396 | |
| 6397 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 6398 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 6399 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK || |
| 6400 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE ) |
| 6401 | { |
| 6402 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6403 | ssl->state++; |
| 6404 | return( 0 ); |
| 6405 | } |
| 6406 | |
| 6407 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6408 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 6409 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6410 | { |
| 6411 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6412 | ssl->state++; |
| 6413 | return( 0 ); |
| 6414 | } |
| 6415 | |
| 6416 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 6417 | authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6418 | { |
| 6419 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6420 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6421 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6422 | ssl->state++; |
| 6423 | return( 0 ); |
| 6424 | } |
| 6425 | #endif |
| 6426 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6427 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 6428 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 6429 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6430 | { |
| 6431 | goto crt_verify; |
| 6432 | } |
| 6433 | #endif |
| 6434 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 6435 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6436 | { |
| 6437 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6438 | let it decide whether to alert. */ |
| 6439 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6440 | return( ret ); |
| 6441 | } |
| 6442 | |
| 6443 | if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 ) |
| 6444 | { |
| 6445 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6446 | if( ret == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE && |
| 6447 | authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6448 | { |
| 6449 | ret = 0; |
| 6450 | } |
| 6451 | #endif |
| 6452 | |
| 6453 | ssl->state++; |
| 6454 | return( ret ); |
| 6455 | } |
| 6456 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6457 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 6458 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 6459 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6460 | |
| 6461 | crt_verify: |
| 6462 | if( ssl->handshake->ecrs_enabled) |
| 6463 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6464 | #endif |
| 6465 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 6466 | if( authmode != MBEDTLS_SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6467 | { |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 6468 | mbedtls_x509_crt *ca_chain; |
| 6469 | mbedtls_x509_crl *ca_crl; |
| 6470 | |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6471 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 6472 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6473 | { |
| 6474 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6475 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6476 | } |
| 6477 | else |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6478 | #endif |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 6479 | { |
| 6480 | ca_chain = ssl->conf->ca_chain; |
| 6481 | ca_crl = ssl->conf->ca_crl; |
| 6482 | } |
| 6483 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6484 | /* |
| 6485 | * Main check: verify certificate |
| 6486 | */ |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6487 | ret = mbedtls_x509_crt_verify_restartable( |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 6488 | ssl->session_negotiate->peer_cert, |
| 6489 | ca_chain, ca_crl, |
| 6490 | ssl->conf->cert_profile, |
| 6491 | ssl->hostname, |
| 6492 | &ssl->session_negotiate->verify_result, |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6493 | ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6494 | |
| 6495 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6496 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6497 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6498 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6499 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6500 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 6501 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 558da9c | 2018-06-13 12:02:12 +0200 | [diff] [blame] | 6502 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6503 | #endif |
| 6504 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6505 | /* |
| 6506 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6507 | */ |
| 6508 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 6509 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6510 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6511 | 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] | 6512 | |
| 6513 | /* 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] | 6514 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6515 | 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] | 6516 | { |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 6517 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6519 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6520 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6521 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6522 | } |
| 6523 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 6524 | #endif /* MBEDTLS_ECP_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6526 | if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert, |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 6527 | ciphersuite_info, |
| 6528 | ! ssl->conf->endpoint, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 6529 | &ssl->session_negotiate->verify_result ) != 0 ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6531 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6532 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6533 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6534 | } |
| 6535 | |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 6536 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6537 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6538 | * with details encoded in the verification flags. All other kinds |
| 6539 | * of error codes, including those from the user provided f_vrfy |
| 6540 | * functions, are treated as fatal and lead to a failure of |
| 6541 | * ssl_parse_certificate even if verification was optional. */ |
| 6542 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6543 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6544 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
| 6545 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6546 | ret = 0; |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 6547 | } |
| 6548 | |
| 6549 | if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6550 | { |
| 6551 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6552 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6553 | } |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6554 | |
| 6555 | if( ret != 0 ) |
| 6556 | { |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6557 | uint8_t alert; |
| 6558 | |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6559 | /* The certificate may have been rejected for several reasons. |
| 6560 | Pick one and send the corresponding alert. Which alert to send |
| 6561 | may be a subject of debate in some cases. */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6562 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6563 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6564 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6565 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6566 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6567 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6568 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6569 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6570 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6571 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6572 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6573 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6574 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6575 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6576 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6577 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6578 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6579 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6580 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6581 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
Gilles Peskine | 8498cb3 | 2017-05-10 15:39:40 +0200 | [diff] [blame] | 6582 | else |
| 6583 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6584 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6585 | alert ); |
| 6586 | } |
Hanno Becker | 39ae8cd | 2017-05-08 16:31:14 +0100 | [diff] [blame] | 6587 | |
Hanno Becker | e6706e6 | 2017-05-15 16:05:15 +0100 | [diff] [blame] | 6588 | #if defined(MBEDTLS_DEBUG_C) |
| 6589 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6590 | { |
| 6591 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 6592 | ssl->session_negotiate->verify_result ) ); |
| 6593 | } |
| 6594 | else |
| 6595 | { |
| 6596 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6597 | } |
| 6598 | #endif /* MBEDTLS_DEBUG_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6601 | ssl->state++; |
| 6602 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6603 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6604 | |
| 6605 | return( ret ); |
| 6606 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6607 | #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED |
| 6608 | !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED |
| 6609 | !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED |
| 6610 | !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED |
| 6611 | !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED |
| 6612 | !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED |
| 6613 | !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6615 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6616 | { |
| 6617 | int ret; |
| 6618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6619 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6620 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6621 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6622 | ssl->out_msglen = 1; |
| 6623 | ssl->out_msg[0] = 1; |
| 6624 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6625 | ssl->state++; |
| 6626 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6627 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6628 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6629 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6630 | return( ret ); |
| 6631 | } |
| 6632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6633 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6634 | |
| 6635 | return( 0 ); |
| 6636 | } |
| 6637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6638 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6639 | { |
| 6640 | int ret; |
| 6641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6642 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6643 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 6644 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6645 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6646 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6647 | return( ret ); |
| 6648 | } |
| 6649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6650 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6651 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6652 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6653 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6654 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6655 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6656 | } |
| 6657 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6658 | /* CCS records are only accepted if they have length 1 and content '1', |
| 6659 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6660 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6661 | /* |
| 6662 | * Switch to our negotiated transform and session parameters for inbound |
| 6663 | * data. |
| 6664 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6665 | 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] | 6666 | ssl->transform_in = ssl->transform_negotiate; |
| 6667 | ssl->session_in = ssl->session_negotiate; |
| 6668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6669 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6670 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6671 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6672 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6673 | ssl_dtls_replay_reset( ssl ); |
| 6674 | #endif |
| 6675 | |
| 6676 | /* Increment epoch */ |
| 6677 | if( ++ssl->in_epoch == 0 ) |
| 6678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6679 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6680 | /* This is highly unlikely to happen for legitimate reasons, so |
| 6681 | 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] | 6682 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6683 | } |
| 6684 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6685 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6686 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 6687 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 6688 | { |
| 6689 | memset( ssl->in_ctr, 0, 8 ); |
| 6690 | } |
| 6691 | #endif |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6692 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 6693 | ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6694 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6695 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 6696 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6697 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6698 | 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] | 6699 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6700 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6701 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6702 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6703 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 6704 | } |
| 6705 | } |
| 6706 | #endif |
| 6707 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6708 | ssl->state++; |
| 6709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6710 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6711 | |
| 6712 | return( 0 ); |
| 6713 | } |
| 6714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6715 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 6716 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6717 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 6718 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6720 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6721 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 6722 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6723 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6724 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6725 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6726 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6727 | #if defined(MBEDTLS_SHA512_C) |
| 6728 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6729 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 6730 | else |
| 6731 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6732 | #if defined(MBEDTLS_SHA256_C) |
| 6733 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6734 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6735 | else |
| 6736 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6737 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 6738 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6739 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6740 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 6741 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6742 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 6743 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6744 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6745 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6746 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6747 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6748 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 6749 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6750 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6751 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6752 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6753 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6754 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6755 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6756 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6757 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6758 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 6759 | } |
| 6760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6761 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6762 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6763 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6764 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6765 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6766 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 6767 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6768 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6769 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6770 | #if defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6771 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6772 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6773 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6774 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6775 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6776 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6777 | } |
| 6778 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6779 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 6780 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 6781 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6782 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6783 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6784 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 6785 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6786 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6787 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6788 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6789 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6790 | #if defined(MBEDTLS_SHA256_C) |
| 6791 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6792 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6793 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6794 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6795 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6796 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6797 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6798 | #if defined(MBEDTLS_SHA512_C) |
| 6799 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6800 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6801 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6802 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6803 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 6804 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6805 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 6806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6807 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6808 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6809 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6810 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6811 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6812 | mbedtls_md5_context md5; |
| 6813 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6814 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6815 | unsigned char padbuf[48]; |
| 6816 | unsigned char md5sum[16]; |
| 6817 | unsigned char sha1sum[20]; |
| 6818 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6819 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6820 | if( !session ) |
| 6821 | session = ssl->session; |
| 6822 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6823 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6824 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6825 | mbedtls_md5_init( &md5 ); |
| 6826 | mbedtls_sha1_init( &sha1 ); |
| 6827 | |
| 6828 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 6829 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6830 | |
| 6831 | /* |
| 6832 | * SSLv3: |
| 6833 | * hash = |
| 6834 | * MD5( master + pad2 + |
| 6835 | * MD5( handshake + sender + master + pad1 ) ) |
| 6836 | * + SHA1( master + pad2 + |
| 6837 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6838 | */ |
| 6839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6840 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6841 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 6842 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6843 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6844 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6845 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6846 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 6847 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6848 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6850 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6851 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6852 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6853 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6854 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6855 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 6856 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 6857 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 6858 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6859 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6860 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 6861 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 6862 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 6863 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6864 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6865 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6866 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6867 | mbedtls_md5_starts_ret( &md5 ); |
| 6868 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 6869 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 6870 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 6871 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6872 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6873 | mbedtls_sha1_starts_ret( &sha1 ); |
| 6874 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 6875 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 6876 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 6877 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6879 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6880 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6881 | mbedtls_md5_free( &md5 ); |
| 6882 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6883 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6884 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6885 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 6886 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6887 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6888 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6889 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6890 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6892 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6893 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6894 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6895 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6896 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6897 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6898 | mbedtls_md5_context md5; |
| 6899 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6900 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6901 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6902 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6903 | if( !session ) |
| 6904 | session = ssl->session; |
| 6905 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6906 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6907 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6908 | mbedtls_md5_init( &md5 ); |
| 6909 | mbedtls_sha1_init( &sha1 ); |
| 6910 | |
| 6911 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 6912 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6913 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6914 | /* |
| 6915 | * TLSv1: |
| 6916 | * hash = PRF( master, finished_label, |
| 6917 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 6918 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6919 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6920 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6921 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 6922 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6923 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6924 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6925 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6926 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 6927 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6928 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6930 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6931 | ? "client finished" |
| 6932 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6933 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6934 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 6935 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6936 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6937 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6938 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6939 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6940 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6941 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6942 | mbedtls_md5_free( &md5 ); |
| 6943 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6944 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6945 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6946 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6947 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6948 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6949 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6950 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6951 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6952 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 6953 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6954 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6955 | { |
| 6956 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6957 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6958 | mbedtls_sha256_context sha256; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6959 | unsigned char padbuf[32]; |
| 6960 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6961 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6962 | if( !session ) |
| 6963 | session = ssl->session; |
| 6964 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6965 | mbedtls_sha256_init( &sha256 ); |
| 6966 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6967 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6968 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 6969 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6970 | |
| 6971 | /* |
| 6972 | * TLSv1.2: |
| 6973 | * hash = PRF( master, finished_label, |
| 6974 | * Hash( handshake ) )[0.11] |
| 6975 | */ |
| 6976 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6977 | #if !defined(MBEDTLS_SHA256_ALT) |
| 6978 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6979 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6980 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6982 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 6983 | ? "client finished" |
| 6984 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6985 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 6986 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6987 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 6988 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6989 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6991 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6992 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 6993 | mbedtls_sha256_free( &sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6994 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6995 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6996 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6997 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 6998 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6999 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7000 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7001 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7002 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7003 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7004 | { |
| 7005 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7006 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7007 | mbedtls_sha512_context sha512; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7008 | unsigned char padbuf[48]; |
| 7009 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7010 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7011 | if( !session ) |
| 7012 | session = ssl->session; |
| 7013 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7014 | mbedtls_sha512_init( &sha512 ); |
| 7015 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7016 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7017 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7018 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7019 | |
| 7020 | /* |
| 7021 | * TLSv1.2: |
| 7022 | * hash = PRF( master, finished_label, |
| 7023 | * Hash( handshake ) )[0.11] |
| 7024 | */ |
| 7025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7026 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7027 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7028 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7029 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7030 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7031 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7032 | ? "client finished" |
| 7033 | : "server finished"; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7034 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7035 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7036 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7037 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7038 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7040 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7041 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7042 | mbedtls_sha512_free( &sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7043 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7044 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7046 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7047 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7048 | #endif /* MBEDTLS_SHA512_C */ |
| 7049 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7051 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7052 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7053 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7054 | |
| 7055 | /* |
| 7056 | * Free our handshake params |
| 7057 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7058 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7059 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7060 | ssl->handshake = NULL; |
| 7061 | |
| 7062 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7063 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7064 | */ |
| 7065 | if( ssl->transform ) |
| 7066 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7067 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7068 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7069 | } |
| 7070 | ssl->transform = ssl->transform_negotiate; |
| 7071 | ssl->transform_negotiate = NULL; |
| 7072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7073 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7074 | } |
| 7075 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7076 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7077 | { |
| 7078 | int resume = ssl->handshake->resume; |
| 7079 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7080 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7081 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7082 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7083 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7084 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7085 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7086 | ssl->renego_records_seen = 0; |
| 7087 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7088 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7089 | |
| 7090 | /* |
| 7091 | * Free the previous session and switch in the current one |
| 7092 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7093 | if( ssl->session ) |
| 7094 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7095 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 7096 | /* RFC 7366 3.1: keep the EtM state */ |
| 7097 | ssl->session_negotiate->encrypt_then_mac = |
| 7098 | ssl->session->encrypt_then_mac; |
| 7099 | #endif |
| 7100 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7101 | mbedtls_ssl_session_free( ssl->session ); |
| 7102 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7103 | } |
| 7104 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7105 | ssl->session_negotiate = NULL; |
| 7106 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7107 | /* |
| 7108 | * Add cache entry |
| 7109 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7110 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 7111 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7112 | resume == 0 ) |
| 7113 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7114 | 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] | 7115 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7116 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7118 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7119 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7120 | ssl->handshake->flight != NULL ) |
| 7121 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 7122 | /* Cancel handshake timer */ |
| 7123 | ssl_set_timer( ssl, 0 ); |
| 7124 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7125 | /* Keep last flight around in case we need to resend it: |
| 7126 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7127 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7128 | } |
| 7129 | else |
| 7130 | #endif |
| 7131 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7132 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7133 | ssl->state++; |
| 7134 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7135 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7138 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7139 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7140 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7142 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7143 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7144 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 7145 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7146 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7147 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 7148 | /* |
| 7149 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7150 | * may define some other value. Currently (early 2016), no defined |
| 7151 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7152 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7153 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7154 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7156 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7157 | ssl->verify_data_len = hash_len; |
| 7158 | 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] | 7159 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7160 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7161 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7162 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7163 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7164 | |
| 7165 | /* |
| 7166 | * In case of session resuming, invert the client and server |
| 7167 | * ChangeCipherSpec messages order. |
| 7168 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7169 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7170 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7171 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7172 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7173 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7174 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7175 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7176 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7177 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7178 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7179 | } |
| 7180 | else |
| 7181 | ssl->state++; |
| 7182 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7183 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7184 | * Switch to our negotiated transform and session parameters for outbound |
| 7185 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7186 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7187 | 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] | 7188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7189 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7190 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7191 | { |
| 7192 | unsigned char i; |
| 7193 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7194 | /* Remember current epoch settings for resending */ |
| 7195 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7196 | 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] | 7197 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7198 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7199 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7200 | |
| 7201 | /* Increment epoch */ |
| 7202 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7203 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7204 | break; |
| 7205 | |
| 7206 | /* The loop goes to its end iff the counter is wrapping */ |
| 7207 | if( i == 0 ) |
| 7208 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7209 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7210 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7211 | } |
| 7212 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7213 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7214 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7215 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 7216 | { |
| 7217 | memset( ssl->cur_out_ctr, 0, 8 ); |
| 7218 | } |
| 7219 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7220 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7221 | ssl->transform_out = ssl->transform_negotiate; |
| 7222 | ssl->session_out = ssl->session_negotiate; |
| 7223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7224 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7225 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7227 | 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] | 7228 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7229 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 7230 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7231 | } |
| 7232 | } |
| 7233 | #endif |
| 7234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7235 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7236 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7237 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 7238 | #endif |
| 7239 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7240 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7241 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7242 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7243 | return( ret ); |
| 7244 | } |
| 7245 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7246 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7247 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7248 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7249 | { |
| 7250 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7251 | return( ret ); |
| 7252 | } |
| 7253 | #endif |
| 7254 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7255 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7256 | |
| 7257 | return( 0 ); |
| 7258 | } |
| 7259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7260 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7261 | #define SSL_MAX_HASH_LEN 36 |
| 7262 | #else |
| 7263 | #define SSL_MAX_HASH_LEN 12 |
| 7264 | #endif |
| 7265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7266 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7267 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 7268 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7269 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7270 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7271 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7272 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7273 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7274 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7275 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7276 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7277 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7278 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7279 | return( ret ); |
| 7280 | } |
| 7281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7282 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7283 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7284 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7285 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7286 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7287 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7288 | } |
| 7289 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7290 | /* 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] | 7291 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 7292 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7293 | hash_len = 36; |
| 7294 | else |
| 7295 | #endif |
| 7296 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7298 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 7299 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7300 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7301 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7302 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7303 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7304 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7307 | 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] | 7308 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7310 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7311 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7312 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7313 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7314 | } |
| 7315 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7316 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7317 | ssl->verify_data_len = hash_len; |
| 7318 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7319 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7320 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7321 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7322 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7323 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7324 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7325 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7326 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7327 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7328 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7329 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7330 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7331 | } |
| 7332 | else |
| 7333 | ssl->state++; |
| 7334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7335 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7336 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7337 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7338 | #endif |
| 7339 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7340 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7341 | |
| 7342 | return( 0 ); |
| 7343 | } |
| 7344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7345 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7346 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7347 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7349 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7350 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7351 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 7352 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7353 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 7354 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7355 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7356 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7357 | #if defined(MBEDTLS_SHA256_C) |
| 7358 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7359 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7360 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7361 | #if defined(MBEDTLS_SHA512_C) |
| 7362 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7363 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7364 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7365 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7366 | |
| 7367 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7368 | |
| 7369 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 7370 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 7371 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 7372 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7374 | #if defined(MBEDTLS_DHM_C) |
| 7375 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7376 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7377 | #if defined(MBEDTLS_ECDH_C) |
| 7378 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7379 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 7380 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 7381 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 7382 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7383 | handshake->ecjpake_cache = NULL; |
| 7384 | handshake->ecjpake_cache_len = 0; |
| 7385 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 7386 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7387 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 7388 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 7389 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 7390 | #endif |
| 7391 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7392 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7393 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 7394 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7395 | } |
| 7396 | |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 7397 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7398 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7399 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 7400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7401 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 7402 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 7403 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 7404 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7405 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 7406 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 7407 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7408 | } |
| 7409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7410 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7411 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7412 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7413 | } |
| 7414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7415 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7416 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7417 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7418 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7419 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7420 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7421 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7422 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7423 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7424 | |
| 7425 | /* |
| 7426 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 7427 | * Now allocate missing structures. |
| 7428 | */ |
| 7429 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7430 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7431 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7432 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7433 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7434 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7435 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7436 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7437 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7438 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 7439 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7440 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7441 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7442 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7443 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7444 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7445 | if( ssl->handshake == NULL || |
| 7446 | ssl->transform_negotiate == NULL || |
| 7447 | ssl->session_negotiate == NULL ) |
| 7448 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 7449 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7451 | mbedtls_free( ssl->handshake ); |
| 7452 | mbedtls_free( ssl->transform_negotiate ); |
| 7453 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7454 | |
| 7455 | ssl->handshake = NULL; |
| 7456 | ssl->transform_negotiate = NULL; |
| 7457 | ssl->session_negotiate = NULL; |
| 7458 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 7459 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7460 | } |
| 7461 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7462 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7463 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | 611a83b | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 7464 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 7465 | ssl_handshake_params_init( ssl->handshake ); |
| 7466 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7467 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 7468 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 7469 | { |
| 7470 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7471 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 7472 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7473 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 7474 | else |
| 7475 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 7476 | |
| 7477 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 7478 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7479 | #endif |
| 7480 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7481 | return( 0 ); |
| 7482 | } |
| 7483 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 7484 | #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] | 7485 | /* Dummy cookie callbacks for defaults */ |
| 7486 | static int ssl_cookie_write_dummy( void *ctx, |
| 7487 | unsigned char **p, unsigned char *end, |
| 7488 | const unsigned char *cli_id, size_t cli_id_len ) |
| 7489 | { |
| 7490 | ((void) ctx); |
| 7491 | ((void) p); |
| 7492 | ((void) end); |
| 7493 | ((void) cli_id); |
| 7494 | ((void) cli_id_len); |
| 7495 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7496 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 7497 | } |
| 7498 | |
| 7499 | static int ssl_cookie_check_dummy( void *ctx, |
| 7500 | const unsigned char *cookie, size_t cookie_len, |
| 7501 | const unsigned char *cli_id, size_t cli_id_len ) |
| 7502 | { |
| 7503 | ((void) ctx); |
| 7504 | ((void) cookie); |
| 7505 | ((void) cookie_len); |
| 7506 | ((void) cli_id); |
| 7507 | ((void) cli_id_len); |
| 7508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7509 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 7510 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 7511 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 7512 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7513 | /* Once ssl->out_hdr as the address of the beginning of the |
| 7514 | * next outgoing record is set, deduce the other pointers. |
| 7515 | * |
| 7516 | * Note: For TLS, we save the implicit record sequence number |
| 7517 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 7518 | * and the caller has to make sure there's space for this. |
| 7519 | */ |
| 7520 | |
| 7521 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 7522 | mbedtls_ssl_transform *transform ) |
| 7523 | { |
| 7524 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7525 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7526 | { |
| 7527 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7528 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7529 | ssl->out_cid = ssl->out_ctr + 8; |
| 7530 | ssl->out_len = ssl->out_cid; |
| 7531 | if( transform != NULL ) |
| 7532 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7533 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7534 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7535 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7536 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7537 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7538 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 7539 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7540 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7541 | { |
| 7542 | ssl->out_ctr = ssl->out_hdr - 8; |
| 7543 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7544 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 7545 | ssl->out_cid = ssl->out_len; |
| 7546 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7547 | ssl->out_iv = ssl->out_hdr + 5; |
| 7548 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7549 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7550 | |
| 7551 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 7552 | if( transform != NULL && |
| 7553 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 7554 | { |
| 7555 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 7556 | } |
| 7557 | else |
| 7558 | ssl->out_msg = ssl->out_iv; |
| 7559 | } |
| 7560 | |
| 7561 | /* Once ssl->in_hdr as the address of the beginning of the |
| 7562 | * next incoming record is set, deduce the other pointers. |
| 7563 | * |
| 7564 | * Note: For TLS, we save the implicit record sequence number |
| 7565 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 7566 | * and the caller has to make sure there's space for this. |
| 7567 | */ |
| 7568 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7569 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7570 | { |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7571 | /* This function sets the pointers to match the case |
| 7572 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 7573 | * and ssl->in_msg pointing to the beginning of the record |
| 7574 | * content. |
| 7575 | * |
| 7576 | * When decrypting a protected record, ssl->in_msg |
| 7577 | * will be shifted to point to the beginning of the |
| 7578 | * record plaintext. |
| 7579 | */ |
| 7580 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7581 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7582 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7583 | { |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7584 | /* This sets the header pointers to match records |
| 7585 | * without CID. When we receive a record containing |
| 7586 | * a CID, the fields are shifted accordingly in |
| 7587 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7588 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7589 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7590 | ssl->in_cid = ssl->in_ctr + 8; |
| 7591 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7592 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7593 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7594 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 70e7928 | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 7595 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7596 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7597 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 7598 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 7599 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7600 | { |
| 7601 | ssl->in_ctr = ssl->in_hdr - 8; |
| 7602 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 7603 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 9bf10ea | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 7604 | ssl->in_cid = ssl->in_len; |
| 7605 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7606 | ssl->in_iv = ssl->in_hdr + 5; |
| 7607 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7608 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7609 | |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7610 | /* This will be adjusted at record decryption time. */ |
| 7611 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7612 | } |
| 7613 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7614 | /* |
| 7615 | * Initialize an SSL context |
| 7616 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 7617 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 7618 | { |
| 7619 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 7620 | } |
| 7621 | |
| 7622 | /* |
| 7623 | * Setup an SSL context |
| 7624 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7625 | |
| 7626 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
| 7627 | { |
| 7628 | /* Set the incoming and outgoing record pointers. */ |
| 7629 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7630 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7631 | { |
| 7632 | ssl->out_hdr = ssl->out_buf; |
| 7633 | ssl->in_hdr = ssl->in_buf; |
| 7634 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7635 | MBEDTLS_SSL_TRANSPORT_ELSE |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7636 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7637 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7638 | { |
| 7639 | ssl->out_hdr = ssl->out_buf + 8; |
| 7640 | ssl->in_hdr = ssl->in_buf + 8; |
| 7641 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 7642 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7643 | |
| 7644 | /* Derive other internal pointers. */ |
| 7645 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
Hanno Becker | f5970a0 | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7646 | ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7647 | } |
| 7648 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 7649 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 7650 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7651 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7652 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7653 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 7654 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 7655 | |
| 7656 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 7657 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 7658 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 7659 | |
| 7660 | /* Set to NULL in case of an error condition */ |
| 7661 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7662 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7663 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 7664 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7665 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7666 | 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] | 7667 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7668 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7669 | } |
| 7670 | |
| 7671 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 7672 | if( ssl->out_buf == NULL ) |
| 7673 | { |
| 7674 | 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] | 7675 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7676 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7677 | } |
| 7678 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 7679 | ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 7680 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7681 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7682 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7683 | |
| 7684 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 7685 | |
| 7686 | error: |
| 7687 | mbedtls_free( ssl->in_buf ); |
| 7688 | mbedtls_free( ssl->out_buf ); |
| 7689 | |
| 7690 | ssl->conf = NULL; |
| 7691 | |
| 7692 | ssl->in_buf = NULL; |
| 7693 | ssl->out_buf = NULL; |
| 7694 | |
| 7695 | ssl->in_hdr = NULL; |
| 7696 | ssl->in_ctr = NULL; |
| 7697 | ssl->in_len = NULL; |
| 7698 | ssl->in_iv = NULL; |
| 7699 | ssl->in_msg = NULL; |
| 7700 | |
| 7701 | ssl->out_hdr = NULL; |
| 7702 | ssl->out_ctr = NULL; |
| 7703 | ssl->out_len = NULL; |
| 7704 | ssl->out_iv = NULL; |
| 7705 | ssl->out_msg = NULL; |
| 7706 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 7707 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7708 | } |
| 7709 | |
| 7710 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7711 | * Reset an initialized and used SSL context for re-use while retaining |
| 7712 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7713 | * |
| 7714 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 7715 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7716 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7717 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7718 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7719 | int ret; |
| 7720 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 7721 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 7722 | !defined(MBEDTLS_SSL_SRV_C) |
| 7723 | ((void) partial); |
| 7724 | #endif |
| 7725 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7726 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7727 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 7728 | /* Cancel any possibly running timer */ |
| 7729 | ssl_set_timer( ssl, 0 ); |
| 7730 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7731 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7732 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7733 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7734 | |
| 7735 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7736 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 7737 | 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] | 7738 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7739 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7740 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7741 | ssl->in_offt = NULL; |
Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 7742 | ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7743 | |
| 7744 | ssl->in_msgtype = 0; |
| 7745 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7746 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 7747 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7748 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 7749 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7750 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 7751 | ssl_dtls_replay_reset( ssl ); |
| 7752 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7753 | |
| 7754 | ssl->in_hslen = 0; |
| 7755 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 7756 | |
| 7757 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7758 | |
| 7759 | ssl->out_msgtype = 0; |
| 7760 | ssl->out_msglen = 0; |
| 7761 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7762 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 7763 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 7764 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 7765 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7766 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7767 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 7768 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7769 | ssl->transform_in = NULL; |
| 7770 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7771 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 7772 | ssl->session_in = NULL; |
| 7773 | ssl->session_out = NULL; |
| 7774 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7775 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7776 | |
| 7777 | #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] | 7778 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7779 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 7780 | { |
| 7781 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 7782 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7783 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7785 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7786 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7787 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7788 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 7789 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7790 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7791 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 7792 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7793 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 7794 | } |
| 7795 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7796 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7797 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7798 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7799 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7800 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7801 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7802 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7803 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 7804 | if( ssl->session ) |
| 7805 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7806 | mbedtls_ssl_session_free( ssl->session ); |
| 7807 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 7808 | ssl->session = NULL; |
| 7809 | } |
| 7810 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7811 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 7812 | ssl->alpn_chosen = NULL; |
| 7813 | #endif |
| 7814 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 7815 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7816 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7817 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 7818 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7819 | { |
| 7820 | mbedtls_free( ssl->cli_id ); |
| 7821 | ssl->cli_id = NULL; |
| 7822 | ssl->cli_id_len = 0; |
| 7823 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 7824 | #endif |
| 7825 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7826 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 7827 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 7828 | |
| 7829 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 7830 | } |
| 7831 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 7832 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 7833 | * Reset an initialized and used SSL context for re-use while retaining |
| 7834 | * all application-set variables, function pointers and data. |
| 7835 | */ |
| 7836 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 7837 | { |
| 7838 | return( ssl_session_reset_int( ssl, 0 ) ); |
| 7839 | } |
| 7840 | |
| 7841 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7842 | * SSL set accessors |
| 7843 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7844 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7845 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7846 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7847 | } |
| 7848 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 7849 | 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] | 7850 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7851 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 7852 | } |
| 7853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7854 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7855 | 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] | 7856 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7857 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 7858 | } |
| 7859 | #endif |
| 7860 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7861 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7862 | 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] | 7863 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7864 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 7865 | } |
| 7866 | #endif |
| 7867 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7868 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 7869 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 7870 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 7871 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 7872 | { |
| 7873 | ssl->disable_datagram_packing = !allow_packing; |
| 7874 | } |
| 7875 | |
| 7876 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 7877 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 7878 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7879 | conf->hs_timeout_min = min; |
| 7880 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 7881 | } |
| 7882 | #endif |
| 7883 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7884 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7885 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7886 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7887 | } |
| 7888 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7889 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7890 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7891 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7892 | void *p_vrfy ) |
| 7893 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7894 | conf->f_vrfy = f_vrfy; |
| 7895 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7896 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7897 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 7898 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7899 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 7900 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7901 | void *p_rng ) |
| 7902 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 7903 | conf->f_rng = f_rng; |
| 7904 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7907 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 7908 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7909 | void *p_dbg ) |
| 7910 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7911 | conf->f_dbg = f_dbg; |
| 7912 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7913 | } |
| 7914 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7915 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7916 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 7917 | mbedtls_ssl_send_t *f_send, |
| 7918 | mbedtls_ssl_recv_t *f_recv, |
| 7919 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7920 | { |
| 7921 | ssl->p_bio = p_bio; |
| 7922 | ssl->f_send = f_send; |
| 7923 | ssl->f_recv = f_recv; |
| 7924 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 7925 | } |
| 7926 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 7927 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7928 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 7929 | { |
| 7930 | ssl->mtu = mtu; |
| 7931 | } |
| 7932 | #endif |
| 7933 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7934 | 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] | 7935 | { |
| 7936 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 7937 | } |
| 7938 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7939 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 7940 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 7941 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 7942 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7943 | { |
| 7944 | ssl->p_timer = p_timer; |
| 7945 | ssl->f_set_timer = f_set_timer; |
| 7946 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 7947 | |
| 7948 | /* Make sure we start with no timer running */ |
| 7949 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 7950 | } |
| 7951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7952 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7953 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7954 | void *p_cache, |
| 7955 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 7956 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7957 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7958 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7959 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7960 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7961 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7962 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7963 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7964 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7965 | 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] | 7966 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7967 | int ret; |
| 7968 | |
| 7969 | if( ssl == NULL || |
| 7970 | session == NULL || |
| 7971 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7972 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7973 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7974 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7975 | } |
| 7976 | |
| 7977 | if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 ) |
| 7978 | return( ret ); |
| 7979 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7980 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 7981 | |
| 7982 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7983 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7984 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7985 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7986 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7987 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7988 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 7989 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 7990 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 7991 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 7992 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 7993 | } |
| 7994 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 7995 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7996 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 7997 | int major, int minor ) |
| 7998 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7999 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8000 | return; |
| 8001 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8002 | 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] | 8003 | return; |
| 8004 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8005 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8006 | } |
| 8007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8008 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8009 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 8010 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8011 | { |
| 8012 | conf->cert_profile = profile; |
| 8013 | } |
| 8014 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8015 | /* Append a new keycert entry to a (possibly empty) list */ |
| 8016 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 8017 | mbedtls_x509_crt *cert, |
| 8018 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8019 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8020 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8021 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8022 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 8023 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8024 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8025 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8026 | new_cert->cert = cert; |
| 8027 | new_cert->key = key; |
| 8028 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8029 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8030 | /* Update head is the list was null, else add to the end */ |
| 8031 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8032 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8033 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8034 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8035 | else |
| 8036 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8037 | mbedtls_ssl_key_cert *cur = *head; |
| 8038 | while( cur->next != NULL ) |
| 8039 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8040 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8041 | } |
| 8042 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8043 | return( 0 ); |
| 8044 | } |
| 8045 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8046 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8047 | mbedtls_x509_crt *own_cert, |
| 8048 | mbedtls_pk_context *pk_key ) |
| 8049 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 8050 | 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] | 8051 | } |
| 8052 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8053 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8054 | mbedtls_x509_crt *ca_chain, |
| 8055 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8056 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8057 | conf->ca_chain = ca_chain; |
| 8058 | conf->ca_crl = ca_crl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8059 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8060 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 8061 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8062 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8063 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 8064 | mbedtls_x509_crt *own_cert, |
| 8065 | mbedtls_pk_context *pk_key ) |
| 8066 | { |
| 8067 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 8068 | own_cert, pk_key ) ); |
| 8069 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 8070 | |
| 8071 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 8072 | mbedtls_x509_crt *ca_chain, |
| 8073 | mbedtls_x509_crl *ca_crl ) |
| 8074 | { |
| 8075 | ssl->handshake->sni_ca_chain = ca_chain; |
| 8076 | ssl->handshake->sni_ca_crl = ca_crl; |
| 8077 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8078 | |
| 8079 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 8080 | int authmode ) |
| 8081 | { |
| 8082 | ssl->handshake->sni_authmode = authmode; |
| 8083 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8084 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8085 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8086 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8087 | /* |
| 8088 | * Set EC J-PAKE password for current handshake |
| 8089 | */ |
| 8090 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 8091 | const unsigned char *pw, |
| 8092 | size_t pw_len ) |
| 8093 | { |
| 8094 | mbedtls_ecjpake_role role; |
| 8095 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 8096 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8097 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8098 | |
| 8099 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 8100 | role = MBEDTLS_ECJPAKE_SERVER; |
| 8101 | else |
| 8102 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 8103 | |
| 8104 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 8105 | role, |
| 8106 | MBEDTLS_MD_SHA256, |
| 8107 | MBEDTLS_ECP_DP_SECP256R1, |
| 8108 | pw, pw_len ) ); |
| 8109 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8110 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8112 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8113 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8114 | const unsigned char *psk, size_t psk_len, |
| 8115 | const unsigned char *psk_identity, size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8116 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8117 | if( psk == NULL || psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8118 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8120 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8121 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 8122 | |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8123 | /* Identity len will be encoded on two bytes */ |
| 8124 | if( ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8125 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8126 | { |
| 8127 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8128 | } |
| 8129 | |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8130 | if( conf->psk != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8131 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8132 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8133 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8134 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 8135 | conf->psk = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8136 | conf->psk_len = 0; |
| 8137 | } |
| 8138 | if( conf->psk_identity != NULL ) |
| 8139 | { |
| 8140 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 173c790 | 2015-10-20 19:56:45 +0200 | [diff] [blame] | 8141 | conf->psk_identity = NULL; |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8142 | conf->psk_identity_len = 0; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8143 | } |
| 8144 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8145 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL || |
| 8146 | ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL ) |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 8147 | { |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8148 | mbedtls_free( conf->psk ); |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 8149 | mbedtls_free( conf->psk_identity ); |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8150 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | 24417f0 | 2015-09-28 18:09:45 +0200 | [diff] [blame] | 8151 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8152 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 8153 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8154 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8155 | conf->psk_len = psk_len; |
| 8156 | conf->psk_identity_len = psk_identity_len; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8157 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8158 | memcpy( conf->psk, psk, conf->psk_len ); |
| 8159 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8160 | |
| 8161 | return( 0 ); |
| 8162 | } |
| 8163 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8164 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 8165 | const unsigned char *psk, size_t psk_len ) |
| 8166 | { |
| 8167 | if( psk == NULL || ssl->handshake == NULL ) |
| 8168 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8169 | |
| 8170 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8171 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8172 | |
| 8173 | if( ssl->handshake->psk != NULL ) |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 8174 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8175 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 8176 | ssl->handshake->psk_len ); |
Simon Butcher | 5b8d1d6 | 2015-10-04 22:06:51 +0100 | [diff] [blame] | 8177 | mbedtls_free( ssl->handshake->psk ); |
Andres Amaya Garcia | bbafd34 | 2017-07-05 14:25:21 +0100 | [diff] [blame] | 8178 | ssl->handshake->psk_len = 0; |
Andres Amaya Garcia | a004988 | 2017-06-26 11:35:17 +0100 | [diff] [blame] | 8179 | } |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8180 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8181 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8182 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8183 | |
| 8184 | ssl->handshake->psk_len = psk_len; |
| 8185 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 8186 | |
| 8187 | return( 0 ); |
| 8188 | } |
| 8189 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8190 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8191 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8192 | size_t), |
| 8193 | void *p_psk ) |
| 8194 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8195 | conf->f_psk = f_psk; |
| 8196 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8197 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8198 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 8199 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8200 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8201 | |
| 8202 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8203 | 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] | 8204 | { |
| 8205 | int ret; |
| 8206 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8207 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 8208 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 8209 | { |
| 8210 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8211 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8212 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8213 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8214 | |
| 8215 | return( 0 ); |
| 8216 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8217 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8218 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 8219 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 8220 | const unsigned char *dhm_P, size_t P_len, |
| 8221 | const unsigned char *dhm_G, size_t G_len ) |
| 8222 | { |
| 8223 | int ret; |
| 8224 | |
| 8225 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 8226 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 8227 | { |
| 8228 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8229 | mbedtls_mpi_free( &conf->dhm_G ); |
| 8230 | return( ret ); |
| 8231 | } |
| 8232 | |
| 8233 | return( 0 ); |
| 8234 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8235 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8236 | 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] | 8237 | { |
| 8238 | int ret; |
| 8239 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8240 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 8241 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 8242 | { |
| 8243 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8244 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8245 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8246 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8247 | |
| 8248 | return( 0 ); |
| 8249 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8250 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8251 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 8252 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 8253 | /* |
| 8254 | * Set the minimum length for Diffie-Hellman parameters |
| 8255 | */ |
| 8256 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 8257 | unsigned int bitlen ) |
| 8258 | { |
| 8259 | conf->dhm_min_bitlen = bitlen; |
| 8260 | } |
| 8261 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 8262 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 8263 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8264 | /* |
| 8265 | * Set allowed/preferred hashes for handshake signatures |
| 8266 | */ |
| 8267 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 8268 | const int *hashes ) |
| 8269 | { |
| 8270 | conf->sig_hashes = hashes; |
| 8271 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8272 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8273 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 8274 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8275 | /* |
| 8276 | * Set the allowed elliptic curves |
| 8277 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8278 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8279 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8280 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8281 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8282 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8283 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8284 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8285 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8286 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8287 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8288 | /* Initialize to suppress unnecessary compiler warning */ |
| 8289 | size_t hostname_len = 0; |
| 8290 | |
| 8291 | /* Check if new hostname is valid before |
| 8292 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8293 | if( hostname != NULL ) |
| 8294 | { |
| 8295 | hostname_len = strlen( hostname ); |
| 8296 | |
| 8297 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 8298 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8299 | } |
| 8300 | |
| 8301 | /* Now it's clear that we will overwrite the old hostname, |
| 8302 | * so we can free it safely */ |
| 8303 | |
| 8304 | if( ssl->hostname != NULL ) |
| 8305 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8306 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8307 | mbedtls_free( ssl->hostname ); |
| 8308 | } |
| 8309 | |
| 8310 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 8311 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8312 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8313 | { |
| 8314 | ssl->hostname = NULL; |
| 8315 | } |
| 8316 | else |
| 8317 | { |
| 8318 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8319 | if( ssl->hostname == NULL ) |
| 8320 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 8321 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8322 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 8323 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8324 | ssl->hostname[hostname_len] = '\0'; |
| 8325 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8326 | |
| 8327 | return( 0 ); |
| 8328 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 8329 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8330 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8331 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8332 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8333 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8334 | const unsigned char *, size_t), |
| 8335 | void *p_sni ) |
| 8336 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8337 | conf->f_sni = f_sni; |
| 8338 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8339 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8340 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 8341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8342 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8343 | 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] | 8344 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 8345 | size_t cur_len, tot_len; |
| 8346 | const char **p; |
| 8347 | |
| 8348 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 8349 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 8350 | * MUST NOT be truncated." |
| 8351 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 8352 | */ |
| 8353 | tot_len = 0; |
| 8354 | for( p = protos; *p != NULL; p++ ) |
| 8355 | { |
| 8356 | cur_len = strlen( *p ); |
| 8357 | tot_len += cur_len; |
| 8358 | |
| 8359 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8360 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 8361 | } |
| 8362 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8363 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 8364 | |
| 8365 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8366 | } |
| 8367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8368 | 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] | 8369 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8370 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8371 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8372 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8373 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8374 | 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] | 8375 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8376 | conf->max_major_ver = major; |
| 8377 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 8378 | } |
| 8379 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8380 | 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] | 8381 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8382 | conf->min_major_ver = major; |
| 8383 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 8384 | } |
| 8385 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8386 | #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] | 8387 | 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] | 8388 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 8389 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 8390 | } |
| 8391 | #endif |
| 8392 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 8393 | #if defined(MBEDTLS_SSL_SRV_C) |
| 8394 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 8395 | char cert_req_ca_list ) |
| 8396 | { |
| 8397 | conf->cert_req_ca_list = cert_req_ca_list; |
| 8398 | } |
| 8399 | #endif |
| 8400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8401 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8402 | 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] | 8403 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8404 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 8405 | } |
| 8406 | #endif |
| 8407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8408 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8409 | 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] | 8410 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8411 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 8412 | } |
Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 8413 | |
| 8414 | void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf, |
Jarno Lamsa | 842be16 | 2019-06-10 15:05:33 +0300 | [diff] [blame] | 8415 | char ems_enf ) |
Jarno Lamsa | 7a5e2be | 2019-06-10 10:13:03 +0300 | [diff] [blame] | 8416 | { |
| 8417 | conf->enforce_extended_master_secret = ems_enf; |
| 8418 | } |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 8419 | #endif |
| 8420 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 8421 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8422 | 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] | 8423 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8424 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 8425 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 8426 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 8427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8428 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8429 | 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] | 8430 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8431 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8432 | 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] | 8433 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8434 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 8435 | } |
| 8436 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 8437 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 8438 | |
| 8439 | return( 0 ); |
| 8440 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8441 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 8442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8443 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8444 | 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] | 8445 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8446 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 8447 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8448 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 8449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8450 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8451 | 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] | 8452 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 8453 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8454 | } |
| 8455 | #endif |
| 8456 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8457 | 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] | 8458 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8459 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8460 | } |
| 8461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8462 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8463 | 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] | 8464 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8465 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8466 | } |
| 8467 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8468 | 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] | 8469 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8470 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 8471 | } |
| 8472 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8473 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 8474 | const unsigned char period[8] ) |
| 8475 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8476 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 8477 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8478 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8480 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8481 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8482 | 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] | 8483 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 8484 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 8485 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8486 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 8487 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8488 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 8489 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 8490 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 8491 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 8492 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 8493 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 8494 | conf->f_ticket_write = f_ticket_write; |
| 8495 | conf->f_ticket_parse = f_ticket_parse; |
| 8496 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 8497 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 8498 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8499 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 8500 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 8501 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 8502 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 8503 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 8504 | void *p_export_keys ) |
| 8505 | { |
| 8506 | conf->f_export_keys = f_export_keys; |
| 8507 | conf->p_export_keys = p_export_keys; |
| 8508 | } |
| 8509 | #endif |
| 8510 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 8511 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 8512 | void mbedtls_ssl_conf_async_private_cb( |
| 8513 | mbedtls_ssl_config *conf, |
| 8514 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 8515 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 8516 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 8517 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8518 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 8519 | { |
| 8520 | conf->f_async_sign_start = f_async_sign; |
| 8521 | conf->f_async_decrypt_start = f_async_decrypt; |
| 8522 | conf->f_async_resume = f_async_resume; |
| 8523 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8524 | conf->p_async_config_data = async_config_data; |
| 8525 | } |
| 8526 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 8527 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 8528 | { |
| 8529 | return( conf->p_async_config_data ); |
| 8530 | } |
| 8531 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 8532 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8533 | { |
| 8534 | if( ssl->handshake == NULL ) |
| 8535 | return( NULL ); |
| 8536 | else |
| 8537 | return( ssl->handshake->user_async_ctx ); |
| 8538 | } |
| 8539 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 8540 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 8541 | void *ctx ) |
| 8542 | { |
| 8543 | if( ssl->handshake != NULL ) |
| 8544 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 8545 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 8546 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 8547 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8548 | /* |
| 8549 | * SSL get accessors |
| 8550 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8551 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8552 | { |
| 8553 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 8554 | } |
| 8555 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 8556 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 8557 | { |
| 8558 | /* |
| 8559 | * Case A: We're currently holding back |
| 8560 | * a message for further processing. |
| 8561 | */ |
| 8562 | |
| 8563 | if( ssl->keep_current_message == 1 ) |
| 8564 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 8565 | 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] | 8566 | return( 1 ); |
| 8567 | } |
| 8568 | |
| 8569 | /* |
| 8570 | * Case B: Further records are pending in the current datagram. |
| 8571 | */ |
| 8572 | |
| 8573 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8574 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 8575 | ssl->in_left > ssl->next_record_offset ) |
| 8576 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 8577 | 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] | 8578 | return( 1 ); |
| 8579 | } |
| 8580 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8581 | |
| 8582 | /* |
| 8583 | * Case C: A handshake message is being processed. |
| 8584 | */ |
| 8585 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 8586 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 8587 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 8588 | 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] | 8589 | return( 1 ); |
| 8590 | } |
| 8591 | |
| 8592 | /* |
| 8593 | * Case D: An application data message is being processed |
| 8594 | */ |
| 8595 | if( ssl->in_offt != NULL ) |
| 8596 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 8597 | 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] | 8598 | return( 1 ); |
| 8599 | } |
| 8600 | |
| 8601 | /* |
| 8602 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 8603 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 8604 | * we implement support for multiple alerts in single records. |
| 8605 | */ |
| 8606 | |
| 8607 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 8608 | return( 0 ); |
| 8609 | } |
| 8610 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8611 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8612 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 8613 | if( ssl->session != NULL ) |
| 8614 | return( ssl->session->verify_result ); |
| 8615 | |
| 8616 | if( ssl->session_negotiate != NULL ) |
| 8617 | return( ssl->session_negotiate->verify_result ); |
| 8618 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 8619 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8620 | } |
| 8621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8622 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 8623 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 8624 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8625 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 8626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8627 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 8628 | } |
| 8629 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8630 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8632 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 8633 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 8634 | { |
| 8635 | switch( ssl->minor_ver ) |
| 8636 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8637 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 8638 | return( "DTLSv1.0" ); |
| 8639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8640 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 8641 | return( "DTLSv1.2" ); |
| 8642 | |
| 8643 | default: |
| 8644 | return( "unknown (DTLS)" ); |
| 8645 | } |
| 8646 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8647 | MBEDTLS_SSL_TRANSPORT_ELSE |
| 8648 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8649 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8650 | { |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8651 | switch( ssl->minor_ver ) |
| 8652 | { |
| 8653 | case MBEDTLS_SSL_MINOR_VERSION_0: |
| 8654 | return( "SSLv3.0" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8655 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8656 | case MBEDTLS_SSL_MINOR_VERSION_1: |
| 8657 | return( "TLSv1.0" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8658 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8659 | case MBEDTLS_SSL_MINOR_VERSION_2: |
| 8660 | return( "TLSv1.1" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8661 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8662 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 8663 | return( "TLSv1.2" ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 8664 | |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8665 | default: |
| 8666 | return( "unknown" ); |
| 8667 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8668 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 8669 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 8670 | } |
| 8671 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8672 | 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] | 8673 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 8674 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8675 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8676 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8677 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 8678 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 8679 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8680 | if( transform == NULL ) |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 8681 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8683 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 8684 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 8685 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8686 | #endif |
| 8687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8688 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8689 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8690 | case MBEDTLS_MODE_GCM: |
| 8691 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8692 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8693 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8694 | transform_expansion = transform->minlen; |
| 8695 | break; |
| 8696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8697 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8698 | |
| 8699 | block_size = mbedtls_cipher_get_block_size( |
| 8700 | &transform->cipher_ctx_enc ); |
| 8701 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 8702 | /* Expansion due to the addition of the MAC. */ |
| 8703 | transform_expansion += transform->maclen; |
| 8704 | |
| 8705 | /* Expansion due to the addition of CBC padding; |
| 8706 | * Theoretically up to 256 bytes, but we never use |
| 8707 | * more than the block size of the underlying cipher. */ |
| 8708 | transform_expansion += block_size; |
| 8709 | |
| 8710 | /* For TLS 1.1 or higher, an explicit IV is added |
| 8711 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8712 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 8713 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 8714 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 8715 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 8716 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8717 | break; |
| 8718 | |
| 8719 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 8720 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8721 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8722 | } |
| 8723 | |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8724 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 8725 | if( transform->out_cid_len != 0 ) |
| 8726 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a5a2b08 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8727 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | add0190 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 8728 | |
Hanno Becker | 4339576 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 8729 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 8730 | } |
| 8731 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 8732 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8733 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 8734 | { |
| 8735 | size_t max_len; |
| 8736 | |
| 8737 | /* |
| 8738 | * Assume mfl_code is correct since it was checked when set |
| 8739 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8740 | 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] | 8741 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 8742 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 8743 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8744 | 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] | 8745 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8746 | 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] | 8747 | } |
| 8748 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 8749 | /* During a handshake, use the value being negotiated */ |
| 8750 | if( ssl->session_negotiate != NULL && |
| 8751 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 8752 | { |
| 8753 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 8754 | } |
| 8755 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8756 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 8757 | } |
| 8758 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 8759 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8760 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8761 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 8762 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 8763 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 8764 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 8765 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 8766 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 8767 | return ( 0 ); |
| 8768 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8769 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 8770 | return( ssl->mtu ); |
| 8771 | |
| 8772 | if( ssl->mtu == 0 ) |
| 8773 | return( ssl->handshake->mtu ); |
| 8774 | |
| 8775 | return( ssl->mtu < ssl->handshake->mtu ? |
| 8776 | ssl->mtu : ssl->handshake->mtu ); |
| 8777 | } |
| 8778 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8779 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8780 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 8781 | { |
| 8782 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 8783 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 8784 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 8785 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8786 | (void) ssl; |
| 8787 | #endif |
| 8788 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8789 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8790 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 8791 | |
| 8792 | if( max_len > mfl ) |
| 8793 | max_len = mfl; |
| 8794 | #endif |
| 8795 | |
| 8796 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8797 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8798 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8799 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8800 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 8801 | const size_t overhead = (size_t) ret; |
| 8802 | |
| 8803 | if( ret < 0 ) |
| 8804 | return( ret ); |
| 8805 | |
| 8806 | if( mtu <= overhead ) |
| 8807 | { |
| 8808 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 8809 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 8810 | } |
| 8811 | |
| 8812 | if( max_len > mtu - overhead ) |
| 8813 | max_len = mtu - overhead; |
| 8814 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 8815 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8816 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 8817 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 8818 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8819 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 8820 | #endif |
| 8821 | |
| 8822 | return( (int) max_len ); |
| 8823 | } |
| 8824 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8825 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8826 | 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] | 8827 | { |
| 8828 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8829 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8830 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 8831 | return( ssl->session->peer_cert ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8832 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8833 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 8834 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8835 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 933b9fc | 2019-02-05 11:42:30 +0000 | [diff] [blame^] | 8836 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 8837 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8838 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8839 | if( ssl == NULL || |
| 8840 | dst == NULL || |
| 8841 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8842 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8843 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8844 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8845 | } |
| 8846 | |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8847 | return( ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8848 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8849 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 8850 | |
Manuel Pégourié-Gonnard | 37a5324 | 2019-05-20 11:12:28 +0200 | [diff] [blame] | 8851 | const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl ) |
| 8852 | { |
| 8853 | if( ssl == NULL ) |
| 8854 | return( NULL ); |
| 8855 | |
| 8856 | return( ssl->session ); |
| 8857 | } |
| 8858 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8859 | /* |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 8860 | * Define ticket header determining Mbed TLS version |
| 8861 | * and structure of the ticket. |
| 8862 | */ |
| 8863 | |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8864 | /* |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8865 | * Define bitflag determining compile-time settings influencing |
| 8866 | * structure of serialized SSL sessions. |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8867 | */ |
| 8868 | |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8869 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8870 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8871 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8872 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8873 | #endif /* MBEDTLS_HAVE_TIME */ |
| 8874 | |
| 8875 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8876 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8877 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8878 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8879 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8880 | |
| 8881 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8882 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8883 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8884 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8885 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 8886 | |
| 8887 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8888 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8889 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8890 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8891 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 8892 | |
| 8893 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8894 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8895 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8896 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8897 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
| 8898 | |
| 8899 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8900 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8901 | #else |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8902 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8903 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 8904 | |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8905 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 8906 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 8907 | #else |
| 8908 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 8909 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 8910 | |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8911 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 8912 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 8913 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 8914 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
| 8915 | #define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4 |
| 8916 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5 |
| 8917 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6 |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8918 | |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8919 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | baf968c | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 8920 | ( (uint16_t) ( \ |
| 8921 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 8922 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 8923 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 8924 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
| 8925 | ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \ |
| 8926 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | 7bf7710 | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 8927 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8928 | |
Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 8929 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 4152762 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 8930 | MBEDTLS_VERSION_MAJOR, |
| 8931 | MBEDTLS_VERSION_MINOR, |
| 8932 | MBEDTLS_VERSION_PATCH, |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8933 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, |
| 8934 | ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, |
Hanno Becker | 557fe9f | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 8935 | }; |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 8936 | |
| 8937 | /* |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 8938 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 8939 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 8940 | * |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8941 | * opaque mbedtls_version[3]; // major, minor, patch |
| 8942 | * opaque session_format[2]; // version-specific 16-bit field determining |
| 8943 | * // the format of the remaining |
| 8944 | * // serialized data. |
Hanno Becker | b36db4f | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 8945 | * |
| 8946 | * Note: When updating the format, remember to keep |
| 8947 | * these version+format bytes. |
| 8948 | * |
Hanno Becker | 7bf7710 | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 8949 | * // In this version, `session_format` determines |
| 8950 | * // the setting of those compile-time |
| 8951 | * // configuration options which influence |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8952 | * // the structure of mbedtls_ssl_session. |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 8953 | * uint64 start_time; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8954 | * uint8 ciphersuite[2]; // defined by the standard |
| 8955 | * uint8 compression; // 0 or 1 |
| 8956 | * uint8 session_id_len; // at most 32 |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 8957 | * opaque session_id[32]; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8958 | * opaque master[48]; // fixed length in the standard |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 8959 | * uint32 verify_result; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8960 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 8961 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 8962 | * uint32 ticket_lifetime; |
Hanno Becker | 26829e9 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 8963 | * uint8 mfl_code; // up to 255 according to standard |
| 8964 | * uint8 trunc_hmac; // 0 or 1 |
| 8965 | * uint8 encrypt_then_mac; // 0 or 1 |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 8966 | * |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 8967 | * The order is the same as in the definition of the structure, except |
| 8968 | * verify_result is put before peer_cert so that all mandatory fields come |
| 8969 | * together in one block. |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 8970 | */ |
| 8971 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 8972 | unsigned char *buf, |
| 8973 | size_t buf_len, |
| 8974 | size_t *olen ) |
| 8975 | { |
| 8976 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 8977 | size_t used = 0; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 8978 | #if defined(MBEDTLS_HAVE_TIME) |
| 8979 | uint64_t start; |
| 8980 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 8981 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8982 | size_t cert_len; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 8983 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 8984 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 8985 | /* |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 8986 | * Add version identifier |
| 8987 | */ |
| 8988 | |
| 8989 | used += sizeof( ssl_serialized_session_header ); |
| 8990 | |
| 8991 | if( used <= buf_len ) |
| 8992 | { |
| 8993 | memcpy( p, ssl_serialized_session_header, |
| 8994 | sizeof( ssl_serialized_session_header ) ); |
| 8995 | p += sizeof( ssl_serialized_session_header ); |
| 8996 | } |
| 8997 | |
| 8998 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 8999 | * Time |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9000 | */ |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9001 | #if defined(MBEDTLS_HAVE_TIME) |
| 9002 | used += 8; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9003 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9004 | if( used <= buf_len ) |
| 9005 | { |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9006 | start = (uint64_t) session->start; |
| 9007 | |
| 9008 | *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); |
| 9009 | *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); |
| 9010 | *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); |
| 9011 | *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); |
| 9012 | *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); |
| 9013 | *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); |
| 9014 | *p++ = (unsigned char)( ( start >> 8 ) & 0xFF ); |
| 9015 | *p++ = (unsigned char)( ( start ) & 0xFF ); |
| 9016 | } |
| 9017 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9018 | |
| 9019 | /* |
| 9020 | * Basic mandatory fields |
| 9021 | */ |
| 9022 | used += 2 /* ciphersuite */ |
| 9023 | + 1 /* compression */ |
| 9024 | + 1 /* id_len */ |
| 9025 | + sizeof( session->id ) |
| 9026 | + sizeof( session->master ) |
| 9027 | + 4; /* verify_result */ |
| 9028 | |
| 9029 | if( used <= buf_len ) |
| 9030 | { |
| 9031 | *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF ); |
| 9032 | *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF ); |
| 9033 | |
| 9034 | *p++ = (unsigned char)( session->compression & 0xFF ); |
| 9035 | |
| 9036 | *p++ = (unsigned char)( session->id_len & 0xFF ); |
| 9037 | memcpy( p, session->id, 32 ); |
| 9038 | p += 32; |
| 9039 | |
| 9040 | memcpy( p, session->master, 48 ); |
| 9041 | p += 48; |
| 9042 | |
| 9043 | *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); |
| 9044 | *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); |
| 9045 | *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF ); |
| 9046 | *p++ = (unsigned char)( ( session->verify_result ) & 0xFF ); |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9047 | } |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9048 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9049 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9050 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9051 | */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9052 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9053 | if( session->peer_cert == NULL ) |
| 9054 | cert_len = 0; |
| 9055 | else |
| 9056 | cert_len = session->peer_cert->raw.len; |
| 9057 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9058 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9059 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9060 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9061 | { |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9062 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 9063 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 9064 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
| 9065 | |
| 9066 | if( session->peer_cert != NULL ) |
| 9067 | { |
| 9068 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 9069 | p += cert_len; |
| 9070 | } |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9071 | } |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9072 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9073 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9074 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9075 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9076 | */ |
| 9077 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9078 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9079 | |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9080 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9081 | { |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9082 | *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); |
| 9083 | *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF ); |
| 9084 | *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF ); |
| 9085 | |
| 9086 | if( session->ticket != NULL ) |
| 9087 | { |
| 9088 | memcpy( p, session->ticket, session->ticket_len ); |
| 9089 | p += session->ticket_len; |
| 9090 | } |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9091 | |
| 9092 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); |
| 9093 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); |
| 9094 | *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF ); |
| 9095 | *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF ); |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9096 | } |
| 9097 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9098 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9099 | /* |
| 9100 | * Misc extension-related info |
| 9101 | */ |
| 9102 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9103 | used += 1; |
| 9104 | |
| 9105 | if( used <= buf_len ) |
| 9106 | *p++ = session->mfl_code; |
| 9107 | #endif |
| 9108 | |
| 9109 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 9110 | used += 1; |
| 9111 | |
| 9112 | if( used <= buf_len ) |
| 9113 | *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF ); |
| 9114 | #endif |
| 9115 | |
| 9116 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9117 | used += 1; |
| 9118 | |
| 9119 | if( used <= buf_len ) |
| 9120 | *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); |
| 9121 | #endif |
| 9122 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9123 | /* Done */ |
Manuel Pégourié-Gonnard | 32ce596 | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 9124 | *olen = used; |
| 9125 | |
| 9126 | if( used > buf_len ) |
| 9127 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9128 | |
| 9129 | return( 0 ); |
| 9130 | } |
| 9131 | |
| 9132 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9133 | * Unserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9134 | * |
| 9135 | * This internal version is wrapped by a public function that cleans up in |
| 9136 | * case of error. |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9137 | */ |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9138 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 9139 | const unsigned char *buf, |
| 9140 | size_t len ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9141 | { |
| 9142 | const unsigned char *p = buf; |
| 9143 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9144 | #if defined(MBEDTLS_HAVE_TIME) |
| 9145 | uint64_t start; |
| 9146 | #endif |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9147 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9148 | size_t cert_len; |
| 9149 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9150 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9151 | /* |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9152 | * Check version identifier |
| 9153 | */ |
| 9154 | |
| 9155 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
Hanno Becker | 1d8b6d7 | 2019-05-28 13:59:44 +0100 | [diff] [blame] | 9156 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9157 | |
| 9158 | if( memcmp( p, ssl_serialized_session_header, |
| 9159 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 9160 | { |
Hanno Becker | 5dbcc9f | 2019-06-03 12:58:39 +0100 | [diff] [blame] | 9161 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
Hanno Becker | b5352f0 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 9162 | } |
| 9163 | p += sizeof( ssl_serialized_session_header ); |
| 9164 | |
| 9165 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9166 | * Time |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9167 | */ |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9168 | #if defined(MBEDTLS_HAVE_TIME) |
| 9169 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9170 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9171 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9172 | start = ( (uint64_t) p[0] << 56 ) | |
| 9173 | ( (uint64_t) p[1] << 48 ) | |
| 9174 | ( (uint64_t) p[2] << 40 ) | |
| 9175 | ( (uint64_t) p[3] << 32 ) | |
| 9176 | ( (uint64_t) p[4] << 24 ) | |
| 9177 | ( (uint64_t) p[5] << 16 ) | |
| 9178 | ( (uint64_t) p[6] << 8 ) | |
| 9179 | ( (uint64_t) p[7] ); |
| 9180 | p += 8; |
| 9181 | |
| 9182 | session->start = (time_t) start; |
| 9183 | #endif /* MBEDTLS_HAVE_TIME */ |
| 9184 | |
| 9185 | /* |
| 9186 | * Basic mandatory fields |
| 9187 | */ |
| 9188 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 9189 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9190 | |
| 9191 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 9192 | p += 2; |
| 9193 | |
| 9194 | session->compression = *p++; |
| 9195 | |
| 9196 | session->id_len = *p++; |
| 9197 | memcpy( session->id, p, 32 ); |
| 9198 | p += 32; |
| 9199 | |
| 9200 | memcpy( session->master, p, 48 ); |
| 9201 | p += 48; |
| 9202 | |
| 9203 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 9204 | ( (uint32_t) p[1] << 16 ) | |
| 9205 | ( (uint32_t) p[2] << 8 ) | |
| 9206 | ( (uint32_t) p[3] ); |
| 9207 | p += 4; |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9208 | |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9209 | /* Immediately clear invalid pointer values that have been read, in case |
| 9210 | * we exit early before we replaced them with valid ones. */ |
| 9211 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9212 | session->peer_cert = NULL; |
| 9213 | #endif |
| 9214 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9215 | session->ticket = NULL; |
| 9216 | #endif |
| 9217 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9218 | /* |
| 9219 | * Peer certificate |
| 9220 | */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9221 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9222 | if( 3 > (size_t)( end - p ) ) |
| 9223 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9224 | |
| 9225 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 9226 | p += 3; |
| 9227 | |
| 9228 | if( cert_len == 0 ) |
| 9229 | { |
| 9230 | session->peer_cert = NULL; |
| 9231 | } |
| 9232 | else |
| 9233 | { |
| 9234 | int ret; |
| 9235 | |
| 9236 | if( cert_len > (size_t)( end - p ) ) |
| 9237 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9238 | |
| 9239 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 9240 | |
| 9241 | if( session->peer_cert == NULL ) |
| 9242 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9243 | |
| 9244 | mbedtls_x509_crt_init( session->peer_cert ); |
| 9245 | |
| 9246 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 9247 | p, cert_len ) ) != 0 ) |
| 9248 | { |
| 9249 | mbedtls_x509_crt_free( session->peer_cert ); |
| 9250 | mbedtls_free( session->peer_cert ); |
| 9251 | session->peer_cert = NULL; |
| 9252 | return( ret ); |
| 9253 | } |
| 9254 | |
| 9255 | p += cert_len; |
| 9256 | } |
| 9257 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 9258 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9259 | /* |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9260 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9261 | */ |
| 9262 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 9263 | if( 3 > (size_t)( end - p ) ) |
| 9264 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9265 | |
| 9266 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 9267 | p += 3; |
| 9268 | |
| 9269 | if( session->ticket_len != 0 ) |
| 9270 | { |
| 9271 | if( session->ticket_len > (size_t)( end - p ) ) |
| 9272 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9273 | |
| 9274 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 9275 | if( session->ticket == NULL ) |
| 9276 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 9277 | |
| 9278 | memcpy( session->ticket, p, session->ticket_len ); |
| 9279 | p += session->ticket_len; |
| 9280 | } |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9281 | |
| 9282 | if( 4 > (size_t)( end - p ) ) |
| 9283 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9284 | |
| 9285 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 9286 | ( (uint32_t) p[1] << 16 ) | |
| 9287 | ( (uint32_t) p[2] << 8 ) | |
| 9288 | ( (uint32_t) p[3] ); |
| 9289 | p += 4; |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9290 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 9291 | |
Manuel Pégourié-Gonnard | 60a4299 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 9292 | /* |
| 9293 | * Misc extension-related info |
| 9294 | */ |
| 9295 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9296 | if( 1 > (size_t)( end - p ) ) |
| 9297 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9298 | |
| 9299 | session->mfl_code = *p++; |
| 9300 | #endif |
| 9301 | |
| 9302 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 9303 | if( 1 > (size_t)( end - p ) ) |
| 9304 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9305 | |
| 9306 | session->trunc_hmac = *p++; |
| 9307 | #endif |
| 9308 | |
| 9309 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 9310 | if( 1 > (size_t)( end - p ) ) |
| 9311 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9312 | |
| 9313 | session->encrypt_then_mac = *p++; |
| 9314 | #endif |
| 9315 | |
Manuel Pégourié-Gonnard | ef4ae61 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 9316 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | 91f4ca2 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 9317 | if( p != end ) |
| 9318 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9319 | |
| 9320 | return( 0 ); |
| 9321 | } |
| 9322 | |
| 9323 | /* |
Manuel Pégourié-Gonnard | 35ccdbb | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 9324 | * Unserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 5709811 | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 9325 | */ |
| 9326 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 9327 | const unsigned char *buf, |
| 9328 | size_t len ) |
| 9329 | { |
| 9330 | int ret = ssl_session_load( session, buf, len ); |
| 9331 | |
| 9332 | if( ret != 0 ) |
| 9333 | mbedtls_ssl_session_free( session ); |
| 9334 | |
| 9335 | return( ret ); |
| 9336 | } |
| 9337 | |
| 9338 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9339 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9340 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9341 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9342 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9343 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9344 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9345 | if( ssl == NULL || ssl->conf == NULL ) |
| 9346 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9348 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9349 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9350 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9351 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9352 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9353 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9354 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9355 | #endif |
| 9356 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9357 | return( ret ); |
| 9358 | } |
| 9359 | |
| 9360 | /* |
| 9361 | * Perform the SSL handshake |
| 9362 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9363 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9364 | { |
| 9365 | int ret = 0; |
| 9366 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9367 | if( ssl == NULL || ssl->conf == NULL ) |
| 9368 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9369 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9370 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9371 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9372 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9373 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9374 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9375 | |
| 9376 | if( ret != 0 ) |
| 9377 | break; |
| 9378 | } |
| 9379 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9380 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9381 | |
| 9382 | return( ret ); |
| 9383 | } |
| 9384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9385 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 9386 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9387 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9388 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9389 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9390 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9391 | { |
| 9392 | int ret; |
| 9393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9394 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9395 | |
| 9396 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9397 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 9398 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9399 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 9400 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9401 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 9402 | 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] | 9403 | return( ret ); |
| 9404 | } |
| 9405 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9406 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9407 | |
| 9408 | return( 0 ); |
| 9409 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9410 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9411 | |
| 9412 | /* |
| 9413 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9414 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 9415 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 9416 | * - 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] | 9417 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9418 | * 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] | 9419 | * 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] | 9420 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9421 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9422 | { |
| 9423 | int ret; |
| 9424 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9425 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9426 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9427 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 9428 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9429 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9430 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 9431 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9432 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9433 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9434 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9435 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9436 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 9437 | ssl->handshake->out_msg_seq = 1; |
| 9438 | else |
| 9439 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9440 | } |
| 9441 | #endif |
| 9442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9443 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 9444 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9446 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9447 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9448 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9449 | return( ret ); |
| 9450 | } |
| 9451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9452 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9453 | |
| 9454 | return( 0 ); |
| 9455 | } |
| 9456 | |
| 9457 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9458 | * Renegotiate current connection on client, |
| 9459 | * or request renegotiation on server |
| 9460 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9461 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9462 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9463 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9464 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9465 | if( ssl == NULL || ssl->conf == NULL ) |
| 9466 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9467 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9468 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9469 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9470 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9471 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9472 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 9473 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9475 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 9476 | |
| 9477 | /* Did we already try/start sending HelloRequest? */ |
| 9478 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9479 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 9480 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9481 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9482 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9483 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9485 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9486 | /* |
| 9487 | * On client, either start the renegotiation process or, |
| 9488 | * if already in progress, continue the handshake |
| 9489 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9490 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9491 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9492 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 9493 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9494 | |
| 9495 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 9496 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9497 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9498 | return( ret ); |
| 9499 | } |
| 9500 | } |
| 9501 | else |
| 9502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9503 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9504 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9505 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9506 | return( ret ); |
| 9507 | } |
| 9508 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9509 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9510 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 9511 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9512 | } |
| 9513 | |
| 9514 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9515 | * Check record counters and renegotiate if they're above the limit. |
| 9516 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9517 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9518 | { |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9519 | size_t ep_len = ssl_ep_len( ssl ); |
| 9520 | int in_ctr_cmp; |
| 9521 | int out_ctr_cmp; |
| 9522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9523 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 9524 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9525 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9526 | { |
| 9527 | return( 0 ); |
| 9528 | } |
| 9529 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9530 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 9531 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 9532 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9533 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 9534 | |
| 9535 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9536 | { |
| 9537 | return( 0 ); |
| 9538 | } |
| 9539 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9540 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9541 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9542 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9543 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9544 | |
| 9545 | /* |
| 9546 | * Receive application data decrypted from the SSL layer |
| 9547 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9548 | 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] | 9549 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9550 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 9551 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9552 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9553 | if( ssl == NULL || ssl->conf == NULL ) |
| 9554 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9558 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9559 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9560 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9561 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9562 | return( ret ); |
| 9563 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9564 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9565 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9566 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 9567 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9568 | return( ret ); |
| 9569 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9570 | } |
| 9571 | #endif |
| 9572 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9573 | /* |
| 9574 | * Check if renegotiation is necessary and/or handshake is |
| 9575 | * in process. If yes, perform/continue, and fall through |
| 9576 | * if an unexpected packet is received while the client |
| 9577 | * is waiting for the ServerHello. |
| 9578 | * |
| 9579 | * (There is no equivalent to the last condition on |
| 9580 | * the server-side as it is not treated as within |
| 9581 | * a handshake while waiting for the ClientHello |
| 9582 | * after a renegotiation request.) |
| 9583 | */ |
| 9584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9585 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9586 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 9587 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 9588 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9589 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9590 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9591 | return( ret ); |
| 9592 | } |
| 9593 | #endif |
| 9594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9595 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9596 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9597 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9598 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 9599 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9600 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9601 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9602 | return( ret ); |
| 9603 | } |
| 9604 | } |
| 9605 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 9606 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 9607 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9608 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 9609 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 9610 | if( ssl->f_get_timer != NULL && |
| 9611 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 9612 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9613 | ssl_set_timer( ssl, ssl->conf->read_timeout ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 9614 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 9615 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 9616 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9617 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9618 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 9619 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 9620 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9621 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 9622 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9623 | } |
| 9624 | |
| 9625 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9626 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9627 | { |
| 9628 | /* |
| 9629 | * OpenSSL sends empty messages to randomize the IV |
| 9630 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 9631 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9632 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9633 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 9634 | return( 0 ); |
| 9635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9636 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9637 | return( ret ); |
| 9638 | } |
| 9639 | } |
| 9640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9641 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9642 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9643 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9644 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9645 | /* |
| 9646 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 9647 | * - For server-side, expect CLIENT_HELLO. |
| 9648 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 9649 | */ |
| 9650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9651 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9652 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9653 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9654 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9655 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9656 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9657 | |
| 9658 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9659 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9660 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 9661 | { |
| 9662 | continue; |
| 9663 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9664 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9665 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9666 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 9667 | { |
| 9668 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 9669 | } |
| 9670 | #endif |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9671 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9672 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9673 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9674 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9675 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9676 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9677 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9678 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9679 | |
| 9680 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9681 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9682 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 9683 | { |
| 9684 | continue; |
| 9685 | } |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9686 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9687 | #endif |
Manuel Pégourié-Gonnard | 889bbc7 | 2019-06-18 10:56:09 +0200 | [diff] [blame] | 9688 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 9689 | { |
| 9690 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 9691 | } |
| 9692 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9693 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9694 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 9695 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 9696 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9697 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 9698 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 9699 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 9700 | ssl->conf->allow_legacy_renegotiation == |
| 9701 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 9702 | { |
| 9703 | /* |
| 9704 | * Accept renegotiation request |
| 9705 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9706 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 9707 | /* DTLS clients need to know renego is server-initiated */ |
| 9708 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 9709 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) && |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 9710 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 9711 | { |
| 9712 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 9713 | } |
| 9714 | #endif |
| 9715 | ret = ssl_start_renegotiation( ssl ); |
| 9716 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 9717 | ret != 0 ) |
| 9718 | { |
| 9719 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 9720 | return( ret ); |
| 9721 | } |
| 9722 | } |
| 9723 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 9724 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9725 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9726 | /* |
| 9727 | * Refuse renegotiation |
| 9728 | */ |
| 9729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9730 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9731 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9732 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 9733 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9734 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 9735 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 9736 | we send a fatal alert and abort the connection. */ |
| 9737 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9738 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 9739 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 9740 | } |
| 9741 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9742 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 9743 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 9744 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9745 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 9746 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9747 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 9748 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 9749 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 9750 | { |
| 9751 | return( ret ); |
| 9752 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9753 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 9754 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9755 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 9756 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 9757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9758 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 9759 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 9760 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9761 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 9762 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 9763 | /* At this point, we don't know whether the renegotiation has been |
| 9764 | * completed or not. The cases to consider are the following: |
| 9765 | * 1) The renegotiation is complete. In this case, no new record |
| 9766 | * has been read yet. |
| 9767 | * 2) The renegotiation is incomplete because the client received |
| 9768 | * an application data record while awaiting the ServerHello. |
| 9769 | * 3) The renegotiation is incomplete because the client received |
| 9770 | * a non-handshake, non-application data message while awaiting |
| 9771 | * the ServerHello. |
| 9772 | * In each of these case, looping will be the proper action: |
| 9773 | * - For 1), the next iteration will read a new record and check |
| 9774 | * if it's application data. |
| 9775 | * - For 2), the loop condition isn't satisfied as application data |
| 9776 | * is present, hence continue is the same as break |
| 9777 | * - For 3), the loop condition is satisfied and read_record |
| 9778 | * will re-deliver the message that was held back by the client |
| 9779 | * when expecting the ServerHello. |
| 9780 | */ |
| 9781 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9782 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 9783 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9784 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 9785 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9786 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9787 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9788 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 9789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9790 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 9791 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9792 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 9793 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9794 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 9795 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9796 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 9797 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9798 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 9799 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 9800 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9801 | 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] | 9802 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 9803 | } |
| 9804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9805 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9806 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9807 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 9808 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9809 | } |
| 9810 | |
| 9811 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 9812 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 9813 | /* We're going to return something now, cancel timer, |
| 9814 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9815 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 9816 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9817 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 9818 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9819 | /* If we requested renego but received AppData, resend HelloRequest. |
| 9820 | * Do it now, after setting in_offt, to avoid taking this branch |
| 9821 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9822 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9823 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9824 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9825 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 9826 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9827 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9828 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9829 | return( ret ); |
| 9830 | } |
| 9831 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9832 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9833 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9834 | } |
| 9835 | |
| 9836 | n = ( len < ssl->in_msglen ) |
| 9837 | ? len : ssl->in_msglen; |
| 9838 | |
| 9839 | memcpy( buf, ssl->in_offt, n ); |
| 9840 | ssl->in_msglen -= n; |
| 9841 | |
| 9842 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9843 | { |
| 9844 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9845 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 9846 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9847 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9848 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9849 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9850 | /* more data available */ |
| 9851 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9852 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9854 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9855 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 9856 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9857 | } |
| 9858 | |
| 9859 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 9860 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 9861 | * fragment length and buffer size. |
| 9862 | * |
| 9863 | * According to RFC 5246 Section 6.2.1: |
| 9864 | * |
| 9865 | * Zero-length fragments of Application data MAY be sent as they are |
| 9866 | * potentially useful as a traffic analysis countermeasure. |
| 9867 | * |
| 9868 | * Therefore, it is possible that the input message length is 0 and the |
| 9869 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9870 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 9871 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 9872 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9873 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9874 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 9875 | const size_t max_len = (size_t) ret; |
| 9876 | |
| 9877 | if( ret < 0 ) |
| 9878 | { |
| 9879 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 9880 | return( ret ); |
| 9881 | } |
| 9882 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9883 | if( len > max_len ) |
| 9884 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9885 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 9886 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9887 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9888 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9889 | "maximum fragment length: %d > %d", |
| 9890 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9891 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9892 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 9893 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9894 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 9895 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
| 9896 | { |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9897 | len = max_len; |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 9898 | } |
| 9899 | #endif |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9900 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 9901 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9902 | if( ssl->out_left != 0 ) |
| 9903 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 9904 | /* |
| 9905 | * The user has previously tried to send the data and |
| 9906 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 9907 | * written. In this case, we expect the high-level write function |
| 9908 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 9909 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9910 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9911 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9912 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9913 | return( ret ); |
| 9914 | } |
| 9915 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 9916 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 9917 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 9918 | /* |
| 9919 | * The user is trying to send a message the first time, so we need to |
| 9920 | * copy the data into the internal buffers and setup the data structure |
| 9921 | * to keep track of partial writes |
| 9922 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9923 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9924 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9925 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 9926 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 9927 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 9928 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9929 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 9930 | return( ret ); |
| 9931 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9932 | } |
| 9933 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 9934 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9935 | } |
| 9936 | |
| 9937 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 9938 | * Write application data, doing 1/n-1 splitting if necessary. |
| 9939 | * |
| 9940 | * 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] | 9941 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 9942 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 9943 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9944 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 9945 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 9946 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 9947 | { |
| 9948 | int ret; |
| 9949 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9950 | if( ssl->conf->cbc_record_splitting == |
| 9951 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9952 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9953 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
| 9954 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 9955 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 9956 | { |
| 9957 | return( ssl_write_real( ssl, buf, len ) ); |
| 9958 | } |
| 9959 | |
| 9960 | if( ssl->split_done == 0 ) |
| 9961 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 9962 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 9963 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 9964 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 9965 | } |
| 9966 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 9967 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 9968 | return( ret ); |
| 9969 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 9970 | |
| 9971 | return( ret + 1 ); |
| 9972 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9973 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 9974 | |
| 9975 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 9976 | * Write application data (public-facing wrapper) |
| 9977 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 9978 | 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] | 9979 | { |
| 9980 | int ret; |
| 9981 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 9982 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 9983 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9984 | if( ssl == NULL || ssl->conf == NULL ) |
| 9985 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9986 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 9987 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 9988 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 9989 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 9990 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 9991 | return( ret ); |
| 9992 | } |
| 9993 | #endif |
| 9994 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 9995 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 9996 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 9997 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 9998 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 9999 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10000 | return( ret ); |
| 10001 | } |
| 10002 | } |
| 10003 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10004 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10005 | ret = ssl_write_split( ssl, buf, len ); |
| 10006 | #else |
| 10007 | ret = ssl_write_real( ssl, buf, len ); |
| 10008 | #endif |
| 10009 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10010 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10011 | |
| 10012 | return( ret ); |
| 10013 | } |
| 10014 | |
| 10015 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10016 | * Notify the peer that the connection is being closed |
| 10017 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10018 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10019 | { |
| 10020 | int ret; |
| 10021 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10022 | if( ssl == NULL || ssl->conf == NULL ) |
| 10023 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10024 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10025 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10026 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10027 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10028 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10030 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10031 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10032 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10033 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10034 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10035 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10036 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10037 | return( ret ); |
| 10038 | } |
| 10039 | } |
| 10040 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10041 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10042 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10043 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10044 | } |
| 10045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10046 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10047 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10048 | if( transform == NULL ) |
| 10049 | return; |
| 10050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10051 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10052 | deflateEnd( &transform->ctx_deflate ); |
| 10053 | inflateEnd( &transform->ctx_inflate ); |
| 10054 | #endif |
| 10055 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10056 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 10057 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 10058 | |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10059 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10060 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 10061 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | 9223132 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10062 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10063 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10064 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10065 | } |
| 10066 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10067 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10068 | 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] | 10069 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10070 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10071 | |
| 10072 | while( cur != NULL ) |
| 10073 | { |
| 10074 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10075 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10076 | cur = next; |
| 10077 | } |
| 10078 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10079 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10080 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10081 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10082 | |
| 10083 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 10084 | { |
| 10085 | unsigned offset; |
| 10086 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10087 | |
| 10088 | if( hs == NULL ) |
| 10089 | return; |
| 10090 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 10091 | ssl_free_buffered_record( ssl ); |
| 10092 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10093 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10094 | ssl_buffering_free_slot( ssl, offset ); |
| 10095 | } |
| 10096 | |
| 10097 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 10098 | uint8_t slot ) |
| 10099 | { |
| 10100 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10101 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 10102 | |
| 10103 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 10104 | return; |
| 10105 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10106 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10107 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10108 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 10109 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10110 | mbedtls_free( hs_buf->data ); |
| 10111 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10112 | } |
| 10113 | } |
| 10114 | |
| 10115 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 10116 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10117 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10118 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10119 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 10120 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10121 | if( handshake == NULL ) |
| 10122 | return; |
| 10123 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10124 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 10125 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 10126 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 10127 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10128 | handshake->async_in_progress = 0; |
| 10129 | } |
| 10130 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 10131 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10132 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 10133 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 10134 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 10135 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 10136 | #endif |
| 10137 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10138 | #if defined(MBEDTLS_SHA256_C) |
| 10139 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 10140 | #endif |
| 10141 | #if defined(MBEDTLS_SHA512_C) |
| 10142 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 10143 | #endif |
| 10144 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 10145 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10146 | #if defined(MBEDTLS_DHM_C) |
| 10147 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10148 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10149 | #if defined(MBEDTLS_ECDH_C) |
| 10150 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10151 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 10152 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10153 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 10154 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10155 | mbedtls_free( handshake->ecjpake_cache ); |
| 10156 | handshake->ecjpake_cache = NULL; |
| 10157 | handshake->ecjpake_cache_len = 0; |
| 10158 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10159 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10160 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 10161 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 10162 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 10163 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10164 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 10165 | #endif |
| 10166 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10167 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10168 | if( handshake->psk != NULL ) |
| 10169 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10170 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10171 | mbedtls_free( handshake->psk ); |
| 10172 | } |
| 10173 | #endif |
| 10174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10175 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10176 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10177 | /* |
| 10178 | * Free only the linked list wrapper, not the keys themselves |
| 10179 | * since the belong to the SNI callback |
| 10180 | */ |
| 10181 | if( handshake->sni_key_cert != NULL ) |
| 10182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10183 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10184 | |
| 10185 | while( cur != NULL ) |
| 10186 | { |
| 10187 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10188 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10189 | cur = next; |
| 10190 | } |
| 10191 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10192 | #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] | 10193 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10194 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 10195 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10196 | #endif |
| 10197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10198 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10199 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 10200 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10201 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 10202 | #endif |
| 10203 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10204 | mbedtls_platform_zeroize( handshake, |
| 10205 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10206 | } |
| 10207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10208 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10209 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10210 | if( session == NULL ) |
| 10211 | return; |
| 10212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10213 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 10214 | if( session->peer_cert != NULL ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10215 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10216 | mbedtls_x509_crt_free( session->peer_cert ); |
| 10217 | mbedtls_free( session->peer_cert ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10218 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 10219 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 10220 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 10221 | #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] | 10222 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 10223 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 10224 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10225 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10226 | } |
| 10227 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10228 | /* |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 10229 | * Serialize a full SSL context |
| 10230 | */ |
| 10231 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 10232 | unsigned char *buf, |
| 10233 | size_t buf_len, |
| 10234 | size_t *olen ) |
| 10235 | { |
Manuel Pégourié-Gonnard | cc71c77 | 2019-06-05 09:47:18 +0200 | [diff] [blame] | 10236 | /* Unimplemented */ |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 10237 | (void) ssl; |
| 10238 | |
| 10239 | if( buf != NULL ) |
| 10240 | memset( buf, 0, buf_len ); |
| 10241 | |
| 10242 | *olen = 0; |
| 10243 | |
| 10244 | return( 0 ); |
| 10245 | } |
| 10246 | |
| 10247 | /* |
| 10248 | * Deserialize a full SSL context |
| 10249 | */ |
| 10250 | int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl, |
| 10251 | const unsigned char *buf, |
| 10252 | size_t len ) |
| 10253 | { |
Manuel Pégourié-Gonnard | cc71c77 | 2019-06-05 09:47:18 +0200 | [diff] [blame] | 10254 | /* Unimplemented */ |
Manuel Pégourié-Gonnard | d87601e | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 10255 | (void) ssl; |
| 10256 | (void) buf; |
| 10257 | (void) len; |
| 10258 | |
| 10259 | return( 0 ); |
| 10260 | } |
| 10261 | |
| 10262 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10263 | * Free an SSL context |
| 10264 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10265 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10266 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10267 | if( ssl == NULL ) |
| 10268 | return; |
| 10269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10270 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10271 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 10272 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10273 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10274 | 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] | 10275 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10276 | } |
| 10277 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 10278 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10279 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10280 | 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] | 10281 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10282 | } |
| 10283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10284 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 10285 | if( ssl->compress_buf != NULL ) |
| 10286 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10287 | 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] | 10288 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 10289 | } |
| 10290 | #endif |
| 10291 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10292 | if( ssl->transform ) |
| 10293 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10294 | mbedtls_ssl_transform_free( ssl->transform ); |
| 10295 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10296 | } |
| 10297 | |
| 10298 | if( ssl->handshake ) |
| 10299 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10300 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10301 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 10302 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10304 | mbedtls_free( ssl->handshake ); |
| 10305 | mbedtls_free( ssl->transform_negotiate ); |
| 10306 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10307 | } |
| 10308 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 10309 | if( ssl->session ) |
| 10310 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10311 | mbedtls_ssl_session_free( ssl->session ); |
| 10312 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 10313 | } |
| 10314 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 10315 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 10316 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10317 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10318 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10319 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10320 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 10321 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10322 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10323 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 10324 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 10325 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10326 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 10327 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 10328 | } |
| 10329 | #endif |
| 10330 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 10331 | #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] | 10332 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 10333 | #endif |
| 10334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10335 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 10336 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 10337 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10338 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10339 | } |
| 10340 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10341 | /* |
| 10342 | * Initialze mbedtls_ssl_config |
| 10343 | */ |
| 10344 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 10345 | { |
| 10346 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | e744eab | 2019-03-18 10:51:18 +0100 | [diff] [blame] | 10347 | |
| 10348 | #if !defined(MBEDTLS_SSL_PROTO_TLS) |
| 10349 | conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM; |
| 10350 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10351 | } |
| 10352 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 10353 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10354 | static int ssl_preset_default_hashes[] = { |
| 10355 | #if defined(MBEDTLS_SHA512_C) |
| 10356 | MBEDTLS_MD_SHA512, |
| 10357 | MBEDTLS_MD_SHA384, |
| 10358 | #endif |
| 10359 | #if defined(MBEDTLS_SHA256_C) |
| 10360 | MBEDTLS_MD_SHA256, |
| 10361 | MBEDTLS_MD_SHA224, |
| 10362 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 10363 | #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] | 10364 | MBEDTLS_MD_SHA1, |
| 10365 | #endif |
| 10366 | MBEDTLS_MD_NONE |
| 10367 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 10368 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10369 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10370 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 10371 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 10372 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 10373 | 0 |
| 10374 | }; |
| 10375 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10376 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10377 | static int ssl_preset_suiteb_hashes[] = { |
| 10378 | MBEDTLS_MD_SHA256, |
| 10379 | MBEDTLS_MD_SHA384, |
| 10380 | MBEDTLS_MD_NONE |
| 10381 | }; |
| 10382 | #endif |
| 10383 | |
| 10384 | #if defined(MBEDTLS_ECP_C) |
| 10385 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
| 10386 | MBEDTLS_ECP_DP_SECP256R1, |
| 10387 | MBEDTLS_ECP_DP_SECP384R1, |
| 10388 | MBEDTLS_ECP_DP_NONE |
| 10389 | }; |
| 10390 | #endif |
| 10391 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10392 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 10393 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10394 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10395 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10396 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10397 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 10398 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10399 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 10400 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10401 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 10402 | /* Use the functions here so that they are covered in tests, |
| 10403 | * but otherwise access member directly for efficiency */ |
| 10404 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 10405 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10406 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10407 | /* |
| 10408 | * Things that are common to all presets |
| 10409 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10410 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10411 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 10412 | { |
| 10413 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 10414 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 10415 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 10416 | #endif |
| 10417 | } |
| 10418 | #endif |
| 10419 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 10420 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10421 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 10422 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10423 | |
| 10424 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10425 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 10426 | #endif |
| 10427 | |
| 10428 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 10429 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
Jarno Lamsa | d9382f8 | 2019-06-10 10:27:14 +0300 | [diff] [blame] | 10430 | conf->enforce_extended_master_secret = |
Jarno Lamsa | 18b9a49 | 2019-06-10 15:23:29 +0300 | [diff] [blame] | 10431 | MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10432 | #endif |
| 10433 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10434 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 10435 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 10436 | #endif |
| 10437 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 10438 | #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] | 10439 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 10440 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 10441 | #endif |
| 10442 | |
| 10443 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 10444 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 10445 | #endif |
| 10446 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 10447 | #if defined(MBEDTLS_SSL_SRV_C) |
| 10448 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 10449 | #endif |
| 10450 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10451 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10452 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 10453 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 10454 | #endif |
| 10455 | |
| 10456 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 10457 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10458 | memset( conf->renego_period, 0x00, 2 ); |
| 10459 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10460 | #endif |
| 10461 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10462 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 10463 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 10464 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 10465 | const unsigned char dhm_p[] = |
| 10466 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 10467 | const unsigned char dhm_g[] = |
| 10468 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 10469 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 10470 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 10471 | dhm_p, sizeof( dhm_p ), |
| 10472 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10473 | { |
| 10474 | return( ret ); |
| 10475 | } |
| 10476 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 10477 | #endif |
| 10478 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10479 | /* |
| 10480 | * Preset-specific defaults |
| 10481 | */ |
| 10482 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10483 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10484 | /* |
| 10485 | * NSA Suite B |
| 10486 | */ |
| 10487 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 10488 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 10489 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 10490 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 10491 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 10492 | |
| 10493 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 10494 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 10495 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 10496 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 10497 | ssl_preset_suiteb_ciphersuites; |
| 10498 | |
| 10499 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10500 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10501 | #endif |
| 10502 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10503 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10504 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 10505 | #endif |
| 10506 | |
| 10507 | #if defined(MBEDTLS_ECP_C) |
| 10508 | conf->curve_list = ssl_preset_suiteb_curves; |
| 10509 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 10510 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10511 | |
| 10512 | /* |
| 10513 | * Default |
| 10514 | */ |
| 10515 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 10516 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 10517 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 10518 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 10519 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 10520 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 10521 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 10522 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 10523 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10524 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 10525 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 10526 | |
| 10527 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 64c1681 | 2019-06-06 12:43:51 +0200 | [diff] [blame] | 10528 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10529 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 10530 | #endif |
| 10531 | |
| 10532 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 10533 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 10534 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 10535 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 10536 | mbedtls_ssl_list_ciphersuites(); |
| 10537 | |
| 10538 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10539 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 10540 | #endif |
| 10541 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10542 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10543 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10544 | #endif |
| 10545 | |
| 10546 | #if defined(MBEDTLS_ECP_C) |
| 10547 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 10548 | #endif |
| 10549 | |
| 10550 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 10551 | conf->dhm_min_bitlen = 1024; |
| 10552 | #endif |
| 10553 | } |
| 10554 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10555 | return( 0 ); |
| 10556 | } |
| 10557 | |
| 10558 | /* |
| 10559 | * Free mbedtls_ssl_config |
| 10560 | */ |
| 10561 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 10562 | { |
| 10563 | #if defined(MBEDTLS_DHM_C) |
| 10564 | mbedtls_mpi_free( &conf->dhm_P ); |
| 10565 | mbedtls_mpi_free( &conf->dhm_G ); |
| 10566 | #endif |
| 10567 | |
| 10568 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10569 | if( conf->psk != NULL ) |
| 10570 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10571 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10572 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 10573 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10574 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 10575 | } |
| 10576 | |
| 10577 | if( conf->psk_identity != NULL ) |
| 10578 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10579 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 10580 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 10581 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10582 | conf->psk_identity_len = 0; |
| 10583 | } |
| 10584 | #endif |
| 10585 | |
| 10586 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10587 | ssl_key_cert_free( conf->key_cert ); |
| 10588 | #endif |
| 10589 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10590 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10591 | } |
| 10592 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 10593 | #if defined(MBEDTLS_PK_C) && \ |
| 10594 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10595 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10596 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10597 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10598 | 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] | 10599 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10600 | #if defined(MBEDTLS_RSA_C) |
| 10601 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 10602 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10603 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10604 | #if defined(MBEDTLS_ECDSA_C) |
| 10605 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 10606 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10607 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10608 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10609 | } |
| 10610 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 10611 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 10612 | { |
| 10613 | switch( type ) { |
| 10614 | case MBEDTLS_PK_RSA: |
| 10615 | return( MBEDTLS_SSL_SIG_RSA ); |
| 10616 | case MBEDTLS_PK_ECDSA: |
| 10617 | case MBEDTLS_PK_ECKEY: |
| 10618 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 10619 | default: |
| 10620 | return( MBEDTLS_SSL_SIG_ANON ); |
| 10621 | } |
| 10622 | } |
| 10623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10624 | 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] | 10625 | { |
| 10626 | switch( sig ) |
| 10627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10628 | #if defined(MBEDTLS_RSA_C) |
| 10629 | case MBEDTLS_SSL_SIG_RSA: |
| 10630 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10631 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10632 | #if defined(MBEDTLS_ECDSA_C) |
| 10633 | case MBEDTLS_SSL_SIG_ECDSA: |
| 10634 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10635 | #endif |
| 10636 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10637 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10638 | } |
| 10639 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 10640 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10641 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 10642 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 10643 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 10644 | |
| 10645 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 10646 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 10647 | mbedtls_pk_type_t sig_alg ) |
| 10648 | { |
| 10649 | switch( sig_alg ) |
| 10650 | { |
| 10651 | case MBEDTLS_PK_RSA: |
| 10652 | return( set->rsa ); |
| 10653 | case MBEDTLS_PK_ECDSA: |
| 10654 | return( set->ecdsa ); |
| 10655 | default: |
| 10656 | return( MBEDTLS_MD_NONE ); |
| 10657 | } |
| 10658 | } |
| 10659 | |
| 10660 | /* Add a signature-hash-pair to a signature-hash set */ |
| 10661 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 10662 | mbedtls_pk_type_t sig_alg, |
| 10663 | mbedtls_md_type_t md_alg ) |
| 10664 | { |
| 10665 | switch( sig_alg ) |
| 10666 | { |
| 10667 | case MBEDTLS_PK_RSA: |
| 10668 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 10669 | set->rsa = md_alg; |
| 10670 | break; |
| 10671 | |
| 10672 | case MBEDTLS_PK_ECDSA: |
| 10673 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 10674 | set->ecdsa = md_alg; |
| 10675 | break; |
| 10676 | |
| 10677 | default: |
| 10678 | break; |
| 10679 | } |
| 10680 | } |
| 10681 | |
| 10682 | /* Allow exactly one hash algorithm for each signature. */ |
| 10683 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 10684 | mbedtls_md_type_t md_alg ) |
| 10685 | { |
| 10686 | set->rsa = md_alg; |
| 10687 | set->ecdsa = md_alg; |
| 10688 | } |
| 10689 | |
| 10690 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 10691 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 10692 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 10693 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10694 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 10695 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10696 | 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] | 10697 | { |
| 10698 | switch( hash ) |
| 10699 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10700 | #if defined(MBEDTLS_MD5_C) |
| 10701 | case MBEDTLS_SSL_HASH_MD5: |
| 10702 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10703 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10704 | #if defined(MBEDTLS_SHA1_C) |
| 10705 | case MBEDTLS_SSL_HASH_SHA1: |
| 10706 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10707 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10708 | #if defined(MBEDTLS_SHA256_C) |
| 10709 | case MBEDTLS_SSL_HASH_SHA224: |
| 10710 | return( MBEDTLS_MD_SHA224 ); |
| 10711 | case MBEDTLS_SSL_HASH_SHA256: |
| 10712 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10713 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10714 | #if defined(MBEDTLS_SHA512_C) |
| 10715 | case MBEDTLS_SSL_HASH_SHA384: |
| 10716 | return( MBEDTLS_MD_SHA384 ); |
| 10717 | case MBEDTLS_SSL_HASH_SHA512: |
| 10718 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10719 | #endif |
| 10720 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10721 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10722 | } |
| 10723 | } |
| 10724 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10725 | /* |
| 10726 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 10727 | */ |
| 10728 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 10729 | { |
| 10730 | switch( md ) |
| 10731 | { |
| 10732 | #if defined(MBEDTLS_MD5_C) |
| 10733 | case MBEDTLS_MD_MD5: |
| 10734 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 10735 | #endif |
| 10736 | #if defined(MBEDTLS_SHA1_C) |
| 10737 | case MBEDTLS_MD_SHA1: |
| 10738 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 10739 | #endif |
| 10740 | #if defined(MBEDTLS_SHA256_C) |
| 10741 | case MBEDTLS_MD_SHA224: |
| 10742 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 10743 | case MBEDTLS_MD_SHA256: |
| 10744 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 10745 | #endif |
| 10746 | #if defined(MBEDTLS_SHA512_C) |
| 10747 | case MBEDTLS_MD_SHA384: |
| 10748 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 10749 | case MBEDTLS_MD_SHA512: |
| 10750 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 10751 | #endif |
| 10752 | default: |
| 10753 | return( MBEDTLS_SSL_HASH_NONE ); |
| 10754 | } |
| 10755 | } |
| 10756 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 10757 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 10758 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10759 | * 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] | 10760 | * 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] | 10761 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 10762 | 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] | 10763 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10764 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 10765 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 10766 | if( ssl->conf->curve_list == NULL ) |
| 10767 | return( -1 ); |
| 10768 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10769 | 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] | 10770 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 10771 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 10772 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 10773 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 10774 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 10775 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10776 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10777 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10778 | /* |
| 10779 | * Check if a hash proposed by the peer is in our list. |
| 10780 | * Return 0 if we're willing to use it, -1 otherwise. |
| 10781 | */ |
| 10782 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 10783 | mbedtls_md_type_t md ) |
| 10784 | { |
| 10785 | const int *cur; |
| 10786 | |
| 10787 | if( ssl->conf->sig_hashes == NULL ) |
| 10788 | return( -1 ); |
| 10789 | |
| 10790 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 10791 | if( *cur == (int) md ) |
| 10792 | return( 0 ); |
| 10793 | |
| 10794 | return( -1 ); |
| 10795 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10796 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10797 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10798 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10799 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 10800 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10801 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 10802 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10803 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10804 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10805 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10806 | int usage = 0; |
| 10807 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10808 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10809 | const char *ext_oid; |
| 10810 | size_t ext_len; |
| 10811 | #endif |
| 10812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10813 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 10814 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10815 | ((void) cert); |
| 10816 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10817 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10818 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10820 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 10821 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10822 | { |
| 10823 | /* Server part of the key exchange */ |
| 10824 | switch( ciphersuite->key_exchange ) |
| 10825 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10826 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 10827 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 10828 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10829 | break; |
| 10830 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10831 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 10832 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 10833 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 10834 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10835 | break; |
| 10836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10837 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 10838 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 10839 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10840 | break; |
| 10841 | |
| 10842 | /* 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] | 10843 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 10844 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 10845 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 10846 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 10847 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10848 | usage = 0; |
| 10849 | } |
| 10850 | } |
| 10851 | else |
| 10852 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10853 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 10854 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10855 | } |
| 10856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10857 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10858 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 10859 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10860 | ret = -1; |
| 10861 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10862 | #else |
| 10863 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10864 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10865 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10866 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 10867 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10868 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10869 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 10870 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10871 | } |
| 10872 | else |
| 10873 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10874 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 10875 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10876 | } |
| 10877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10878 | 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] | 10879 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 10880 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10881 | ret = -1; |
| 10882 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10883 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10884 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10885 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10886 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10887 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 10888 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 10889 | /* |
| 10890 | * Convert version numbers to/from wire format |
| 10891 | * and, for DTLS, to/from TLS equivalent. |
| 10892 | * |
| 10893 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 10894 | * 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] | 10895 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 10896 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 10897 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10898 | 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] | 10899 | unsigned char ver[2] ) |
| 10900 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10901 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) |
| 10902 | ((void) transport); |
| 10903 | #endif |
| 10904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10905 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10906 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 10907 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10908 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 10909 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 10910 | |
| 10911 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 10912 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 10913 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10914 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 10915 | #endif |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10916 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 10917 | { |
| 10918 | ver[0] = (unsigned char) major; |
| 10919 | ver[1] = (unsigned char) minor; |
| 10920 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10921 | #endif |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 10922 | } |
| 10923 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10924 | 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] | 10925 | const unsigned char ver[2] ) |
| 10926 | { |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10927 | #if !defined(MBEDTLS_SSL_TRANSPORT__BOTH) |
| 10928 | ((void) transport); |
| 10929 | #endif |
| 10930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10931 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10932 | if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 10933 | { |
| 10934 | *major = 255 - ver[0] + 2; |
| 10935 | *minor = 255 - ver[1] + 1; |
| 10936 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10937 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 10938 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 10939 | } |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10940 | MBEDTLS_SSL_TRANSPORT_ELSE |
Manuel Pégourié-Gonnard | 8794a42 | 2019-06-11 10:04:57 +0200 | [diff] [blame] | 10941 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ff4bd9f | 2019-06-06 10:34:48 +0200 | [diff] [blame] | 10942 | #if defined(MBEDTLS_SSL_PROTO_TLS) |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 10943 | { |
| 10944 | *major = ver[0]; |
| 10945 | *minor = ver[1]; |
| 10946 | } |
Manuel Pégourié-Gonnard | ec1c222 | 2019-06-12 10:18:26 +0200 | [diff] [blame] | 10947 | #endif /* MBEDTLS_SSL_PROTO_TLS */ |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 10948 | } |
| 10949 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 10950 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 10951 | { |
| 10952 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10953 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 10954 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 10955 | |
| 10956 | switch( md ) |
| 10957 | { |
| 10958 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 10959 | #if defined(MBEDTLS_MD5_C) |
| 10960 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 10961 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 10962 | #endif |
| 10963 | #if defined(MBEDTLS_SHA1_C) |
| 10964 | case MBEDTLS_SSL_HASH_SHA1: |
| 10965 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 10966 | break; |
| 10967 | #endif |
| 10968 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 10969 | #if defined(MBEDTLS_SHA512_C) |
| 10970 | case MBEDTLS_SSL_HASH_SHA384: |
| 10971 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 10972 | break; |
| 10973 | #endif |
| 10974 | #if defined(MBEDTLS_SHA256_C) |
| 10975 | case MBEDTLS_SSL_HASH_SHA256: |
| 10976 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 10977 | break; |
| 10978 | #endif |
| 10979 | default: |
| 10980 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 10981 | } |
| 10982 | |
| 10983 | return 0; |
| 10984 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 10985 | (void) ssl; |
| 10986 | (void) md; |
| 10987 | |
| 10988 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 10989 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 10990 | } |
| 10991 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 10992 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 10993 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 10994 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 10995 | unsigned char *output, |
| 10996 | unsigned char *data, size_t data_len ) |
| 10997 | { |
| 10998 | int ret = 0; |
| 10999 | mbedtls_md5_context mbedtls_md5; |
| 11000 | mbedtls_sha1_context mbedtls_sha1; |
| 11001 | |
| 11002 | mbedtls_md5_init( &mbedtls_md5 ); |
| 11003 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 11004 | |
| 11005 | /* |
| 11006 | * digitally-signed struct { |
| 11007 | * opaque md5_hash[16]; |
| 11008 | * opaque sha_hash[20]; |
| 11009 | * }; |
| 11010 | * |
| 11011 | * md5_hash |
| 11012 | * MD5(ClientHello.random + ServerHello.random |
| 11013 | * + ServerParams); |
| 11014 | * sha_hash |
| 11015 | * SHA(ClientHello.random + ServerHello.random |
| 11016 | * + ServerParams); |
| 11017 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11018 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11019 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11020 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11021 | goto exit; |
| 11022 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11023 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11024 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11025 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11026 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11027 | goto exit; |
| 11028 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11029 | 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] | 11030 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11031 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11032 | goto exit; |
| 11033 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11034 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11035 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11036 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11037 | goto exit; |
| 11038 | } |
| 11039 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11040 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11041 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11042 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11043 | goto exit; |
| 11044 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11045 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11046 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11047 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11048 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11049 | goto exit; |
| 11050 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11051 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11052 | data_len ) ) != 0 ) |
| 11053 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11054 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11055 | goto exit; |
| 11056 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11057 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11058 | output + 16 ) ) != 0 ) |
| 11059 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11060 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11061 | goto exit; |
| 11062 | } |
| 11063 | |
| 11064 | exit: |
| 11065 | mbedtls_md5_free( &mbedtls_md5 ); |
| 11066 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 11067 | |
| 11068 | if( ret != 0 ) |
| 11069 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11070 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 11071 | |
| 11072 | return( ret ); |
| 11073 | |
| 11074 | } |
| 11075 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 11076 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 11077 | |
| 11078 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 11079 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 11080 | 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] | 11081 | unsigned char *hash, size_t *hashlen, |
| 11082 | unsigned char *data, size_t data_len, |
| 11083 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11084 | { |
| 11085 | int ret = 0; |
| 11086 | mbedtls_md_context_t ctx; |
| 11087 | 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] | 11088 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11089 | |
| 11090 | mbedtls_md_init( &ctx ); |
| 11091 | |
| 11092 | /* |
| 11093 | * digitally-signed struct { |
| 11094 | * opaque client_random[32]; |
| 11095 | * opaque server_random[32]; |
| 11096 | * ServerDHParams params; |
| 11097 | * }; |
| 11098 | */ |
| 11099 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 11100 | { |
| 11101 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 11102 | goto exit; |
| 11103 | } |
| 11104 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 11105 | { |
| 11106 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 11107 | goto exit; |
| 11108 | } |
| 11109 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11110 | { |
| 11111 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 11112 | goto exit; |
| 11113 | } |
| 11114 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 11115 | { |
| 11116 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 11117 | goto exit; |
| 11118 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 11119 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11120 | { |
| 11121 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 11122 | goto exit; |
| 11123 | } |
| 11124 | |
| 11125 | exit: |
| 11126 | mbedtls_md_free( &ctx ); |
| 11127 | |
| 11128 | if( ret != 0 ) |
| 11129 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11130 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 11131 | |
| 11132 | return( ret ); |
| 11133 | } |
| 11134 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 11135 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11137 | #endif /* MBEDTLS_SSL_TLS_C */ |