Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 shared functions |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | /* |
| 22 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 23 | * and became an IETF standard in 1999. |
| 24 | * |
| 25 | * http://wp.netscape.com/eng/ssl3/ |
| 26 | * http://www.ietf.org/rfc/rfc2246.txt |
| 27 | * http://www.ietf.org/rfc/rfc4346.txt |
| 28 | */ |
| 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #if defined(MBEDTLS_PLATFORM_C) |
| 39 | #include "mbedtls/platform.h" |
| 40 | #else |
| 41 | #include <stdlib.h> |
| 42 | #define mbedtls_calloc calloc |
| 43 | #define mbedtls_free free |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 44 | #endif |
| 45 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/debug.h" |
| 47 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 48 | #include "mbedtls/ssl_internal.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 49 | #include "mbedtls/platform_util.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 50 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 51 | #include <string.h> |
| 52 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 53 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 54 | #include "mbedtls/psa_util.h" |
| 55 | #include "psa/crypto.h" |
| 56 | #endif |
| 57 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 58 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 59 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 60 | #endif |
| 61 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 62 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 63 | #include "mbedtls/psa_util.h" |
| 64 | #endif |
| 65 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 66 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 67 | 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] | 68 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 69 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | 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] | 71 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 73 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 74 | return( 2 ); |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 75 | #else |
| 76 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 77 | #endif |
| 78 | return( 0 ); |
| 79 | } |
| 80 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 81 | /* |
| 82 | * Start a timer. |
| 83 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 84 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | 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] | 86 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 87 | if( ssl->f_set_timer == NULL ) |
| 88 | return; |
| 89 | |
| 90 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 91 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Return -1 is timer is expired, 0 if it isn't. |
| 96 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 98 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 99 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 100 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 101 | |
| 102 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 103 | { |
| 104 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 105 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 106 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 107 | |
| 108 | return( 0 ); |
| 109 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 110 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 111 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 112 | mbedtls_ssl_transform *transform ); |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 113 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 114 | |
| 115 | #define SSL_DONT_FORCE_FLUSH 0 |
| 116 | #define SSL_FORCE_FLUSH 1 |
| 117 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 118 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 119 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 120 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 121 | /* Top-level Connection ID API */ |
| 122 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 123 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 124 | size_t len, |
| 125 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 126 | { |
| 127 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 128 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 129 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 130 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 131 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 132 | { |
| 133 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 134 | } |
| 135 | |
| 136 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 137 | conf->cid_len = len; |
| 138 | return( 0 ); |
| 139 | } |
| 140 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 141 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 142 | int enable, |
| 143 | unsigned char const *own_cid, |
| 144 | size_t own_cid_len ) |
| 145 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 146 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 147 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 148 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 149 | ssl->negotiate_cid = enable; |
| 150 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 151 | { |
| 152 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 153 | return( 0 ); |
| 154 | } |
| 155 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 156 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 157 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 158 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 159 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 160 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 161 | (unsigned) own_cid_len, |
| 162 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 163 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 164 | } |
| 165 | |
| 166 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 167 | /* Truncation is not an issue here because |
| 168 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 169 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 170 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 171 | return( 0 ); |
| 172 | } |
| 173 | |
| 174 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 175 | int *enabled, |
| 176 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 177 | size_t *peer_cid_len ) |
| 178 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 179 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 180 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 181 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 182 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 183 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 184 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 185 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 186 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 187 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 188 | * were used, but client and server requested the empty CID. |
| 189 | * This is indistinguishable from not using the CID extension |
| 190 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 191 | if( ssl->transform_in->in_cid_len == 0 && |
| 192 | ssl->transform_in->out_cid_len == 0 ) |
| 193 | { |
| 194 | return( 0 ); |
| 195 | } |
| 196 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 197 | if( peer_cid_len != NULL ) |
| 198 | { |
| 199 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 200 | if( peer_cid != NULL ) |
| 201 | { |
| 202 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 203 | ssl->transform_in->out_cid_len ); |
| 204 | } |
| 205 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 206 | |
| 207 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 208 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 209 | return( 0 ); |
| 210 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 211 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 212 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 213 | /* Forward declarations for functions related to message buffering. */ |
| 214 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ); |
| 215 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 216 | uint8_t slot ); |
| 217 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 218 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 219 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 220 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
| 221 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 222 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 223 | |
Hanno Becker | a67dee2 | 2018-08-22 10:05:20 +0100 | [diff] [blame] | 224 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ); |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 225 | 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] | 226 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 227 | size_t mtu = ssl_get_current_mtu( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 228 | |
| 229 | if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 230 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 231 | |
| 232 | return( MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 233 | } |
| 234 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 235 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 236 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 237 | size_t const bytes_written = ssl->out_left; |
| 238 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 239 | |
| 240 | /* Double-check that the write-index hasn't gone |
| 241 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 242 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 243 | { |
| 244 | /* Should never happen... */ |
| 245 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 246 | } |
| 247 | |
| 248 | return( (int) ( mtu - bytes_written ) ); |
| 249 | } |
| 250 | |
| 251 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 252 | { |
| 253 | int ret; |
| 254 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 255 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 256 | |
| 257 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 258 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 259 | |
| 260 | if( max_len > mfl ) |
| 261 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 262 | |
| 263 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 264 | * only limits the maximum record payload size, so in theory |
| 265 | * we would be allowed to pack multiple records of payload size |
| 266 | * MFL into a single datagram. However, this would mean that there's |
| 267 | * no way to explicitly communicate MTU restrictions to the peer. |
| 268 | * |
| 269 | * The following reduction of max_len makes sure that we never |
| 270 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 271 | */ |
| 272 | if( max_len <= ssl->out_left ) |
| 273 | return( 0 ); |
| 274 | |
| 275 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 276 | #endif |
| 277 | |
| 278 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 279 | if( ret < 0 ) |
| 280 | return( ret ); |
| 281 | remaining = (size_t) ret; |
| 282 | |
| 283 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 284 | if( ret < 0 ) |
| 285 | return( ret ); |
| 286 | expansion = (size_t) ret; |
| 287 | |
| 288 | if( remaining <= expansion ) |
| 289 | return( 0 ); |
| 290 | |
| 291 | remaining -= expansion; |
| 292 | if( remaining >= max_len ) |
| 293 | remaining = max_len; |
| 294 | |
| 295 | return( (int) remaining ); |
| 296 | } |
| 297 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 298 | /* |
| 299 | * Double the retransmit timeout value, within the allowed range, |
| 300 | * returning -1 if the maximum value has already been reached. |
| 301 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 303 | { |
| 304 | uint32_t new_timeout; |
| 305 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 306 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 307 | return( -1 ); |
| 308 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 309 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 310 | * in the following way: after the initial transmission and a first |
| 311 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 312 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 313 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 314 | * on most non-IP stacks too. */ |
| 315 | if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 316 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 317 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 318 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 319 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 321 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 322 | |
| 323 | /* Avoid arithmetic overflow and range overflow */ |
| 324 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 325 | new_timeout > ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 326 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 327 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | 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] | 332 | ssl->handshake->retransmit_timeout ) ); |
| 333 | |
| 334 | return( 0 ); |
| 335 | } |
| 336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 338 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 339 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | 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] | 341 | ssl->handshake->retransmit_timeout ) ); |
| 342 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 346 | /* |
| 347 | * Convert max_fragment_length codes to length. |
| 348 | * RFC 6066 says: |
| 349 | * enum{ |
| 350 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 351 | * } MaxFragmentLength; |
| 352 | * and we add 0 -> extension unused |
| 353 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 354 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 355 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 356 | switch( mfl ) |
| 357 | { |
| 358 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 359 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 360 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 361 | return 512; |
| 362 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 363 | return 1024; |
| 364 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 365 | return 2048; |
| 366 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 367 | return 4096; |
| 368 | default: |
| 369 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 370 | } |
| 371 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 372 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 373 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 374 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 375 | const mbedtls_ssl_session *src ) |
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 | mbedtls_ssl_session_free( dst ); |
| 378 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 379 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 380 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 381 | |
| 382 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 383 | if( src->peer_cert != NULL ) |
| 384 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 385 | int ret; |
| 386 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 387 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 388 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 389 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
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_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 392 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | 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] | 394 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 395 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 397 | dst->peer_cert = NULL; |
| 398 | return( ret ); |
| 399 | } |
| 400 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 401 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 402 | if( src->peer_cert_digest != NULL ) |
| 403 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 404 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 405 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 406 | if( dst->peer_cert_digest == NULL ) |
| 407 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 408 | |
| 409 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 410 | src->peer_cert_digest_len ); |
| 411 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 412 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 413 | } |
| 414 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 417 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 418 | #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] | 419 | if( src->ticket != NULL ) |
| 420 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 421 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 422 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 423 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 424 | |
| 425 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 426 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 427 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 428 | |
| 429 | return( 0 ); |
| 430 | } |
| 431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 433 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 434 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 435 | size_t keylen, |
| 436 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 437 | size_t ivlen, |
| 438 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 439 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 440 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 441 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 442 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 443 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 444 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 445 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 446 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 447 | /* |
| 448 | * Key material generation |
| 449 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 450 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 451 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 452 | const char *label, |
| 453 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 454 | unsigned char *dstbuf, size_t dlen ) |
| 455 | { |
Andres Amaya Garcia | 3395250 | 2017-07-20 16:29:16 +0100 | [diff] [blame] | 456 | int ret = 0; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 457 | size_t i; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 458 | mbedtls_md5_context md5; |
| 459 | mbedtls_sha1_context sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 460 | unsigned char padding[16]; |
| 461 | unsigned char sha1sum[20]; |
| 462 | ((void)label); |
| 463 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 464 | mbedtls_md5_init( &md5 ); |
| 465 | mbedtls_sha1_init( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 466 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 467 | /* |
| 468 | * SSLv3: |
| 469 | * block = |
| 470 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 471 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 472 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 473 | * ... |
| 474 | */ |
| 475 | for( i = 0; i < dlen / 16; i++ ) |
| 476 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 477 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 478 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 479 | if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 480 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 481 | if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 482 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 483 | if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 484 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 485 | if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 486 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 487 | if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 488 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 489 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 490 | if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 491 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 492 | if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 493 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 494 | if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 495 | goto exit; |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 496 | if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 ) |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 497 | goto exit; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 500 | exit: |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 501 | mbedtls_md5_free( &md5 ); |
| 502 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 503 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 504 | mbedtls_platform_zeroize( padding, sizeof( padding ) ); |
| 505 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 506 | |
Andres Amaya Garcia | 1a607a1 | 2017-06-29 17:09:42 +0100 | [diff] [blame] | 507 | return( ret ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 508 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 511 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 512 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 513 | const char *label, |
| 514 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 515 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 516 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 517 | size_t nb, hs; |
| 518 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 519 | const unsigned char *S1, *S2; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 520 | unsigned char *tmp; |
| 521 | size_t tmp_len = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 522 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 523 | const mbedtls_md_info_t *md_info; |
| 524 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 525 | int ret; |
| 526 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 528 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 529 | tmp_len = 20 + strlen( label ) + rlen; |
| 530 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 531 | if( tmp == NULL ) |
| 532 | { |
| 533 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 534 | goto exit; |
| 535 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 536 | |
| 537 | hs = ( slen + 1 ) / 2; |
| 538 | S1 = secret; |
| 539 | S2 = secret + slen - hs; |
| 540 | |
| 541 | nb = strlen( label ); |
| 542 | memcpy( tmp + 20, label, nb ); |
| 543 | memcpy( tmp + 20 + nb, random, rlen ); |
| 544 | nb += rlen; |
| 545 | |
| 546 | /* |
| 547 | * First compute P_md5(secret,label+random)[0..dlen] |
| 548 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 549 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 550 | { |
| 551 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 552 | goto exit; |
| 553 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 555 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 556 | { |
| 557 | goto exit; |
| 558 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 559 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 561 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 562 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 563 | |
| 564 | for( i = 0; i < dlen; i += 16 ) |
| 565 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 567 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 568 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 569 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 570 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 571 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 572 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 573 | |
| 574 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 575 | |
| 576 | for( j = 0; j < k; j++ ) |
| 577 | dstbuf[i + j] = h_i[j]; |
| 578 | } |
| 579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 581 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 582 | /* |
| 583 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 584 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 585 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 586 | { |
| 587 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 588 | goto exit; |
| 589 | } |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 590 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 591 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 592 | { |
| 593 | goto exit; |
| 594 | } |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 595 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 597 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 598 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 599 | |
| 600 | for( i = 0; i < dlen; i += 20 ) |
| 601 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 603 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 604 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 606 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 607 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 608 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 609 | |
| 610 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 611 | |
| 612 | for( j = 0; j < k; j++ ) |
| 613 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 614 | } |
| 615 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 616 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 618 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 619 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 620 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 621 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 622 | mbedtls_free( tmp ); |
| 623 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 624 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 627 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 628 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 629 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 630 | const unsigned char *secret, size_t slen, |
| 631 | const char *label, |
| 632 | const unsigned char *random, size_t rlen, |
| 633 | unsigned char *dstbuf, size_t dlen ) |
| 634 | { |
| 635 | psa_status_t status; |
| 636 | psa_algorithm_t alg; |
| 637 | psa_key_policy_t policy; |
Andrzej Kurek | ac5dc34 | 2019-01-23 06:57:34 -0500 | [diff] [blame] | 638 | psa_key_handle_t master_slot; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 639 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 640 | |
Andrzej Kurek | 2f76075 | 2019-01-28 08:08:15 -0500 | [diff] [blame] | 641 | if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS ) |
Andrzej Kurek | ac5dc34 | 2019-01-23 06:57:34 -0500 | [diff] [blame] | 642 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 2d4faa6 | 2019-01-29 03:14:15 -0500 | [diff] [blame] | 643 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 644 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 645 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 646 | else |
| 647 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 648 | |
Andrzej Kurek | 2f76075 | 2019-01-28 08:08:15 -0500 | [diff] [blame] | 649 | policy = psa_key_policy_init(); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 650 | psa_key_policy_set_usage( &policy, |
| 651 | PSA_KEY_USAGE_DERIVE, |
| 652 | alg ); |
| 653 | status = psa_set_key_policy( master_slot, &policy ); |
| 654 | if( status != PSA_SUCCESS ) |
| 655 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 656 | |
| 657 | status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen ); |
| 658 | if( status != PSA_SUCCESS ) |
| 659 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 660 | |
| 661 | status = psa_key_derivation( &generator, |
| 662 | master_slot, alg, |
| 663 | random, rlen, |
| 664 | (unsigned char const *) label, |
| 665 | (size_t) strlen( label ), |
| 666 | dlen ); |
| 667 | if( status != PSA_SUCCESS ) |
| 668 | { |
| 669 | psa_generator_abort( &generator ); |
| 670 | psa_destroy_key( master_slot ); |
| 671 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 672 | } |
| 673 | |
| 674 | status = psa_generator_read( &generator, dstbuf, dlen ); |
| 675 | if( status != PSA_SUCCESS ) |
| 676 | { |
| 677 | psa_generator_abort( &generator ); |
| 678 | psa_destroy_key( master_slot ); |
| 679 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 680 | } |
| 681 | |
| 682 | status = psa_generator_abort( &generator ); |
| 683 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 684 | { |
| 685 | psa_destroy_key( master_slot ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 686 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 687 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 688 | |
| 689 | status = psa_destroy_key( master_slot ); |
| 690 | if( status != PSA_SUCCESS ) |
| 691 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 692 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 693 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | 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] | 699 | const unsigned char *secret, size_t slen, |
| 700 | const char *label, |
| 701 | const unsigned char *random, size_t rlen, |
| 702 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 703 | { |
| 704 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 705 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 706 | unsigned char *tmp; |
| 707 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 709 | const mbedtls_md_info_t *md_info; |
| 710 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 711 | int ret; |
| 712 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 713 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 716 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 719 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 720 | tmp_len = md_len + strlen( label ) + rlen; |
| 721 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 722 | if( tmp == NULL ) |
| 723 | { |
| 724 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 725 | goto exit; |
| 726 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 727 | |
| 728 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 729 | memcpy( tmp + md_len, label, nb ); |
| 730 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 731 | nb += rlen; |
| 732 | |
| 733 | /* |
| 734 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 735 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 737 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 738 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 739 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 740 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 741 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 742 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 743 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 744 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 745 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 746 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 747 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 748 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 749 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 750 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 751 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 752 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 753 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 754 | |
| 755 | for( j = 0; j < k; j++ ) |
| 756 | dstbuf[i + j] = h_i[j]; |
| 757 | } |
| 758 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 759 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 760 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 761 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 762 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 763 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 764 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 765 | mbedtls_free( tmp ); |
| 766 | |
| 767 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 768 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 769 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 771 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 772 | const char *label, |
| 773 | const unsigned char *random, size_t rlen, |
| 774 | unsigned char *dstbuf, size_t dlen ) |
| 775 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 776 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 777 | label, random, rlen, dstbuf, dlen ) ); |
| 778 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 780 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 781 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 782 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 783 | const char *label, |
| 784 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 785 | unsigned char *dstbuf, size_t dlen ) |
| 786 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 787 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 788 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 789 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | #endif /* MBEDTLS_SHA512_C */ |
| 791 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 792 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 793 | 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] | 794 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 795 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 796 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 797 | 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] | 798 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 800 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 801 | static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * ); |
| 802 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 803 | #endif |
| 804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 805 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 806 | static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * ); |
| 807 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 808 | #endif |
| 809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 810 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 811 | #if defined(MBEDTLS_SHA256_C) |
| 812 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 813 | static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * ); |
| 814 | 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] | 815 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 816 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 817 | #if defined(MBEDTLS_SHA512_C) |
| 818 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 819 | static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * ); |
| 820 | 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] | 821 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 822 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 823 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 824 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 825 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 826 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 827 | { |
| 828 | if( ssl->conf->f_psk != NULL ) |
| 829 | { |
| 830 | /* If we've used a callback to select the PSK, |
| 831 | * the static configuration is irrelevant. */ |
| 832 | if( ssl->handshake->psk_opaque != 0 ) |
| 833 | return( 1 ); |
| 834 | |
| 835 | return( 0 ); |
| 836 | } |
| 837 | |
| 838 | if( ssl->conf->psk_opaque != 0 ) |
| 839 | return( 1 ); |
| 840 | |
| 841 | return( 0 ); |
| 842 | } |
| 843 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 844 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 845 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 846 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 847 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 848 | { |
| 849 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 850 | if( tls_prf == ssl3_prf ) |
| 851 | { |
Ron Eldor | 0810f0b | 2019-05-15 12:32:32 +0300 | [diff] [blame] | 852 | return( MBEDTLS_SSL_TLS_PRF_SSL3 ); |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 853 | } |
| 854 | else |
| 855 | #endif |
| 856 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 857 | if( tls_prf == tls1_prf ) |
| 858 | { |
| 859 | return( MBEDTLS_SSL_TLS_PRF_TLS1 ); |
| 860 | } |
| 861 | else |
| 862 | #endif |
| 863 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 864 | #if defined(MBEDTLS_SHA512_C) |
| 865 | if( tls_prf == tls_prf_sha384 ) |
| 866 | { |
| 867 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 868 | } |
| 869 | else |
| 870 | #endif |
| 871 | #if defined(MBEDTLS_SHA256_C) |
| 872 | if( tls_prf == tls_prf_sha256 ) |
| 873 | { |
| 874 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 875 | } |
| 876 | else |
| 877 | #endif |
| 878 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 879 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 880 | } |
| 881 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 882 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 883 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 884 | const unsigned char *secret, size_t slen, |
| 885 | const char *label, |
| 886 | const unsigned char *random, size_t rlen, |
| 887 | unsigned char *dstbuf, size_t dlen ) |
| 888 | { |
| 889 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 890 | |
| 891 | switch( prf ) |
| 892 | { |
| 893 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 894 | case MBEDTLS_SSL_TLS_PRF_SSL3: |
| 895 | tls_prf = ssl3_prf; |
| 896 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 897 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 898 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 899 | case MBEDTLS_SSL_TLS_PRF_TLS1: |
| 900 | tls_prf = tls1_prf; |
| 901 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 902 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 903 | |
| 904 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 905 | #if defined(MBEDTLS_SHA512_C) |
| 906 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 907 | tls_prf = tls_prf_sha384; |
| 908 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 909 | #endif /* MBEDTLS_SHA512_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 910 | #if defined(MBEDTLS_SHA256_C) |
| 911 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 912 | tls_prf = tls_prf_sha256; |
| 913 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 914 | #endif /* MBEDTLS_SHA256_C */ |
| 915 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 916 | default: |
| 917 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 918 | } |
| 919 | |
| 920 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 921 | } |
| 922 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 923 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 924 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 925 | int ret = 0; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 926 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 927 | int psa_fallthrough; |
| 928 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 929 | unsigned char tmp[64]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 930 | unsigned char keyblk[256]; |
| 931 | unsigned char *key1; |
| 932 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 933 | unsigned char *mac_enc; |
| 934 | unsigned char *mac_dec; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 935 | size_t mac_key_len; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 936 | size_t iv_copy_len; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 937 | unsigned keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 938 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 939 | const mbedtls_cipher_info_t *cipher_info; |
| 940 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 941 | |
Hanno Becker | f9ed7d5 | 2018-11-05 12:45:16 +0000 | [diff] [blame] | 942 | /* cf. RFC 5246, Section 8.1: |
| 943 | * "The master secret is always exactly 48 bytes in length." */ |
| 944 | size_t const master_secret_len = 48; |
| 945 | |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 946 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 947 | unsigned char session_hash[48]; |
| 948 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 950 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 951 | mbedtls_ssl_transform *transform = ssl->transform_negotiate; |
| 952 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 953 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 954 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 955 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 956 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 957 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 958 | transform->encrypt_then_mac = session->encrypt_then_mac; |
| 959 | #endif |
| 960 | transform->minor_ver = ssl->minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 961 | |
| 962 | ciphersuite_info = handshake->ciphersuite_info; |
| 963 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 964 | if( cipher_info == NULL ) |
| 965 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 966 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 967 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 968 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 969 | } |
| 970 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 971 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 972 | if( md_info == NULL ) |
| 973 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 974 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 975 | ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 976 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 977 | } |
| 978 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 979 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 980 | /* Copy own and peer's CID if the use of the CID |
| 981 | * extension has been negotiated. */ |
| 982 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 983 | { |
| 984 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 985 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 986 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 987 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 988 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 989 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 990 | |
| 991 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 992 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 993 | ssl->handshake->peer_cid_len ); |
| 994 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 995 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 996 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 997 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 998 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 999 | /* |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1000 | * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1001 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1002 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1003 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1004 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1005 | handshake->tls_prf = ssl3_prf; |
| 1006 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 1007 | handshake->calc_finished = ssl_calc_finished_ssl; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1008 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1009 | else |
| 1010 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 1012 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1013 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1014 | handshake->tls_prf = tls1_prf; |
| 1015 | handshake->calc_verify = ssl_calc_verify_tls; |
| 1016 | handshake->calc_finished = ssl_calc_finished_tls; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1017 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1018 | else |
| 1019 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1020 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1021 | #if defined(MBEDTLS_SHA512_C) |
| 1022 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1023 | ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1024 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1025 | handshake->tls_prf = tls_prf_sha384; |
| 1026 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1027 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1028 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1029 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1030 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1031 | #if defined(MBEDTLS_SHA256_C) |
| 1032 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1033 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1034 | handshake->tls_prf = tls_prf_sha256; |
| 1035 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1036 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1037 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1038 | else |
| 1039 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1041 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1042 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1043 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1044 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1045 | |
| 1046 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1047 | * SSLv3: |
| 1048 | * master = |
| 1049 | * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) + |
| 1050 | * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) + |
| 1051 | * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) ) |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 1052 | * |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1053 | * TLSv1+: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1054 | * master = PRF( premaster, "master secret", randbytes )[0..47] |
| 1055 | */ |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1056 | if( handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1057 | { |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1058 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 1059 | } |
| 1060 | else |
| 1061 | { |
| 1062 | /* The label for the KDF used for key expansion. |
| 1063 | * This is either "master secret" or "extended master secret" |
| 1064 | * depending on whether the Extended Master Secret extension |
| 1065 | * is used. */ |
| 1066 | char const *lbl = "master secret"; |
| 1067 | |
| 1068 | /* The salt for the KDF used for key expansion. |
| 1069 | * - If the Extended Master Secret extension is not used, |
| 1070 | * this is ClientHello.Random + ServerHello.Random |
| 1071 | * (see Sect. 8.1 in RFC 5246). |
| 1072 | * - If the Extended Master Secret extension is used, |
| 1073 | * this is the transcript of the handshake so far. |
| 1074 | * (see Sect. 4 in RFC 7627). */ |
| 1075 | unsigned char const *salt = handshake->randbytes; |
| 1076 | size_t salt_len = 64; |
| 1077 | |
| 1078 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1079 | if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1080 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1081 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1082 | |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1083 | lbl = "extended master secret"; |
| 1084 | salt = session_hash; |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1085 | ssl->handshake->calc_verify( ssl, session_hash ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1086 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1087 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1088 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | #if defined(MBEDTLS_SHA512_C) |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1090 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 1091 | { |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1092 | salt_len = 48; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1093 | } |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1094 | else |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1095 | #endif /* MBEDTLS_SHA512_C */ |
| 1096 | salt_len = 32; |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1097 | } |
| 1098 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1099 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1100 | salt_len = 36; |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1101 | |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1102 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1103 | } |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1104 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1105 | |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1106 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1107 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1108 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
| 1109 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1110 | ssl_use_opaque_psk( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1111 | { |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1112 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1113 | psa_status_t status; |
| 1114 | psa_algorithm_t alg; |
| 1115 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 1116 | psa_key_handle_t psk; |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1117 | |
| 1118 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 1119 | |
| 1120 | psk = ssl->conf->psk_opaque; |
| 1121 | if( ssl->handshake->psk_opaque != 0 ) |
| 1122 | psk = ssl->handshake->psk_opaque; |
| 1123 | |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1124 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1125 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 1126 | else |
| 1127 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1128 | |
| 1129 | status = psa_key_derivation( &generator, psk, alg, |
| 1130 | salt, salt_len, |
| 1131 | (unsigned char const *) lbl, |
| 1132 | (size_t) strlen( lbl ), |
Hanno Becker | f9ed7d5 | 2018-11-05 12:45:16 +0000 | [diff] [blame] | 1133 | master_secret_len ); |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1134 | if( status != PSA_SUCCESS ) |
| 1135 | { |
| 1136 | psa_generator_abort( &generator ); |
| 1137 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1138 | } |
| 1139 | |
Hanno Becker | f9ed7d5 | 2018-11-05 12:45:16 +0000 | [diff] [blame] | 1140 | status = psa_generator_read( &generator, session->master, |
| 1141 | master_secret_len ); |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1142 | if( status != PSA_SUCCESS ) |
| 1143 | { |
| 1144 | psa_generator_abort( &generator ); |
| 1145 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1146 | } |
| 1147 | |
| 1148 | status = psa_generator_abort( &generator ); |
| 1149 | if( status != PSA_SUCCESS ) |
| 1150 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1151 | } |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1152 | else |
| 1153 | #endif |
| 1154 | { |
| 1155 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1156 | lbl, salt, salt_len, |
Hanno Becker | f9ed7d5 | 2018-11-05 12:45:16 +0000 | [diff] [blame] | 1157 | session->master, |
| 1158 | master_secret_len ); |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1159 | if( ret != 0 ) |
| 1160 | { |
| 1161 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1162 | return( ret ); |
| 1163 | } |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 1164 | |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1165 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1166 | handshake->premaster, |
| 1167 | handshake->pmslen ); |
Hanno Becker | 35b23c7 | 2018-10-23 12:10:41 +0100 | [diff] [blame] | 1168 | |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 1169 | mbedtls_platform_zeroize( handshake->premaster, |
| 1170 | sizeof(handshake->premaster) ); |
| 1171 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1172 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1173 | |
| 1174 | /* |
| 1175 | * Swap the client and server random values. |
| 1176 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1177 | memcpy( tmp, handshake->randbytes, 64 ); |
| 1178 | memcpy( handshake->randbytes, tmp + 32, 32 ); |
| 1179 | memcpy( handshake->randbytes + 32, tmp, 32 ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1180 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1181 | |
| 1182 | /* |
| 1183 | * SSLv3: |
| 1184 | * key block = |
| 1185 | * MD5( master + SHA1( 'A' + master + randbytes ) ) + |
| 1186 | * MD5( master + SHA1( 'BB' + master + randbytes ) ) + |
| 1187 | * MD5( master + SHA1( 'CCC' + master + randbytes ) ) + |
| 1188 | * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) + |
| 1189 | * ... |
| 1190 | * |
| 1191 | * TLSv1: |
| 1192 | * key block = PRF( master, "key expansion", randbytes ) |
| 1193 | */ |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1194 | ret = handshake->tls_prf( session->master, 48, "key expansion", |
| 1195 | handshake->randbytes, 64, keyblk, 256 ); |
| 1196 | if( ret != 0 ) |
| 1197 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 1199 | return( ret ); |
| 1200 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1202 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 1203 | mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) ); |
| 1204 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 ); |
| 1205 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 ); |
| 1206 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1207 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1208 | /* |
| 1209 | * Determine the appropriate key, IV and MAC length. |
| 1210 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1211 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1212 | keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1213 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1214 | #if defined(MBEDTLS_GCM_C) || \ |
| 1215 | defined(MBEDTLS_CCM_C) || \ |
| 1216 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1217 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1218 | cipher_info->mode == MBEDTLS_MODE_CCM || |
| 1219 | cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1220 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1221 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1222 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1223 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1224 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1225 | transform->taglen = |
| 1226 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1227 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1228 | /* All modes haves 96-bit IVs; |
| 1229 | * GCM and CCM has 4 implicit and 8 explicit bytes |
| 1230 | * ChachaPoly has all 12 bytes implicit |
| 1231 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1232 | transform->ivlen = 12; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1233 | if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1234 | transform->fixed_ivlen = 12; |
| 1235 | else |
| 1236 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1237 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1238 | /* Minimum length of encrypted record */ |
| 1239 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1240 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1241 | } |
| 1242 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1243 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 1244 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1245 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1246 | cipher_info->mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1247 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1248 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1249 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 1250 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1251 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1252 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1253 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1254 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1255 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1256 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1257 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 1258 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1260 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1261 | /* |
| 1262 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 1263 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 1264 | * so we only need to adjust the length here. |
| 1265 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1267 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1268 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1269 | |
| 1270 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT) |
| 1271 | /* Fall back to old, non-compliant version of the truncated |
Hanno Becker | 563423f | 2017-11-21 17:20:17 +0000 | [diff] [blame] | 1272 | * HMAC implementation which also truncates the key |
| 1273 | * (Mbed TLS versions from 1.3 to 2.6.0) */ |
Hanno Becker | e89353a | 2017-11-20 16:36:41 +0000 | [diff] [blame] | 1274 | mac_key_len = transform->maclen; |
| 1275 | #endif |
| 1276 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 1278 | |
| 1279 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1280 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1281 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1282 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1283 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1284 | transform->minlen = transform->maclen; |
| 1285 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1286 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1287 | /* |
| 1288 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1289 | * 1. if EtM is in use: one block plus MAC |
| 1290 | * otherwise: * first multiple of blocklen greater than maclen |
| 1291 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1292 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1293 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1294 | if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 1295 | { |
| 1296 | transform->minlen = transform->maclen |
| 1297 | + cipher_info->block_size; |
| 1298 | } |
| 1299 | else |
| 1300 | #endif |
| 1301 | { |
| 1302 | transform->minlen = transform->maclen |
| 1303 | + cipher_info->block_size |
| 1304 | - transform->maclen % cipher_info->block_size; |
| 1305 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1306 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1307 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
| 1308 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 1309 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1310 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1311 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1312 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1313 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1314 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 1315 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1316 | { |
| 1317 | transform->minlen += transform->ivlen; |
| 1318 | } |
| 1319 | else |
| 1320 | #endif |
| 1321 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1322 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1323 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1324 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 1325 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1326 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1327 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 1328 | else |
| 1329 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1330 | { |
| 1331 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1332 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1333 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1334 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1335 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 1336 | (unsigned) keylen, |
| 1337 | (unsigned) transform->minlen, |
| 1338 | (unsigned) transform->ivlen, |
| 1339 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1340 | |
| 1341 | /* |
| 1342 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 1343 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1344 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1345 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1346 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1347 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1348 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1349 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1350 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1351 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1352 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1353 | /* |
| 1354 | * This is not used in TLS v1.1. |
| 1355 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1356 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1357 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1358 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 1359 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1360 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1361 | } |
| 1362 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1363 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1364 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1365 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1366 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1367 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1368 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1369 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1370 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1371 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1372 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1373 | /* |
| 1374 | * This is not used in TLS v1.1. |
| 1375 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1376 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 1377 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1378 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 1379 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1380 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1381 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1382 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1383 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1384 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1385 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1386 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1387 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1388 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1389 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1390 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1391 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1392 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1393 | { |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1394 | if( mac_key_len > sizeof( transform->mac_enc ) ) |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1395 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1396 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1397 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1398 | goto end; |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 1399 | } |
| 1400 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1401 | memcpy( transform->mac_enc, mac_enc, mac_key_len ); |
| 1402 | memcpy( transform->mac_dec, mac_dec, mac_key_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1403 | } |
| 1404 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1405 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1406 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1407 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1408 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1409 | { |
Gilles Peskine | 039fd12 | 2018-03-19 19:06:08 +0100 | [diff] [blame] | 1410 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 1411 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 1412 | if( mac_key_len != 0 ) |
| 1413 | { |
| 1414 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 1415 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 1416 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1417 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1418 | else |
| 1419 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1420 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1421 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1422 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 1423 | goto end; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1424 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1425 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1427 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 1428 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1429 | { |
| 1430 | int ret = 0; |
| 1431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1432 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1433 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1434 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 1435 | transform->iv_enc, transform->iv_dec, |
| 1436 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1437 | mac_enc, mac_dec, |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 1438 | mac_key_len ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1440 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1441 | ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
| 1442 | goto end; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1443 | } |
| 1444 | } |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1445 | #else |
| 1446 | ((void) mac_dec); |
| 1447 | ((void) mac_enc); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1448 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1449 | |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1450 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 1451 | if( ssl->conf->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1452 | { |
Manuel Pégourié-Gonnard | 024b6df | 2015-10-19 13:52:53 +0200 | [diff] [blame] | 1453 | ssl->conf->f_export_keys( ssl->conf->p_export_keys, |
| 1454 | session->master, keyblk, |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 1455 | mac_key_len, keylen, |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1456 | iv_copy_len ); |
| 1457 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1458 | |
| 1459 | if( ssl->conf->f_export_keys_ext != NULL ) |
| 1460 | { |
| 1461 | ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys, |
| 1462 | session->master, keyblk, |
Ron Eldor | b7fd64c | 2019-05-12 11:03:32 +0300 | [diff] [blame] | 1463 | mac_key_len, keylen, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1464 | iv_copy_len, |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1465 | handshake->randbytes + 32, |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 1466 | handshake->randbytes, |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 1467 | tls_prf_get_type( handshake->tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1468 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1469 | #endif |
| 1470 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1471 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1472 | |
| 1473 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1474 | * That's relevant at least for TLS-1.0, where |
| 1475 | * we assume that mbedtls_cipher_crypt() updates |
| 1476 | * the structure field for the IV, which the PSA-based |
| 1477 | * implementation currently doesn't. */ |
| 1478 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1479 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1480 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1481 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1482 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1483 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1484 | { |
| 1485 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1486 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | if( ret == 0 ) |
| 1490 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1491 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1492 | psa_fallthrough = 0; |
| 1493 | } |
| 1494 | else |
| 1495 | { |
| 1496 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) ); |
| 1497 | psa_fallthrough = 1; |
| 1498 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1499 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1500 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1501 | psa_fallthrough = 1; |
| 1502 | #else |
| 1503 | psa_fallthrough = 1; |
| 1504 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1505 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1506 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1507 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1508 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1509 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1510 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1511 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1512 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1513 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1514 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1515 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1516 | /* Only use PSA-based ciphers for TLS-1.2. |
| 1517 | * That's relevant at least for TLS-1.0, where |
| 1518 | * we assume that mbedtls_cipher_crypt() updates |
| 1519 | * the structure field for the IV, which the PSA-based |
| 1520 | * implementation currently doesn't. */ |
| 1521 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1522 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1523 | { |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1524 | ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec, |
Hanno Becker | 22bf145 | 2019-04-05 11:21:08 +0100 | [diff] [blame] | 1525 | cipher_info, transform->taglen ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1526 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 1527 | { |
| 1528 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1529 | goto end; |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | if( ret == 0 ) |
| 1533 | { |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 1534 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) ); |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1535 | psa_fallthrough = 0; |
| 1536 | } |
| 1537 | else |
| 1538 | { |
| 1539 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) ); |
| 1540 | psa_fallthrough = 1; |
| 1541 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1542 | } |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1543 | else |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1544 | psa_fallthrough = 1; |
| 1545 | #else |
| 1546 | psa_fallthrough = 1; |
| 1547 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1548 | |
Hanno Becker | cb1cc80 | 2018-11-17 22:27:38 +0000 | [diff] [blame] | 1549 | if( psa_fallthrough == 1 ) |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1550 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1551 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1552 | cipher_info ) ) != 0 ) |
| 1553 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1554 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1555 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1556 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1558 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1559 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1560 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1561 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1562 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1563 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1564 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1566 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 1567 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1568 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1569 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1570 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1571 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1572 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1574 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1575 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1576 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1577 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1578 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1579 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1580 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1581 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1582 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1583 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1584 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1585 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1586 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1587 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1588 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1589 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1590 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1591 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1592 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1595 | // Initialize compression |
| 1596 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1597 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1598 | { |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1599 | if( ssl->compress_buf == NULL ) |
| 1600 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1601 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1602 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1603 | if( ssl->compress_buf == NULL ) |
| 1604 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 1605 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1606 | MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1607 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 1608 | goto end; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1612 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1613 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1614 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 1615 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1616 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1617 | if( deflateInit( &transform->ctx_deflate, |
| 1618 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1619 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1620 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1621 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1622 | ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
| 1623 | goto end; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1624 | } |
| 1625 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1626 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1628 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1629 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1630 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 1631 | mbedtls_platform_zeroize( handshake->randbytes, |
| 1632 | sizeof( handshake->randbytes ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1633 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1637 | void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1638 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1639 | mbedtls_md5_context md5; |
| 1640 | mbedtls_sha1_context sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1641 | unsigned char pad_1[48]; |
| 1642 | unsigned char pad_2[48]; |
| 1643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1644 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1645 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1646 | mbedtls_md5_init( &md5 ); |
| 1647 | mbedtls_sha1_init( &sha1 ); |
| 1648 | |
| 1649 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1650 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1651 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1652 | memset( pad_1, 0x36, 48 ); |
| 1653 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1654 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1655 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1656 | mbedtls_md5_update_ret( &md5, pad_1, 48 ); |
| 1657 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1658 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1659 | mbedtls_md5_starts_ret( &md5 ); |
| 1660 | mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 ); |
| 1661 | mbedtls_md5_update_ret( &md5, pad_2, 48 ); |
| 1662 | mbedtls_md5_update_ret( &md5, hash, 16 ); |
| 1663 | mbedtls_md5_finish_ret( &md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1664 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1665 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1666 | mbedtls_sha1_update_ret( &sha1, pad_1, 40 ); |
| 1667 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1668 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1669 | mbedtls_sha1_starts_ret( &sha1 ); |
| 1670 | mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 ); |
| 1671 | mbedtls_sha1_update_ret( &sha1, pad_2, 40 ); |
| 1672 | mbedtls_sha1_update_ret( &sha1, hash + 16, 20 ); |
| 1673 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1675 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); |
| 1676 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1677 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1678 | mbedtls_md5_free( &md5 ); |
| 1679 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1680 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1681 | return; |
| 1682 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1683 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1684 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1685 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 1686 | void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1687 | { |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1688 | mbedtls_md5_context md5; |
| 1689 | mbedtls_sha1_context sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1691 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1692 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1693 | mbedtls_md5_init( &md5 ); |
| 1694 | mbedtls_sha1_init( &sha1 ); |
| 1695 | |
| 1696 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 1697 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1698 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1699 | mbedtls_md5_finish_ret( &md5, hash ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1700 | mbedtls_sha1_finish_ret( &sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1702 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); |
| 1703 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1704 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1705 | mbedtls_md5_free( &md5 ); |
| 1706 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1707 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1708 | return; |
| 1709 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1710 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1711 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1712 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1713 | #if defined(MBEDTLS_SHA256_C) |
| 1714 | void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1715 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1716 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1717 | size_t hash_size; |
| 1718 | psa_status_t status; |
| 1719 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1720 | |
| 1721 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1722 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1723 | if( status != PSA_SUCCESS ) |
| 1724 | { |
| 1725 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1726 | return; |
| 1727 | } |
| 1728 | |
| 1729 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1730 | if( status != PSA_SUCCESS ) |
| 1731 | { |
| 1732 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1733 | return; |
| 1734 | } |
| 1735 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 ); |
| 1736 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1737 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1738 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1739 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1740 | mbedtls_sha256_init( &sha256 ); |
| 1741 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1742 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1743 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1744 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1745 | mbedtls_sha256_finish_ret( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1747 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 ); |
| 1748 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1749 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1750 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1751 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1752 | return; |
| 1753 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1754 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1756 | #if defined(MBEDTLS_SHA512_C) |
| 1757 | void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1758 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1759 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1760 | size_t hash_size; |
| 1761 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1762 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1763 | |
| 1764 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1765 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1766 | if( status != PSA_SUCCESS ) |
| 1767 | { |
| 1768 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1769 | return; |
| 1770 | } |
| 1771 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1772 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1773 | if( status != PSA_SUCCESS ) |
| 1774 | { |
| 1775 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1776 | return; |
| 1777 | } |
| 1778 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 ); |
| 1779 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1780 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1781 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1782 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1783 | mbedtls_sha512_init( &sha512 ); |
| 1784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1785 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1786 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1787 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 1788 | mbedtls_sha512_finish_ret( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1789 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1790 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 ); |
| 1791 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1792 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1793 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1794 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1795 | return; |
| 1796 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1797 | #endif /* MBEDTLS_SHA512_C */ |
| 1798 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1800 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1801 | 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] | 1802 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1803 | unsigned char *p = ssl->handshake->premaster; |
| 1804 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1805 | const unsigned char *psk = ssl->conf->psk; |
| 1806 | size_t psk_len = ssl->conf->psk_len; |
| 1807 | |
| 1808 | /* If the psk callback was called, use its result */ |
| 1809 | if( ssl->handshake->psk != NULL ) |
| 1810 | { |
| 1811 | psk = ssl->handshake->psk; |
| 1812 | psk_len = ssl->handshake->psk_len; |
| 1813 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1814 | |
| 1815 | /* |
| 1816 | * PMS = struct { |
| 1817 | * opaque other_secret<0..2^16-1>; |
| 1818 | * opaque psk<0..2^16-1>; |
| 1819 | * }; |
| 1820 | * with "other_secret" depending on the particular key exchange |
| 1821 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1823 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1824 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1825 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1826 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1827 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1828 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1829 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1830 | |
| 1831 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1832 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1833 | |
| 1834 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1835 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1836 | } |
| 1837 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1838 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1839 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1840 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1841 | { |
| 1842 | /* |
| 1843 | * other_secret already set by the ClientKeyExchange message, |
| 1844 | * and is 48 bytes long |
| 1845 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1846 | if( end - p < 2 ) |
| 1847 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1848 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1849 | *p++ = 0; |
| 1850 | *p++ = 48; |
| 1851 | p += 48; |
| 1852 | } |
| 1853 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1854 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1855 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1856 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1857 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1858 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1859 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1860 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1861 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1862 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1863 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1864 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1865 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1866 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1867 | return( ret ); |
| 1868 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1869 | *(p++) = (unsigned char)( len >> 8 ); |
| 1870 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1871 | p += len; |
| 1872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1873 | 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] | 1874 | } |
| 1875 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1876 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1877 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1878 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1879 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1880 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1881 | size_t zlen; |
| 1882 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1883 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1884 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1885 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1886 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1887 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1888 | return( ret ); |
| 1889 | } |
| 1890 | |
| 1891 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 1892 | *(p++) = (unsigned char)( zlen ); |
| 1893 | p += zlen; |
| 1894 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1895 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1896 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1897 | } |
| 1898 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1899 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1900 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1901 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1902 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1906 | if( end - p < 2 ) |
| 1907 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1908 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1909 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1910 | *(p++) = (unsigned char)( psk_len ); |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1911 | |
| 1912 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1913 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1914 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1915 | memcpy( p, psk, psk_len ); |
| 1916 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1917 | |
| 1918 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1919 | |
| 1920 | return( 0 ); |
| 1921 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1922 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1923 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1924 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1925 | /* |
| 1926 | * SSLv3.0 MAC functions |
| 1927 | */ |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1928 | #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] | 1929 | static void ssl_mac( mbedtls_md_context_t *md_ctx, |
| 1930 | const unsigned char *secret, |
| 1931 | const unsigned char *buf, size_t len, |
| 1932 | const unsigned char *ctr, int type, |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 1933 | unsigned char out[SSL_MAC_MAX_BYTES] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1934 | { |
| 1935 | unsigned char header[11]; |
| 1936 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1937 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1938 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 1939 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1940 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1941 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1942 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1943 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1944 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1945 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1946 | |
| 1947 | memcpy( header, ctr, 8 ); |
| 1948 | header[ 8] = (unsigned char) type; |
| 1949 | header[ 9] = (unsigned char)( len >> 8 ); |
| 1950 | header[10] = (unsigned char)( len ); |
| 1951 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1952 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1953 | mbedtls_md_starts( md_ctx ); |
| 1954 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1955 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 1956 | mbedtls_md_update( md_ctx, header, 11 ); |
| 1957 | mbedtls_md_update( md_ctx, buf, len ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1958 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1959 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1960 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1961 | mbedtls_md_starts( md_ctx ); |
| 1962 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1963 | mbedtls_md_update( md_ctx, padding, padlen ); |
Manuel Pégourié-Gonnard | 464147c | 2017-12-18 18:04:59 +0100 | [diff] [blame] | 1964 | mbedtls_md_update( md_ctx, out, md_size ); |
| 1965 | mbedtls_md_finish( md_ctx, out ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1966 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1967 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1968 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1969 | /* The function below is only used in the Lucky 13 counter-measure in |
Hanno Becker | b2ca87d | 2018-10-18 15:43:13 +0100 | [diff] [blame] | 1970 | * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1971 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \ |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1972 | ( defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 1973 | defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1974 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
| 1975 | /* This function makes sure every byte in the memory region is accessed |
| 1976 | * (in ascending addresses order) */ |
| 1977 | static void ssl_read_memory( unsigned char *p, size_t len ) |
| 1978 | { |
| 1979 | unsigned char acc = 0; |
| 1980 | volatile unsigned char force; |
| 1981 | |
| 1982 | for( ; len != 0; p++, len-- ) |
| 1983 | acc ^= *p; |
| 1984 | |
| 1985 | force = acc; |
| 1986 | (void) force; |
| 1987 | } |
| 1988 | #endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */ |
| 1989 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1990 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1991 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 1992 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1993 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1994 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d3f8c79 | 2019-05-20 15:06:12 +0100 | [diff] [blame] | 1995 | /* This functions transforms a DTLS plaintext fragment and a record content |
| 1996 | * type into an instance of the DTLSInnerPlaintext structure: |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1997 | * |
| 1998 | * struct { |
| 1999 | * opaque content[DTLSPlaintext.length]; |
| 2000 | * ContentType real_type; |
| 2001 | * uint8 zeros[length_of_padding]; |
| 2002 | * } DTLSInnerPlaintext; |
| 2003 | * |
| 2004 | * Input: |
| 2005 | * - `content`: The beginning of the buffer holding the |
| 2006 | * plaintext to be wrapped. |
| 2007 | * - `*content_size`: The length of the plaintext in Bytes. |
| 2008 | * - `max_len`: The number of Bytes available starting from |
| 2009 | * `content`. This must be `>= *content_size`. |
| 2010 | * - `rec_type`: The desired record content type. |
| 2011 | * |
| 2012 | * Output: |
| 2013 | * - `content`: The beginning of the resulting DTLSInnerPlaintext structure. |
| 2014 | * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure. |
| 2015 | * |
| 2016 | * Returns: |
| 2017 | * - `0` on success. |
| 2018 | * - A negative error code if `max_len` didn't offer enough space |
| 2019 | * for the expansion. |
| 2020 | */ |
| 2021 | static int ssl_cid_build_inner_plaintext( unsigned char *content, |
| 2022 | size_t *content_size, |
| 2023 | size_t remaining, |
| 2024 | uint8_t rec_type ) |
| 2025 | { |
| 2026 | size_t len = *content_size; |
Hanno Becker | b9ec44f | 2019-05-13 15:31:17 +0100 | [diff] [blame] | 2027 | size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY - |
| 2028 | ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) % |
| 2029 | MBEDTLS_SSL_CID_PADDING_GRANULARITY; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2030 | |
| 2031 | /* Write real content type */ |
| 2032 | if( remaining == 0 ) |
| 2033 | return( -1 ); |
| 2034 | content[ len ] = rec_type; |
| 2035 | len++; |
| 2036 | remaining--; |
| 2037 | |
| 2038 | if( remaining < pad ) |
| 2039 | return( -1 ); |
| 2040 | memset( content + len, 0, pad ); |
| 2041 | len += pad; |
| 2042 | remaining -= pad; |
| 2043 | |
| 2044 | *content_size = len; |
| 2045 | return( 0 ); |
| 2046 | } |
| 2047 | |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2048 | /* This function parses a DTLSInnerPlaintext structure. |
| 2049 | * See ssl_cid_build_inner_plaintext() for details. */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2050 | static int ssl_cid_parse_inner_plaintext( unsigned char const *content, |
| 2051 | size_t *content_size, |
| 2052 | uint8_t *rec_type ) |
| 2053 | { |
| 2054 | size_t remaining = *content_size; |
| 2055 | |
| 2056 | /* Determine length of padding by skipping zeroes from the back. */ |
| 2057 | do |
| 2058 | { |
| 2059 | if( remaining == 0 ) |
| 2060 | return( -1 ); |
| 2061 | remaining--; |
| 2062 | } while( content[ remaining ] == 0 ); |
| 2063 | |
| 2064 | *content_size = remaining; |
| 2065 | *rec_type = content[ remaining ]; |
| 2066 | |
| 2067 | return( 0 ); |
| 2068 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2069 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2070 | |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2071 | /* `add_data` must have size 13 Bytes if the CID extension is disabled, |
Hanno Becker | c4a190b | 2019-05-08 18:15:21 +0100 | [diff] [blame] | 2072 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2073 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2074 | size_t *add_data_len, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2075 | mbedtls_record *rec ) |
| 2076 | { |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2077 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2078 | * |
| 2079 | * additional_data = seq_num + TLSCompressed.type + |
| 2080 | * TLSCompressed.version + TLSCompressed.length; |
| 2081 | * |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2082 | * For the CID extension, this is extended as follows |
| 2083 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 2084 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2085 | * |
| 2086 | * additional_data = seq_num + DTLSPlaintext.type + |
| 2087 | * DTLSPlaintext.version + |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 2088 | * cid + |
| 2089 | * cid_length + |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2090 | * length_of_DTLSInnerPlaintext; |
| 2091 | */ |
| 2092 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2093 | memcpy( add_data, rec->ctr, sizeof( rec->ctr ) ); |
| 2094 | add_data[8] = rec->type; |
Hanno Becker | edb24f8 | 2019-05-20 15:01:46 +0100 | [diff] [blame] | 2095 | memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) ); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2096 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2097 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2098 | if( rec->cid_len != 0 ) |
| 2099 | { |
| 2100 | memcpy( add_data + 11, rec->cid, rec->cid_len ); |
| 2101 | add_data[11 + rec->cid_len + 0] = rec->cid_len; |
| 2102 | add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF; |
| 2103 | add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF; |
| 2104 | *add_data_len = 13 + 1 + rec->cid_len; |
| 2105 | } |
| 2106 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2107 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 2108 | { |
| 2109 | add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF; |
| 2110 | add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF; |
| 2111 | *add_data_len = 13; |
| 2112 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2115 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 2116 | mbedtls_ssl_transform *transform, |
| 2117 | mbedtls_record *rec, |
| 2118 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2119 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2121 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2122 | int auth_done = 0; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2123 | unsigned char * data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2124 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2125 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2126 | size_t post_avail; |
| 2127 | |
| 2128 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2129 | #if !defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2130 | ((void) ssl); |
| 2131 | #endif |
| 2132 | |
| 2133 | /* The PRNG is used for dynamic IV generation that's used |
| 2134 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
| 2135 | #if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 2136 | ( defined(MBEDTLS_AES_C) || \ |
| 2137 | defined(MBEDTLS_ARIA_C) || \ |
| 2138 | defined(MBEDTLS_CAMELLIA_C) ) && \ |
| 2139 | ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) ) |
| 2140 | ((void) f_rng); |
| 2141 | ((void) p_rng); |
| 2142 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2144 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2145 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2146 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2147 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2148 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 2149 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2150 | } |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2151 | if( rec == NULL |
| 2152 | || rec->buf == NULL |
| 2153 | || rec->buf_len < rec->data_offset |
| 2154 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2155 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2156 | || rec->cid_len != 0 |
| 2157 | #endif |
| 2158 | ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2159 | { |
| 2160 | 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] | 2161 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2162 | } |
| 2163 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2164 | data = rec->buf + rec->data_offset; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2165 | 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] | 2166 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2167 | data, rec->data_len ); |
| 2168 | |
| 2169 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 2170 | |
| 2171 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 2172 | { |
| 2173 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d", |
| 2174 | (unsigned) rec->data_len, |
| 2175 | MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 2176 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2177 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 2178 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2179 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2180 | /* |
| 2181 | * Add CID information |
| 2182 | */ |
| 2183 | rec->cid_len = transform->out_cid_len; |
| 2184 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 2185 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2186 | |
| 2187 | if( rec->cid_len != 0 ) |
| 2188 | { |
| 2189 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2190 | * Wrap plaintext into DTLSInnerPlaintext structure. |
| 2191 | * See ssl_cid_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2192 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 2193 | * Note that this changes `rec->data_len`, and hence |
| 2194 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2195 | */ |
| 2196 | if( ssl_cid_build_inner_plaintext( data, |
| 2197 | &rec->data_len, |
| 2198 | post_avail, |
| 2199 | rec->type ) != 0 ) |
| 2200 | { |
| 2201 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2202 | } |
| 2203 | |
| 2204 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 2205 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2206 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2207 | |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2208 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 2209 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2210 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2211 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2212 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2213 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2214 | if( mode == MBEDTLS_MODE_STREAM || |
| 2215 | ( mode == MBEDTLS_MODE_CBC |
| 2216 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2217 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2218 | #endif |
| 2219 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2220 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2221 | if( post_avail < transform->maclen ) |
| 2222 | { |
| 2223 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2224 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2225 | } |
| 2226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2228 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2229 | { |
Manuel Pégourié-Gonnard | b053efb | 2017-12-19 10:03:46 +0100 | [diff] [blame] | 2230 | unsigned char mac[SSL_MAC_MAX_BYTES]; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2231 | ssl_mac( &transform->md_ctx_enc, transform->mac_enc, |
| 2232 | data, rec->data_len, rec->ctr, rec->type, mac ); |
| 2233 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2234 | } |
| 2235 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2236 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2237 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2238 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2239 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2240 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2241 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2242 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2243 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2244 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2245 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2246 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2247 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2248 | data, rec->data_len ); |
| 2249 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2250 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
| 2251 | |
| 2252 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2253 | } |
| 2254 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2255 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2256 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2257 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2258 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2259 | } |
| 2260 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2261 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 2262 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2263 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2264 | rec->data_len += transform->maclen; |
| 2265 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2266 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2267 | } |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2268 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2269 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2270 | /* |
| 2271 | * Encrypt |
| 2272 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2273 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2274 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2275 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2276 | int ret; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2277 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2278 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2279 | "including %d bytes of padding", |
| 2280 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2281 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2282 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2283 | transform->iv_enc, transform->ivlen, |
| 2284 | data, rec->data_len, |
| 2285 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2286 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2287 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2288 | return( ret ); |
| 2289 | } |
| 2290 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2291 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2293 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2294 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2295 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2296 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2297 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2298 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2299 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2300 | #if defined(MBEDTLS_GCM_C) || \ |
| 2301 | defined(MBEDTLS_CCM_C) || \ |
| 2302 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2303 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2304 | mode == MBEDTLS_MODE_CCM || |
| 2305 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2306 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 2307 | int ret; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2308 | unsigned char iv[12]; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2309 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2310 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2311 | /* Check that there's space for both the authentication tag |
| 2312 | * and the explicit IV before and after the record content. */ |
| 2313 | if( post_avail < transform->taglen || |
| 2314 | rec->data_offset < explicit_iv_len ) |
| 2315 | { |
| 2316 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2317 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2318 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2319 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2320 | /* |
| 2321 | * Generate IV |
| 2322 | */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2323 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2324 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2325 | /* GCM and CCM: fixed || explicit (=seqnum) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2326 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2327 | memcpy( iv + transform->fixed_ivlen, rec->ctr, |
| 2328 | explicit_iv_len ); |
| 2329 | /* Prefix record content with explicit IV. */ |
| 2330 | memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2331 | } |
| 2332 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2333 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2334 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2335 | unsigned char i; |
| 2336 | |
| 2337 | memcpy( iv, transform->iv_enc, transform->fixed_ivlen ); |
| 2338 | |
| 2339 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2340 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2341 | } |
| 2342 | else |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2343 | { |
| 2344 | /* 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] | 2345 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2346 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 2347 | } |
| 2348 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2349 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2350 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2351 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
| 2352 | iv, transform->ivlen ); |
| 2353 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2354 | data - explicit_iv_len, explicit_iv_len ); |
| 2355 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2356 | add_data, add_data_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2357 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2358 | "including 0 bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2359 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2360 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2361 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2362 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2363 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2364 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2365 | if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2366 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2367 | add_data, add_data_len, /* add data */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2368 | data, rec->data_len, /* source */ |
| 2369 | data, &rec->data_len, /* destination */ |
| 2370 | data + rec->data_len, transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2371 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2372 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2373 | return( ret ); |
| 2374 | } |
| 2375 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2376 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
| 2377 | data + rec->data_len, transform->taglen ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2378 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2379 | rec->data_len += transform->taglen + explicit_iv_len; |
| 2380 | rec->data_offset -= explicit_iv_len; |
| 2381 | post_avail -= transform->taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2382 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2383 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2384 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2385 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2386 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2387 | ( 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] | 2388 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2389 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2390 | int ret; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2391 | size_t padlen, i; |
| 2392 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2393 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2394 | /* Currently we're always using minimal padding |
| 2395 | * (up to 255 bytes would be allowed). */ |
| 2396 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 2397 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2398 | padlen = 0; |
| 2399 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2400 | /* Check there's enough space in the buffer for the padding. */ |
| 2401 | if( post_avail < padlen + 1 ) |
| 2402 | { |
| 2403 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2404 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2405 | } |
| 2406 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2407 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2408 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2409 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2410 | rec->data_len += padlen + 1; |
| 2411 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2412 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2413 | #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] | 2414 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2415 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 2416 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2417 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2418 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2419 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2420 | if( f_rng == NULL ) |
| 2421 | { |
| 2422 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 2423 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2424 | } |
| 2425 | |
| 2426 | if( rec->data_offset < transform->ivlen ) |
| 2427 | { |
| 2428 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2429 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2430 | } |
| 2431 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2432 | /* |
| 2433 | * Generate IV |
| 2434 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2435 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2436 | if( ret != 0 ) |
| 2437 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2438 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2439 | memcpy( data - transform->ivlen, transform->iv_enc, |
| 2440 | transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2441 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2442 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2443 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2445 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2446 | "including %d bytes of IV and %d bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2447 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2448 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2449 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2450 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 2451 | transform->iv_enc, |
| 2452 | transform->ivlen, |
| 2453 | data, rec->data_len, |
| 2454 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2455 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2456 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2457 | return( ret ); |
| 2458 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2459 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2460 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2463 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2464 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2466 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2467 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2468 | { |
| 2469 | /* |
| 2470 | * Save IV in SSL3 and TLS1 |
| 2471 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2472 | memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 2473 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2474 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2475 | else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2476 | #endif |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2477 | { |
| 2478 | data -= transform->ivlen; |
| 2479 | rec->data_offset -= transform->ivlen; |
| 2480 | rec->data_len += transform->ivlen; |
| 2481 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2483 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2484 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2485 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 2486 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 2487 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2488 | /* |
| 2489 | * MAC(MAC_write_key, seq_num + |
| 2490 | * TLSCipherText.type + |
| 2491 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2492 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2493 | * IV + // except for TLS 1.0 |
| 2494 | * ENC(content + padding + padding_length)); |
| 2495 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2496 | |
| 2497 | if( post_avail < transform->maclen) |
| 2498 | { |
| 2499 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 2500 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2501 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2502 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2503 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 2504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2505 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2506 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2507 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2508 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2509 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2510 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2511 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 2512 | data, rec->data_len ); |
| 2513 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 2514 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2515 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2516 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2517 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2518 | rec->data_len += transform->maclen; |
| 2519 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2520 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2521 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2522 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2523 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2524 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2525 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2526 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2527 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2528 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2529 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2530 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2531 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2532 | /* Make extra sure authentication was performed, exactly once */ |
| 2533 | if( auth_done != 1 ) |
| 2534 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2535 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2536 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2537 | } |
| 2538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2539 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2540 | |
| 2541 | return( 0 ); |
| 2542 | } |
| 2543 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2544 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl, |
| 2545 | mbedtls_ssl_transform *transform, |
| 2546 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2547 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2548 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2549 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2550 | int ret, auth_done = 0; |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2551 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2552 | size_t padlen = 0, correct = 1; |
| 2553 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2554 | unsigned char* data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 2555 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2556 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2557 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2558 | #if !defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2559 | ((void) ssl); |
| 2560 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2562 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2563 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2564 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2565 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) ); |
| 2566 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2567 | } |
| 2568 | if( rec == NULL || |
| 2569 | rec->buf == NULL || |
| 2570 | rec->buf_len < rec->data_offset || |
| 2571 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 2572 | { |
| 2573 | 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] | 2574 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2575 | } |
| 2576 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2577 | data = rec->buf + rec->data_offset; |
| 2578 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2579 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2580 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2581 | /* |
| 2582 | * Match record's CID with incoming CID. |
| 2583 | */ |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2584 | if( rec->cid_len != transform->in_cid_len || |
| 2585 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 2586 | { |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 2587 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 2588 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2589 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2590 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2591 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 2592 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2593 | { |
| 2594 | padlen = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2595 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2596 | transform->iv_dec, |
| 2597 | transform->ivlen, |
| 2598 | data, rec->data_len, |
| 2599 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2600 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2601 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2602 | return( ret ); |
| 2603 | } |
| 2604 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2605 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2606 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2607 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2608 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 2609 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2610 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2611 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2612 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2613 | #if defined(MBEDTLS_GCM_C) || \ |
| 2614 | defined(MBEDTLS_CCM_C) || \ |
| 2615 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2616 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2617 | mode == MBEDTLS_MODE_CCM || |
| 2618 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2619 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2620 | unsigned char iv[12]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2621 | size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2622 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2623 | /* |
| 2624 | * Compute and update sizes |
| 2625 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2626 | if( rec->data_len < explicit_iv_len + transform->taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2628 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2629 | "+ taglen (%d)", rec->data_len, |
| 2630 | explicit_iv_len, transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2631 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 2632 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2633 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2634 | /* |
| 2635 | * Prepare IV |
| 2636 | */ |
| 2637 | if( transform->ivlen == 12 && transform->fixed_ivlen == 4 ) |
| 2638 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2639 | /* GCM and CCM: fixed || explicit (transmitted) */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2640 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2641 | memcpy( iv + transform->fixed_ivlen, data, 8 ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2642 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2643 | } |
| 2644 | else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 ) |
| 2645 | { |
Manuel Pégourié-Gonnard | 8744a02 | 2018-07-11 12:30:40 +0200 | [diff] [blame] | 2646 | /* ChachaPoly: fixed XOR sequence number */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2647 | unsigned char i; |
| 2648 | |
| 2649 | memcpy( iv, transform->iv_dec, transform->fixed_ivlen ); |
| 2650 | |
| 2651 | for( i = 0; i < 8; i++ ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2652 | iv[i+4] ^= rec->ctr[i]; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2653 | } |
| 2654 | else |
| 2655 | { |
| 2656 | /* Reminder if we ever add an AEAD mode with a different size */ |
| 2657 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2658 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2659 | } |
| 2660 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2661 | data += explicit_iv_len; |
| 2662 | rec->data_offset += explicit_iv_len; |
| 2663 | rec->data_len -= explicit_iv_len + transform->taglen; |
| 2664 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2665 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2666 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2667 | add_data, add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2668 | |
| 2669 | memcpy( transform->iv_dec + transform->fixed_ivlen, |
| 2670 | data - explicit_iv_len, explicit_iv_len ); |
| 2671 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 2672 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2673 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2674 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 2675 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2676 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2677 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2678 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2679 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2680 | if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec, |
| 2681 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2682 | add_data, add_data_len, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2683 | data, rec->data_len, |
| 2684 | data, &olen, |
| 2685 | data + rec->data_len, |
| 2686 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2688 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2690 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 2691 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 2692 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2693 | return( ret ); |
| 2694 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2695 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2696 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2697 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2698 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2699 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2700 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 2701 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2702 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2703 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2704 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 2705 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2706 | ( 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] | 2707 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2708 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2709 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2710 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2711 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2712 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2713 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2714 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2715 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 2716 | { |
| 2717 | /* The ciphertext is prefixed with the CBC IV. */ |
| 2718 | minlen += transform->ivlen; |
| 2719 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2720 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2721 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2722 | /* Size considerations: |
| 2723 | * |
| 2724 | * - The CBC cipher text must not be empty and hence |
| 2725 | * at least of size transform->ivlen. |
| 2726 | * |
| 2727 | * Together with the potential IV-prefix, this explains |
| 2728 | * the first of the two checks below. |
| 2729 | * |
| 2730 | * - The record must contain a MAC, either in plain or |
| 2731 | * encrypted, depending on whether Encrypt-then-MAC |
| 2732 | * is used or not. |
| 2733 | * - If it is, the message contains the IV-prefix, |
| 2734 | * the CBC ciphertext, and the MAC. |
| 2735 | * - If it is not, the padded plaintext, and hence |
| 2736 | * the CBC ciphertext, has at least length maclen + 1 |
| 2737 | * because there is at least the padding length byte. |
| 2738 | * |
| 2739 | * As the CBC ciphertext is not empty, both cases give the |
| 2740 | * lower bound minlen + maclen + 1 on the record size, which |
| 2741 | * we test for in the second check below. |
| 2742 | */ |
| 2743 | if( rec->data_len < minlen + transform->ivlen || |
| 2744 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2745 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2746 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2747 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 2748 | transform->ivlen, |
| 2749 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2750 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2751 | } |
| 2752 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2753 | /* |
| 2754 | * Authenticate before decrypt if enabled |
| 2755 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2756 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2757 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2758 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2759 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2761 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2762 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2763 | /* Safe due to the check data_len >= minlen + maclen + 1 above. */ |
| 2764 | rec->data_len -= transform->maclen; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2765 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2766 | 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] | 2767 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2768 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 2769 | add_data_len ); |
| 2770 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 2771 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2772 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 2773 | data, rec->data_len ); |
| 2774 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 2775 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 2776 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2777 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 2778 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2779 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2780 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2781 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2782 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 2783 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2785 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2786 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2787 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2788 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2789 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2790 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2791 | |
| 2792 | /* |
| 2793 | * Check length sanity |
| 2794 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2795 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2796 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2797 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2798 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2799 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2800 | } |
| 2801 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2802 | #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] | 2803 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2804 | * 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] | 2805 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2806 | if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2807 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2808 | /* This is safe because data_len >= minlen + maclen + 1 initially, |
| 2809 | * and at this point we have at most subtracted maclen (note that |
| 2810 | * minlen == transform->ivlen here). */ |
| 2811 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2812 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2813 | data += transform->ivlen; |
| 2814 | rec->data_offset += transform->ivlen; |
| 2815 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2816 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2817 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 2818 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2819 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 2820 | transform->iv_dec, transform->ivlen, |
| 2821 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 2822 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2823 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2824 | return( ret ); |
| 2825 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2826 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2827 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2828 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2829 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2830 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2831 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 2832 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2833 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2834 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2835 | { |
| 2836 | /* |
| 2837 | * Save IV in SSL3 and TLS1 |
| 2838 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2839 | memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 2840 | transform->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2841 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 2842 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2843 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2844 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 2845 | * subtracted at most minlen and maclen up to this point, |
| 2846 | * data_len > 0. */ |
| 2847 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2848 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2849 | if( auth_done == 1 ) |
| 2850 | { |
| 2851 | correct *= ( rec->data_len >= padlen + 1 ); |
| 2852 | padlen *= ( rec->data_len >= padlen + 1 ); |
| 2853 | } |
| 2854 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2855 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2856 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2857 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 2858 | { |
| 2859 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
| 2860 | rec->data_len, |
| 2861 | transform->maclen, |
| 2862 | padlen + 1 ) ); |
| 2863 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2864 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2865 | |
| 2866 | correct *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
| 2867 | padlen *= ( rec->data_len >= transform->maclen + padlen + 1 ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2868 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2869 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2870 | padlen++; |
| 2871 | |
| 2872 | /* Regardless of the validity of the padding, |
| 2873 | * we have data_len >= padlen here. */ |
| 2874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2875 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2876 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2877 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2878 | if( padlen > transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2879 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2880 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 2881 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2882 | "should be no more than %d", |
| 2883 | padlen, transform->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2884 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 2885 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2886 | } |
| 2887 | } |
| 2888 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2889 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2890 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2891 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2892 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2893 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2894 | /* The padding check involves a series of up to 256 |
| 2895 | * consecutive memory reads at the end of the record |
| 2896 | * plaintext buffer. In order to hide the length and |
| 2897 | * validity of the padding, always perform exactly |
| 2898 | * `min(256,plaintext_len)` reads (but take into account |
| 2899 | * only the last `padlen` bytes for the padding check). */ |
| 2900 | size_t pad_count = 0; |
| 2901 | size_t real_count = 0; |
| 2902 | volatile unsigned char* const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2903 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2904 | /* Index of first padding byte; it has been ensured above |
| 2905 | * that the subtraction is safe. */ |
| 2906 | size_t const padding_idx = rec->data_len - padlen; |
| 2907 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 2908 | size_t const start_idx = rec->data_len - num_checks; |
| 2909 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 2910 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2911 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2912 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2913 | real_count |= ( idx >= padding_idx ); |
| 2914 | pad_count += real_count * ( check[idx] == padlen - 1 ); |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 2915 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2916 | correct &= ( pad_count == padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2917 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2918 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 2919 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2920 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 2921 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2922 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2923 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 2924 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2925 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 2926 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2927 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2928 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2929 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 2930 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2931 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2932 | /* If the padding was found to be invalid, padlen == 0 |
| 2933 | * and the subtraction is safe. If the padding was found valid, |
| 2934 | * padlen hasn't been changed and the previous assertion |
| 2935 | * data_len >= padlen still holds. */ |
| 2936 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2937 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2938 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2939 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
Markku-Juhani O. Saarinen | c06e101 | 2017-12-07 11:51:13 +0000 | [diff] [blame] | 2940 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2941 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2942 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2943 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 2944 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2945 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2946 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2947 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2948 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 2949 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2950 | |
| 2951 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 2952 | * Authenticate if not done yet. |
| 2953 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2954 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 2955 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2956 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2957 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 2958 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2959 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2960 | /* If the initial value of padlen was such that |
| 2961 | * data_len < maclen + padlen + 1, then padlen |
| 2962 | * got reset to 1, and the initial check |
| 2963 | * data_len >= minlen + maclen + 1 |
| 2964 | * guarantees that at this point we still |
| 2965 | * have at least data_len >= maclen. |
| 2966 | * |
| 2967 | * If the initial value of padlen was such that |
| 2968 | * data_len >= maclen + padlen + 1, then we have |
| 2969 | * subtracted either padlen + 1 (if the padding was correct) |
| 2970 | * or 0 (if the padding was incorrect) since then, |
| 2971 | * hence data_len >= maclen in any case. |
| 2972 | */ |
| 2973 | rec->data_len -= transform->maclen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2974 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2975 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2976 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2977 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2978 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2979 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2980 | ssl_mac( &transform->md_ctx_dec, |
| 2981 | transform->mac_dec, |
| 2982 | data, rec->data_len, |
| 2983 | rec->ctr, rec->type, |
| 2984 | mac_expect ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2985 | } |
| 2986 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 2988 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 2989 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2990 | if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2991 | { |
| 2992 | /* |
| 2993 | * Process MAC and always update for padlen afterwards to make |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2994 | * total time independent of padlen. |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2995 | * |
| 2996 | * Known timing attacks: |
| 2997 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 2998 | * |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 2999 | * To compensate for different timings for the MAC calculation |
| 3000 | * depending on how much padding was removed (which is determined |
| 3001 | * by padlen), process extra_run more blocks through the hash |
| 3002 | * function. |
| 3003 | * |
| 3004 | * The formula in the paper is |
| 3005 | * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 ) |
| 3006 | * where L1 is the size of the header plus the decrypted message |
| 3007 | * plus CBC padding and L2 is the size of the header plus the |
| 3008 | * decrypted message. This is for an underlying hash function |
| 3009 | * with 64-byte blocks. |
| 3010 | * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values |
| 3011 | * correctly. We round down instead of up, so -56 is the correct |
| 3012 | * value for our calculations instead of -55. |
| 3013 | * |
Gilles Peskine | 1bd9d58 | 2018-06-04 11:58:44 +0200 | [diff] [blame] | 3014 | * Repeat the formula rather than defining a block_size variable. |
| 3015 | * This avoids requiring division by a variable at runtime |
| 3016 | * (which would be marginally less efficient and would require |
| 3017 | * linking an extra division function in some builds). |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3018 | */ |
| 3019 | size_t j, extra_run = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3020 | unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE]; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3021 | |
| 3022 | /* |
| 3023 | * The next two sizes are the minimum and maximum values of |
| 3024 | * in_msglen over all padlen values. |
| 3025 | * |
| 3026 | * They're independent of padlen, since we previously did |
| 3027 | * in_msglen -= padlen. |
| 3028 | * |
| 3029 | * Note that max_len + maclen is never more than the buffer |
| 3030 | * length, as we previously did in_msglen -= maclen too. |
| 3031 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3032 | const size_t max_len = rec->data_len + padlen; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3033 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 3034 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3035 | memset( tmp, 0, sizeof( tmp ) ); |
| 3036 | |
| 3037 | switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) ) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3038 | { |
Gilles Peskine | d0e55a4 | 2018-06-04 12:03:30 +0200 | [diff] [blame] | 3039 | #if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \ |
| 3040 | defined(MBEDTLS_SHA256_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3041 | case MBEDTLS_MD_MD5: |
| 3042 | case MBEDTLS_MD_SHA1: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3043 | case MBEDTLS_MD_SHA256: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3044 | /* 8 bytes of message size, 64-byte compression blocks */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3045 | extra_run = |
| 3046 | ( add_data_len + rec->data_len + padlen + 8 ) / 64 - |
| 3047 | ( add_data_len + rec->data_len + 8 ) / 64; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3048 | break; |
| 3049 | #endif |
Gilles Peskine | a7fe25d | 2018-06-04 12:01:18 +0200 | [diff] [blame] | 3050 | #if defined(MBEDTLS_SHA512_C) |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3051 | case MBEDTLS_MD_SHA384: |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3052 | /* 16 bytes of message size, 128-byte compression blocks */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3053 | extra_run = |
| 3054 | ( add_data_len + rec->data_len + padlen + 16 ) / 128 - |
| 3055 | ( add_data_len + rec->data_len + 16 ) / 128; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3056 | break; |
| 3057 | #endif |
| 3058 | default: |
Gilles Peskine | 5c38984 | 2018-06-04 12:02:43 +0200 | [diff] [blame] | 3059 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 3060 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3061 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3062 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3063 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3064 | |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 3065 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 3066 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3067 | mbedtls_md_hmac_update( &transform->md_ctx_dec, data, |
| 3068 | rec->data_len ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3069 | /* Make sure we access everything even when padlen > 0. This |
| 3070 | * makes the synchronisation requirements for just-in-time |
| 3071 | * Prime+Probe attacks much tighter and hopefully impractical. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3072 | ssl_read_memory( data + rec->data_len, padlen ); |
| 3073 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3074 | |
| 3075 | /* Call mbedtls_md_process at least once due to cache attacks |
| 3076 | * that observe whether md_process() was called of not */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 3077 | for( j = 0; j < extra_run + 1; j++ ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3078 | mbedtls_md_process( &transform->md_ctx_dec, tmp ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3079 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3080 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3081 | |
| 3082 | /* Make sure we access all the memory that could contain the MAC, |
| 3083 | * before we check it in the next code block. This makes the |
| 3084 | * synchronisation requirements for just-in-time Prime+Probe |
| 3085 | * attacks much tighter and hopefully impractical. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3086 | ssl_read_memory( data + min_len, |
| 3087 | max_len - min_len + transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3088 | } |
| 3089 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3090 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3091 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3092 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3093 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3094 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3095 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3096 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 3097 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3098 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
| 3099 | 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] | 3100 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3101 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3102 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 3103 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3104 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3105 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 3106 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 3107 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3108 | correct = 0; |
| 3109 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3110 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 3111 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 3112 | |
| 3113 | /* |
| 3114 | * Finally check the correct flag |
| 3115 | */ |
| 3116 | if( correct == 0 ) |
| 3117 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 3118 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3119 | |
| 3120 | /* Make extra sure authentication was performed, exactly once */ |
| 3121 | if( auth_done != 1 ) |
| 3122 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3123 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3124 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 3125 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3126 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3127 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3128 | if( rec->cid_len != 0 ) |
| 3129 | { |
| 3130 | ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len, |
| 3131 | &rec->type ); |
| 3132 | if( ret != 0 ) |
| 3133 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3134 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3135 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 3136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3137 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3138 | |
| 3139 | return( 0 ); |
| 3140 | } |
| 3141 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 3142 | #undef MAC_NONE |
| 3143 | #undef MAC_PLAINTEXT |
| 3144 | #undef MAC_CIPHERTEXT |
| 3145 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3146 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3147 | /* |
| 3148 | * Compression/decompression functions |
| 3149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3150 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3151 | { |
| 3152 | int ret; |
| 3153 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3154 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3155 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3156 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3157 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3158 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3159 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3160 | if( len_pre == 0 ) |
| 3161 | return( 0 ); |
| 3162 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3163 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 3164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3165 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3166 | ssl->out_msglen ) ); |
| 3167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3168 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3169 | ssl->out_msg, ssl->out_msglen ); |
| 3170 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3171 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 3172 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 3173 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3174 | 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] | 3175 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3176 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3177 | if( ret != Z_OK ) |
| 3178 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3179 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 3180 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3183 | ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 3184 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3185 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3186 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3187 | ssl->out_msglen ) ); |
| 3188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3189 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3190 | ssl->out_msg, ssl->out_msglen ); |
| 3191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3192 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3193 | |
| 3194 | return( 0 ); |
| 3195 | } |
| 3196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3197 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3198 | { |
| 3199 | int ret; |
| 3200 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3201 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3202 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 3203 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3205 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3206 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 3207 | if( len_pre == 0 ) |
| 3208 | return( 0 ); |
| 3209 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3210 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 3211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3212 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3213 | ssl->in_msglen ) ); |
| 3214 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3215 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3216 | ssl->in_msg, ssl->in_msglen ); |
| 3217 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3218 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 3219 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 3220 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3221 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3222 | header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3223 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3224 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3225 | if( ret != Z_OK ) |
| 3226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3227 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 3228 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3231 | ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 3232 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3234 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3235 | ssl->in_msglen ) ); |
| 3236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3237 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3238 | ssl->in_msg, ssl->in_msglen ); |
| 3239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3240 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3241 | |
| 3242 | return( 0 ); |
| 3243 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3244 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3245 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3246 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3247 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3248 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3249 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3250 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3251 | { |
| 3252 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 3253 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3254 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3255 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3256 | 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] | 3257 | unsigned char doublings = 1; |
| 3258 | |
| 3259 | while( ratio != 0 ) |
| 3260 | { |
| 3261 | ++doublings; |
| 3262 | ratio >>= 1; |
| 3263 | } |
| 3264 | |
| 3265 | if( ++ssl->renego_records_seen > doublings ) |
| 3266 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 3267 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3268 | return( 0 ); |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | return( ssl_write_hello_request( ssl ) ); |
| 3273 | } |
| 3274 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3275 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3276 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3277 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3278 | * Fill the input message buffer by appending data to it. |
| 3279 | * 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] | 3280 | * |
| 3281 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 3282 | * available (from this read and/or a previous one). Otherwise, an error code |
| 3283 | * is returned (possibly EOF or WANT_READ). |
| 3284 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3285 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 3286 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 3287 | * since we always read a whole datagram at once. |
| 3288 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3289 | * 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] | 3290 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3291 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3292 | 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] | 3293 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3294 | int ret; |
| 3295 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3298 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3299 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 3300 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3301 | 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] | 3302 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3303 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3304 | } |
| 3305 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3306 | 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] | 3307 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3308 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 3309 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3310 | } |
| 3311 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3312 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3313 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3314 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3315 | uint32_t timeout; |
| 3316 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3317 | /* Just to be sure */ |
| 3318 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) |
| 3319 | { |
| 3320 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3321 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3322 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3323 | } |
| 3324 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3325 | /* |
| 3326 | * The point is, we need to always read a full datagram at once, so we |
| 3327 | * sometimes read more then requested, and handle the additional data. |
| 3328 | * It could be the rest of the current record (while fetching the |
| 3329 | * header) and/or some other records in the same datagram. |
| 3330 | */ |
| 3331 | |
| 3332 | /* |
| 3333 | * Move to the next record in the already read datagram if applicable |
| 3334 | */ |
| 3335 | if( ssl->next_record_offset != 0 ) |
| 3336 | { |
| 3337 | if( ssl->in_left < ssl->next_record_offset ) |
| 3338 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3339 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3340 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | ssl->in_left -= ssl->next_record_offset; |
| 3344 | |
| 3345 | if( ssl->in_left != 0 ) |
| 3346 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3347 | 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] | 3348 | ssl->next_record_offset ) ); |
| 3349 | memmove( ssl->in_hdr, |
| 3350 | ssl->in_hdr + ssl->next_record_offset, |
| 3351 | ssl->in_left ); |
| 3352 | } |
| 3353 | |
| 3354 | ssl->next_record_offset = 0; |
| 3355 | } |
| 3356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3357 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3358 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3359 | |
| 3360 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3361 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3362 | */ |
| 3363 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3364 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3365 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3366 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3367 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3368 | |
| 3369 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 3370 | * A record can't be split across datagrams. If we need to read but |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3371 | * are not at the beginning of a new record, the caller did something |
| 3372 | * wrong. |
| 3373 | */ |
| 3374 | if( ssl->in_left != 0 ) |
| 3375 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3376 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3377 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3378 | } |
| 3379 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3380 | /* |
| 3381 | * Don't even try to read if time's out already. |
| 3382 | * This avoids by-passing the timer when repeatedly receiving messages |
| 3383 | * that will end up being dropped. |
| 3384 | */ |
| 3385 | if( ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3386 | { |
| 3387 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3388 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 3389 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3390 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3391 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3392 | 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] | 3393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3394 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3395 | timeout = ssl->handshake->retransmit_timeout; |
| 3396 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3397 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3399 | 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] | 3400 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3401 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3402 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 3403 | timeout ); |
| 3404 | else |
| 3405 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 3406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3407 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3408 | |
| 3409 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3410 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3411 | } |
| 3412 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3413 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3414 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3415 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3416 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3417 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3418 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3419 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3420 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 3421 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3422 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3423 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3424 | } |
| 3425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3426 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3427 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3428 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 3429 | return( ret ); |
| 3430 | } |
| 3431 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3432 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3433 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3434 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3435 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3436 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3437 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 3438 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3440 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3441 | return( ret ); |
| 3442 | } |
| 3443 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3444 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 3445 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3446 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3449 | if( ret < 0 ) |
| 3450 | return( ret ); |
| 3451 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3452 | ssl->in_left = ret; |
| 3453 | } |
| 3454 | else |
| 3455 | #endif |
| 3456 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3457 | 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] | 3458 | ssl->in_left, nb_want ) ); |
| 3459 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3460 | while( ssl->in_left < nb_want ) |
| 3461 | { |
| 3462 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3463 | |
| 3464 | if( ssl_check_timer( ssl ) != 0 ) |
| 3465 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 3466 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 3467 | { |
| 3468 | if( ssl->f_recv_timeout != NULL ) |
| 3469 | { |
| 3470 | ret = ssl->f_recv_timeout( ssl->p_bio, |
| 3471 | ssl->in_hdr + ssl->in_left, len, |
| 3472 | ssl->conf->read_timeout ); |
| 3473 | } |
| 3474 | else |
| 3475 | { |
| 3476 | ret = ssl->f_recv( ssl->p_bio, |
| 3477 | ssl->in_hdr + ssl->in_left, len ); |
| 3478 | } |
| 3479 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3481 | 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] | 3482 | ssl->in_left, nb_want ) ); |
| 3483 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3484 | |
| 3485 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3486 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3487 | |
| 3488 | if( ret < 0 ) |
| 3489 | return( ret ); |
| 3490 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3491 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3492 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3493 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3494 | ( "f_recv returned %d bytes but only %lu were requested", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3495 | ret, (unsigned long)len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 3496 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3497 | } |
| 3498 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 3499 | ssl->in_left += ret; |
| 3500 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3503 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3504 | |
| 3505 | return( 0 ); |
| 3506 | } |
| 3507 | |
| 3508 | /* |
| 3509 | * Flush any data not yet written |
| 3510 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3511 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3512 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 3513 | int ret; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3514 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3516 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3517 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3518 | if( ssl->f_send == NULL ) |
| 3519 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3520 | 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] | 3521 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3522 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3523 | } |
| 3524 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3525 | /* Avoid incrementing counter if data is flushed */ |
| 3526 | if( ssl->out_left == 0 ) |
| 3527 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3528 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3529 | return( 0 ); |
| 3530 | } |
| 3531 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3532 | while( ssl->out_left > 0 ) |
| 3533 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3534 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 3535 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3536 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3537 | buf = ssl->out_hdr - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 3538 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 3539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3540 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3541 | |
| 3542 | if( ret <= 0 ) |
| 3543 | return( ret ); |
| 3544 | |
mohammad1603 | 52aecb9 | 2018-03-28 23:41:40 -0700 | [diff] [blame] | 3545 | 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] | 3546 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3547 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 3548 | ( "f_send returned %d bytes but only %lu bytes were sent", |
mohammad1603 | 19d392b | 2018-04-02 07:25:26 -0700 | [diff] [blame] | 3549 | ret, (unsigned long)ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 3550 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3551 | } |
| 3552 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3553 | ssl->out_left -= ret; |
| 3554 | } |
| 3555 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3556 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3557 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3558 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3559 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3560 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3561 | else |
| 3562 | #endif |
| 3563 | { |
| 3564 | ssl->out_hdr = ssl->out_buf + 8; |
| 3565 | } |
| 3566 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3568 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3569 | |
| 3570 | return( 0 ); |
| 3571 | } |
| 3572 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3573 | /* |
| 3574 | * Functions to handle the DTLS retransmission state machine |
| 3575 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3576 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3577 | /* |
| 3578 | * Append current handshake message to current outgoing flight |
| 3579 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3580 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3581 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3582 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3583 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 3584 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 3585 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3586 | |
| 3587 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3588 | 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] | 3589 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3590 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3591 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3592 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3593 | } |
| 3594 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3595 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3596 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3597 | 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] | 3598 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3599 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | /* Copy current handshake message with headers */ |
| 3603 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 3604 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3605 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3606 | msg->next = NULL; |
| 3607 | |
| 3608 | /* Append to the current flight */ |
| 3609 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3610 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3611 | else |
| 3612 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3613 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3614 | while( cur->next != NULL ) |
| 3615 | cur = cur->next; |
| 3616 | cur->next = msg; |
| 3617 | } |
| 3618 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 3619 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3620 | return( 0 ); |
| 3621 | } |
| 3622 | |
| 3623 | /* |
| 3624 | * Free the current flight of handshake messages |
| 3625 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3626 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3628 | mbedtls_ssl_flight_item *cur = flight; |
| 3629 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3630 | |
| 3631 | while( cur != NULL ) |
| 3632 | { |
| 3633 | next = cur->next; |
| 3634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3635 | mbedtls_free( cur->p ); |
| 3636 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3637 | |
| 3638 | cur = next; |
| 3639 | } |
| 3640 | } |
| 3641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3642 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3643 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3644 | #endif |
| 3645 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3646 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3647 | * Swap transform_out and out_ctr with the alternative ones |
| 3648 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3649 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3651 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3652 | unsigned char tmp_out_ctr[8]; |
| 3653 | |
| 3654 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 3655 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3656 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3657 | return; |
| 3658 | } |
| 3659 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3660 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3661 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3662 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3663 | tmp_transform = ssl->transform_out; |
| 3664 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 3665 | ssl->handshake->alt_transform_out = tmp_transform; |
| 3666 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3667 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 3668 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 3669 | 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] | 3670 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3671 | |
| 3672 | /* Adjust to the newly activated transform */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 3673 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3675 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3676 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3677 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3678 | 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] | 3679 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3680 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 3681 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3682 | } |
| 3683 | } |
| 3684 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | /* |
| 3688 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3689 | */ |
| 3690 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 3691 | { |
| 3692 | int ret = 0; |
| 3693 | |
| 3694 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 3695 | |
| 3696 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 3697 | |
| 3698 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 3699 | |
| 3700 | return( ret ); |
| 3701 | } |
| 3702 | |
| 3703 | /* |
| 3704 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3705 | * |
| 3706 | * Need to remember the current message in case flush_output returns |
| 3707 | * 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] | 3708 | * 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] | 3709 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3710 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3711 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3712 | int ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3713 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3715 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3716 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3717 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3718 | |
| 3719 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3720 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3721 | ssl_swap_epochs( ssl ); |
| 3722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3723 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3724 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3725 | |
| 3726 | while( ssl->handshake->cur_msg != NULL ) |
| 3727 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3728 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3729 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3730 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3731 | int const is_finished = |
| 3732 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3733 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 3734 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3735 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 3736 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 3737 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 3738 | /* Swap epochs before sending Finished: we can't do it after |
| 3739 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 3740 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3741 | 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] | 3742 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3743 | 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] | 3744 | ssl_swap_epochs( ssl ); |
| 3745 | } |
| 3746 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3747 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 3748 | if( ret < 0 ) |
| 3749 | return( ret ); |
| 3750 | max_frag_len = (size_t) ret; |
| 3751 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3752 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 3753 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3754 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3755 | if( max_frag_len == 0 ) |
| 3756 | { |
| 3757 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3758 | return( ret ); |
| 3759 | |
| 3760 | continue; |
| 3761 | } |
| 3762 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3763 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3764 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3765 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3766 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3767 | /* Update position inside current message */ |
| 3768 | ssl->handshake->cur_msg_p += cur->len; |
| 3769 | } |
| 3770 | else |
| 3771 | { |
| 3772 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 3773 | const size_t hs_len = cur->len - 12; |
| 3774 | const size_t frag_off = p - ( cur->p + 12 ); |
| 3775 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3776 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3777 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3778 | 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] | 3779 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 3780 | if( is_finished ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3781 | ssl_swap_epochs( ssl ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3782 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3783 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3784 | return( ret ); |
| 3785 | |
| 3786 | continue; |
| 3787 | } |
| 3788 | max_hs_frag_len = max_frag_len - 12; |
| 3789 | |
| 3790 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 3791 | max_hs_frag_len : rem_len; |
| 3792 | |
| 3793 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3794 | { |
| 3795 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3796 | (unsigned) cur_hs_frag_len, |
| 3797 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 3798 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 3799 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3800 | /* Messages are stored with handshake headers as if not fragmented, |
| 3801 | * copy beginning of headers then fill fragmentation fields. |
| 3802 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 3803 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3804 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3805 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 3806 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 3807 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 3808 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3809 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 3810 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 3811 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3812 | |
| 3813 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 3814 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 3815 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3816 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 3817 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3818 | ssl->out_msgtype = cur->type; |
| 3819 | |
| 3820 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3821 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | /* If done with the current message move to the next one if any */ |
| 3825 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 3826 | { |
| 3827 | if( cur->next != NULL ) |
| 3828 | { |
| 3829 | ssl->handshake->cur_msg = cur->next; |
| 3830 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 3831 | } |
| 3832 | else |
| 3833 | { |
| 3834 | ssl->handshake->cur_msg = NULL; |
| 3835 | ssl->handshake->cur_msg_p = NULL; |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3840 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3841 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3842 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3843 | return( ret ); |
| 3844 | } |
| 3845 | } |
| 3846 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3847 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 3848 | return( ret ); |
| 3849 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 3850 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3851 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 3852 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 3853 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3854 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3855 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 3856 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 3857 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3858 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3859 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3860 | |
| 3861 | return( 0 ); |
| 3862 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3863 | |
| 3864 | /* |
| 3865 | * To be called when the last message of an incoming flight is received. |
| 3866 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3867 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3868 | { |
| 3869 | /* We won't need to resend that one any more */ |
| 3870 | ssl_flight_free( ssl->handshake->flight ); |
| 3871 | ssl->handshake->flight = NULL; |
| 3872 | ssl->handshake->cur_msg = NULL; |
| 3873 | |
| 3874 | /* The next incoming flight will start with this msg_seq */ |
| 3875 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 3876 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3877 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3878 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3879 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3880 | /* Clear future message buffering structure. */ |
| 3881 | ssl_buffering_free( ssl ); |
| 3882 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3883 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3884 | ssl_set_timer( ssl, 0 ); |
| 3885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3886 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3887 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3888 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3889 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3890 | } |
| 3891 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3892 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3893 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3894 | |
| 3895 | /* |
| 3896 | * To be called when the last message of an outgoing flight is send. |
| 3897 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3898 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3899 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 3900 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 3901 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3903 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3904 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3905 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3906 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3907 | } |
| 3908 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3909 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 3910 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3911 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3912 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3913 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3914 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3915 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3916 | |
| 3917 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3918 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3919 | * |
| 3920 | * - fill in handshake headers |
| 3921 | * - update handshake checksum |
| 3922 | * - DTLS: save message for resending |
| 3923 | * - then pass to the record layer |
| 3924 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 3925 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 3926 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3927 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3928 | * Inputs: |
| 3929 | * - ssl->out_msglen: 4 + actual handshake message len |
| 3930 | * (4 is the size of handshake headers for TLS) |
| 3931 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 3932 | * - ssl->out_msg + 4: the handshake message body |
| 3933 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 3934 | * 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] | 3935 | * - ssl->out_msglen: the length of the record contents |
| 3936 | * (including handshake headers but excluding record headers) |
| 3937 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3938 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3939 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3940 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3941 | int ret; |
| 3942 | const size_t hs_len = ssl->out_msglen - 4; |
| 3943 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3944 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3945 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 3946 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3947 | /* |
| 3948 | * Sanity checks |
| 3949 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3950 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3951 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 3952 | { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 3953 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 3954 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
| 3955 | if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 3956 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 3957 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ) |
| 3958 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 3959 | { |
| 3960 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3961 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3962 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3963 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3964 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3965 | /* Whenever we send anything different from a |
| 3966 | * HelloRequest we should be in a handshake - double check. */ |
| 3967 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3968 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3969 | ssl->handshake == NULL ) |
| 3970 | { |
| 3971 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3972 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3973 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3974 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3975 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3976 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3977 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3978 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3979 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3980 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3981 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3982 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 3983 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 3984 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 3985 | /* Double-check that we did not exceed the bounds |
| 3986 | * of the outgoing record buffer. |
| 3987 | * This should never fail as the various message |
| 3988 | * writing functions must obey the bounds of the |
| 3989 | * outgoing record buffer, but better be safe. |
| 3990 | * |
| 3991 | * Note: We deliberately do not check for the MTU or MFL here. |
| 3992 | */ |
| 3993 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 3994 | { |
| 3995 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
| 3996 | "size %u, maximum %u", |
| 3997 | (unsigned) ssl->out_msglen, |
| 3998 | (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 3999 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4000 | } |
| 4001 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4002 | /* |
| 4003 | * Fill handshake headers |
| 4004 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4005 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4006 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4007 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 4008 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 4009 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4010 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4011 | /* |
| 4012 | * DTLS has additional fields in the Handshake layer, |
| 4013 | * between the length field and the actual payload: |
| 4014 | * uint16 message_seq; |
| 4015 | * uint24 fragment_offset; |
| 4016 | * uint24 fragment_length; |
| 4017 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4018 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4019 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4020 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4021 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4022 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4023 | { |
| 4024 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
| 4025 | "size %u, maximum %u", |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4026 | (unsigned) ( hs_len ), |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4027 | (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 4028 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4029 | } |
| 4030 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4031 | 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] | 4032 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4033 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4034 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4035 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4036 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 4037 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 4038 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 4039 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 4040 | } |
| 4041 | else |
| 4042 | { |
| 4043 | ssl->out_msg[4] = 0; |
| 4044 | ssl->out_msg[5] = 0; |
| 4045 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 4046 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4047 | /* Handshake hashes are computed without fragmentation, |
| 4048 | * 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] | 4049 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 4050 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4051 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4052 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4053 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 4054 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 4055 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 4056 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4059 | /* 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] | 4060 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4061 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 4062 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4063 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4064 | { |
| 4065 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 4066 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4067 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4068 | return( ret ); |
| 4069 | } |
| 4070 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4071 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4072 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4073 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4074 | 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] | 4075 | { |
| 4076 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 4077 | return( ret ); |
| 4078 | } |
| 4079 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4080 | |
| 4081 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 4082 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 4083 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | /* |
| 4087 | * Record layer functions |
| 4088 | */ |
| 4089 | |
| 4090 | /* |
| 4091 | * Write current record. |
| 4092 | * |
| 4093 | * Uses: |
| 4094 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 4095 | * - ssl->out_msglen: length of the record content (excl headers) |
| 4096 | * - ssl->out_msg: record content |
| 4097 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4098 | 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] | 4099 | { |
| 4100 | int ret, done = 0; |
| 4101 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4102 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4103 | |
| 4104 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 4105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4106 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4107 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4108 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4109 | { |
| 4110 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 4111 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4112 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4113 | return( ret ); |
| 4114 | } |
| 4115 | |
| 4116 | len = ssl->out_msglen; |
| 4117 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4118 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4120 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4121 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4122 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4123 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4125 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 4126 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4127 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4128 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 4129 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4130 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 4131 | |
| 4132 | if( ret == 0 ) |
| 4133 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4134 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4135 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4136 | if( !done ) |
| 4137 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4138 | unsigned i; |
| 4139 | size_t protected_record_size; |
| 4140 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4141 | /* Skip writing the record content type to after the encryption, |
| 4142 | * as it may change when using the CID extension. */ |
| 4143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4144 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4145 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4146 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 4147 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 4148 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 4149 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4150 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4151 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4152 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4153 | mbedtls_record rec; |
| 4154 | |
| 4155 | rec.buf = ssl->out_iv; |
| 4156 | rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - |
| 4157 | ( ssl->out_iv - ssl->out_buf ); |
| 4158 | rec.data_len = ssl->out_msglen; |
| 4159 | rec.data_offset = ssl->out_msg - rec.buf; |
| 4160 | |
| 4161 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
| 4162 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 4163 | ssl->conf->transport, rec.ver ); |
| 4164 | rec.type = ssl->out_msgtype; |
| 4165 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4166 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 4167 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4168 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4169 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 4170 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 4171 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4172 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4173 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4174 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4175 | return( ret ); |
| 4176 | } |
| 4177 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4178 | if( rec.data_offset != 0 ) |
| 4179 | { |
| 4180 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4181 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4182 | } |
| 4183 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4184 | /* Update the record content type and CID. */ |
| 4185 | ssl->out_msgtype = rec.type; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4186 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4187 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4188 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 4189 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 4190 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4191 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4194 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4195 | |
| 4196 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4197 | /* In case of DTLS, double-check that we don't exceed |
| 4198 | * the remaining space in the datagram. */ |
| 4199 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4200 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 4201 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4202 | if( ret < 0 ) |
| 4203 | return( ret ); |
| 4204 | |
| 4205 | if( protected_record_size > (size_t) ret ) |
| 4206 | { |
| 4207 | /* Should never happen */ |
| 4208 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4209 | } |
| 4210 | } |
| 4211 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4212 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4213 | /* Now write the potentially updated record content type. */ |
| 4214 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 4215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4216 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4217 | "version = [%d:%d], msglen = %d", |
| 4218 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 4219 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 4220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4221 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 4222 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 4223 | |
| 4224 | ssl->out_left += protected_record_size; |
| 4225 | ssl->out_hdr += protected_record_size; |
| 4226 | ssl_update_out_pointers( ssl, ssl->transform_out ); |
| 4227 | |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 4228 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 4229 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 4230 | break; |
| 4231 | |
| 4232 | /* The loop goes to its end iff the counter is wrapping */ |
| 4233 | if( i == ssl_ep_len( ssl ) ) |
| 4234 | { |
| 4235 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 4236 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 4237 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4240 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 4241 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4242 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4243 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 4244 | size_t remaining; |
| 4245 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 4246 | if( ret < 0 ) |
| 4247 | { |
| 4248 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 4249 | ret ); |
| 4250 | return( ret ); |
| 4251 | } |
| 4252 | |
| 4253 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4254 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4255 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4256 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 4257 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4258 | else |
| 4259 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 4260 | 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] | 4261 | } |
| 4262 | } |
| 4263 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4264 | |
| 4265 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 4266 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4267 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4268 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4269 | return( ret ); |
| 4270 | } |
| 4271 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4272 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4273 | |
| 4274 | return( 0 ); |
| 4275 | } |
| 4276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4277 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4278 | |
| 4279 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 4280 | { |
| 4281 | if( ssl->in_msglen < ssl->in_hslen || |
| 4282 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 4283 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 4284 | { |
| 4285 | return( 1 ); |
| 4286 | } |
| 4287 | return( 0 ); |
| 4288 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4289 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4290 | 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] | 4291 | { |
| 4292 | return( ( ssl->in_msg[9] << 16 ) | |
| 4293 | ( ssl->in_msg[10] << 8 ) | |
| 4294 | ssl->in_msg[11] ); |
| 4295 | } |
| 4296 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4297 | 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] | 4298 | { |
| 4299 | return( ( ssl->in_msg[6] << 16 ) | |
| 4300 | ( ssl->in_msg[7] << 8 ) | |
| 4301 | ssl->in_msg[8] ); |
| 4302 | } |
| 4303 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4304 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4305 | { |
| 4306 | uint32_t msg_len, frag_off, frag_len; |
| 4307 | |
| 4308 | msg_len = ssl_get_hs_total_len( ssl ); |
| 4309 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4310 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4311 | |
| 4312 | if( frag_off > msg_len ) |
| 4313 | return( -1 ); |
| 4314 | |
| 4315 | if( frag_len > msg_len - frag_off ) |
| 4316 | return( -1 ); |
| 4317 | |
| 4318 | if( frag_len + 12 > ssl->in_msglen ) |
| 4319 | return( -1 ); |
| 4320 | |
| 4321 | return( 0 ); |
| 4322 | } |
| 4323 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4324 | /* |
| 4325 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 4326 | */ |
| 4327 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 4328 | { |
| 4329 | unsigned int start_bits, end_bits; |
| 4330 | |
| 4331 | start_bits = 8 - ( offset % 8 ); |
| 4332 | if( start_bits != 8 ) |
| 4333 | { |
| 4334 | size_t first_byte_idx = offset / 8; |
| 4335 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 4336 | /* Special case */ |
| 4337 | if( len <= start_bits ) |
| 4338 | { |
| 4339 | for( ; len != 0; len-- ) |
| 4340 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 4341 | |
| 4342 | /* Avoid potential issues with offset or len becoming invalid */ |
| 4343 | return; |
| 4344 | } |
| 4345 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4346 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 4347 | len -= start_bits; |
| 4348 | |
| 4349 | for( ; start_bits != 0; start_bits-- ) |
| 4350 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 4351 | } |
| 4352 | |
| 4353 | end_bits = len % 8; |
| 4354 | if( end_bits != 0 ) |
| 4355 | { |
| 4356 | size_t last_byte_idx = ( offset + len ) / 8; |
| 4357 | |
| 4358 | len -= end_bits; /* Now len % 8 == 0 */ |
| 4359 | |
| 4360 | for( ; end_bits != 0; end_bits-- ) |
| 4361 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 4362 | } |
| 4363 | |
| 4364 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 4365 | } |
| 4366 | |
| 4367 | /* |
| 4368 | * Check that bitmask is full |
| 4369 | */ |
| 4370 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 4371 | { |
| 4372 | size_t i; |
| 4373 | |
| 4374 | for( i = 0; i < len / 8; i++ ) |
| 4375 | if( mask[i] != 0xFF ) |
| 4376 | return( -1 ); |
| 4377 | |
| 4378 | for( i = 0; i < len % 8; i++ ) |
| 4379 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 4380 | return( -1 ); |
| 4381 | |
| 4382 | return( 0 ); |
| 4383 | } |
| 4384 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4385 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 4386 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4387 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4388 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4389 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4390 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4391 | alloc_len = 12; /* Handshake header */ |
| 4392 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4393 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 4394 | if( add_bitmap ) |
| 4395 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 4396 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4397 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4398 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 4399 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4400 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4401 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 4402 | 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] | 4403 | { |
| 4404 | return( ( ssl->in_msg[1] << 16 ) | |
| 4405 | ( ssl->in_msg[2] << 8 ) | |
| 4406 | ssl->in_msg[3] ); |
| 4407 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4408 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4409 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4410 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4411 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4412 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4413 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4414 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4415 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 4416 | } |
| 4417 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 4418 | 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] | 4419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4420 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4421 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 4422 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4424 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4425 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4426 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4427 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4428 | 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] | 4429 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 4430 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 4431 | { |
| 4432 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 4433 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4434 | } |
| 4435 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4436 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4437 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4438 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 4439 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4440 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4441 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 4442 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 4443 | { |
| 4444 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 4445 | recv_msg_seq, |
| 4446 | ssl->handshake->in_msg_seq ) ); |
| 4447 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4448 | } |
| 4449 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 4450 | /* Retransmit only on last message from previous flight, to avoid |
| 4451 | * too many retransmissions. |
| 4452 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 4453 | 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] | 4454 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4455 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4456 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4457 | "message_seq = %d, start_of_flight = %d", |
| 4458 | recv_msg_seq, |
| 4459 | ssl->handshake->in_flight_start_seq ) ); |
| 4460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4461 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4462 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4463 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4464 | return( ret ); |
| 4465 | } |
| 4466 | } |
| 4467 | else |
| 4468 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4469 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 4470 | "message_seq = %d, expected = %d", |
| 4471 | recv_msg_seq, |
| 4472 | ssl->handshake->in_msg_seq ) ); |
| 4473 | } |
| 4474 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4475 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4476 | } |
| 4477 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4478 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4479 | /* Message reassembly is handled alongside buffering of future |
| 4480 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 4481 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4482 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4483 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4484 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4485 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 4486 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4487 | } |
| 4488 | } |
| 4489 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4490 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 4491 | /* With TLS we don't handle fragmentation (for now) */ |
| 4492 | if( ssl->in_msglen < ssl->in_hslen ) |
| 4493 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4494 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 4495 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4496 | } |
| 4497 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4498 | return( 0 ); |
| 4499 | } |
| 4500 | |
| 4501 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 4502 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4503 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4504 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4505 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 4506 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4507 | 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] | 4508 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4509 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4510 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4511 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4512 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4513 | ssl->handshake != NULL ) |
| 4514 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4515 | unsigned offset; |
| 4516 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 4517 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4518 | /* Increment handshake sequence number */ |
| 4519 | hs->in_msg_seq++; |
| 4520 | |
| 4521 | /* |
| 4522 | * Clear up handshake buffering and reassembly structure. |
| 4523 | */ |
| 4524 | |
| 4525 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4526 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4527 | |
| 4528 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 4529 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 4530 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 4531 | offset++, hs_buf++ ) |
| 4532 | { |
| 4533 | *hs_buf = *(hs_buf + 1); |
| 4534 | } |
| 4535 | |
| 4536 | /* Create a fresh last entry */ |
| 4537 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 4538 | } |
| 4539 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4540 | } |
| 4541 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4542 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4543 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 4544 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4545 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 4546 | * Bit n is set iff record number in_window_top - n has been seen. |
| 4547 | * |
| 4548 | * Usually, in_window_top is the last record number seen and the lsb of |
| 4549 | * in_window is set. The only exception is the initial state (record number 0 |
| 4550 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4551 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4552 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4553 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4554 | { |
| 4555 | ssl->in_window_top = 0; |
| 4556 | ssl->in_window = 0; |
| 4557 | } |
| 4558 | |
| 4559 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 4560 | { |
| 4561 | return( ( (uint64_t) buf[0] << 40 ) | |
| 4562 | ( (uint64_t) buf[1] << 32 ) | |
| 4563 | ( (uint64_t) buf[2] << 24 ) | |
| 4564 | ( (uint64_t) buf[3] << 16 ) | |
| 4565 | ( (uint64_t) buf[4] << 8 ) | |
| 4566 | ( (uint64_t) buf[5] ) ); |
| 4567 | } |
| 4568 | |
| 4569 | /* |
| 4570 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 4571 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4572 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4573 | { |
| 4574 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4575 | uint64_t bit; |
| 4576 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4577 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4578 | return( 0 ); |
| 4579 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4580 | if( rec_seqnum > ssl->in_window_top ) |
| 4581 | return( 0 ); |
| 4582 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4583 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4584 | |
| 4585 | if( bit >= 64 ) |
| 4586 | return( -1 ); |
| 4587 | |
| 4588 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 4589 | return( -1 ); |
| 4590 | |
| 4591 | return( 0 ); |
| 4592 | } |
| 4593 | |
| 4594 | /* |
| 4595 | * Update replay window on new validated record |
| 4596 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4597 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4598 | { |
| 4599 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 4600 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4601 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 4602 | return; |
| 4603 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4604 | if( rec_seqnum > ssl->in_window_top ) |
| 4605 | { |
| 4606 | /* Update window_top and the contents of the window */ |
| 4607 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 4608 | |
| 4609 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4610 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4611 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4612 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4613 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4614 | ssl->in_window |= 1; |
| 4615 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4616 | |
| 4617 | ssl->in_window_top = rec_seqnum; |
| 4618 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4619 | else |
| 4620 | { |
| 4621 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4622 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4623 | |
| 4624 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 4625 | ssl->in_window |= (uint64_t) 1 << bit; |
| 4626 | } |
| 4627 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4628 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4629 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4630 | #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] | 4631 | /* Forward declaration */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 4632 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ); |
| 4633 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4634 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4635 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 4636 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 4637 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4638 | * |
| 4639 | * - if cookie is valid, return 0 |
| 4640 | * - if ClientHello looks superficially valid but cookie is not, |
| 4641 | * fill obuf and set olen, then |
| 4642 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 4643 | * - otherwise return a specific error code |
| 4644 | */ |
| 4645 | static int ssl_check_dtls_clihlo_cookie( |
| 4646 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 4647 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 4648 | void *p_cookie, |
| 4649 | const unsigned char *cli_id, size_t cli_id_len, |
| 4650 | const unsigned char *in, size_t in_len, |
| 4651 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 4652 | { |
| 4653 | size_t sid_len, cookie_len; |
| 4654 | unsigned char *p; |
| 4655 | |
| 4656 | if( f_cookie_write == NULL || f_cookie_check == NULL ) |
| 4657 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4658 | |
| 4659 | /* |
| 4660 | * Structure of ClientHello with record and handshake headers, |
| 4661 | * and expected values. We don't need to check a lot, more checks will be |
| 4662 | * done when actually parsing the ClientHello - skipping those checks |
| 4663 | * avoids code duplication and does not make cookie forging any easier. |
| 4664 | * |
| 4665 | * 0-0 ContentType type; copied, must be handshake |
| 4666 | * 1-2 ProtocolVersion version; copied |
| 4667 | * 3-4 uint16 epoch; copied, must be 0 |
| 4668 | * 5-10 uint48 sequence_number; copied |
| 4669 | * 11-12 uint16 length; (ignored) |
| 4670 | * |
| 4671 | * 13-13 HandshakeType msg_type; (ignored) |
| 4672 | * 14-16 uint24 length; (ignored) |
| 4673 | * 17-18 uint16 message_seq; copied |
| 4674 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 4675 | * 22-24 uint24 fragment_length; (ignored) |
| 4676 | * |
| 4677 | * 25-26 ProtocolVersion client_version; (ignored) |
| 4678 | * 27-58 Random random; (ignored) |
| 4679 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 4680 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 4681 | * ... |
| 4682 | * |
| 4683 | * Minimum length is 61 bytes. |
| 4684 | */ |
| 4685 | if( in_len < 61 || |
| 4686 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 4687 | in[3] != 0 || in[4] != 0 || |
| 4688 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 4689 | { |
| 4690 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4691 | } |
| 4692 | |
| 4693 | sid_len = in[59]; |
| 4694 | if( sid_len > in_len - 61 ) |
| 4695 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4696 | |
| 4697 | cookie_len = in[60 + sid_len]; |
| 4698 | if( cookie_len > in_len - 60 ) |
| 4699 | return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO ); |
| 4700 | |
| 4701 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 4702 | cli_id, cli_id_len ) == 0 ) |
| 4703 | { |
| 4704 | /* Valid cookie */ |
| 4705 | return( 0 ); |
| 4706 | } |
| 4707 | |
| 4708 | /* |
| 4709 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 4710 | * |
| 4711 | * 0-0 ContentType type; copied |
| 4712 | * 1-2 ProtocolVersion version; copied |
| 4713 | * 3-4 uint16 epoch; copied |
| 4714 | * 5-10 uint48 sequence_number; copied |
| 4715 | * 11-12 uint16 length; olen - 13 |
| 4716 | * |
| 4717 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 4718 | * 14-16 uint24 length; olen - 25 |
| 4719 | * 17-18 uint16 message_seq; copied |
| 4720 | * 19-21 uint24 fragment_offset; copied |
| 4721 | * 22-24 uint24 fragment_length; olen - 25 |
| 4722 | * |
| 4723 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 4724 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 4725 | * |
| 4726 | * Minimum length is 28. |
| 4727 | */ |
| 4728 | if( buf_len < 28 ) |
| 4729 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4730 | |
| 4731 | /* Copy most fields and adapt others */ |
| 4732 | memcpy( obuf, in, 25 ); |
| 4733 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 4734 | obuf[25] = 0xfe; |
| 4735 | obuf[26] = 0xff; |
| 4736 | |
| 4737 | /* Generate and write actual cookie */ |
| 4738 | p = obuf + 28; |
| 4739 | if( f_cookie_write( p_cookie, |
| 4740 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 4741 | { |
| 4742 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4743 | } |
| 4744 | |
| 4745 | *olen = p - obuf; |
| 4746 | |
| 4747 | /* Go back and fill length fields */ |
| 4748 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 4749 | |
| 4750 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 4751 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 4752 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 4753 | |
| 4754 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 4755 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 4756 | |
| 4757 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 4758 | } |
| 4759 | |
| 4760 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4761 | * Handle possible client reconnect with the same UDP quadruplet |
| 4762 | * (RFC 6347 Section 4.2.8). |
| 4763 | * |
| 4764 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 4765 | * that looks like a ClientHello. |
| 4766 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4767 | * - if the input looks like a ClientHello without cookies, |
| 4768 | * send back HelloVerifyRequest, then |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4769 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4770 | * - if the input looks like a ClientHello with a valid cookie, |
| 4771 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 4772 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4773 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4774 | * |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4775 | * mbedtls_ssl_read_record() will ignore the record if anything else than |
Simon Butcher | d0bf6a3 | 2015-09-11 17:34:49 +0100 | [diff] [blame] | 4776 | * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function |
| 4777 | * cannot not return 0. |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4778 | */ |
| 4779 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 4780 | { |
| 4781 | int ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4782 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4783 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4784 | ret = ssl_check_dtls_clihlo_cookie( |
| 4785 | ssl->conf->f_cookie_write, |
| 4786 | ssl->conf->f_cookie_check, |
| 4787 | ssl->conf->p_cookie, |
| 4788 | ssl->cli_id, ssl->cli_id_len, |
| 4789 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4790 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4791 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4792 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 4793 | |
| 4794 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4795 | { |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4796 | /* 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] | 4797 | * If the error is permanent we'll catch it later, |
| 4798 | * if it's not, then hopefully it'll work next time. */ |
| 4799 | (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len ); |
| 4800 | |
| 4801 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4802 | } |
| 4803 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4804 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4805 | { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 4806 | /* Got a valid cookie, partially reset context */ |
| 4807 | if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
| 4808 | { |
| 4809 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 4810 | return( ret ); |
| 4811 | } |
| 4812 | |
| 4813 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4814 | } |
| 4815 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 4816 | return( ret ); |
| 4817 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 4818 | #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] | 4819 | |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4820 | static int ssl_check_record_type( uint8_t record_type ) |
| 4821 | { |
| 4822 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4823 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 4824 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 4825 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 4826 | { |
| 4827 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4828 | } |
| 4829 | |
| 4830 | return( 0 ); |
| 4831 | } |
| 4832 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 4833 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4834 | * ContentType type; |
| 4835 | * ProtocolVersion version; |
| 4836 | * uint16 epoch; // DTLS only |
| 4837 | * uint48 sequence_number; // DTLS only |
| 4838 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4839 | * |
| 4840 | * 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] | 4841 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4842 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 4843 | * |
| 4844 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 4845 | * 1. proceed with the record if this function returns 0 |
| 4846 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 4847 | * 3. return CLIENT_RECONNECT if this function return that value |
| 4848 | * 4. drop the whole datagram if this function returns anything else. |
| 4849 | * Point 2 is needed when the peer is resending, and we have already received |
| 4850 | * 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] | 4851 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4852 | static int ssl_parse_record_header( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4853 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4854 | int major_ver, minor_ver; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4855 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4856 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4857 | /* Parse and validate record content type and version */ |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 4858 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4859 | ssl->in_msgtype = ssl->in_hdr[0]; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4860 | 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] | 4861 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4862 | /* Check record type */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4863 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4864 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4865 | ssl->in_msgtype == MBEDTLS_SSL_MSG_CID && |
| 4866 | ssl->conf->cid_len != 0 ) |
| 4867 | { |
| 4868 | /* Shift pointers to account for record header including CID |
| 4869 | * struct { |
| 4870 | * ContentType special_type = tls12_cid; |
| 4871 | * ProtocolVersion version; |
| 4872 | * uint16 epoch; |
| 4873 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 4874 | * opaque cid[cid_length]; // Additional field compared to |
| 4875 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4876 | * uint16 length; |
| 4877 | * opaque enc_content[DTLSCiphertext.length]; |
| 4878 | * } DTLSCiphertext; |
| 4879 | */ |
| 4880 | |
| 4881 | /* So far, we only support static CID lengths |
| 4882 | * fixed in the configuration. */ |
| 4883 | ssl->in_len = ssl->in_cid + ssl->conf->cid_len; |
| 4884 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 4885 | } |
| 4886 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4887 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 4888 | if( ssl_check_record_type( ssl->in_msgtype ) ) |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4889 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4890 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4891 | |
| 4892 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Andres Amaya Garcia | 0169253 | 2017-06-28 09:26:46 +0100 | [diff] [blame] | 4893 | /* Silently ignore invalid DTLS records as recommended by RFC 6347 |
| 4894 | * Section 4.1.2.7 */ |
Andres Amaya Garcia | 2fad94b | 2017-06-26 15:11:59 +0100 | [diff] [blame] | 4895 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4896 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4897 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4898 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 4899 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4900 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | /* Check version */ |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 4904 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4905 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4906 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 4907 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4910 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4911 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4912 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 4913 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4916 | /* Now that the total length of the record header is known, ensure |
| 4917 | * that the current datagram is large enough to hold it. |
| 4918 | * This would fail, for example, if we received a datagram of |
| 4919 | * size 13 + n Bytes where n is less than the size of incoming CIDs. */ |
| 4920 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 4921 | if( ret != 0 ) |
| 4922 | { |
| 4923 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 4924 | return( ret ); |
| 4925 | } |
| 4926 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 4927 | |
| 4928 | /* Parse and validate record length |
| 4929 | * This must happen after the CID parsing because |
| 4930 | * its position in the record header depends on |
| 4931 | * the presence of a CID. */ |
| 4932 | |
| 4933 | ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4934 | if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 4935 | - (size_t)( ssl->in_msg - ssl->in_buf ) ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4936 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4937 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 4938 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 4939 | } |
| 4940 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4941 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Hanno Becker | 92d30f5 | 2019-05-23 17:03:44 +0100 | [diff] [blame] | 4942 | "version = [%d:%d], msglen = %d", |
| 4943 | ssl->in_msgtype, |
| 4944 | major_ver, minor_ver, ssl->in_msglen ) ); |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4945 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4946 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 4947 | * DTLS-related tests. |
| 4948 | * Check epoch before checking length constraint because |
| 4949 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 4950 | * message gets duplicated before the corresponding Finished message, |
| 4951 | * the second ChangeCipherSpec should be discarded because it belongs |
| 4952 | * to an old epoch, but not because its length is shorter than |
| 4953 | * the minimum record length for packets using the new record transform. |
| 4954 | * Note that these two kinds of failures are handled differently, |
| 4955 | * as an unexpected record is silently skipped but an invalid |
| 4956 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4957 | */ |
| 4958 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4959 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4960 | { |
| 4961 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 4962 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4963 | /* Check epoch (and sequence number) with DTLS */ |
| 4964 | if( rec_epoch != ssl->in_epoch ) |
| 4965 | { |
| 4966 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
| 4967 | "expected %d, received %d", |
| 4968 | ssl->in_epoch, rec_epoch ) ); |
| 4969 | |
| 4970 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 4971 | /* |
| 4972 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 4973 | * access the first byte of record content (handshake type), as we |
| 4974 | * have an active transform (possibly iv_len != 0), so use the |
| 4975 | * fact that the record header len is 13 instead. |
| 4976 | */ |
| 4977 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 4978 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 4979 | rec_epoch == 0 && |
| 4980 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4981 | ssl->in_left > 13 && |
| 4982 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 4983 | { |
| 4984 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 4985 | "from the same port" ) ); |
| 4986 | return( ssl_handle_possible_reconnect( ssl ) ); |
| 4987 | } |
| 4988 | else |
| 4989 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4990 | { |
| 4991 | /* Consider buffering the record. */ |
| 4992 | if( rec_epoch == (unsigned int) ssl->in_epoch + 1 ) |
| 4993 | { |
| 4994 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 4995 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4996 | } |
| 4997 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4998 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4999 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5003 | /* Replay detection only works for the current epoch */ |
| 5004 | if( rec_epoch == ssl->in_epoch && |
| 5005 | mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
| 5006 | { |
| 5007 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 5008 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 5009 | } |
| 5010 | #endif |
| 5011 | } |
| 5012 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5013 | |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5014 | |
| 5015 | /* Check length against bounds of the current transform and version */ |
| 5016 | if( ssl->transform_in == NULL ) |
| 5017 | { |
| 5018 | if( ssl->in_msglen < 1 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5019 | ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5020 | { |
| 5021 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5022 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5023 | } |
| 5024 | } |
| 5025 | else |
| 5026 | { |
| 5027 | if( ssl->in_msglen < ssl->transform_in->minlen ) |
| 5028 | { |
| 5029 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5030 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5031 | } |
| 5032 | |
| 5033 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 5034 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5035 | ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5036 | { |
| 5037 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5038 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5039 | } |
| 5040 | #endif |
| 5041 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 5042 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5043 | /* |
| 5044 | * TLS encrypted messages can have up to 256 bytes of padding |
| 5045 | */ |
| 5046 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 && |
| 5047 | ssl->in_msglen > ssl->transform_in->minlen + |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5048 | MBEDTLS_SSL_IN_CONTENT_LEN + 256 ) |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 5049 | { |
| 5050 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5051 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5052 | } |
| 5053 | #endif |
| 5054 | } |
| 5055 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5056 | return( 0 ); |
| 5057 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5058 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5059 | /* |
| 5060 | * If applicable, decrypt (and decompress) record content |
| 5061 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5062 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5063 | { |
| 5064 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5065 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5066 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5067 | ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5068 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5069 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 5070 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5071 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5072 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5073 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5074 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 5075 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5076 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5077 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 5078 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5079 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 5080 | |
| 5081 | if( ret == 0 ) |
| 5082 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5083 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5084 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5085 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5086 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5087 | mbedtls_record rec; |
| 5088 | |
| 5089 | rec.buf = ssl->in_iv; |
| 5090 | rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN |
| 5091 | - ( ssl->in_iv - ssl->in_buf ); |
| 5092 | rec.data_len = ssl->in_msglen; |
| 5093 | rec.data_offset = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5094 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | 2cdc5c3 | 2019-05-09 15:54:28 +0100 | [diff] [blame] | 5095 | rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid ); |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 5096 | memcpy( rec.cid, ssl->in_cid, rec.cid_len ); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5097 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5098 | |
| 5099 | memcpy( &rec.ctr[0], ssl->in_ctr, 8 ); |
| 5100 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 5101 | ssl->conf->transport, rec.ver ); |
| 5102 | rec.type = ssl->in_msgtype; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 5103 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
| 5104 | &rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5105 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5106 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5107 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5108 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5109 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
| 5110 | ssl->conf->ignore_unexpected_cid |
| 5111 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 5112 | { |
Hanno Becker | e8d6afd | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 5113 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5114 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 5115 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5116 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 5117 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5118 | return( ret ); |
| 5119 | } |
| 5120 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5121 | if( ssl->in_msgtype != rec.type ) |
| 5122 | { |
| 5123 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
| 5124 | ssl->in_msgtype, rec.type ) ); |
| 5125 | } |
| 5126 | |
| 5127 | /* The record content type may change during decryption, |
| 5128 | * so re-read it. */ |
| 5129 | ssl->in_msgtype = rec.type; |
| 5130 | /* Also update the input buffer, because unfortunately |
| 5131 | * the server-side ssl_parse_client_hello() reparses the |
| 5132 | * record header when receiving a ClientHello initiating |
| 5133 | * a renegotiation. */ |
| 5134 | ssl->in_hdr[0] = rec.type; |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 5135 | ssl->in_msg = rec.buf + rec.data_offset; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5136 | ssl->in_msglen = rec.data_len; |
| 5137 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 5138 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 5139 | |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 5140 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
| 5141 | ssl->in_msg, ssl->in_msglen ); |
| 5142 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5143 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5144 | /* We have already checked the record content type |
| 5145 | * in ssl_parse_record_header(), failing or silently |
| 5146 | * dropping the record in the case of an unknown type. |
| 5147 | * |
| 5148 | * Since with the use of CIDs, the record content type |
| 5149 | * might change during decryption, re-check the record |
| 5150 | * content type, but treat a failure as fatal this time. */ |
| 5151 | if( ssl_check_record_type( ssl->in_msgtype ) ) |
| 5152 | { |
| 5153 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 5154 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5155 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5156 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 5157 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 5158 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5159 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5160 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 5161 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5162 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5163 | else if( ssl->in_msglen == 0 ) |
| 5164 | { |
| 5165 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5166 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
| 5167 | && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 5168 | { |
| 5169 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 5170 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 5171 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 5172 | } |
| 5173 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5174 | |
| 5175 | ssl->nb_zero++; |
| 5176 | |
| 5177 | /* |
| 5178 | * Three or more empty messages may be a DoS attack |
| 5179 | * (excessive CPU consumption). |
| 5180 | */ |
| 5181 | if( ssl->nb_zero > 3 ) |
| 5182 | { |
| 5183 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 5184 | "messages, possible DoS attack" ) ); |
| 5185 | /* Treat the records as if they were not properly authenticated, |
| 5186 | * thereby failing the connection if we see more than allowed |
| 5187 | * by the configured bad MAC threshold. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 5188 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 5189 | } |
| 5190 | } |
| 5191 | else |
| 5192 | ssl->nb_zero = 0; |
| 5193 | |
| 5194 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5195 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5196 | { |
| 5197 | ; /* in_ctr read from peer, not maintained internally */ |
| 5198 | } |
| 5199 | else |
| 5200 | #endif |
| 5201 | { |
| 5202 | unsigned i; |
| 5203 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 5204 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 5205 | break; |
| 5206 | |
| 5207 | /* The loop goes to its end iff the counter is wrapping */ |
| 5208 | if( i == ssl_ep_len( ssl ) ) |
| 5209 | { |
| 5210 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 5211 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 5212 | } |
| 5213 | } |
| 5214 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5217 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5218 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5219 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5220 | { |
| 5221 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 5222 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5223 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5224 | return( ret ); |
| 5225 | } |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5226 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5227 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5229 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5230 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5231 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5232 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5233 | } |
| 5234 | #endif |
| 5235 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5236 | return( 0 ); |
| 5237 | } |
| 5238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5239 | 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] | 5240 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5241 | /* |
| 5242 | * Read a record. |
| 5243 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5244 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 5245 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 5246 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5247 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5248 | |
| 5249 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 5250 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5251 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 5252 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 5253 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5254 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5255 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5256 | { |
| 5257 | int ret; |
| 5258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5259 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5260 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5261 | if( ssl->keep_current_message == 0 ) |
| 5262 | { |
| 5263 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5264 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5265 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5266 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5267 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 5268 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5269 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5270 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5271 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5272 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5273 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5274 | /* We only check for buffered messages if the |
| 5275 | * current datagram is fully consumed. */ |
| 5276 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5277 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5278 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5279 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 5280 | have_buffered = 1; |
| 5281 | } |
| 5282 | |
| 5283 | if( have_buffered == 0 ) |
| 5284 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5285 | { |
| 5286 | ret = ssl_get_next_record( ssl ); |
| 5287 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 5288 | continue; |
| 5289 | |
| 5290 | if( ret != 0 ) |
| 5291 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5292 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5293 | return( ret ); |
| 5294 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5295 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 5299 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5300 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5301 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5302 | { |
| 5303 | /* Buffer future message */ |
| 5304 | ret = ssl_buffer_message( ssl ); |
| 5305 | if( ret != 0 ) |
| 5306 | return( ret ); |
| 5307 | |
| 5308 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 5309 | } |
| 5310 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5311 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5312 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 5313 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5314 | |
| 5315 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5316 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 5317 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5318 | return( ret ); |
| 5319 | } |
| 5320 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5321 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 5322 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5323 | { |
| 5324 | mbedtls_ssl_update_handshake_status( ssl ); |
| 5325 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5326 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5327 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5328 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 5329 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 5330 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 5334 | |
| 5335 | return( 0 ); |
| 5336 | } |
| 5337 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5338 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5339 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5340 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5341 | if( ssl->in_left > ssl->next_record_offset ) |
| 5342 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5343 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5344 | return( 0 ); |
| 5345 | } |
| 5346 | |
| 5347 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 5348 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5349 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5350 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5351 | int ret = 0; |
| 5352 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5353 | if( hs == NULL ) |
| 5354 | return( -1 ); |
| 5355 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 5356 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 5357 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5358 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 5359 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 5360 | { |
| 5361 | /* Check if we have seen a ChangeCipherSpec before. |
| 5362 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5363 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5364 | { |
| 5365 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 5366 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 5367 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5368 | } |
| 5369 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 5370 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5371 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 5372 | ssl->in_msglen = 1; |
| 5373 | ssl->in_msg[0] = 1; |
| 5374 | |
| 5375 | /* As long as they are equal, the exact value doesn't matter. */ |
| 5376 | ssl->in_left = 0; |
| 5377 | ssl->next_record_offset = 0; |
| 5378 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5379 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5380 | goto exit; |
| 5381 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5382 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5383 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5384 | /* Debug only */ |
| 5385 | { |
| 5386 | unsigned offset; |
| 5387 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 5388 | { |
| 5389 | hs_buf = &hs->buffering.hs[offset]; |
| 5390 | if( hs_buf->is_valid == 1 ) |
| 5391 | { |
| 5392 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 5393 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 5394 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5395 | } |
| 5396 | } |
| 5397 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 5398 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5399 | |
| 5400 | /* Check if we have buffered and/or fully reassembled the |
| 5401 | * next handshake message. */ |
| 5402 | hs_buf = &hs->buffering.hs[0]; |
| 5403 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 5404 | { |
| 5405 | /* Synthesize a record containing the buffered HS message. */ |
| 5406 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 5407 | ( hs_buf->data[2] << 8 ) | |
| 5408 | hs_buf->data[3]; |
| 5409 | |
| 5410 | /* Double-check that we haven't accidentally buffered |
| 5411 | * a message that doesn't fit into the input buffer. */ |
| 5412 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 5413 | { |
| 5414 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5415 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5416 | } |
| 5417 | |
| 5418 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 5419 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 5420 | hs_buf->data, msg_len + 12 ); |
| 5421 | |
| 5422 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5423 | ssl->in_hslen = msg_len + 12; |
| 5424 | ssl->in_msglen = msg_len + 12; |
| 5425 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 5426 | |
| 5427 | ret = 0; |
| 5428 | goto exit; |
| 5429 | } |
| 5430 | else |
| 5431 | { |
| 5432 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 5433 | hs->in_msg_seq ) ); |
| 5434 | } |
| 5435 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5436 | ret = -1; |
| 5437 | |
| 5438 | exit: |
| 5439 | |
| 5440 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 5441 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5442 | } |
| 5443 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5444 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 5445 | size_t desired ) |
| 5446 | { |
| 5447 | int offset; |
| 5448 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5449 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 5450 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5451 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5452 | /* Get rid of future records epoch first, if such exist. */ |
| 5453 | ssl_free_buffered_record( ssl ); |
| 5454 | |
| 5455 | /* Check if we have enough space available now. */ |
| 5456 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5457 | hs->buffering.total_bytes_buffered ) ) |
| 5458 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5459 | 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] | 5460 | return( 0 ); |
| 5461 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5462 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 5463 | /* We don't have enough space to buffer the next expected handshake |
| 5464 | * message. Remove buffers used for future messages to gain space, |
| 5465 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5466 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 5467 | offset >= 0; offset-- ) |
| 5468 | { |
| 5469 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 5470 | offset ) ); |
| 5471 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5472 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5473 | |
| 5474 | /* Check if we have enough space available now. */ |
| 5475 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5476 | hs->buffering.total_bytes_buffered ) ) |
| 5477 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5478 | 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] | 5479 | return( 0 ); |
| 5480 | } |
| 5481 | } |
| 5482 | |
| 5483 | return( -1 ); |
| 5484 | } |
| 5485 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5486 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 5487 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5488 | int ret = 0; |
| 5489 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5490 | |
| 5491 | if( hs == NULL ) |
| 5492 | return( 0 ); |
| 5493 | |
| 5494 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 5495 | |
| 5496 | switch( ssl->in_msgtype ) |
| 5497 | { |
| 5498 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 5499 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5500 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 5501 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5502 | break; |
| 5503 | |
| 5504 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5505 | { |
| 5506 | unsigned recv_msg_seq_offset; |
| 5507 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 5508 | mbedtls_ssl_hs_buffer *hs_buf; |
| 5509 | size_t msg_len = ssl->in_hslen - 12; |
| 5510 | |
| 5511 | /* We should never receive an old handshake |
| 5512 | * message - double-check nonetheless. */ |
| 5513 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 5514 | { |
| 5515 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5516 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5517 | } |
| 5518 | |
| 5519 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 5520 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5521 | { |
| 5522 | /* Silently ignore -- message too far in the future */ |
| 5523 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 5524 | ( "Ignore future HS message with sequence number %u, " |
| 5525 | "buffering window %u - %u", |
| 5526 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 5527 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 5528 | |
| 5529 | goto exit; |
| 5530 | } |
| 5531 | |
| 5532 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 5533 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 5534 | |
| 5535 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 5536 | |
| 5537 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5538 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5539 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5540 | size_t reassembly_buf_sz; |
| 5541 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5542 | hs_buf->is_fragmented = |
| 5543 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 5544 | |
| 5545 | /* We copy the message back into the input buffer |
| 5546 | * after reassembly, so check that it's not too large. |
| 5547 | * This is an implementation-specific limitation |
| 5548 | * and not one from the standard, hence it is not |
| 5549 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 5550 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5551 | { |
| 5552 | /* Ignore message */ |
| 5553 | goto exit; |
| 5554 | } |
| 5555 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5556 | /* Check if we have enough space to buffer the message. */ |
| 5557 | if( hs->buffering.total_bytes_buffered > |
| 5558 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 5559 | { |
| 5560 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5561 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5562 | } |
| 5563 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5564 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 5565 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5566 | |
| 5567 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5568 | hs->buffering.total_bytes_buffered ) ) |
| 5569 | { |
| 5570 | if( recv_msg_seq_offset > 0 ) |
| 5571 | { |
| 5572 | /* If we can't buffer a future message because |
| 5573 | * of space limitations -- ignore. */ |
| 5574 | 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", |
| 5575 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5576 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5577 | goto exit; |
| 5578 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 5579 | else |
| 5580 | { |
| 5581 | 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", |
| 5582 | (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5583 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
| 5584 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5585 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 5586 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5587 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 5588 | 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", |
| 5589 | (unsigned) msg_len, |
| 5590 | (unsigned) reassembly_buf_sz, |
| 5591 | MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5592 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 5593 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 5594 | goto exit; |
| 5595 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5596 | } |
| 5597 | |
| 5598 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
| 5599 | msg_len ) ); |
| 5600 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 5601 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 5602 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5603 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5604 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5605 | goto exit; |
| 5606 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5607 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5608 | |
| 5609 | /* Prepare final header: copy msg_type, length and message_seq, |
| 5610 | * then add standardised fragment_offset and fragment_length */ |
| 5611 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 5612 | memset( hs_buf->data + 6, 0, 3 ); |
| 5613 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 5614 | |
| 5615 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 5616 | |
| 5617 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5618 | } |
| 5619 | else |
| 5620 | { |
| 5621 | /* Make sure msg_type and length are consistent */ |
| 5622 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 5623 | { |
| 5624 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 5625 | /* Ignore */ |
| 5626 | goto exit; |
| 5627 | } |
| 5628 | } |
| 5629 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 5630 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5631 | { |
| 5632 | size_t frag_len, frag_off; |
| 5633 | unsigned char * const msg = hs_buf->data + 12; |
| 5634 | |
| 5635 | /* |
| 5636 | * Check and copy current fragment |
| 5637 | */ |
| 5638 | |
| 5639 | /* Validation of header fields already done in |
| 5640 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 5641 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 5642 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 5643 | |
| 5644 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
| 5645 | frag_off, frag_len ) ); |
| 5646 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 5647 | |
| 5648 | if( hs_buf->is_fragmented ) |
| 5649 | { |
| 5650 | unsigned char * const bitmask = msg + msg_len; |
| 5651 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 5652 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 5653 | msg_len ) == 0 ); |
| 5654 | } |
| 5655 | else |
| 5656 | { |
| 5657 | hs_buf->is_complete = 1; |
| 5658 | } |
| 5659 | |
| 5660 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 5661 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 5662 | } |
| 5663 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5664 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 5665 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5666 | |
| 5667 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 5668 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5669 | break; |
| 5670 | } |
| 5671 | |
| 5672 | exit: |
| 5673 | |
| 5674 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 5675 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 5676 | } |
| 5677 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5678 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5679 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5680 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5681 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5682 | * Consume last content-layer message and potentially |
| 5683 | * update in_msglen which keeps track of the contents' |
| 5684 | * consumption state. |
| 5685 | * |
| 5686 | * (1) Handshake messages: |
| 5687 | * Remove last handshake message, move content |
| 5688 | * and adapt in_msglen. |
| 5689 | * |
| 5690 | * (2) Alert messages: |
| 5691 | * Consume whole record content, in_msglen = 0. |
| 5692 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5693 | * (3) Change cipher spec: |
| 5694 | * Consume whole record content, in_msglen = 0. |
| 5695 | * |
| 5696 | * (4) Application data: |
| 5697 | * Don't do anything - the record layer provides |
| 5698 | * the application data as a stream transport |
| 5699 | * and consumes through mbedtls_ssl_read only. |
| 5700 | * |
| 5701 | */ |
| 5702 | |
| 5703 | /* Case (1): Handshake messages */ |
| 5704 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5705 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5706 | /* Hard assertion to be sure that no application data |
| 5707 | * is in flight, as corrupting ssl->in_msglen during |
| 5708 | * ssl->in_offt != NULL is fatal. */ |
| 5709 | if( ssl->in_offt != NULL ) |
| 5710 | { |
| 5711 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5712 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5713 | } |
| 5714 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5715 | /* |
| 5716 | * Get next Handshake message in the current record |
| 5717 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5718 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5719 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5720 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5721 | * current handshake content: If DTLS handshake |
| 5722 | * fragmentation is used, that's the fragment |
| 5723 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 5724 | * size here is faulty and should be changed at |
| 5725 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5726 | * (2) While it doesn't seem to cause problems, one |
| 5727 | * has to be very careful not to assume that in_hslen |
| 5728 | * is always <= in_msglen in a sensible communication. |
| 5729 | * Again, it's wrong for DTLS handshake fragmentation. |
| 5730 | * The following check is therefore mandatory, and |
| 5731 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 5732 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 5733 | * bounds after handling a DTLS message with an unexpected |
| 5734 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5735 | */ |
| 5736 | if( ssl->in_hslen < ssl->in_msglen ) |
| 5737 | { |
| 5738 | ssl->in_msglen -= ssl->in_hslen; |
| 5739 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 5740 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5741 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5742 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 5743 | ssl->in_msg, ssl->in_msglen ); |
| 5744 | } |
| 5745 | else |
| 5746 | { |
| 5747 | ssl->in_msglen = 0; |
| 5748 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 5749 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5750 | ssl->in_hslen = 0; |
| 5751 | } |
| 5752 | /* Case (4): Application data */ |
| 5753 | else if( ssl->in_offt != NULL ) |
| 5754 | { |
| 5755 | return( 0 ); |
| 5756 | } |
| 5757 | /* Everything else (CCS & Alerts) */ |
| 5758 | else |
| 5759 | { |
| 5760 | ssl->in_msglen = 0; |
| 5761 | } |
| 5762 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5763 | return( 0 ); |
| 5764 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5765 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5766 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 5767 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5768 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5769 | return( 1 ); |
| 5770 | |
| 5771 | return( 0 ); |
| 5772 | } |
| 5773 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5774 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5775 | |
| 5776 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 5777 | { |
| 5778 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5779 | if( hs == NULL ) |
| 5780 | return; |
| 5781 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5782 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5783 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5784 | hs->buffering.total_bytes_buffered -= |
| 5785 | hs->buffering.future_record.len; |
| 5786 | |
| 5787 | mbedtls_free( hs->buffering.future_record.data ); |
| 5788 | hs->buffering.future_record.data = NULL; |
| 5789 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 5793 | { |
| 5794 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5795 | unsigned char * rec; |
| 5796 | size_t rec_len; |
| 5797 | unsigned rec_epoch; |
| 5798 | |
| 5799 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5800 | return( 0 ); |
| 5801 | |
| 5802 | if( hs == NULL ) |
| 5803 | return( 0 ); |
| 5804 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5805 | rec = hs->buffering.future_record.data; |
| 5806 | rec_len = hs->buffering.future_record.len; |
| 5807 | rec_epoch = hs->buffering.future_record.epoch; |
| 5808 | |
| 5809 | if( rec == NULL ) |
| 5810 | return( 0 ); |
| 5811 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5812 | /* Only consider loading future records if the |
| 5813 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 5814 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 5815 | return( 0 ); |
| 5816 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5817 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 5818 | |
| 5819 | if( rec_epoch != ssl->in_epoch ) |
| 5820 | { |
| 5821 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 5822 | goto exit; |
| 5823 | } |
| 5824 | |
| 5825 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 5826 | |
| 5827 | /* Double-check that the record is not too large */ |
| 5828 | if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN - |
| 5829 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 5830 | { |
| 5831 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5832 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5833 | } |
| 5834 | |
| 5835 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 5836 | ssl->in_left = rec_len; |
| 5837 | ssl->next_record_offset = 0; |
| 5838 | |
| 5839 | ssl_free_buffered_record( ssl ); |
| 5840 | |
| 5841 | exit: |
| 5842 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 5843 | return( 0 ); |
| 5844 | } |
| 5845 | |
| 5846 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl ) |
| 5847 | { |
| 5848 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5849 | size_t const rec_hdr_len = 13; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5850 | size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5851 | |
| 5852 | /* Don't buffer future records outside handshakes. */ |
| 5853 | if( hs == NULL ) |
| 5854 | return( 0 ); |
| 5855 | |
| 5856 | /* Only buffer handshake records (we are only interested |
| 5857 | * in Finished messages). */ |
| 5858 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 5859 | return( 0 ); |
| 5860 | |
| 5861 | /* Don't buffer more than one future epoch record. */ |
| 5862 | if( hs->buffering.future_record.data != NULL ) |
| 5863 | return( 0 ); |
| 5864 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5865 | /* Don't buffer record if there's not enough buffering space remaining. */ |
| 5866 | if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 5867 | hs->buffering.total_bytes_buffered ) ) |
| 5868 | { |
| 5869 | 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", |
| 5870 | (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 5871 | (unsigned) hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5872 | return( 0 ); |
| 5873 | } |
| 5874 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5875 | /* Buffer record */ |
| 5876 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
| 5877 | ssl->in_epoch + 1 ) ); |
| 5878 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr, |
| 5879 | rec_hdr_len + ssl->in_msglen ); |
| 5880 | |
| 5881 | /* ssl_parse_record_header() only considers records |
| 5882 | * of the next epoch as candidates for buffering. */ |
| 5883 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5884 | hs->buffering.future_record.len = total_buf_sz; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5885 | |
| 5886 | hs->buffering.future_record.data = |
| 5887 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 5888 | if( hs->buffering.future_record.data == NULL ) |
| 5889 | { |
| 5890 | /* If we run out of RAM trying to buffer a |
| 5891 | * record from the next epoch, just ignore. */ |
| 5892 | return( 0 ); |
| 5893 | } |
| 5894 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5895 | memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5896 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 5897 | hs->buffering.total_bytes_buffered += total_buf_sz; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5898 | return( 0 ); |
| 5899 | } |
| 5900 | |
| 5901 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5902 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 5903 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 5904 | { |
| 5905 | int ret; |
| 5906 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5907 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5908 | /* We might have buffered a future record; if so, |
| 5909 | * and if the epoch matches now, load it. |
| 5910 | * On success, this call will set ssl->in_left to |
| 5911 | * the length of the buffered record, so that |
| 5912 | * the calls to ssl_fetch_input() below will |
| 5913 | * essentially be no-ops. */ |
| 5914 | ret = ssl_load_buffered_record( ssl ); |
| 5915 | if( ret != 0 ) |
| 5916 | return( ret ); |
| 5917 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5918 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 5919 | /* Reset in pointers to default state for TLS/DTLS records, |
| 5920 | * assuming no CID and no offset between record content and |
| 5921 | * record plaintext. */ |
| 5922 | ssl_update_in_pointers( ssl ); |
| 5923 | |
| 5924 | /* Ensure that we have enough space available for the default form |
| 5925 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 5926 | * with no space for CIDs counted in). */ |
| 5927 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 5928 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5929 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5930 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5931 | return( ret ); |
| 5932 | } |
| 5933 | |
| 5934 | if( ( ret = ssl_parse_record_header( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5935 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5936 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 5937 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5938 | ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5939 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 5940 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 5941 | { |
| 5942 | ret = ssl_buffer_future_record( ssl ); |
| 5943 | if( ret != 0 ) |
| 5944 | return( ret ); |
| 5945 | |
| 5946 | /* Fall through to handling of unexpected records */ |
| 5947 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 5948 | } |
| 5949 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5950 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 5951 | { |
| 5952 | /* Skip unexpected record (but not whole datagram) */ |
| 5953 | ssl->next_record_offset = ssl->in_msglen |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5954 | + mbedtls_ssl_in_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5955 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 5956 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 5957 | "(header)" ) ); |
| 5958 | } |
| 5959 | else |
| 5960 | { |
| 5961 | /* Skip invalid record and the rest of the datagram */ |
| 5962 | ssl->next_record_offset = 0; |
| 5963 | ssl->in_left = 0; |
| 5964 | |
| 5965 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 5966 | "(header)" ) ); |
| 5967 | } |
| 5968 | |
| 5969 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5970 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5971 | } |
| 5972 | #endif |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5973 | return( ret ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5974 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5975 | |
| 5976 | /* |
| 5977 | * Read and optionally decrypt the message contents |
| 5978 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5979 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5980 | mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5981 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5982 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5983 | return( ret ); |
| 5984 | } |
| 5985 | |
| 5986 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5987 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5988 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5989 | { |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5990 | 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] | 5991 | if( ssl->next_record_offset < ssl->in_left ) |
| 5992 | { |
| 5993 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 5994 | } |
| 5995 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5996 | else |
| 5997 | #endif |
| 5998 | ssl->in_left = 0; |
| 5999 | |
| 6000 | if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6001 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6002 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6003 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6004 | { |
| 6005 | /* Silently discard invalid records */ |
Hanno Becker | 82e2a39 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 6006 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6007 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 6008 | /* Except when waiting for Finished as a bad mac here |
| 6009 | * probably means something went wrong in the handshake |
| 6010 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 6011 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 6012 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 6013 | { |
| 6014 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6015 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 6016 | { |
| 6017 | mbedtls_ssl_send_alert_message( ssl, |
| 6018 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6019 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 6020 | } |
| 6021 | #endif |
| 6022 | return( ret ); |
| 6023 | } |
| 6024 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6025 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6026 | if( ssl->conf->badmac_limit != 0 && |
| 6027 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6028 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6029 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 6030 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 6031 | } |
| 6032 | #endif |
| 6033 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6034 | /* As above, invalid records cause |
| 6035 | * dismissal of the whole datagram. */ |
| 6036 | |
| 6037 | ssl->next_record_offset = 0; |
| 6038 | ssl->in_left = 0; |
| 6039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6040 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6041 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6042 | } |
| 6043 | |
| 6044 | return( ret ); |
| 6045 | } |
| 6046 | else |
| 6047 | #endif |
| 6048 | { |
| 6049 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6050 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 6051 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6052 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6053 | mbedtls_ssl_send_alert_message( ssl, |
| 6054 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6055 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 6056 | } |
| 6057 | #endif |
| 6058 | return( ret ); |
| 6059 | } |
| 6060 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6061 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6062 | return( 0 ); |
| 6063 | } |
| 6064 | |
| 6065 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 6066 | { |
| 6067 | int ret; |
| 6068 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6069 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 6070 | * Handle particular types of records |
| 6071 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6072 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6073 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6074 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 6075 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 6076 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6077 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6080 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6081 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6082 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6083 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6084 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d", |
| 6085 | ssl->in_msglen ) ); |
| 6086 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6087 | } |
| 6088 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6089 | if( ssl->in_msg[0] != 1 ) |
| 6090 | { |
| 6091 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 6092 | ssl->in_msg[0] ) ); |
| 6093 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6094 | } |
| 6095 | |
| 6096 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6097 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6098 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 6099 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 6100 | { |
| 6101 | if( ssl->handshake == NULL ) |
| 6102 | { |
| 6103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 6104 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 6105 | } |
| 6106 | |
| 6107 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 6108 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 6109 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6110 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 6111 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 6112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6113 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6114 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 6115 | if( ssl->in_msglen != 2 ) |
| 6116 | { |
| 6117 | /* Note: Standard allows for more than one 2 byte alert |
| 6118 | to be packed in a single message, but Mbed TLS doesn't |
| 6119 | currently support this. */ |
| 6120 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d", |
| 6121 | ssl->in_msglen ) ); |
| 6122 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 6123 | } |
| 6124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6125 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6126 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 6127 | |
| 6128 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 6129 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6130 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6131 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6133 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 6134 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6135 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6138 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6139 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6141 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 6142 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6143 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 6144 | |
| 6145 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 6146 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6147 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 6148 | { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 6149 | 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] | 6150 | /* Will be handled when trying to parse ServerHello */ |
| 6151 | return( 0 ); |
| 6152 | } |
| 6153 | #endif |
| 6154 | |
| 6155 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
| 6156 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 6157 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 6158 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6159 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6160 | { |
| 6161 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) ); |
| 6162 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
| 6163 | return( 0 ); |
| 6164 | } |
| 6165 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 6166 | |
| 6167 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 6168 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6169 | } |
| 6170 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6171 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6172 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6173 | { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6174 | /* Drop unexpected ApplicationData records, |
| 6175 | * except at the beginning of renegotiations */ |
| 6176 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 6177 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 6178 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6179 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 6180 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6181 | #endif |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 6182 | ) |
| 6183 | { |
| 6184 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 6185 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 6186 | } |
| 6187 | |
| 6188 | if( ssl->handshake != NULL && |
| 6189 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 6190 | { |
| 6191 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6192 | } |
| 6193 | } |
Hanno Becker | 4a4af9f | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 6194 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 6195 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6196 | return( 0 ); |
| 6197 | } |
| 6198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6199 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6200 | { |
| 6201 | int ret; |
| 6202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6203 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 6204 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6205 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6206 | { |
| 6207 | return( ret ); |
| 6208 | } |
| 6209 | |
| 6210 | return( 0 ); |
| 6211 | } |
| 6212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6213 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6214 | unsigned char level, |
| 6215 | unsigned char message ) |
| 6216 | { |
| 6217 | int ret; |
| 6218 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6219 | if( ssl == NULL || ssl->conf == NULL ) |
| 6220 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6222 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6223 | 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] | 6224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6225 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6226 | ssl->out_msglen = 2; |
| 6227 | ssl->out_msg[0] = level; |
| 6228 | ssl->out_msg[1] = message; |
| 6229 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 6230 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6231 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6232 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6233 | return( ret ); |
| 6234 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6235 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 6236 | |
| 6237 | return( 0 ); |
| 6238 | } |
| 6239 | |
Hanno Becker | b9d4479 | 2019-02-08 07:19:04 +0000 | [diff] [blame] | 6240 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6241 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 6242 | { |
| 6243 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6244 | if( session->peer_cert != NULL ) |
| 6245 | { |
| 6246 | mbedtls_x509_crt_free( session->peer_cert ); |
| 6247 | mbedtls_free( session->peer_cert ); |
| 6248 | session->peer_cert = NULL; |
| 6249 | } |
| 6250 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6251 | if( session->peer_cert_digest != NULL ) |
| 6252 | { |
| 6253 | /* Zeroization is not necessary. */ |
| 6254 | mbedtls_free( session->peer_cert_digest ); |
| 6255 | session->peer_cert_digest = NULL; |
| 6256 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 6257 | session->peer_cert_digest_len = 0; |
| 6258 | } |
| 6259 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6260 | } |
| 6261 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 6262 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6263 | /* |
| 6264 | * Handshake functions |
| 6265 | */ |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6266 | #if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6267 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6268 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6269 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6270 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6271 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6272 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6273 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6274 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6275 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6276 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6277 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 6278 | ssl->state++; |
| 6279 | return( 0 ); |
| 6280 | } |
| 6281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6282 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6283 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6284 | } |
| 6285 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6286 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6287 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6288 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6289 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6291 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6292 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6293 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6294 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6295 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6296 | ssl->state++; |
| 6297 | return( 0 ); |
| 6298 | } |
| 6299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6300 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6301 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6302 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6303 | |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 6304 | #else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 6305 | /* Some certificate support -> implement write and parse */ |
| 6306 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6307 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6308 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6309 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6310 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6311 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6312 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6313 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6315 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6316 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 6317 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6318 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6319 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 6320 | ssl->state++; |
| 6321 | return( 0 ); |
| 6322 | } |
| 6323 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6324 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6325 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6326 | { |
| 6327 | if( ssl->client_auth == 0 ) |
| 6328 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6329 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6330 | ssl->state++; |
| 6331 | return( 0 ); |
| 6332 | } |
| 6333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6334 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6335 | /* |
| 6336 | * If using SSLv3 and got no cert, send an Alert message |
| 6337 | * (otherwise an empty Certificate message will be sent). |
| 6338 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6339 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 6340 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6341 | { |
| 6342 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6343 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 6344 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 6345 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6346 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6347 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6348 | goto write_msg; |
| 6349 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6350 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6351 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6352 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 6353 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6354 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6355 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6356 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6357 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6358 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 6359 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6360 | } |
| 6361 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6362 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6364 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6365 | |
| 6366 | /* |
| 6367 | * 0 . 0 handshake type |
| 6368 | * 1 . 3 handshake length |
| 6369 | * 4 . 6 length of all certs |
| 6370 | * 7 . 9 length of cert. 1 |
| 6371 | * 10 . n-1 peer certificate |
| 6372 | * n . n+2 length of cert. 2 |
| 6373 | * n+3 . ... upper level cert, etc. |
| 6374 | */ |
| 6375 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6376 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6377 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 6378 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6379 | { |
| 6380 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6381 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6383 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 6384 | i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6385 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
| 6388 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 6389 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 6390 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 6391 | |
| 6392 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 6393 | i += n; crt = crt->next; |
| 6394 | } |
| 6395 | |
| 6396 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 6397 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 6398 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 6399 | |
| 6400 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6401 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6402 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6403 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 6404 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6405 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6406 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6407 | |
| 6408 | ssl->state++; |
| 6409 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6410 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6411 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 6412 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6413 | return( ret ); |
| 6414 | } |
| 6415 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6416 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6417 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6418 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6421 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6422 | |
| 6423 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6424 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6425 | unsigned char *crt_buf, |
| 6426 | size_t crt_buf_len ) |
| 6427 | { |
| 6428 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 6429 | |
| 6430 | if( peer_crt == NULL ) |
| 6431 | return( -1 ); |
| 6432 | |
| 6433 | if( peer_crt->raw.len != crt_buf_len ) |
| 6434 | return( -1 ); |
| 6435 | |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6436 | return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6437 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 6438 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6439 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 6440 | unsigned char *crt_buf, |
| 6441 | size_t crt_buf_len ) |
| 6442 | { |
| 6443 | int ret; |
| 6444 | unsigned char const * const peer_cert_digest = |
| 6445 | ssl->session->peer_cert_digest; |
| 6446 | mbedtls_md_type_t const peer_cert_digest_type = |
| 6447 | ssl->session->peer_cert_digest_type; |
| 6448 | mbedtls_md_info_t const * const digest_info = |
| 6449 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 6450 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 6451 | size_t digest_len; |
| 6452 | |
| 6453 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 6454 | return( -1 ); |
| 6455 | |
| 6456 | digest_len = mbedtls_md_get_size( digest_info ); |
| 6457 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 6458 | return( -1 ); |
| 6459 | |
| 6460 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 6461 | if( ret != 0 ) |
| 6462 | return( -1 ); |
| 6463 | |
| 6464 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 6465 | } |
| 6466 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 6467 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6468 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6469 | /* |
| 6470 | * Once the certificate message is read, parse it into a cert chain and |
| 6471 | * perform basic checks, but leave actual verification to the caller |
| 6472 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6473 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 6474 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6475 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6476 | int ret; |
| 6477 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6478 | int crt_cnt=0; |
| 6479 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6480 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 6481 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6483 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6484 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6485 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6486 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6487 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6488 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6491 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 6492 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6493 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6494 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6495 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6496 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6497 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6498 | } |
| 6499 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6500 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6501 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6502 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6503 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6504 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6505 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6506 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 6507 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6508 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6509 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6510 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6511 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6512 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6513 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6514 | } |
| 6515 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6516 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6517 | i += 3; |
| 6518 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6519 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6520 | while( i < ssl->in_hslen ) |
| 6521 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6522 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6523 | if ( i + 3 > ssl->in_hslen ) { |
| 6524 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6525 | mbedtls_ssl_send_alert_message( ssl, |
| 6526 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6527 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 6528 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
| 6529 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6530 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6531 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6532 | if( ssl->in_msg[i] != 0 ) |
| 6533 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6534 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6535 | mbedtls_ssl_send_alert_message( ssl, |
| 6536 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6537 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6538 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6541 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6542 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6543 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6544 | i += 3; |
| 6545 | |
| 6546 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6547 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6548 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 6549 | mbedtls_ssl_send_alert_message( ssl, |
| 6550 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6551 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6552 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6553 | } |
| 6554 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6555 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6556 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6557 | if( crt_cnt++ == 0 && |
| 6558 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6559 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6560 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 6561 | /* During client-side renegotiation, check that the server's |
| 6562 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6563 | * mitigating the triple handshake attack. On success, reuse |
| 6564 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6565 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6566 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6567 | &ssl->in_msg[i], |
| 6568 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6569 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6570 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6571 | mbedtls_ssl_send_alert_message( ssl, |
| 6572 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6573 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6574 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6575 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6576 | |
| 6577 | /* Now we can safely free the original chain. */ |
| 6578 | ssl_clear_peer_cert( ssl->session ); |
| 6579 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6580 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6581 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6582 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6583 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6584 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6585 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 6586 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 6587 | * it in-place from the input buffer instead of making a copy. */ |
| 6588 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6589 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6590 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6591 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6592 | case 0: /*ok*/ |
| 6593 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6594 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6595 | prior certificate was already trusted. */ |
| 6596 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6597 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6598 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6599 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6600 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6601 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6602 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6603 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6604 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 6605 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 6606 | default: |
| 6607 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6608 | crt_parse_der_failed: |
| 6609 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6610 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6611 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6612 | } |
| 6613 | |
| 6614 | i += n; |
| 6615 | } |
| 6616 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6617 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6618 | return( 0 ); |
| 6619 | } |
| 6620 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6621 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6622 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6623 | { |
| 6624 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6625 | return( -1 ); |
| 6626 | |
| 6627 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 6628 | /* |
| 6629 | * Check if the client sent an empty certificate |
| 6630 | */ |
| 6631 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
| 6632 | { |
| 6633 | if( ssl->in_msglen == 2 && |
| 6634 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 6635 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 6636 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
| 6637 | { |
| 6638 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
| 6639 | return( 0 ); |
| 6640 | } |
| 6641 | |
| 6642 | return( -1 ); |
| 6643 | } |
| 6644 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 6645 | |
| 6646 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 6647 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6648 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6649 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6650 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6651 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6652 | { |
| 6653 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 6654 | return( 0 ); |
| 6655 | } |
| 6656 | |
| 6657 | return( -1 ); |
| 6658 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 6659 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6660 | } |
| 6661 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6662 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6663 | /* Check if a certificate message is expected. |
| 6664 | * Return either |
| 6665 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6666 | * - SSL_CERTIFICATE_SKIP |
| 6667 | * indicating whether a Certificate message is expected or not. |
| 6668 | */ |
| 6669 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6670 | #define SSL_CERTIFICATE_SKIP 1 |
| 6671 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6672 | int authmode ) |
| 6673 | { |
| 6674 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6675 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6676 | |
| 6677 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6678 | return( SSL_CERTIFICATE_SKIP ); |
| 6679 | |
| 6680 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6681 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6682 | { |
| 6683 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6684 | return( SSL_CERTIFICATE_SKIP ); |
| 6685 | |
| 6686 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6687 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6688 | ssl->session_negotiate->verify_result = |
| 6689 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6690 | return( SSL_CERTIFICATE_SKIP ); |
| 6691 | } |
| 6692 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 6693 | #else |
| 6694 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6695 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6696 | |
| 6697 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6698 | } |
| 6699 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6700 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6701 | int authmode, |
| 6702 | mbedtls_x509_crt *chain, |
| 6703 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6704 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6705 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6706 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 6707 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6708 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6709 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 6710 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6711 | void *p_vrfy; |
| 6712 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6713 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6714 | return( 0 ); |
| 6715 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 6716 | if( ssl->f_vrfy != NULL ) |
| 6717 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 6718 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 6719 | f_vrfy = ssl->f_vrfy; |
| 6720 | p_vrfy = ssl->p_vrfy; |
| 6721 | } |
| 6722 | else |
| 6723 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 6724 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 6725 | f_vrfy = ssl->conf->f_vrfy; |
| 6726 | p_vrfy = ssl->conf->p_vrfy; |
| 6727 | } |
| 6728 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6729 | /* |
| 6730 | * Main check: verify certificate |
| 6731 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6732 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6733 | if( ssl->conf->f_ca_cb != NULL ) |
| 6734 | { |
| 6735 | ((void) rs_ctx); |
| 6736 | have_ca_chain = 1; |
| 6737 | |
| 6738 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
Jarno Lamsa | 9822c0d | 2019-04-01 16:59:48 +0300 | [diff] [blame] | 6739 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6740 | chain, |
| 6741 | ssl->conf->f_ca_cb, |
| 6742 | ssl->conf->p_ca_cb, |
| 6743 | ssl->conf->cert_profile, |
| 6744 | ssl->hostname, |
| 6745 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 6746 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6747 | } |
| 6748 | else |
| 6749 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6750 | { |
| 6751 | mbedtls_x509_crt *ca_chain; |
| 6752 | mbedtls_x509_crl *ca_crl; |
| 6753 | |
| 6754 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6755 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6756 | { |
| 6757 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6758 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6759 | } |
| 6760 | else |
| 6761 | #endif |
| 6762 | { |
| 6763 | ca_chain = ssl->conf->ca_chain; |
| 6764 | ca_crl = ssl->conf->ca_crl; |
| 6765 | } |
| 6766 | |
| 6767 | if( ca_chain != NULL ) |
| 6768 | have_ca_chain = 1; |
| 6769 | |
| 6770 | ret = mbedtls_x509_crt_verify_restartable( |
| 6771 | chain, |
| 6772 | ca_chain, ca_crl, |
| 6773 | ssl->conf->cert_profile, |
| 6774 | ssl->hostname, |
| 6775 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 6776 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6777 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6778 | |
| 6779 | if( ret != 0 ) |
| 6780 | { |
| 6781 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6782 | } |
| 6783 | |
| 6784 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 6785 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6786 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6787 | #endif |
| 6788 | |
| 6789 | /* |
| 6790 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6791 | */ |
| 6792 | |
| 6793 | #if defined(MBEDTLS_ECP_C) |
| 6794 | { |
| 6795 | const mbedtls_pk_context *pk = &chain->pk; |
| 6796 | |
| 6797 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 6798 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 6799 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 6800 | { |
| 6801 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6802 | |
| 6803 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6804 | if( ret == 0 ) |
| 6805 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 6806 | } |
| 6807 | } |
| 6808 | #endif /* MBEDTLS_ECP_C */ |
| 6809 | |
| 6810 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6811 | ciphersuite_info, |
| 6812 | ! ssl->conf->endpoint, |
| 6813 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6814 | { |
| 6815 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6816 | if( ret == 0 ) |
| 6817 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
| 6818 | } |
| 6819 | |
| 6820 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6821 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6822 | * with details encoded in the verification flags. All other kinds |
| 6823 | * of error codes, including those from the user provided f_vrfy |
| 6824 | * functions, are treated as fatal and lead to a failure of |
| 6825 | * ssl_parse_certificate even if verification was optional. */ |
| 6826 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6827 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6828 | ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) ) |
| 6829 | { |
| 6830 | ret = 0; |
| 6831 | } |
| 6832 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 6833 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6834 | { |
| 6835 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6836 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6837 | } |
| 6838 | |
| 6839 | if( ret != 0 ) |
| 6840 | { |
| 6841 | uint8_t alert; |
| 6842 | |
| 6843 | /* The certificate may have been rejected for several reasons. |
| 6844 | Pick one and send the corresponding alert. Which alert to send |
| 6845 | may be a subject of debate in some cases. */ |
| 6846 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6847 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6848 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6849 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6850 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6851 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6852 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6853 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6854 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6855 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6856 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6857 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6858 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6859 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6860 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6861 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6862 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6863 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6864 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6865 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6866 | else |
| 6867 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6868 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6869 | alert ); |
| 6870 | } |
| 6871 | |
| 6872 | #if defined(MBEDTLS_DEBUG_C) |
| 6873 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6874 | { |
| 6875 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x", |
| 6876 | ssl->session_negotiate->verify_result ) ); |
| 6877 | } |
| 6878 | else |
| 6879 | { |
| 6880 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6881 | } |
| 6882 | #endif /* MBEDTLS_DEBUG_C */ |
| 6883 | |
| 6884 | return( ret ); |
| 6885 | } |
| 6886 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 6887 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6888 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6889 | unsigned char *start, size_t len ) |
| 6890 | { |
| 6891 | int ret; |
| 6892 | /* Remember digest of the peer's end-CRT. */ |
| 6893 | ssl->session_negotiate->peer_cert_digest = |
| 6894 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6895 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6896 | { |
| 6897 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6898 | sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) ); |
| 6899 | mbedtls_ssl_send_alert_message( ssl, |
| 6900 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6901 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6902 | |
| 6903 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6904 | } |
| 6905 | |
| 6906 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6907 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6908 | start, len, |
| 6909 | ssl->session_negotiate->peer_cert_digest ); |
| 6910 | |
| 6911 | ssl->session_negotiate->peer_cert_digest_type = |
| 6912 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6913 | ssl->session_negotiate->peer_cert_digest_len = |
| 6914 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6915 | |
| 6916 | return( ret ); |
| 6917 | } |
| 6918 | |
| 6919 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6920 | unsigned char *start, size_t len ) |
| 6921 | { |
| 6922 | unsigned char *end = start + len; |
| 6923 | int ret; |
| 6924 | |
| 6925 | /* Make a copy of the peer's raw public key. */ |
| 6926 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6927 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6928 | &ssl->handshake->peer_pubkey ); |
| 6929 | if( ret != 0 ) |
| 6930 | { |
| 6931 | /* We should have parsed the public key before. */ |
| 6932 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6933 | } |
| 6934 | |
| 6935 | return( 0 ); |
| 6936 | } |
| 6937 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6938 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 6939 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6940 | { |
| 6941 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6942 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6943 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6944 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6945 | ? ssl->handshake->sni_authmode |
| 6946 | : ssl->conf->authmode; |
| 6947 | #else |
| 6948 | const int authmode = ssl->conf->authmode; |
| 6949 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6950 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6951 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6952 | |
| 6953 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6954 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 6955 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6956 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6957 | { |
| 6958 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6959 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6960 | } |
| 6961 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6962 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 6963 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 6964 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6965 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6966 | chain = ssl->handshake->ecrs_peer_cert; |
| 6967 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 6968 | goto crt_verify; |
| 6969 | } |
| 6970 | #endif |
| 6971 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 6972 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6973 | { |
| 6974 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6975 | let it decide whether to alert. */ |
| 6976 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6977 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 6978 | } |
| 6979 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6980 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6981 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6982 | { |
| 6983 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6984 | |
| 6985 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6986 | ret = 0; |
| 6987 | else |
| 6988 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6989 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 6990 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 6991 | } |
| 6992 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6993 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 6994 | /* Clear existing peer CRT structure in case we tried to |
| 6995 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 6996 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 6997 | |
| 6998 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6999 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7000 | { |
| 7001 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 7002 | sizeof( mbedtls_x509_crt ) ) ); |
| 7003 | mbedtls_ssl_send_alert_message( ssl, |
| 7004 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7005 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 7006 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7007 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7008 | goto exit; |
| 7009 | } |
| 7010 | mbedtls_x509_crt_init( chain ); |
| 7011 | |
| 7012 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 7013 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7014 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 7015 | |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7016 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7017 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 7018 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 7019 | |
| 7020 | crt_verify: |
| 7021 | if( ssl->handshake->ecrs_enabled) |
| 7022 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 7023 | #endif |
| 7024 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7025 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7026 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 7027 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7028 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7029 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7030 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7031 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7032 | unsigned char *crt_start, *pk_start; |
| 7033 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7034 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7035 | /* We parse the CRT chain without copying, so |
| 7036 | * these pointers point into the input buffer, |
| 7037 | * and are hence still valid after freeing the |
| 7038 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7039 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7040 | crt_start = chain->raw.p; |
| 7041 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 7042 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7043 | pk_start = chain->pk_raw.p; |
| 7044 | pk_len = chain->pk_raw.len; |
| 7045 | |
| 7046 | /* Free the CRT structures before computing |
| 7047 | * digest and copying the peer's public key. */ |
| 7048 | mbedtls_x509_crt_free( chain ); |
| 7049 | mbedtls_free( chain ); |
| 7050 | chain = NULL; |
| 7051 | |
| 7052 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7053 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7054 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7055 | |
| 7056 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 7057 | if( ret != 0 ) |
| 7058 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 7059 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7060 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7061 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7062 | ssl->session_negotiate->peer_cert = chain; |
| 7063 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 7064 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7065 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7066 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7067 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 7068 | exit: |
| 7069 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 7070 | if( ret == 0 ) |
| 7071 | ssl->state++; |
| 7072 | |
| 7073 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
| 7074 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 7075 | { |
| 7076 | ssl->handshake->ecrs_peer_cert = chain; |
| 7077 | chain = NULL; |
| 7078 | } |
| 7079 | #endif |
| 7080 | |
| 7081 | if( chain != NULL ) |
| 7082 | { |
| 7083 | mbedtls_x509_crt_free( chain ); |
| 7084 | mbedtls_free( chain ); |
| 7085 | } |
| 7086 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7087 | return( ret ); |
| 7088 | } |
Hanno Becker | 2148993 | 2019-02-05 13:20:55 +0000 | [diff] [blame] | 7089 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7091 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7092 | { |
| 7093 | int ret; |
| 7094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7095 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7096 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7097 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7098 | ssl->out_msglen = 1; |
| 7099 | ssl->out_msg[0] = 1; |
| 7100 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7101 | ssl->state++; |
| 7102 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7103 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7104 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7105 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7106 | return( ret ); |
| 7107 | } |
| 7108 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7109 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7110 | |
| 7111 | return( 0 ); |
| 7112 | } |
| 7113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7114 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7115 | { |
| 7116 | int ret; |
| 7117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7118 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7119 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7120 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7121 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7122 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7123 | return( ret ); |
| 7124 | } |
| 7125 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7126 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7127 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7128 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7129 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7130 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7131 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7132 | } |
| 7133 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 7134 | /* CCS records are only accepted if they have length 1 and content '1', |
| 7135 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7136 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7137 | /* |
| 7138 | * Switch to our negotiated transform and session parameters for inbound |
| 7139 | * data. |
| 7140 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7141 | 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] | 7142 | ssl->transform_in = ssl->transform_negotiate; |
| 7143 | ssl->session_in = ssl->session_negotiate; |
| 7144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7145 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7146 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7147 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7148 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7149 | ssl_dtls_replay_reset( ssl ); |
| 7150 | #endif |
| 7151 | |
| 7152 | /* Increment epoch */ |
| 7153 | if( ++ssl->in_epoch == 0 ) |
| 7154 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7155 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7156 | /* This is highly unlikely to happen for legitimate reasons, so |
| 7157 | 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] | 7158 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7159 | } |
| 7160 | } |
| 7161 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7162 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7163 | memset( ssl->in_ctr, 0, 8 ); |
| 7164 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 7165 | ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7167 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7168 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7169 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7170 | 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] | 7171 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7172 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7173 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7174 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7175 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 7176 | } |
| 7177 | } |
| 7178 | #endif |
| 7179 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7180 | ssl->state++; |
| 7181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7182 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7183 | |
| 7184 | return( 0 ); |
| 7185 | } |
| 7186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7187 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 7188 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7189 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 7190 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7192 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7193 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7194 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7195 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7196 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7197 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7198 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7199 | #if defined(MBEDTLS_SHA512_C) |
| 7200 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7201 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 7202 | else |
| 7203 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7204 | #if defined(MBEDTLS_SHA256_C) |
| 7205 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7206 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7207 | else |
| 7208 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7209 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7211 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7212 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 7213 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7214 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 7215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7216 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7217 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7218 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7219 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7220 | mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 ); |
| 7221 | mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7222 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7223 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7224 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7225 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 7226 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7227 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 7228 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7229 | mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7230 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7231 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7232 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7233 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 2ad2297 | 2019-01-30 03:32:12 -0500 | [diff] [blame] | 7234 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7235 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7236 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7237 | mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7238 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7239 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7240 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 7241 | } |
| 7242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7243 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7244 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7245 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7246 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7247 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7248 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7249 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7250 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7251 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7252 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7253 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7254 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 7255 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7256 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7257 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7258 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7259 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7260 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7261 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7262 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7263 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7264 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7265 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7266 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7269 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7270 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7271 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7272 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7273 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7274 | mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len ); |
| 7275 | mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7276 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7277 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7279 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7280 | #if defined(MBEDTLS_SHA256_C) |
| 7281 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7282 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7283 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7284 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7285 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 7286 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7287 | mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7288 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7289 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 7290 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7291 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7292 | #if defined(MBEDTLS_SHA512_C) |
| 7293 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7294 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7295 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7296 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7297 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7298 | #else |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7299 | mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7300 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7301 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 7302 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7303 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 7304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7305 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7306 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7307 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7308 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7309 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7310 | mbedtls_md5_context md5; |
| 7311 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7312 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7313 | unsigned char padbuf[48]; |
| 7314 | unsigned char md5sum[16]; |
| 7315 | unsigned char sha1sum[20]; |
| 7316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7317 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7318 | if( !session ) |
| 7319 | session = ssl->session; |
| 7320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7321 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7322 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7323 | mbedtls_md5_init( &md5 ); |
| 7324 | mbedtls_sha1_init( &sha1 ); |
| 7325 | |
| 7326 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7327 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7328 | |
| 7329 | /* |
| 7330 | * SSLv3: |
| 7331 | * hash = |
| 7332 | * MD5( master + pad2 + |
| 7333 | * MD5( handshake + sender + master + pad1 ) ) |
| 7334 | * + SHA1( master + pad2 + |
| 7335 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7336 | */ |
| 7337 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7338 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7339 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7340 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7341 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7343 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7344 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7345 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7346 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7348 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7349 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7350 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7351 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7352 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7353 | mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 ); |
| 7354 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7355 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7356 | mbedtls_md5_finish_ret( &md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7357 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7358 | mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 ); |
| 7359 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7360 | mbedtls_sha1_update_ret( &sha1, padbuf, 40 ); |
| 7361 | mbedtls_sha1_finish_ret( &sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7362 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7363 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7364 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7365 | mbedtls_md5_starts_ret( &md5 ); |
| 7366 | mbedtls_md5_update_ret( &md5, session->master, 48 ); |
| 7367 | mbedtls_md5_update_ret( &md5, padbuf, 48 ); |
| 7368 | mbedtls_md5_update_ret( &md5, md5sum, 16 ); |
| 7369 | mbedtls_md5_finish_ret( &md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7370 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7371 | mbedtls_sha1_starts_ret( &sha1 ); |
| 7372 | mbedtls_sha1_update_ret( &sha1, session->master, 48 ); |
| 7373 | mbedtls_sha1_update_ret( &sha1, padbuf , 40 ); |
| 7374 | mbedtls_sha1_update_ret( &sha1, sha1sum, 20 ); |
| 7375 | mbedtls_sha1_finish_ret( &sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7377 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7378 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7379 | mbedtls_md5_free( &md5 ); |
| 7380 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7381 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7382 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 7383 | mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) ); |
| 7384 | mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7385 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7386 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7387 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7388 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7390 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7391 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7392 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7393 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7394 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7395 | const char *sender; |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7396 | mbedtls_md5_context md5; |
| 7397 | mbedtls_sha1_context sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7398 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7399 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7400 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7401 | if( !session ) |
| 7402 | session = ssl->session; |
| 7403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7404 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7405 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7406 | mbedtls_md5_init( &md5 ); |
| 7407 | mbedtls_sha1_init( &sha1 ); |
| 7408 | |
| 7409 | mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); |
| 7410 | mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7411 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7412 | /* |
| 7413 | * TLSv1: |
| 7414 | * hash = PRF( master, finished_label, |
| 7415 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 7416 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7417 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7418 | #if !defined(MBEDTLS_MD5_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7419 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *) |
| 7420 | md5.state, sizeof( md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7421 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7423 | #if !defined(MBEDTLS_SHA1_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7424 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *) |
| 7425 | sha1.state, sizeof( sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7426 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7428 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7429 | ? "client finished" |
| 7430 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7431 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7432 | mbedtls_md5_finish_ret( &md5, padbuf ); |
| 7433 | mbedtls_sha1_finish_ret( &sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7434 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7435 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7436 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7438 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7439 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7440 | mbedtls_md5_free( &md5 ); |
| 7441 | mbedtls_sha1_free( &sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7442 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7443 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7445 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7446 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7447 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7449 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7450 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7451 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7452 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7453 | { |
| 7454 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7455 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7456 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7457 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7458 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7459 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7460 | psa_status_t status; |
| 7461 | #else |
| 7462 | mbedtls_sha256_context sha256; |
| 7463 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7465 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7466 | if( !session ) |
| 7467 | session = ssl->session; |
| 7468 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7469 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7470 | ? "client finished" |
| 7471 | : "server finished"; |
| 7472 | |
| 7473 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7474 | sha256_psa = psa_hash_operation_init(); |
| 7475 | |
| 7476 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 7477 | |
| 7478 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 7479 | if( status != PSA_SUCCESS ) |
| 7480 | { |
| 7481 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7482 | return; |
| 7483 | } |
| 7484 | |
| 7485 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 7486 | if( status != PSA_SUCCESS ) |
| 7487 | { |
| 7488 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7489 | return; |
| 7490 | } |
| 7491 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 7492 | #else |
| 7493 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7494 | mbedtls_sha256_init( &sha256 ); |
| 7495 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7496 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7497 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7498 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7499 | |
| 7500 | /* |
| 7501 | * TLSv1.2: |
| 7502 | * hash = PRF( master, finished_label, |
| 7503 | * Hash( handshake ) )[0.11] |
| 7504 | */ |
| 7505 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7506 | #if !defined(MBEDTLS_SHA256_ALT) |
| 7507 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7508 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7509 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7510 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7511 | mbedtls_sha256_finish_ret( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7512 | mbedtls_sha256_free( &sha256 ); |
| 7513 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7514 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7515 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7516 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7518 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7519 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7520 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7521 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7522 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7523 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7524 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7526 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7527 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7528 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7529 | { |
| 7530 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 7531 | const char *sender; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7532 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7533 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7534 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7535 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7536 | psa_status_t status; |
| 7537 | #else |
| 7538 | mbedtls_sha512_context sha512; |
| 7539 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7541 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7542 | if( !session ) |
| 7543 | session = ssl->session; |
| 7544 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7545 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 7546 | ? "client finished" |
| 7547 | : "server finished"; |
| 7548 | |
| 7549 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7550 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7551 | |
| 7552 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 7553 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7554 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7555 | if( status != PSA_SUCCESS ) |
| 7556 | { |
| 7557 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 7558 | return; |
| 7559 | } |
| 7560 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7561 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7562 | if( status != PSA_SUCCESS ) |
| 7563 | { |
| 7564 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 7565 | return; |
| 7566 | } |
| 7567 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 7568 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7569 | mbedtls_sha512_init( &sha512 ); |
| 7570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7571 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7572 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 7573 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7574 | |
| 7575 | /* |
| 7576 | * TLSv1.2: |
| 7577 | * hash = PRF( master, finished_label, |
| 7578 | * Hash( handshake ) )[0.11] |
| 7579 | */ |
| 7580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7581 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 7582 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 7583 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 7584 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7585 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7586 | mbedtls_sha512_finish_ret( &sha512, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7587 | mbedtls_sha512_free( &sha512 ); |
| 7588 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7589 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 7590 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7591 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7593 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7594 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7595 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7597 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7598 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7599 | #endif /* MBEDTLS_SHA512_C */ |
| 7600 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 7601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7602 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7603 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7604 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7605 | |
| 7606 | /* |
| 7607 | * Free our handshake params |
| 7608 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7609 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7610 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7611 | ssl->handshake = NULL; |
| 7612 | |
| 7613 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7614 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7615 | */ |
| 7616 | if( ssl->transform ) |
| 7617 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7618 | mbedtls_ssl_transform_free( ssl->transform ); |
| 7619 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7620 | } |
| 7621 | ssl->transform = ssl->transform_negotiate; |
| 7622 | ssl->transform_negotiate = NULL; |
| 7623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7624 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7625 | } |
| 7626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7627 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7628 | { |
| 7629 | int resume = ssl->handshake->resume; |
| 7630 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7631 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7633 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 7634 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7635 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7636 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7637 | ssl->renego_records_seen = 0; |
| 7638 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7639 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7640 | |
| 7641 | /* |
| 7642 | * Free the previous session and switch in the current one |
| 7643 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7644 | if( ssl->session ) |
| 7645 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7646 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 7647 | /* RFC 7366 3.1: keep the EtM state */ |
| 7648 | ssl->session_negotiate->encrypt_then_mac = |
| 7649 | ssl->session->encrypt_then_mac; |
| 7650 | #endif |
| 7651 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7652 | mbedtls_ssl_session_free( ssl->session ); |
| 7653 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7654 | } |
| 7655 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7656 | ssl->session_negotiate = NULL; |
| 7657 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7658 | /* |
| 7659 | * Add cache entry |
| 7660 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7661 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 7662 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7663 | resume == 0 ) |
| 7664 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 7665 | 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] | 7666 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 7667 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7669 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7670 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7671 | ssl->handshake->flight != NULL ) |
| 7672 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 7673 | /* Cancel handshake timer */ |
| 7674 | ssl_set_timer( ssl, 0 ); |
| 7675 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7676 | /* Keep last flight around in case we need to resend it: |
| 7677 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7678 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 7679 | } |
| 7680 | else |
| 7681 | #endif |
| 7682 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 7683 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7684 | ssl->state++; |
| 7685 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7686 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7687 | } |
| 7688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7689 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7690 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7691 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7692 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7693 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7694 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 7695 | ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 7696 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7697 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 7698 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 7699 | /* |
| 7700 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 7701 | * may define some other value. Currently (early 2016), no defined |
| 7702 | * ciphersuite does this (and this is unlikely to change as activity has |
| 7703 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 7704 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7705 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7707 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7708 | ssl->verify_data_len = hash_len; |
| 7709 | 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] | 7710 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7711 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7712 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7713 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 7714 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7715 | |
| 7716 | /* |
| 7717 | * In case of session resuming, invert the client and server |
| 7718 | * ChangeCipherSpec messages order. |
| 7719 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7720 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7721 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7722 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7723 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7724 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7725 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7726 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7727 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7728 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7729 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7730 | } |
| 7731 | else |
| 7732 | ssl->state++; |
| 7733 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7734 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 7735 | * Switch to our negotiated transform and session parameters for outbound |
| 7736 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7737 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7738 | 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] | 7739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7740 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7741 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7742 | { |
| 7743 | unsigned char i; |
| 7744 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7745 | /* Remember current epoch settings for resending */ |
| 7746 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7747 | 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] | 7748 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7749 | /* Set sequence_number to zero */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7750 | memset( ssl->cur_out_ctr + 2, 0, 6 ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7751 | |
| 7752 | /* Increment epoch */ |
| 7753 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7754 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7755 | break; |
| 7756 | |
| 7757 | /* The loop goes to its end iff the counter is wrapping */ |
| 7758 | if( i == 0 ) |
| 7759 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7760 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 7761 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7762 | } |
| 7763 | } |
| 7764 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7765 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 7766 | memset( ssl->cur_out_ctr, 0, 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7767 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7768 | ssl->transform_out = ssl->transform_negotiate; |
| 7769 | ssl->session_out = ssl->session_negotiate; |
| 7770 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7771 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 7772 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7773 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7774 | 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] | 7775 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7776 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 7777 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 7778 | } |
| 7779 | } |
| 7780 | #endif |
| 7781 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7782 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7783 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7784 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 7785 | #endif |
| 7786 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7787 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7788 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 7789 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7790 | return( ret ); |
| 7791 | } |
| 7792 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 7793 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7794 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 7795 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 7796 | { |
| 7797 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 7798 | return( ret ); |
| 7799 | } |
| 7800 | #endif |
| 7801 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7802 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7803 | |
| 7804 | return( 0 ); |
| 7805 | } |
| 7806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7807 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7808 | #define SSL_MAX_HASH_LEN 36 |
| 7809 | #else |
| 7810 | #define SSL_MAX_HASH_LEN 12 |
| 7811 | #endif |
| 7812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7813 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7814 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 7815 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 7816 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7817 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7818 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7819 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7820 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7821 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7822 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 7823 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7824 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7825 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7826 | return( ret ); |
| 7827 | } |
| 7828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7829 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7830 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7831 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7832 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7833 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7834 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7835 | } |
| 7836 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7837 | /* 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] | 7838 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 7839 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 7840 | hash_len = 36; |
| 7841 | else |
| 7842 | #endif |
| 7843 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7844 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7845 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 7846 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7847 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7848 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7849 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7850 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7851 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7852 | } |
| 7853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7854 | 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] | 7855 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7856 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7857 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 7858 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7859 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7860 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7861 | } |
| 7862 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7863 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7864 | ssl->verify_data_len = hash_len; |
| 7865 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 7866 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7867 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7868 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7869 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7870 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7871 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7872 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7873 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7874 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7875 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7876 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 7877 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7878 | } |
| 7879 | else |
| 7880 | ssl->state++; |
| 7881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7882 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 7883 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7884 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 7885 | #endif |
| 7886 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7887 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7888 | |
| 7889 | return( 0 ); |
| 7890 | } |
| 7891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7892 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7893 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7894 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7895 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7896 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 7897 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 7898 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 7899 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7900 | mbedtls_md5_starts_ret( &handshake->fin_md5 ); |
| 7901 | mbedtls_sha1_starts_ret( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7902 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7903 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7904 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7905 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7906 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 7907 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 7908 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7909 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7910 | mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7911 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7912 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7913 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7914 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 7915 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 7916 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7917 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7918 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 7919 | mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7920 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 7921 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7922 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7923 | |
| 7924 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7925 | |
| 7926 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 7927 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 7928 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 7929 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7931 | #if defined(MBEDTLS_DHM_C) |
| 7932 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7933 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7934 | #if defined(MBEDTLS_ECDH_C) |
| 7935 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7936 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 7937 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 7938 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 7939 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7940 | handshake->ecjpake_cache = NULL; |
| 7941 | handshake->ecjpake_cache_len = 0; |
| 7942 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 7943 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7944 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 7945 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 7946 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 7947 | #endif |
| 7948 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 7949 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7950 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 7951 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 7952 | |
| 7953 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 7954 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7955 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 7956 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7957 | } |
| 7958 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 7959 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7960 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7961 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 7962 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7963 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 7964 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 7965 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 7966 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7967 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 7968 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 7969 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7970 | } |
| 7971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7972 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7973 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7974 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7975 | } |
| 7976 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7977 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7978 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7979 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7980 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7981 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7982 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7983 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7984 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 7985 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7986 | |
| 7987 | /* |
| 7988 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 7989 | * Now allocate missing structures. |
| 7990 | */ |
| 7991 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7992 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7993 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7994 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 7995 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 7996 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7997 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 7998 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 7999 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8000 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 8001 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8002 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8003 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 8004 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8005 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8006 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8007 | if( ssl->handshake == NULL || |
| 8008 | ssl->transform_negotiate == NULL || |
| 8009 | ssl->session_negotiate == NULL ) |
| 8010 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 8011 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8013 | mbedtls_free( ssl->handshake ); |
| 8014 | mbedtls_free( ssl->transform_negotiate ); |
| 8015 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8016 | |
| 8017 | ssl->handshake = NULL; |
| 8018 | ssl->transform_negotiate = NULL; |
| 8019 | ssl->session_negotiate = NULL; |
| 8020 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8021 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8022 | } |
| 8023 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 8024 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8025 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 8026 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 8027 | ssl_handshake_params_init( ssl->handshake ); |
| 8028 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8029 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8030 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8031 | { |
| 8032 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8033 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8034 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 8035 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 8036 | else |
| 8037 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8038 | |
| 8039 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 8040 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 8041 | #endif |
| 8042 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8043 | return( 0 ); |
| 8044 | } |
| 8045 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8046 | #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] | 8047 | /* Dummy cookie callbacks for defaults */ |
| 8048 | static int ssl_cookie_write_dummy( void *ctx, |
| 8049 | unsigned char **p, unsigned char *end, |
| 8050 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8051 | { |
| 8052 | ((void) ctx); |
| 8053 | ((void) p); |
| 8054 | ((void) end); |
| 8055 | ((void) cli_id); |
| 8056 | ((void) cli_id_len); |
| 8057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8058 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8059 | } |
| 8060 | |
| 8061 | static int ssl_cookie_check_dummy( void *ctx, |
| 8062 | const unsigned char *cookie, size_t cookie_len, |
| 8063 | const unsigned char *cli_id, size_t cli_id_len ) |
| 8064 | { |
| 8065 | ((void) ctx); |
| 8066 | ((void) cookie); |
| 8067 | ((void) cookie_len); |
| 8068 | ((void) cli_id); |
| 8069 | ((void) cli_id_len); |
| 8070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8071 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8072 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8073 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 8074 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8075 | /* Once ssl->out_hdr as the address of the beginning of the |
| 8076 | * next outgoing record is set, deduce the other pointers. |
| 8077 | * |
| 8078 | * Note: For TLS, we save the implicit record sequence number |
| 8079 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 8080 | * and the caller has to make sure there's space for this. |
| 8081 | */ |
| 8082 | |
| 8083 | static void ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 8084 | mbedtls_ssl_transform *transform ) |
| 8085 | { |
| 8086 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8087 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8088 | { |
| 8089 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8090 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8091 | ssl->out_cid = ssl->out_ctr + 8; |
| 8092 | ssl->out_len = ssl->out_cid; |
| 8093 | if( transform != NULL ) |
| 8094 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8095 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8096 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8097 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8098 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8099 | } |
| 8100 | else |
| 8101 | #endif |
| 8102 | { |
| 8103 | ssl->out_ctr = ssl->out_hdr - 8; |
| 8104 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8105 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8106 | ssl->out_cid = ssl->out_len; |
| 8107 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8108 | ssl->out_iv = ssl->out_hdr + 5; |
| 8109 | } |
| 8110 | |
| 8111 | /* Adjust out_msg to make space for explicit IV, if used. */ |
| 8112 | if( transform != NULL && |
| 8113 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
| 8114 | { |
| 8115 | ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen; |
| 8116 | } |
| 8117 | else |
| 8118 | ssl->out_msg = ssl->out_iv; |
| 8119 | } |
| 8120 | |
| 8121 | /* Once ssl->in_hdr as the address of the beginning of the |
| 8122 | * next incoming record is set, deduce the other pointers. |
| 8123 | * |
| 8124 | * Note: For TLS, we save the implicit record sequence number |
| 8125 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 8126 | * and the caller has to make sure there's space for this. |
| 8127 | */ |
| 8128 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8129 | static void ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8130 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8131 | /* This function sets the pointers to match the case |
| 8132 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 8133 | * and ssl->in_msg pointing to the beginning of the record |
| 8134 | * content. |
| 8135 | * |
| 8136 | * When decrypting a protected record, ssl->in_msg |
| 8137 | * will be shifted to point to the beginning of the |
| 8138 | * record plaintext. |
| 8139 | */ |
| 8140 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8141 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8142 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8143 | { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8144 | /* This sets the header pointers to match records |
| 8145 | * without CID. When we receive a record containing |
| 8146 | * a CID, the fields are shifted accordingly in |
| 8147 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8148 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8149 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8150 | ssl->in_cid = ssl->in_ctr + 8; |
| 8151 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8152 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8153 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8154 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 8155 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8156 | } |
| 8157 | else |
| 8158 | #endif |
| 8159 | { |
| 8160 | ssl->in_ctr = ssl->in_hdr - 8; |
| 8161 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 8162 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 8163 | ssl->in_cid = ssl->in_len; |
| 8164 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8165 | ssl->in_iv = ssl->in_hdr + 5; |
| 8166 | } |
| 8167 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8168 | /* This will be adjusted at record decryption time. */ |
| 8169 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 8170 | } |
| 8171 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8172 | /* |
| 8173 | * Initialize an SSL context |
| 8174 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 8175 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 8176 | { |
| 8177 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 8178 | } |
| 8179 | |
| 8180 | /* |
| 8181 | * Setup an SSL context |
| 8182 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8183 | |
| 8184 | static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
| 8185 | { |
| 8186 | /* Set the incoming and outgoing record pointers. */ |
| 8187 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8188 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 8189 | { |
| 8190 | ssl->out_hdr = ssl->out_buf; |
| 8191 | ssl->in_hdr = ssl->in_buf; |
| 8192 | } |
| 8193 | else |
| 8194 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 8195 | { |
| 8196 | ssl->out_hdr = ssl->out_buf + 8; |
| 8197 | ssl->in_hdr = ssl->in_buf + 8; |
| 8198 | } |
| 8199 | |
| 8200 | /* Derive other internal pointers. */ |
| 8201 | ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 8202 | ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8203 | } |
| 8204 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8205 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 8206 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8207 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8208 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8209 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 8210 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8211 | |
| 8212 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 8213 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 8214 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 8215 | |
| 8216 | /* Set to NULL in case of an error condition */ |
| 8217 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8218 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8219 | ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 8220 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8221 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8222 | 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] | 8223 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8224 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8225 | } |
| 8226 | |
| 8227 | ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 8228 | if( ssl->out_buf == NULL ) |
| 8229 | { |
| 8230 | 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] | 8231 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8232 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8233 | } |
| 8234 | |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 8235 | ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 8236 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8237 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8238 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8239 | |
| 8240 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 8241 | |
| 8242 | error: |
| 8243 | mbedtls_free( ssl->in_buf ); |
| 8244 | mbedtls_free( ssl->out_buf ); |
| 8245 | |
| 8246 | ssl->conf = NULL; |
| 8247 | |
| 8248 | ssl->in_buf = NULL; |
| 8249 | ssl->out_buf = NULL; |
| 8250 | |
| 8251 | ssl->in_hdr = NULL; |
| 8252 | ssl->in_ctr = NULL; |
| 8253 | ssl->in_len = NULL; |
| 8254 | ssl->in_iv = NULL; |
| 8255 | ssl->in_msg = NULL; |
| 8256 | |
| 8257 | ssl->out_hdr = NULL; |
| 8258 | ssl->out_ctr = NULL; |
| 8259 | ssl->out_len = NULL; |
| 8260 | ssl->out_iv = NULL; |
| 8261 | ssl->out_msg = NULL; |
| 8262 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 8263 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8264 | } |
| 8265 | |
| 8266 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8267 | * Reset an initialized and used SSL context for re-use while retaining |
| 8268 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8269 | * |
| 8270 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 8271 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8272 | */ |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8273 | static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8274 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8275 | int ret; |
| 8276 | |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 8277 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \ |
| 8278 | !defined(MBEDTLS_SSL_SRV_C) |
| 8279 | ((void) partial); |
| 8280 | #endif |
| 8281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8282 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8283 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8284 | /* Cancel any possibly running timer */ |
| 8285 | ssl_set_timer( ssl, 0 ); |
| 8286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8287 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 8288 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 8289 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8290 | |
| 8291 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8292 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 8293 | 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] | 8294 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8295 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8296 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8297 | ssl->in_offt = NULL; |
Hanno Becker | f29d470 | 2018-08-10 11:31:15 +0100 | [diff] [blame] | 8298 | ssl_reset_in_out_pointers( ssl ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8299 | |
| 8300 | ssl->in_msgtype = 0; |
| 8301 | ssl->in_msglen = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8302 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8303 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 8304 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 8305 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8306 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 8307 | ssl_dtls_replay_reset( ssl ); |
| 8308 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8309 | |
| 8310 | ssl->in_hslen = 0; |
| 8311 | ssl->nb_zero = 0; |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 8312 | |
| 8313 | ssl->keep_current_message = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8314 | |
| 8315 | ssl->out_msgtype = 0; |
| 8316 | ssl->out_msglen = 0; |
| 8317 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8318 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 8319 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 8320 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 8321 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8322 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 8323 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 8324 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8325 | ssl->transform_in = NULL; |
| 8326 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8327 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 8328 | ssl->session_in = NULL; |
| 8329 | ssl->session_out = NULL; |
| 8330 | |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8331 | memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8332 | |
| 8333 | #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] | 8334 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8335 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
| 8336 | { |
| 8337 | ssl->in_left = 0; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8338 | memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN ); |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8339 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8341 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 8342 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8343 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8344 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 8345 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8346 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8347 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 8348 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8349 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 8350 | } |
| 8351 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8352 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8353 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8354 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8355 | mbedtls_ssl_transform_free( ssl->transform ); |
| 8356 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8357 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8358 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8359 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8360 | if( ssl->session ) |
| 8361 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8362 | mbedtls_ssl_session_free( ssl->session ); |
| 8363 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 8364 | ssl->session = NULL; |
| 8365 | } |
| 8366 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8367 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 8368 | ssl->alpn_chosen = NULL; |
| 8369 | #endif |
| 8370 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 8371 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8372 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8373 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 8374 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8375 | { |
| 8376 | mbedtls_free( ssl->cli_id ); |
| 8377 | ssl->cli_id = NULL; |
| 8378 | ssl->cli_id_len = 0; |
| 8379 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 8380 | #endif |
| 8381 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 8382 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 8383 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 8384 | |
| 8385 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 8388 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 8389 | * Reset an initialized and used SSL context for re-use while retaining |
| 8390 | * all application-set variables, function pointers and data. |
| 8391 | */ |
| 8392 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 8393 | { |
| 8394 | return( ssl_session_reset_int( ssl, 0 ) ); |
| 8395 | } |
| 8396 | |
| 8397 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8398 | * SSL set accessors |
| 8399 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8400 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8401 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8402 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8403 | } |
| 8404 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 8405 | 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] | 8406 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8407 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 8408 | } |
| 8409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8410 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8411 | 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] | 8412 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8413 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 8414 | } |
| 8415 | #endif |
| 8416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8417 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8418 | 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] | 8419 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8420 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 8421 | } |
| 8422 | #endif |
| 8423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8424 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8425 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 8426 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 8427 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 8428 | { |
| 8429 | ssl->disable_datagram_packing = !allow_packing; |
| 8430 | } |
| 8431 | |
| 8432 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 8433 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8434 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8435 | conf->hs_timeout_min = min; |
| 8436 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 8437 | } |
| 8438 | #endif |
| 8439 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8440 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8441 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8442 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8443 | } |
| 8444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8445 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8446 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 8447 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8448 | void *p_vrfy ) |
| 8449 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8450 | conf->f_vrfy = f_vrfy; |
| 8451 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8452 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8453 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 8454 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8455 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 8456 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8457 | void *p_rng ) |
| 8458 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 8459 | conf->f_rng = f_rng; |
| 8460 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8461 | } |
| 8462 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8463 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 8464 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8465 | void *p_dbg ) |
| 8466 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8467 | conf->f_dbg = f_dbg; |
| 8468 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8469 | } |
| 8470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8471 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8472 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8473 | mbedtls_ssl_send_t *f_send, |
| 8474 | mbedtls_ssl_recv_t *f_recv, |
| 8475 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8476 | { |
| 8477 | ssl->p_bio = p_bio; |
| 8478 | ssl->f_send = f_send; |
| 8479 | ssl->f_recv = f_recv; |
| 8480 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 8481 | } |
| 8482 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 8483 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 8484 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 8485 | { |
| 8486 | ssl->mtu = mtu; |
| 8487 | } |
| 8488 | #endif |
| 8489 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8490 | 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] | 8491 | { |
| 8492 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 8493 | } |
| 8494 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8495 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 8496 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 8497 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 8498 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8499 | { |
| 8500 | ssl->p_timer = p_timer; |
| 8501 | ssl->f_set_timer = f_set_timer; |
| 8502 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 8503 | |
| 8504 | /* Make sure we start with no timer running */ |
| 8505 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 8506 | } |
| 8507 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8508 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8509 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8510 | void *p_cache, |
| 8511 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 8512 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8513 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 8514 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8515 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8516 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8517 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8518 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8520 | #if defined(MBEDTLS_SSL_CLI_C) |
| 8521 | 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] | 8522 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8523 | int ret; |
| 8524 | |
| 8525 | if( ssl == NULL || |
| 8526 | session == NULL || |
| 8527 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 8528 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8529 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8530 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8531 | } |
| 8532 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 8533 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 8534 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8535 | return( ret ); |
| 8536 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 8537 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 8538 | |
| 8539 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8540 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8541 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8542 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8543 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8544 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8545 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8546 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 8547 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 8548 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 8549 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8550 | } |
| 8551 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8552 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 8553 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8554 | int major, int minor ) |
| 8555 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8556 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 8557 | return; |
| 8558 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8559 | 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] | 8560 | return; |
| 8561 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8562 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8563 | } |
| 8564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8565 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8566 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 8567 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 8568 | { |
| 8569 | conf->cert_profile = profile; |
| 8570 | } |
| 8571 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8572 | /* Append a new keycert entry to a (possibly empty) list */ |
| 8573 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 8574 | mbedtls_x509_crt *cert, |
| 8575 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8576 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8577 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8578 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8579 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 8580 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8581 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8582 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8583 | new_cert->cert = cert; |
| 8584 | new_cert->key = key; |
| 8585 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8586 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8587 | /* Update head is the list was null, else add to the end */ |
| 8588 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8589 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8590 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 8591 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8592 | else |
| 8593 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8594 | mbedtls_ssl_key_cert *cur = *head; |
| 8595 | while( cur->next != NULL ) |
| 8596 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 8597 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 8598 | } |
| 8599 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8600 | return( 0 ); |
| 8601 | } |
| 8602 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8603 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 8604 | mbedtls_x509_crt *own_cert, |
| 8605 | mbedtls_pk_context *pk_key ) |
| 8606 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 8607 | 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] | 8608 | } |
| 8609 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8610 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8611 | mbedtls_x509_crt *ca_chain, |
| 8612 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8613 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8614 | conf->ca_chain = ca_chain; |
| 8615 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8616 | |
| 8617 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 8618 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 8619 | * cannot be used together. */ |
| 8620 | conf->f_ca_cb = NULL; |
| 8621 | conf->p_ca_cb = NULL; |
| 8622 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8623 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8624 | |
| 8625 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 8626 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 8627 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 8628 | void *p_ca_cb ) |
| 8629 | { |
| 8630 | conf->f_ca_cb = f_ca_cb; |
| 8631 | conf->p_ca_cb = p_ca_cb; |
| 8632 | |
| 8633 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 8634 | * cannot be used together. */ |
| 8635 | conf->ca_chain = NULL; |
| 8636 | conf->ca_crl = NULL; |
| 8637 | } |
| 8638 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8639 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 8640 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8641 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 8642 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 8643 | mbedtls_x509_crt *own_cert, |
| 8644 | mbedtls_pk_context *pk_key ) |
| 8645 | { |
| 8646 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 8647 | own_cert, pk_key ) ); |
| 8648 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 8649 | |
| 8650 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 8651 | mbedtls_x509_crt *ca_chain, |
| 8652 | mbedtls_x509_crl *ca_crl ) |
| 8653 | { |
| 8654 | ssl->handshake->sni_ca_chain = ca_chain; |
| 8655 | ssl->handshake->sni_ca_crl = ca_crl; |
| 8656 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 8657 | |
| 8658 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 8659 | int authmode ) |
| 8660 | { |
| 8661 | ssl->handshake->sni_authmode = authmode; |
| 8662 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 8663 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8664 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 8665 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 8666 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 8667 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 8668 | void *p_vrfy ) |
| 8669 | { |
| 8670 | ssl->f_vrfy = f_vrfy; |
| 8671 | ssl->p_vrfy = p_vrfy; |
| 8672 | } |
| 8673 | #endif |
| 8674 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8675 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8676 | /* |
| 8677 | * Set EC J-PAKE password for current handshake |
| 8678 | */ |
| 8679 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 8680 | const unsigned char *pw, |
| 8681 | size_t pw_len ) |
| 8682 | { |
| 8683 | mbedtls_ecjpake_role role; |
| 8684 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 8685 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8686 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8687 | |
| 8688 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 8689 | role = MBEDTLS_ECJPAKE_SERVER; |
| 8690 | else |
| 8691 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 8692 | |
| 8693 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 8694 | role, |
| 8695 | MBEDTLS_MD_SHA256, |
| 8696 | MBEDTLS_ECP_DP_SECP256R1, |
| 8697 | pw, pw_len ) ); |
| 8698 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 8699 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 8700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8701 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8702 | |
| 8703 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 8704 | { |
| 8705 | /* Remove reference to existing PSK, if any. */ |
| 8706 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8707 | if( conf->psk_opaque != 0 ) |
| 8708 | { |
| 8709 | /* The maintenance of the PSK key slot is the |
| 8710 | * user's responsibility. */ |
| 8711 | conf->psk_opaque = 0; |
| 8712 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 8713 | /* This and the following branch should never |
| 8714 | * be taken simultaenously as we maintain the |
| 8715 | * invariant that raw and opaque PSKs are never |
| 8716 | * configured simultaneously. As a safeguard, |
| 8717 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8718 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8719 | if( conf->psk != NULL ) |
| 8720 | { |
| 8721 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 8722 | |
| 8723 | mbedtls_free( conf->psk ); |
| 8724 | conf->psk = NULL; |
| 8725 | conf->psk_len = 0; |
| 8726 | } |
| 8727 | |
| 8728 | /* Remove reference to PSK identity, if any. */ |
| 8729 | if( conf->psk_identity != NULL ) |
| 8730 | { |
| 8731 | mbedtls_free( conf->psk_identity ); |
| 8732 | conf->psk_identity = NULL; |
| 8733 | conf->psk_identity_len = 0; |
| 8734 | } |
| 8735 | } |
| 8736 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8737 | /* This function assumes that PSK identity in the SSL config is unset. |
| 8738 | * It checks that the provided identity is well-formed and attempts |
| 8739 | * to make a copy of it in the SSL config. |
| 8740 | * On failure, the PSK identity in the config remains unset. */ |
| 8741 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 8742 | unsigned char const *psk_identity, |
| 8743 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8744 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8745 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8746 | if( psk_identity == NULL || |
| 8747 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 8748 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 8749 | { |
| 8750 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8751 | } |
| 8752 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8753 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 8754 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8755 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8756 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8757 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 8758 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 8759 | |
| 8760 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8761 | } |
| 8762 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8763 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 8764 | const unsigned char *psk, size_t psk_len, |
| 8765 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 8766 | { |
| 8767 | int ret; |
| 8768 | /* Remove opaque/raw PSK + PSK Identity */ |
| 8769 | ssl_conf_remove_psk( conf ); |
| 8770 | |
| 8771 | /* Check and set raw PSK */ |
| 8772 | if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8773 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8774 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 8775 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8776 | conf->psk_len = psk_len; |
| 8777 | memcpy( conf->psk, psk, conf->psk_len ); |
| 8778 | |
| 8779 | /* Check and set PSK Identity */ |
| 8780 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 8781 | if( ret != 0 ) |
| 8782 | ssl_conf_remove_psk( conf ); |
| 8783 | |
| 8784 | return( ret ); |
| 8785 | } |
| 8786 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8787 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 8788 | { |
| 8789 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8790 | if( ssl->handshake->psk_opaque != 0 ) |
| 8791 | { |
| 8792 | ssl->handshake->psk_opaque = 0; |
| 8793 | } |
| 8794 | else |
| 8795 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8796 | if( ssl->handshake->psk != NULL ) |
| 8797 | { |
| 8798 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 8799 | ssl->handshake->psk_len ); |
| 8800 | mbedtls_free( ssl->handshake->psk ); |
| 8801 | ssl->handshake->psk_len = 0; |
| 8802 | } |
| 8803 | } |
| 8804 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8805 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 8806 | const unsigned char *psk, size_t psk_len ) |
| 8807 | { |
| 8808 | if( psk == NULL || ssl->handshake == NULL ) |
| 8809 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8810 | |
| 8811 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 8812 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8813 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8814 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8815 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 8816 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 8817 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 8818 | |
| 8819 | ssl->handshake->psk_len = psk_len; |
| 8820 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 8821 | |
| 8822 | return( 0 ); |
| 8823 | } |
| 8824 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8825 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 8826 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 8827 | psa_key_handle_t psk_slot, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8828 | const unsigned char *psk_identity, |
| 8829 | size_t psk_identity_len ) |
| 8830 | { |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8831 | int ret; |
| 8832 | /* Clear opaque/raw PSK + PSK Identity, if present. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8833 | ssl_conf_remove_psk( conf ); |
| 8834 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8835 | /* Check and set opaque PSK */ |
| 8836 | if( psk_slot == 0 ) |
| 8837 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8838 | conf->psk_opaque = psk_slot; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 8839 | |
| 8840 | /* Check and set PSK Identity */ |
| 8841 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 8842 | psk_identity_len ); |
| 8843 | if( ret != 0 ) |
| 8844 | ssl_conf_remove_psk( conf ); |
| 8845 | |
| 8846 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8847 | } |
| 8848 | |
| 8849 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 8850 | psa_key_handle_t psk_slot ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 8851 | { |
| 8852 | if( psk_slot == 0 || ssl->handshake == NULL ) |
| 8853 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8854 | |
| 8855 | ssl_remove_psk( ssl ); |
| 8856 | ssl->handshake->psk_opaque = psk_slot; |
| 8857 | return( 0 ); |
| 8858 | } |
| 8859 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 8860 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8861 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8862 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 8863 | size_t), |
| 8864 | void *p_psk ) |
| 8865 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8866 | conf->f_psk = f_psk; |
| 8867 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 8868 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8869 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 8870 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8871 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8872 | |
| 8873 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8874 | 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] | 8875 | { |
| 8876 | int ret; |
| 8877 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8878 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 8879 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 8880 | { |
| 8881 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8882 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8883 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8884 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8885 | |
| 8886 | return( 0 ); |
| 8887 | } |
Hanno Becker | 470a8c4 | 2017-10-04 15:28:46 +0100 | [diff] [blame] | 8888 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8889 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 8890 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 8891 | const unsigned char *dhm_P, size_t P_len, |
| 8892 | const unsigned char *dhm_G, size_t G_len ) |
| 8893 | { |
| 8894 | int ret; |
| 8895 | |
| 8896 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 8897 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 8898 | { |
| 8899 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8900 | mbedtls_mpi_free( &conf->dhm_G ); |
| 8901 | return( ret ); |
| 8902 | } |
| 8903 | |
| 8904 | return( 0 ); |
| 8905 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8906 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8907 | 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] | 8908 | { |
| 8909 | int ret; |
| 8910 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8911 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 8912 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 8913 | { |
| 8914 | mbedtls_mpi_free( &conf->dhm_P ); |
| 8915 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8916 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 8917 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8918 | |
| 8919 | return( 0 ); |
| 8920 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 8921 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 8922 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 8923 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 8924 | /* |
| 8925 | * Set the minimum length for Diffie-Hellman parameters |
| 8926 | */ |
| 8927 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 8928 | unsigned int bitlen ) |
| 8929 | { |
| 8930 | conf->dhm_min_bitlen = bitlen; |
| 8931 | } |
| 8932 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 8933 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 8934 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8935 | /* |
| 8936 | * Set allowed/preferred hashes for handshake signatures |
| 8937 | */ |
| 8938 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 8939 | const int *hashes ) |
| 8940 | { |
| 8941 | conf->sig_hashes = hashes; |
| 8942 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8943 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 8944 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 8945 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8946 | /* |
| 8947 | * Set the allowed elliptic curves |
| 8948 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 8949 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8950 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8951 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 8952 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8953 | } |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8954 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 8955 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 8956 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8957 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8958 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8959 | /* Initialize to suppress unnecessary compiler warning */ |
| 8960 | size_t hostname_len = 0; |
| 8961 | |
| 8962 | /* Check if new hostname is valid before |
| 8963 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8964 | if( hostname != NULL ) |
| 8965 | { |
| 8966 | hostname_len = strlen( hostname ); |
| 8967 | |
| 8968 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 8969 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8970 | } |
| 8971 | |
| 8972 | /* Now it's clear that we will overwrite the old hostname, |
| 8973 | * so we can free it safely */ |
| 8974 | |
| 8975 | if( ssl->hostname != NULL ) |
| 8976 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 8977 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8978 | mbedtls_free( ssl->hostname ); |
| 8979 | } |
| 8980 | |
| 8981 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 8982 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8983 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8984 | { |
| 8985 | ssl->hostname = NULL; |
| 8986 | } |
| 8987 | else |
| 8988 | { |
| 8989 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8990 | if( ssl->hostname == NULL ) |
| 8991 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 8992 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8993 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 8994 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 8995 | ssl->hostname[hostname_len] = '\0'; |
| 8996 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8997 | |
| 8998 | return( 0 ); |
| 8999 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 9000 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9001 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 9002 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9003 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9004 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9005 | const unsigned char *, size_t), |
| 9006 | void *p_sni ) |
| 9007 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9008 | conf->f_sni = f_sni; |
| 9009 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9010 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9011 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 9012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9013 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9014 | 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] | 9015 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9016 | size_t cur_len, tot_len; |
| 9017 | const char **p; |
| 9018 | |
| 9019 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 9020 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 9021 | * MUST NOT be truncated." |
| 9022 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9023 | */ |
| 9024 | tot_len = 0; |
| 9025 | for( p = protos; *p != NULL; p++ ) |
| 9026 | { |
| 9027 | cur_len = strlen( *p ); |
| 9028 | tot_len += cur_len; |
| 9029 | |
| 9030 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9031 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9032 | } |
| 9033 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9034 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 9035 | |
| 9036 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9037 | } |
| 9038 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9039 | 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] | 9040 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9041 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9042 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9043 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 9044 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9045 | 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] | 9046 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9047 | conf->max_major_ver = major; |
| 9048 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 9049 | } |
| 9050 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9051 | 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] | 9052 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9053 | conf->min_major_ver = major; |
| 9054 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 9055 | } |
| 9056 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9057 | #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] | 9058 | 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] | 9059 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 9060 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 9061 | } |
| 9062 | #endif |
| 9063 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 9064 | #if defined(MBEDTLS_SSL_SRV_C) |
| 9065 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 9066 | char cert_req_ca_list ) |
| 9067 | { |
| 9068 | conf->cert_req_ca_list = cert_req_ca_list; |
| 9069 | } |
| 9070 | #endif |
| 9071 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9072 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9073 | 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] | 9074 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9075 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 9076 | } |
| 9077 | #endif |
| 9078 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9079 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9080 | 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] | 9081 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9082 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 9083 | } |
| 9084 | #endif |
| 9085 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9086 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9087 | 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] | 9088 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9089 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9090 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 9091 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 9092 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9093 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9094 | 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] | 9095 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9096 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9097 | 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] | 9098 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9099 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9100 | } |
| 9101 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 9102 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9103 | |
| 9104 | return( 0 ); |
| 9105 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9106 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 9107 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9108 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 9109 | 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] | 9110 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9111 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9112 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9113 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 9114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9115 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9116 | 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] | 9117 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 9118 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 9119 | } |
| 9120 | #endif |
| 9121 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9122 | 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] | 9123 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9124 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9125 | } |
| 9126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9127 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9128 | 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] | 9129 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9130 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 9131 | } |
| 9132 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9133 | 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] | 9134 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9135 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9136 | } |
| 9137 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 9138 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9139 | const unsigned char period[8] ) |
| 9140 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 9141 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 9142 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9143 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9145 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9146 | #if defined(MBEDTLS_SSL_CLI_C) |
| 9147 | 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] | 9148 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 9149 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9150 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9151 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9152 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9153 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9154 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 9155 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 9156 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 9157 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9158 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 9159 | conf->f_ticket_write = f_ticket_write; |
| 9160 | conf->f_ticket_parse = f_ticket_parse; |
| 9161 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 9162 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 9163 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9164 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 9165 | |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9166 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 9167 | void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf, |
| 9168 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 9169 | void *p_export_keys ) |
| 9170 | { |
| 9171 | conf->f_export_keys = f_export_keys; |
| 9172 | conf->p_export_keys = p_export_keys; |
| 9173 | } |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 9174 | |
| 9175 | void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf, |
| 9176 | mbedtls_ssl_export_keys_ext_t *f_export_keys_ext, |
| 9177 | void *p_export_keys ) |
| 9178 | { |
| 9179 | conf->f_export_keys_ext = f_export_keys_ext; |
| 9180 | conf->p_export_keys = p_export_keys; |
| 9181 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 9182 | #endif |
| 9183 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9184 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9185 | void mbedtls_ssl_conf_async_private_cb( |
| 9186 | mbedtls_ssl_config *conf, |
| 9187 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 9188 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 9189 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 9190 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9191 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9192 | { |
| 9193 | conf->f_async_sign_start = f_async_sign; |
| 9194 | conf->f_async_decrypt_start = f_async_decrypt; |
| 9195 | conf->f_async_resume = f_async_resume; |
| 9196 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9197 | conf->p_async_config_data = async_config_data; |
| 9198 | } |
| 9199 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 9200 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 9201 | { |
| 9202 | return( conf->p_async_config_data ); |
| 9203 | } |
| 9204 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9205 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9206 | { |
| 9207 | if( ssl->handshake == NULL ) |
| 9208 | return( NULL ); |
| 9209 | else |
| 9210 | return( ssl->handshake->user_async_ctx ); |
| 9211 | } |
| 9212 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 9213 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 9214 | void *ctx ) |
| 9215 | { |
| 9216 | if( ssl->handshake != NULL ) |
| 9217 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9218 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 9219 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 9220 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9221 | /* |
| 9222 | * SSL get accessors |
| 9223 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9224 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9225 | { |
| 9226 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 9227 | } |
| 9228 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9229 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 9230 | { |
| 9231 | /* |
| 9232 | * Case A: We're currently holding back |
| 9233 | * a message for further processing. |
| 9234 | */ |
| 9235 | |
| 9236 | if( ssl->keep_current_message == 1 ) |
| 9237 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9238 | 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] | 9239 | return( 1 ); |
| 9240 | } |
| 9241 | |
| 9242 | /* |
| 9243 | * Case B: Further records are pending in the current datagram. |
| 9244 | */ |
| 9245 | |
| 9246 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9247 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 9248 | ssl->in_left > ssl->next_record_offset ) |
| 9249 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9250 | 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] | 9251 | return( 1 ); |
| 9252 | } |
| 9253 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9254 | |
| 9255 | /* |
| 9256 | * Case C: A handshake message is being processed. |
| 9257 | */ |
| 9258 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9259 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 9260 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9261 | 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] | 9262 | return( 1 ); |
| 9263 | } |
| 9264 | |
| 9265 | /* |
| 9266 | * Case D: An application data message is being processed |
| 9267 | */ |
| 9268 | if( ssl->in_offt != NULL ) |
| 9269 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 9270 | 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] | 9271 | return( 1 ); |
| 9272 | } |
| 9273 | |
| 9274 | /* |
| 9275 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 9276 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 9277 | * we implement support for multiple alerts in single records. |
| 9278 | */ |
| 9279 | |
| 9280 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 9281 | return( 0 ); |
| 9282 | } |
| 9283 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 9284 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9285 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 9286 | if( ssl->session != NULL ) |
| 9287 | return( ssl->session->verify_result ); |
| 9288 | |
| 9289 | if( ssl->session_negotiate != NULL ) |
| 9290 | return( ssl->session_negotiate->verify_result ); |
| 9291 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 9292 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9293 | } |
| 9294 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9295 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9296 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9297 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9298 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 9299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9300 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 9301 | } |
| 9302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9303 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9304 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9305 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9306 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9307 | { |
| 9308 | switch( ssl->minor_ver ) |
| 9309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9310 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9311 | return( "DTLSv1.0" ); |
| 9312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9313 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9314 | return( "DTLSv1.2" ); |
| 9315 | |
| 9316 | default: |
| 9317 | return( "unknown (DTLS)" ); |
| 9318 | } |
| 9319 | } |
| 9320 | #endif |
| 9321 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9322 | switch( ssl->minor_ver ) |
| 9323 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9324 | case MBEDTLS_SSL_MINOR_VERSION_0: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9325 | return( "SSLv3.0" ); |
| 9326 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9327 | case MBEDTLS_SSL_MINOR_VERSION_1: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9328 | return( "TLSv1.0" ); |
| 9329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9330 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9331 | return( "TLSv1.1" ); |
| 9332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9333 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 9334 | return( "TLSv1.2" ); |
| 9335 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9336 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 9337 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9338 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 9339 | } |
| 9340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9341 | 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] | 9342 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9343 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9344 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9345 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9346 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9347 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 9348 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9349 | if( transform == NULL ) |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9350 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 9351 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9352 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 9353 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 9354 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9355 | #endif |
| 9356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9357 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9358 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9359 | case MBEDTLS_MODE_GCM: |
| 9360 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9361 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9362 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9363 | transform_expansion = transform->minlen; |
| 9364 | break; |
| 9365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9366 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9367 | |
| 9368 | block_size = mbedtls_cipher_get_block_size( |
| 9369 | &transform->cipher_ctx_enc ); |
| 9370 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9371 | /* Expansion due to the addition of the MAC. */ |
| 9372 | transform_expansion += transform->maclen; |
| 9373 | |
| 9374 | /* Expansion due to the addition of CBC padding; |
| 9375 | * Theoretically up to 256 bytes, but we never use |
| 9376 | * more than the block size of the underlying cipher. */ |
| 9377 | transform_expansion += block_size; |
| 9378 | |
| 9379 | /* For TLS 1.1 or higher, an explicit IV is added |
| 9380 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9381 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9382 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9383 | transform_expansion += block_size; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 9384 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 9385 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9386 | break; |
| 9387 | |
| 9388 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9389 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9390 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9391 | } |
| 9392 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9393 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9394 | if( transform->out_cid_len != 0 ) |
| 9395 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 9396 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 9397 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 9398 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 9399 | } |
| 9400 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9401 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9402 | size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 9403 | { |
| 9404 | size_t max_len; |
| 9405 | |
| 9406 | /* |
| 9407 | * Assume mfl_code is correct since it was checked when set |
| 9408 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9409 | 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] | 9410 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9411 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9412 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9413 | 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] | 9414 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 9415 | 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] | 9416 | } |
| 9417 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 9418 | /* During a handshake, use the value being negotiated */ |
| 9419 | if( ssl->session_negotiate != NULL && |
| 9420 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 9421 | { |
| 9422 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 9423 | } |
| 9424 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9425 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 9426 | } |
| 9427 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 9428 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9429 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9430 | static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
| 9431 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 9432 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 9433 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 9434 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 9435 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 9436 | return ( 0 ); |
| 9437 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9438 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 9439 | return( ssl->mtu ); |
| 9440 | |
| 9441 | if( ssl->mtu == 0 ) |
| 9442 | return( ssl->handshake->mtu ); |
| 9443 | |
| 9444 | return( ssl->mtu < ssl->handshake->mtu ? |
| 9445 | ssl->mtu : ssl->handshake->mtu ); |
| 9446 | } |
| 9447 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 9448 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9449 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 9450 | { |
| 9451 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 9452 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 9453 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9454 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9455 | (void) ssl; |
| 9456 | #endif |
| 9457 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9458 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 9459 | const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl ); |
| 9460 | |
| 9461 | if( max_len > mfl ) |
| 9462 | max_len = mfl; |
| 9463 | #endif |
| 9464 | |
| 9465 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9466 | if( ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9467 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9468 | const size_t mtu = ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9469 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 9470 | const size_t overhead = (size_t) ret; |
| 9471 | |
| 9472 | if( ret < 0 ) |
| 9473 | return( ret ); |
| 9474 | |
| 9475 | if( mtu <= overhead ) |
| 9476 | { |
| 9477 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 9478 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 9479 | } |
| 9480 | |
| 9481 | if( max_len > mtu - overhead ) |
| 9482 | max_len = mtu - overhead; |
| 9483 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 9484 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9485 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 9486 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 9487 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9488 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 9489 | #endif |
| 9490 | |
| 9491 | return( (int) max_len ); |
| 9492 | } |
| 9493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9494 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 9495 | 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] | 9496 | { |
| 9497 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9498 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9499 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9500 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 9501 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 9502 | #else |
| 9503 | return( NULL ); |
| 9504 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9505 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9506 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 9507 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9508 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 9509 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 9510 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9511 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9512 | if( ssl == NULL || |
| 9513 | dst == NULL || |
| 9514 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9515 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9516 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9517 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9518 | } |
| 9519 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 9520 | return( mbedtls_ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9521 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9522 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 9523 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9524 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9525 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9526 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9527 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9528 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9529 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9530 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9531 | if( ssl == NULL || ssl->conf == NULL ) |
| 9532 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9534 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9535 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9536 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9537 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9538 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9539 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9540 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9541 | #endif |
| 9542 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9543 | return( ret ); |
| 9544 | } |
| 9545 | |
| 9546 | /* |
| 9547 | * Perform the SSL handshake |
| 9548 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9549 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9550 | { |
| 9551 | int ret = 0; |
| 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, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9558 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9560 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 9561 | |
| 9562 | if( ret != 0 ) |
| 9563 | break; |
| 9564 | } |
| 9565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9566 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9567 | |
| 9568 | return( ret ); |
| 9569 | } |
| 9570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9571 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 9572 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9573 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9574 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9575 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9576 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9577 | { |
| 9578 | int ret; |
| 9579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9580 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9581 | |
| 9582 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9583 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 9584 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9585 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 9586 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9587 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 9588 | 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] | 9589 | return( ret ); |
| 9590 | } |
| 9591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9592 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9593 | |
| 9594 | return( 0 ); |
| 9595 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9596 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9597 | |
| 9598 | /* |
| 9599 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9600 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 9601 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 9602 | * - 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] | 9603 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9604 | * 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] | 9605 | * 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] | 9606 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9607 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9608 | { |
| 9609 | int ret; |
| 9610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9611 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9612 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9613 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 9614 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9615 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9616 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 9617 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9618 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9619 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9620 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9621 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9622 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 9623 | ssl->handshake->out_msg_seq = 1; |
| 9624 | else |
| 9625 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 9626 | } |
| 9627 | #endif |
| 9628 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9629 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 9630 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9631 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9632 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9633 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9634 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9635 | return( ret ); |
| 9636 | } |
| 9637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9638 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9639 | |
| 9640 | return( 0 ); |
| 9641 | } |
| 9642 | |
| 9643 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9644 | * Renegotiate current connection on client, |
| 9645 | * or request renegotiation on server |
| 9646 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9647 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9649 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9650 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9651 | if( ssl == NULL || ssl->conf == NULL ) |
| 9652 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9654 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9655 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9656 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9657 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9658 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 9659 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9661 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 9662 | |
| 9663 | /* Did we already try/start sending HelloRequest? */ |
| 9664 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9665 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 9666 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9667 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9668 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9669 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9671 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9672 | /* |
| 9673 | * On client, either start the renegotiation process or, |
| 9674 | * if already in progress, continue the handshake |
| 9675 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9676 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9677 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9678 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 9679 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9680 | |
| 9681 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 9682 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9683 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9684 | return( ret ); |
| 9685 | } |
| 9686 | } |
| 9687 | else |
| 9688 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9689 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9690 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9691 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9692 | return( ret ); |
| 9693 | } |
| 9694 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9695 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 9696 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 9697 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 9698 | } |
| 9699 | |
| 9700 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9701 | * Check record counters and renegotiate if they're above the limit. |
| 9702 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9703 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9704 | { |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9705 | size_t ep_len = ssl_ep_len( ssl ); |
| 9706 | int in_ctr_cmp; |
| 9707 | int out_ctr_cmp; |
| 9708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9709 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 9710 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9711 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9712 | { |
| 9713 | return( 0 ); |
| 9714 | } |
| 9715 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9716 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 9717 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 9718 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 9719 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 9720 | |
| 9721 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9722 | { |
| 9723 | return( 0 ); |
| 9724 | } |
| 9725 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 9726 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9727 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9728 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9729 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9730 | |
| 9731 | /* |
| 9732 | * Receive application data decrypted from the SSL layer |
| 9733 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9734 | 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] | 9735 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9736 | int ret; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 9737 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9738 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 9739 | if( ssl == NULL || ssl->conf == NULL ) |
| 9740 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 9741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9742 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9743 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9744 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9745 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9746 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9747 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9748 | return( ret ); |
| 9749 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9750 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9751 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9752 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 9753 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9754 | return( ret ); |
| 9755 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 9756 | } |
| 9757 | #endif |
| 9758 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9759 | /* |
| 9760 | * Check if renegotiation is necessary and/or handshake is |
| 9761 | * in process. If yes, perform/continue, and fall through |
| 9762 | * if an unexpected packet is received while the client |
| 9763 | * is waiting for the ServerHello. |
| 9764 | * |
| 9765 | * (There is no equivalent to the last condition on |
| 9766 | * the server-side as it is not treated as within |
| 9767 | * a handshake while waiting for the ClientHello |
| 9768 | * after a renegotiation request.) |
| 9769 | */ |
| 9770 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9771 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9772 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 9773 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 9774 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9775 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9776 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 9777 | return( ret ); |
| 9778 | } |
| 9779 | #endif |
| 9780 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9781 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9782 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9783 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9784 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 9785 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9786 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9787 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9788 | return( ret ); |
| 9789 | } |
| 9790 | } |
| 9791 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 9792 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 9793 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9794 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 9795 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 9796 | if( ssl->f_get_timer != NULL && |
| 9797 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 9798 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9799 | ssl_set_timer( ssl, ssl->conf->read_timeout ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 9800 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 9801 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 9802 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9803 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9804 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 9805 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 9806 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9807 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 9808 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9809 | } |
| 9810 | |
| 9811 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9812 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9813 | { |
| 9814 | /* |
| 9815 | * OpenSSL sends empty messages to randomize the IV |
| 9816 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 9817 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9818 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9819 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 9820 | return( 0 ); |
| 9821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9822 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9823 | return( ret ); |
| 9824 | } |
| 9825 | } |
| 9826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9827 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9828 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9829 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9830 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9831 | /* |
| 9832 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 9833 | * - For server-side, expect CLIENT_HELLO. |
| 9834 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 9835 | */ |
| 9836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9837 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9838 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9839 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9840 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9841 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9842 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9843 | |
| 9844 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9845 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9846 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 9847 | { |
| 9848 | continue; |
| 9849 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9850 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9851 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9852 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9853 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9854 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9855 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9856 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9857 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9858 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9859 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9860 | |
| 9861 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9862 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9863 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 9864 | { |
| 9865 | continue; |
| 9866 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 9867 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9868 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9869 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9870 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 9871 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 9872 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9873 | /* Determine whether renegotiation attempt should be accepted */ |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 9874 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 9875 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 9876 | ssl->conf->allow_legacy_renegotiation == |
| 9877 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 9878 | { |
| 9879 | /* |
| 9880 | * Accept renegotiation request |
| 9881 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9882 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 9883 | /* DTLS clients need to know renego is server-initiated */ |
| 9884 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 9885 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 9886 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 9887 | { |
| 9888 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 9889 | } |
| 9890 | #endif |
| 9891 | ret = ssl_start_renegotiation( ssl ); |
| 9892 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 9893 | ret != 0 ) |
| 9894 | { |
| 9895 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
| 9896 | return( ret ); |
| 9897 | } |
| 9898 | } |
| 9899 | else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 9900 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9901 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 9902 | /* |
| 9903 | * Refuse renegotiation |
| 9904 | */ |
| 9905 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9906 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9908 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 9909 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9910 | { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 9911 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 9912 | we send a fatal alert and abort the connection. */ |
| 9913 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 9914 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 9915 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 9916 | } |
| 9917 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9918 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 9919 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 9920 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 9921 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 9922 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9923 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 9924 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 9925 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 9926 | { |
| 9927 | return( ret ); |
| 9928 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9929 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 9930 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9931 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 9932 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 9933 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 9935 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 9936 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9937 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 9938 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 9939 | /* At this point, we don't know whether the renegotiation has been |
| 9940 | * completed or not. The cases to consider are the following: |
| 9941 | * 1) The renegotiation is complete. In this case, no new record |
| 9942 | * has been read yet. |
| 9943 | * 2) The renegotiation is incomplete because the client received |
| 9944 | * an application data record while awaiting the ServerHello. |
| 9945 | * 3) The renegotiation is incomplete because the client received |
| 9946 | * a non-handshake, non-application data message while awaiting |
| 9947 | * the ServerHello. |
| 9948 | * In each of these case, looping will be the proper action: |
| 9949 | * - For 1), the next iteration will read a new record and check |
| 9950 | * if it's application data. |
| 9951 | * - For 2), the loop condition isn't satisfied as application data |
| 9952 | * is present, hence continue is the same as break |
| 9953 | * - For 3), the loop condition is satisfied and read_record |
| 9954 | * will re-deliver the message that was held back by the client |
| 9955 | * when expecting the ServerHello. |
| 9956 | */ |
| 9957 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 9958 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 9959 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9960 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 9961 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9962 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9963 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9964 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 9965 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9966 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 9967 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9968 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 9969 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 9970 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 9971 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9972 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 9973 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9974 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 9975 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 9976 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9977 | 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] | 9978 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 9979 | } |
| 9980 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9981 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9982 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9983 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 9984 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9985 | } |
| 9986 | |
| 9987 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 9988 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 9989 | /* We're going to return something now, cancel timer, |
| 9990 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9991 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 9992 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9993 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 9994 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 9995 | /* If we requested renego but received AppData, resend HelloRequest. |
| 9996 | * Do it now, after setting in_offt, to avoid taking this branch |
| 9997 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9998 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 9999 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10000 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10001 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 10002 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10003 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10004 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 10005 | return( ret ); |
| 10006 | } |
| 10007 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10008 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10009 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10010 | } |
| 10011 | |
| 10012 | n = ( len < ssl->in_msglen ) |
| 10013 | ? len : ssl->in_msglen; |
| 10014 | |
| 10015 | memcpy( buf, ssl->in_offt, n ); |
| 10016 | ssl->in_msglen -= n; |
| 10017 | |
| 10018 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10019 | { |
| 10020 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10021 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 10022 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10023 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10024 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10025 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10026 | /* more data available */ |
| 10027 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 10028 | } |
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 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10031 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 10032 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10033 | } |
| 10034 | |
| 10035 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10036 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 10037 | * fragment length and buffer size. |
| 10038 | * |
| 10039 | * According to RFC 5246 Section 6.2.1: |
| 10040 | * |
| 10041 | * Zero-length fragments of Application data MAY be sent as they are |
| 10042 | * potentially useful as a traffic analysis countermeasure. |
| 10043 | * |
| 10044 | * Therefore, it is possible that the input message length is 0 and the |
| 10045 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10046 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10047 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10048 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10049 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 10050 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 10051 | const size_t max_len = (size_t) ret; |
| 10052 | |
| 10053 | if( ret < 0 ) |
| 10054 | { |
| 10055 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 10056 | return( ret ); |
| 10057 | } |
| 10058 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10059 | if( len > max_len ) |
| 10060 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10061 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10062 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10063 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10064 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10065 | "maximum fragment length: %d > %d", |
| 10066 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10067 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10068 | } |
| 10069 | else |
| 10070 | #endif |
| 10071 | len = max_len; |
| 10072 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10073 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10074 | if( ssl->out_left != 0 ) |
| 10075 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10076 | /* |
| 10077 | * The user has previously tried to send the data and |
| 10078 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 10079 | * written. In this case, we expect the high-level write function |
| 10080 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 10081 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10082 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10083 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10084 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10085 | return( ret ); |
| 10086 | } |
| 10087 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10088 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 10089 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 10090 | /* |
| 10091 | * The user is trying to send a message the first time, so we need to |
| 10092 | * copy the data into the internal buffers and setup the data structure |
| 10093 | * to keep track of partial writes |
| 10094 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10095 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10096 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10097 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10098 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 10099 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10100 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10101 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 10102 | return( ret ); |
| 10103 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10104 | } |
| 10105 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 10106 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10107 | } |
| 10108 | |
| 10109 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10110 | * Write application data, doing 1/n-1 splitting if necessary. |
| 10111 | * |
| 10112 | * 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] | 10113 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 10114 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10115 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10116 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10117 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10118 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10119 | { |
| 10120 | int ret; |
| 10121 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10122 | if( ssl->conf->cbc_record_splitting == |
| 10123 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 10124 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10125 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
| 10126 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 10127 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10128 | { |
| 10129 | return( ssl_write_real( ssl, buf, len ) ); |
| 10130 | } |
| 10131 | |
| 10132 | if( ssl->split_done == 0 ) |
| 10133 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10134 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10135 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10136 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10137 | } |
| 10138 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 10139 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 10140 | return( ret ); |
| 10141 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10142 | |
| 10143 | return( ret + 1 ); |
| 10144 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10145 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 10146 | |
| 10147 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10148 | * Write application data (public-facing wrapper) |
| 10149 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10150 | 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] | 10151 | { |
| 10152 | int ret; |
| 10153 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10154 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10155 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10156 | if( ssl == NULL || ssl->conf == NULL ) |
| 10157 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10158 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10159 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10160 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 10161 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10162 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10163 | return( ret ); |
| 10164 | } |
| 10165 | #endif |
| 10166 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10167 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10168 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10169 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10170 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 10171 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10172 | return( ret ); |
| 10173 | } |
| 10174 | } |
| 10175 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10176 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10177 | ret = ssl_write_split( ssl, buf, len ); |
| 10178 | #else |
| 10179 | ret = ssl_write_real( ssl, buf, len ); |
| 10180 | #endif |
| 10181 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 10182 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 10183 | |
| 10184 | return( ret ); |
| 10185 | } |
| 10186 | |
| 10187 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10188 | * Notify the peer that the connection is being closed |
| 10189 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10190 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10191 | { |
| 10192 | int ret; |
| 10193 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 10194 | if( ssl == NULL || ssl->conf == NULL ) |
| 10195 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 10196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10197 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10198 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10199 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10200 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10202 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10203 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10204 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 10205 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 10206 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10207 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10208 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10209 | return( ret ); |
| 10210 | } |
| 10211 | } |
| 10212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10213 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10214 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 10215 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10216 | } |
| 10217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10218 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10219 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10220 | if( transform == NULL ) |
| 10221 | return; |
| 10222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10223 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10224 | deflateEnd( &transform->ctx_deflate ); |
| 10225 | inflateEnd( &transform->ctx_inflate ); |
| 10226 | #endif |
| 10227 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10228 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 10229 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 10230 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10231 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10232 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 10233 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 10234 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10235 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10236 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10237 | } |
| 10238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10239 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10240 | 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] | 10241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10242 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10243 | |
| 10244 | while( cur != NULL ) |
| 10245 | { |
| 10246 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10247 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10248 | cur = next; |
| 10249 | } |
| 10250 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10251 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 10252 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10253 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10254 | |
| 10255 | static void ssl_buffering_free( mbedtls_ssl_context *ssl ) |
| 10256 | { |
| 10257 | unsigned offset; |
| 10258 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10259 | |
| 10260 | if( hs == NULL ) |
| 10261 | return; |
| 10262 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 10263 | ssl_free_buffered_record( ssl ); |
| 10264 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10265 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10266 | ssl_buffering_free_slot( ssl, offset ); |
| 10267 | } |
| 10268 | |
| 10269 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 10270 | uint8_t slot ) |
| 10271 | { |
| 10272 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 10273 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 10274 | |
| 10275 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 10276 | return; |
| 10277 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10278 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10279 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10280 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 10281 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 10282 | mbedtls_free( hs_buf->data ); |
| 10283 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10284 | } |
| 10285 | } |
| 10286 | |
| 10287 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 10288 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10289 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10290 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10291 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 10292 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10293 | if( handshake == NULL ) |
| 10294 | return; |
| 10295 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10296 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 10297 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 10298 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 10299 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 10300 | handshake->async_in_progress = 0; |
| 10301 | } |
| 10302 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 10303 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10304 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 10305 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 10306 | mbedtls_md5_free( &handshake->fin_md5 ); |
| 10307 | mbedtls_sha1_free( &handshake->fin_sha1 ); |
| 10308 | #endif |
| 10309 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 10310 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10311 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 10312 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 10313 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10314 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 10315 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10316 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10317 | #if defined(MBEDTLS_SHA512_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10318 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 10319 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10320 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10321 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 10322 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 10323 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 10324 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 10325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10326 | #if defined(MBEDTLS_DHM_C) |
| 10327 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10328 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10329 | #if defined(MBEDTLS_ECDH_C) |
| 10330 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10331 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 10332 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10333 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 10334 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10335 | mbedtls_free( handshake->ecjpake_cache ); |
| 10336 | handshake->ecjpake_cache = NULL; |
| 10337 | handshake->ecjpake_cache_len = 0; |
| 10338 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 10339 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 10340 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 10341 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 10342 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 10343 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10344 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 10345 | #endif |
| 10346 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10347 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10348 | if( handshake->psk != NULL ) |
| 10349 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10350 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 10351 | mbedtls_free( handshake->psk ); |
| 10352 | } |
| 10353 | #endif |
| 10354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10355 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10356 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10357 | /* |
| 10358 | * Free only the linked list wrapper, not the keys themselves |
| 10359 | * since the belong to the SNI callback |
| 10360 | */ |
| 10361 | if( handshake->sni_key_cert != NULL ) |
| 10362 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10363 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10364 | |
| 10365 | while( cur != NULL ) |
| 10366 | { |
| 10367 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10368 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 10369 | cur = next; |
| 10370 | } |
| 10371 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10372 | #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] | 10373 | |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10374 | #if defined(MBEDTLS_SSL__ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 10375 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 10376 | if( handshake->ecrs_peer_cert != NULL ) |
| 10377 | { |
| 10378 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 10379 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 10380 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 10381 | #endif |
| 10382 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 10383 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 10384 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 10385 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 10386 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 10387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10388 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10389 | mbedtls_free( handshake->verify_cookie ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 10390 | ssl_flight_free( handshake->flight ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 10391 | ssl_buffering_free( ssl ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 10392 | #endif |
| 10393 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 10394 | #if defined(MBEDTLS_ECDH_C) && \ |
| 10395 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 10396 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 10397 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 10398 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10399 | mbedtls_platform_zeroize( handshake, |
| 10400 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10401 | } |
| 10402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10403 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10404 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10405 | if( session == NULL ) |
| 10406 | return; |
| 10407 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10408 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 10409 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 10410 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 10411 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 10412 | #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] | 10413 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 10414 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 10415 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10416 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10417 | } |
| 10418 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10419 | /* |
| 10420 | * Free an SSL context |
| 10421 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10422 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10423 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 10424 | if( ssl == NULL ) |
| 10425 | return; |
| 10426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10427 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10428 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 10429 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10430 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10431 | 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] | 10432 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10433 | } |
| 10434 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 10435 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10436 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10437 | 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] | 10438 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10439 | } |
| 10440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10441 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 10442 | if( ssl->compress_buf != NULL ) |
| 10443 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 10444 | 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] | 10445 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 10446 | } |
| 10447 | #endif |
| 10448 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10449 | if( ssl->transform ) |
| 10450 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10451 | mbedtls_ssl_transform_free( ssl->transform ); |
| 10452 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10453 | } |
| 10454 | |
| 10455 | if( ssl->handshake ) |
| 10456 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 10457 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10458 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 10459 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10461 | mbedtls_free( ssl->handshake ); |
| 10462 | mbedtls_free( ssl->transform_negotiate ); |
| 10463 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 10464 | } |
| 10465 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 10466 | if( ssl->session ) |
| 10467 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10468 | mbedtls_ssl_session_free( ssl->session ); |
| 10469 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 10470 | } |
| 10471 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 10472 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 10473 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10474 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10475 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10476 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10477 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 10478 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10480 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 10481 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 10482 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10483 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 10484 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 10485 | } |
| 10486 | #endif |
| 10487 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 10488 | #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] | 10489 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 10490 | #endif |
| 10491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10492 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 10493 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 10494 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10495 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10496 | } |
| 10497 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10498 | /* |
| 10499 | * Initialze mbedtls_ssl_config |
| 10500 | */ |
| 10501 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 10502 | { |
| 10503 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 10504 | } |
| 10505 | |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 10506 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10507 | static int ssl_preset_default_hashes[] = { |
| 10508 | #if defined(MBEDTLS_SHA512_C) |
| 10509 | MBEDTLS_MD_SHA512, |
| 10510 | MBEDTLS_MD_SHA384, |
| 10511 | #endif |
| 10512 | #if defined(MBEDTLS_SHA256_C) |
| 10513 | MBEDTLS_MD_SHA256, |
| 10514 | MBEDTLS_MD_SHA224, |
| 10515 | #endif |
Gilles Peskine | 5d2511c | 2017-05-12 13:16:40 +0200 | [diff] [blame] | 10516 | #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] | 10517 | MBEDTLS_MD_SHA1, |
| 10518 | #endif |
| 10519 | MBEDTLS_MD_NONE |
| 10520 | }; |
Simon Butcher | c97b697 | 2015-12-27 23:48:17 +0000 | [diff] [blame] | 10521 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10522 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10523 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 10524 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 10525 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 10526 | 0 |
| 10527 | }; |
| 10528 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10529 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10530 | static int ssl_preset_suiteb_hashes[] = { |
| 10531 | MBEDTLS_MD_SHA256, |
| 10532 | MBEDTLS_MD_SHA384, |
| 10533 | MBEDTLS_MD_NONE |
| 10534 | }; |
| 10535 | #endif |
| 10536 | |
| 10537 | #if defined(MBEDTLS_ECP_C) |
| 10538 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
| 10539 | MBEDTLS_ECP_DP_SECP256R1, |
| 10540 | MBEDTLS_ECP_DP_SECP384R1, |
| 10541 | MBEDTLS_ECP_DP_NONE |
| 10542 | }; |
| 10543 | #endif |
| 10544 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10545 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 10546 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10547 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10548 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10549 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10550 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 10551 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10552 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 10553 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10554 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 10555 | /* Use the functions here so that they are covered in tests, |
| 10556 | * but otherwise access member directly for efficiency */ |
| 10557 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 10558 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10559 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10560 | /* |
| 10561 | * Things that are common to all presets |
| 10562 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 10563 | #if defined(MBEDTLS_SSL_CLI_C) |
| 10564 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 10565 | { |
| 10566 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 10567 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 10568 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 10569 | #endif |
| 10570 | } |
| 10571 | #endif |
| 10572 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 10573 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10574 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 10575 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10576 | |
| 10577 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 10578 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 10579 | #endif |
| 10580 | |
| 10581 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 10582 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 10583 | #endif |
| 10584 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 10585 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 10586 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 10587 | #endif |
| 10588 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 10589 | #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] | 10590 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 10591 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 10592 | #endif |
| 10593 | |
| 10594 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 10595 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 10596 | #endif |
| 10597 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 10598 | #if defined(MBEDTLS_SSL_SRV_C) |
| 10599 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
| 10600 | #endif |
| 10601 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10602 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10603 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 10604 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 10605 | #endif |
| 10606 | |
| 10607 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 10608 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 10609 | memset( conf->renego_period, 0x00, 2 ); |
| 10610 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10611 | #endif |
| 10612 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10613 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 10614 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 10615 | { |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 10616 | const unsigned char dhm_p[] = |
| 10617 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 10618 | const unsigned char dhm_g[] = |
| 10619 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
| 10620 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 10621 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 10622 | dhm_p, sizeof( dhm_p ), |
| 10623 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10624 | { |
| 10625 | return( ret ); |
| 10626 | } |
| 10627 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 10628 | #endif |
| 10629 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10630 | /* |
| 10631 | * Preset-specific defaults |
| 10632 | */ |
| 10633 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10634 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10635 | /* |
| 10636 | * NSA Suite B |
| 10637 | */ |
| 10638 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 10639 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 10640 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 10641 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 10642 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 10643 | |
| 10644 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 10645 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 10646 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 10647 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 10648 | ssl_preset_suiteb_ciphersuites; |
| 10649 | |
| 10650 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10651 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10652 | #endif |
| 10653 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10654 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10655 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 10656 | #endif |
| 10657 | |
| 10658 | #if defined(MBEDTLS_ECP_C) |
| 10659 | conf->curve_list = ssl_preset_suiteb_curves; |
| 10660 | #endif |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 10661 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10662 | |
| 10663 | /* |
| 10664 | * Default |
| 10665 | */ |
| 10666 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 10667 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 10668 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 10669 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 10670 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 10671 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 10672 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 10673 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 10674 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10675 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 10676 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 10677 | |
| 10678 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 10679 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 10680 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 10681 | #endif |
| 10682 | |
| 10683 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 10684 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 10685 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 10686 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 10687 | mbedtls_ssl_list_ciphersuites(); |
| 10688 | |
| 10689 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10690 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 10691 | #endif |
| 10692 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10693 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 10694 | conf->sig_hashes = ssl_preset_default_hashes; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 10695 | #endif |
| 10696 | |
| 10697 | #if defined(MBEDTLS_ECP_C) |
| 10698 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 10699 | #endif |
| 10700 | |
| 10701 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 10702 | conf->dhm_min_bitlen = 1024; |
| 10703 | #endif |
| 10704 | } |
| 10705 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10706 | return( 0 ); |
| 10707 | } |
| 10708 | |
| 10709 | /* |
| 10710 | * Free mbedtls_ssl_config |
| 10711 | */ |
| 10712 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 10713 | { |
| 10714 | #if defined(MBEDTLS_DHM_C) |
| 10715 | mbedtls_mpi_free( &conf->dhm_P ); |
| 10716 | mbedtls_mpi_free( &conf->dhm_G ); |
| 10717 | #endif |
| 10718 | |
| 10719 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 10720 | if( conf->psk != NULL ) |
| 10721 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10722 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10723 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 10724 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10725 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 10726 | } |
| 10727 | |
| 10728 | if( conf->psk_identity != NULL ) |
| 10729 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10730 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 10731 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 10732 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10733 | conf->psk_identity_len = 0; |
| 10734 | } |
| 10735 | #endif |
| 10736 | |
| 10737 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10738 | ssl_key_cert_free( conf->key_cert ); |
| 10739 | #endif |
| 10740 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 10741 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 10742 | } |
| 10743 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 10744 | #if defined(MBEDTLS_PK_C) && \ |
| 10745 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10746 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10747 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10748 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10749 | 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] | 10750 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10751 | #if defined(MBEDTLS_RSA_C) |
| 10752 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 10753 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10754 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10755 | #if defined(MBEDTLS_ECDSA_C) |
| 10756 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 10757 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10758 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10759 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 10760 | } |
| 10761 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 10762 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 10763 | { |
| 10764 | switch( type ) { |
| 10765 | case MBEDTLS_PK_RSA: |
| 10766 | return( MBEDTLS_SSL_SIG_RSA ); |
| 10767 | case MBEDTLS_PK_ECDSA: |
| 10768 | case MBEDTLS_PK_ECKEY: |
| 10769 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 10770 | default: |
| 10771 | return( MBEDTLS_SSL_SIG_ANON ); |
| 10772 | } |
| 10773 | } |
| 10774 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10775 | 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] | 10776 | { |
| 10777 | switch( sig ) |
| 10778 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10779 | #if defined(MBEDTLS_RSA_C) |
| 10780 | case MBEDTLS_SSL_SIG_RSA: |
| 10781 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10782 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10783 | #if defined(MBEDTLS_ECDSA_C) |
| 10784 | case MBEDTLS_SSL_SIG_ECDSA: |
| 10785 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10786 | #endif |
| 10787 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10788 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10789 | } |
| 10790 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 10791 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10792 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 10793 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 10794 | defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
| 10795 | |
| 10796 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 10797 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 10798 | mbedtls_pk_type_t sig_alg ) |
| 10799 | { |
| 10800 | switch( sig_alg ) |
| 10801 | { |
| 10802 | case MBEDTLS_PK_RSA: |
| 10803 | return( set->rsa ); |
| 10804 | case MBEDTLS_PK_ECDSA: |
| 10805 | return( set->ecdsa ); |
| 10806 | default: |
| 10807 | return( MBEDTLS_MD_NONE ); |
| 10808 | } |
| 10809 | } |
| 10810 | |
| 10811 | /* Add a signature-hash-pair to a signature-hash set */ |
| 10812 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 10813 | mbedtls_pk_type_t sig_alg, |
| 10814 | mbedtls_md_type_t md_alg ) |
| 10815 | { |
| 10816 | switch( sig_alg ) |
| 10817 | { |
| 10818 | case MBEDTLS_PK_RSA: |
| 10819 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 10820 | set->rsa = md_alg; |
| 10821 | break; |
| 10822 | |
| 10823 | case MBEDTLS_PK_ECDSA: |
| 10824 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 10825 | set->ecdsa = md_alg; |
| 10826 | break; |
| 10827 | |
| 10828 | default: |
| 10829 | break; |
| 10830 | } |
| 10831 | } |
| 10832 | |
| 10833 | /* Allow exactly one hash algorithm for each signature. */ |
| 10834 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 10835 | mbedtls_md_type_t md_alg ) |
| 10836 | { |
| 10837 | set->rsa = md_alg; |
| 10838 | set->ecdsa = md_alg; |
| 10839 | } |
| 10840 | |
| 10841 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
| 10842 | MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
| 10843 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 10844 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10845 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 10846 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10847 | 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] | 10848 | { |
| 10849 | switch( hash ) |
| 10850 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10851 | #if defined(MBEDTLS_MD5_C) |
| 10852 | case MBEDTLS_SSL_HASH_MD5: |
| 10853 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10854 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10855 | #if defined(MBEDTLS_SHA1_C) |
| 10856 | case MBEDTLS_SSL_HASH_SHA1: |
| 10857 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10858 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10859 | #if defined(MBEDTLS_SHA256_C) |
| 10860 | case MBEDTLS_SSL_HASH_SHA224: |
| 10861 | return( MBEDTLS_MD_SHA224 ); |
| 10862 | case MBEDTLS_SSL_HASH_SHA256: |
| 10863 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10864 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10865 | #if defined(MBEDTLS_SHA512_C) |
| 10866 | case MBEDTLS_SSL_HASH_SHA384: |
| 10867 | return( MBEDTLS_MD_SHA384 ); |
| 10868 | case MBEDTLS_SSL_HASH_SHA512: |
| 10869 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10870 | #endif |
| 10871 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10872 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 10873 | } |
| 10874 | } |
| 10875 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10876 | /* |
| 10877 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 10878 | */ |
| 10879 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 10880 | { |
| 10881 | switch( md ) |
| 10882 | { |
| 10883 | #if defined(MBEDTLS_MD5_C) |
| 10884 | case MBEDTLS_MD_MD5: |
| 10885 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 10886 | #endif |
| 10887 | #if defined(MBEDTLS_SHA1_C) |
| 10888 | case MBEDTLS_MD_SHA1: |
| 10889 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 10890 | #endif |
| 10891 | #if defined(MBEDTLS_SHA256_C) |
| 10892 | case MBEDTLS_MD_SHA224: |
| 10893 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 10894 | case MBEDTLS_MD_SHA256: |
| 10895 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 10896 | #endif |
| 10897 | #if defined(MBEDTLS_SHA512_C) |
| 10898 | case MBEDTLS_MD_SHA384: |
| 10899 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 10900 | case MBEDTLS_MD_SHA512: |
| 10901 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 10902 | #endif |
| 10903 | default: |
| 10904 | return( MBEDTLS_SSL_HASH_NONE ); |
| 10905 | } |
| 10906 | } |
| 10907 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 10908 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 10909 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10910 | * 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] | 10911 | * 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] | 10912 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 10913 | 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] | 10914 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10915 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 10916 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 10917 | if( ssl->conf->curve_list == NULL ) |
| 10918 | return( -1 ); |
| 10919 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 10920 | 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] | 10921 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 10922 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 10923 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 10924 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 10925 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 10926 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10927 | |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10928 | #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10929 | /* |
| 10930 | * Check if a hash proposed by the peer is in our list. |
| 10931 | * Return 0 if we're willing to use it, -1 otherwise. |
| 10932 | */ |
| 10933 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 10934 | mbedtls_md_type_t md ) |
| 10935 | { |
| 10936 | const int *cur; |
| 10937 | |
| 10938 | if( ssl->conf->sig_hashes == NULL ) |
| 10939 | return( -1 ); |
| 10940 | |
| 10941 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 10942 | if( *cur == (int) md ) |
| 10943 | return( 0 ); |
| 10944 | |
| 10945 | return( -1 ); |
| 10946 | } |
Manuel Pégourié-Gonnard | e5f3072 | 2015-10-22 17:01:15 +0200 | [diff] [blame] | 10947 | #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */ |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 10948 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10949 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 10950 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 10951 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10952 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 10953 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10954 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10955 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10956 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10957 | int usage = 0; |
| 10958 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10959 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10960 | const char *ext_oid; |
| 10961 | size_t ext_len; |
| 10962 | #endif |
| 10963 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10964 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 10965 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10966 | ((void) cert); |
| 10967 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 10968 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 10969 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10970 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10971 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 10972 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10973 | { |
| 10974 | /* Server part of the key exchange */ |
| 10975 | switch( ciphersuite->key_exchange ) |
| 10976 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10977 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 10978 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 10979 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10980 | break; |
| 10981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10982 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 10983 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 10984 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 10985 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10986 | break; |
| 10987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10988 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 10989 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 10990 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10991 | break; |
| 10992 | |
| 10993 | /* 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] | 10994 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 10995 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 10996 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 10997 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 10998 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 10999 | usage = 0; |
| 11000 | } |
| 11001 | } |
| 11002 | else |
| 11003 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11004 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 11005 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11006 | } |
| 11007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11008 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11009 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11010 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11011 | ret = -1; |
| 11012 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11013 | #else |
| 11014 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11015 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11017 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 11018 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11019 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11020 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 11021 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11022 | } |
| 11023 | else |
| 11024 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11025 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 11026 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11027 | } |
| 11028 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11029 | 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] | 11030 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 11031 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11032 | ret = -1; |
| 11033 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11034 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 11035 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 11036 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 11037 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11038 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 11039 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11040 | /* |
| 11041 | * Convert version numbers to/from wire format |
| 11042 | * and, for DTLS, to/from TLS equivalent. |
| 11043 | * |
| 11044 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 11045 | * 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] | 11046 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 11047 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 11048 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11049 | 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] | 11050 | unsigned char ver[2] ) |
| 11051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11052 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11053 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11054 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11055 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11056 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 11057 | |
| 11058 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 11059 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 11060 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11061 | else |
| 11062 | #else |
| 11063 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11064 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11065 | { |
| 11066 | ver[0] = (unsigned char) major; |
| 11067 | ver[1] = (unsigned char) minor; |
| 11068 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11069 | } |
| 11070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11071 | 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] | 11072 | const unsigned char ver[2] ) |
| 11073 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11074 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 11075 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11076 | { |
| 11077 | *major = 255 - ver[0] + 2; |
| 11078 | *minor = 255 - ver[1] + 1; |
| 11079 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11080 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11081 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 11082 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11083 | else |
| 11084 | #else |
| 11085 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11086 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 11087 | { |
| 11088 | *major = ver[0]; |
| 11089 | *minor = ver[1]; |
| 11090 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 11091 | } |
| 11092 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 11093 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 11094 | { |
| 11095 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 11096 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 11097 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11098 | |
| 11099 | switch( md ) |
| 11100 | { |
| 11101 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 11102 | #if defined(MBEDTLS_MD5_C) |
| 11103 | case MBEDTLS_SSL_HASH_MD5: |
Janos Follath | 182013f | 2016-10-25 10:50:22 +0100 | [diff] [blame] | 11104 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 11105 | #endif |
| 11106 | #if defined(MBEDTLS_SHA1_C) |
| 11107 | case MBEDTLS_SSL_HASH_SHA1: |
| 11108 | ssl->handshake->calc_verify = ssl_calc_verify_tls; |
| 11109 | break; |
| 11110 | #endif |
| 11111 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 11112 | #if defined(MBEDTLS_SHA512_C) |
| 11113 | case MBEDTLS_SSL_HASH_SHA384: |
| 11114 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 11115 | break; |
| 11116 | #endif |
| 11117 | #if defined(MBEDTLS_SHA256_C) |
| 11118 | case MBEDTLS_SSL_HASH_SHA256: |
| 11119 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 11120 | break; |
| 11121 | #endif |
| 11122 | default: |
| 11123 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11124 | } |
| 11125 | |
| 11126 | return 0; |
| 11127 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11128 | (void) ssl; |
| 11129 | (void) md; |
| 11130 | |
| 11131 | return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH; |
| 11132 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11133 | } |
| 11134 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11135 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 11136 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 11137 | int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, |
| 11138 | unsigned char *output, |
| 11139 | unsigned char *data, size_t data_len ) |
| 11140 | { |
| 11141 | int ret = 0; |
| 11142 | mbedtls_md5_context mbedtls_md5; |
| 11143 | mbedtls_sha1_context mbedtls_sha1; |
| 11144 | |
| 11145 | mbedtls_md5_init( &mbedtls_md5 ); |
| 11146 | mbedtls_sha1_init( &mbedtls_sha1 ); |
| 11147 | |
| 11148 | /* |
| 11149 | * digitally-signed struct { |
| 11150 | * opaque md5_hash[16]; |
| 11151 | * opaque sha_hash[20]; |
| 11152 | * }; |
| 11153 | * |
| 11154 | * md5_hash |
| 11155 | * MD5(ClientHello.random + ServerHello.random |
| 11156 | * + ServerParams); |
| 11157 | * sha_hash |
| 11158 | * SHA(ClientHello.random + ServerHello.random |
| 11159 | * + ServerParams); |
| 11160 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11161 | if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11162 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11163 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11164 | goto exit; |
| 11165 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11166 | if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11167 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11168 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11169 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11170 | goto exit; |
| 11171 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11172 | 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] | 11173 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11174 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11175 | goto exit; |
| 11176 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11177 | if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11178 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11179 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11180 | goto exit; |
| 11181 | } |
| 11182 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11183 | if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11184 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11185 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11186 | goto exit; |
| 11187 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11188 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11189 | ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11190 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11191 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11192 | goto exit; |
| 11193 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11194 | if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11195 | data_len ) ) != 0 ) |
| 11196 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11197 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11198 | goto exit; |
| 11199 | } |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11200 | if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1, |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11201 | output + 16 ) ) != 0 ) |
| 11202 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 11203 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11204 | goto exit; |
| 11205 | } |
| 11206 | |
| 11207 | exit: |
| 11208 | mbedtls_md5_free( &mbedtls_md5 ); |
| 11209 | mbedtls_sha1_free( &mbedtls_sha1 ); |
| 11210 | |
| 11211 | if( ret != 0 ) |
| 11212 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11213 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 11214 | |
| 11215 | return( ret ); |
| 11216 | |
| 11217 | } |
| 11218 | #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \ |
| 11219 | MBEDTLS_SSL_PROTO_TLS1_1 */ |
| 11220 | |
| 11221 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 11222 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11223 | |
| 11224 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 11225 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 11226 | unsigned char *hash, size_t *hashlen, |
| 11227 | unsigned char *data, size_t data_len, |
| 11228 | mbedtls_md_type_t md_alg ) |
| 11229 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11230 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 11231 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11232 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 11233 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 11234 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11235 | |
| 11236 | if( ( status = psa_hash_setup( &hash_operation, |
| 11237 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11238 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11239 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11240 | goto exit; |
| 11241 | } |
| 11242 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11243 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 11244 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11245 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11246 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11247 | goto exit; |
| 11248 | } |
| 11249 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11250 | if( ( status = psa_hash_update( &hash_operation, |
| 11251 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11252 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11253 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11254 | goto exit; |
| 11255 | } |
| 11256 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11257 | if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, |
| 11258 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11259 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11260 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11261 | goto exit; |
| 11262 | } |
| 11263 | |
| 11264 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11265 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11266 | { |
| 11267 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11268 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11269 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11270 | { |
| 11271 | case PSA_ERROR_NOT_SUPPORTED: |
| 11272 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11273 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11274 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 11275 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 11276 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 11277 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 11278 | default: |
| 11279 | return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); |
| 11280 | } |
| 11281 | } |
| 11282 | return( 0 ); |
| 11283 | } |
| 11284 | |
| 11285 | #else |
| 11286 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11287 | 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] | 11288 | unsigned char *hash, size_t *hashlen, |
| 11289 | unsigned char *data, size_t data_len, |
| 11290 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11291 | { |
| 11292 | int ret = 0; |
| 11293 | mbedtls_md_context_t ctx; |
| 11294 | 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] | 11295 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11296 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 11297 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 11298 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11299 | mbedtls_md_init( &ctx ); |
| 11300 | |
| 11301 | /* |
| 11302 | * digitally-signed struct { |
| 11303 | * opaque client_random[32]; |
| 11304 | * opaque server_random[32]; |
| 11305 | * ServerDHParams params; |
| 11306 | * }; |
| 11307 | */ |
| 11308 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 11309 | { |
| 11310 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 11311 | goto exit; |
| 11312 | } |
| 11313 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 11314 | { |
| 11315 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 11316 | goto exit; |
| 11317 | } |
| 11318 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 11319 | { |
| 11320 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 11321 | goto exit; |
| 11322 | } |
| 11323 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 11324 | { |
| 11325 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 11326 | goto exit; |
| 11327 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 11328 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11329 | { |
| 11330 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 11331 | goto exit; |
| 11332 | } |
| 11333 | |
| 11334 | exit: |
| 11335 | mbedtls_md_free( &ctx ); |
| 11336 | |
| 11337 | if( ret != 0 ) |
| 11338 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 11339 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 11340 | |
| 11341 | return( ret ); |
| 11342 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 11343 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 11344 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 11345 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 11346 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 11347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11348 | #endif /* MBEDTLS_SSL_TLS_C */ |