Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2 | * TLS shared functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | * http://www.ietf.org/rfc/rfc2246.txt |
| 21 | * http://www.ietf.org/rfc/rfc4346.txt |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_C) |
| 31 | #include "mbedtls/platform.h" |
| 32 | #else |
| 33 | #include <stdlib.h> |
Jerry Yu | 6ade743 | 2022-01-25 10:39:33 +0800 | [diff] [blame] | 34 | #include <stdio.h> |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #define mbedtls_calloc calloc |
| 36 | #define mbedtls_free free |
Jerry Yu | 6ade743 | 2022-01-25 10:39:33 +0800 | [diff] [blame] | 37 | #define mbedtls_printf printf |
| 38 | #endif /* !MBEDTLS_PLATFORM_C */ |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 39 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/ssl.h" |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 41 | #include "ssl_client.h" |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 42 | #include "ssl_debug_helpers.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 43 | #include "ssl_misc.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 44 | #include "mbedtls/debug.h" |
| 45 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 46 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 47 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 48 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 49 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 50 | #include <string.h> |
| 51 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 52 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 53 | #include "mbedtls/psa_util.h" |
| 54 | #include "psa/crypto.h" |
| 55 | #endif |
| 56 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 57 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 58 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 59 | #endif |
| 60 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 62 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 63 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 64 | /* Top-level Connection ID API */ |
| 65 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 66 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 67 | size_t len, |
| 68 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 69 | { |
| 70 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 71 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 72 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 73 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 74 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 75 | { |
| 76 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 77 | } |
| 78 | |
| 79 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 80 | conf->cid_len = len; |
| 81 | return( 0 ); |
| 82 | } |
| 83 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 84 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 85 | int enable, |
| 86 | unsigned char const *own_cid, |
| 87 | size_t own_cid_len ) |
| 88 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 89 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 90 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 91 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 92 | ssl->negotiate_cid = enable; |
| 93 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 94 | { |
| 95 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 96 | return( 0 ); |
| 97 | } |
| 98 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 99 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 100 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 101 | if( own_cid_len != ssl->conf->cid_len ) |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 102 | { |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 103 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 104 | (unsigned) own_cid_len, |
| 105 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 106 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 107 | } |
| 108 | |
| 109 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 110 | /* Truncation is not an issue here because |
| 111 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 112 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 113 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 114 | return( 0 ); |
| 115 | } |
| 116 | |
Paul Elliott | 0113cf1 | 2022-03-11 20:26:47 +0000 | [diff] [blame] | 117 | int mbedtls_ssl_get_own_cid( mbedtls_ssl_context *ssl, |
| 118 | int *enabled, |
| 119 | unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX], |
| 120 | size_t *own_cid_len ) |
| 121 | { |
| 122 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
| 123 | |
| 124 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 125 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 126 | |
| 127 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is |
| 128 | * zero as this is indistinguishable from not requesting to use |
| 129 | * the CID extension. */ |
| 130 | if( ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED ) |
| 131 | return( 0 ); |
| 132 | |
| 133 | if( own_cid_len != NULL ) |
| 134 | { |
| 135 | *own_cid_len = ssl->own_cid_len; |
| 136 | if( own_cid != NULL ) |
| 137 | memcpy( own_cid, ssl->own_cid, ssl->own_cid_len ); |
| 138 | } |
| 139 | |
| 140 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 141 | |
| 142 | return( 0 ); |
| 143 | } |
| 144 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 145 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 146 | int *enabled, |
| 147 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 148 | size_t *peer_cid_len ) |
| 149 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 150 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 151 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 152 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 153 | mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 154 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 155 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 156 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 157 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 158 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 159 | * were used, but client and server requested the empty CID. |
| 160 | * This is indistinguishable from not using the CID extension |
| 161 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 162 | if( ssl->transform_in->in_cid_len == 0 && |
| 163 | ssl->transform_in->out_cid_len == 0 ) |
| 164 | { |
| 165 | return( 0 ); |
| 166 | } |
| 167 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 168 | if( peer_cid_len != NULL ) |
| 169 | { |
| 170 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 171 | if( peer_cid != NULL ) |
| 172 | { |
| 173 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 174 | ssl->transform_in->out_cid_len ); |
| 175 | } |
| 176 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 177 | |
| 178 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 179 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 180 | return( 0 ); |
| 181 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 182 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 185 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 187 | /* |
| 188 | * Convert max_fragment_length codes to length. |
| 189 | * RFC 6066 says: |
| 190 | * enum{ |
| 191 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 192 | * } MaxFragmentLength; |
| 193 | * and we add 0 -> extension unused |
| 194 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 195 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 196 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 197 | switch( mfl ) |
| 198 | { |
| 199 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 200 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 201 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 202 | return 512; |
| 203 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 204 | return 1024; |
| 205 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 206 | return 2048; |
| 207 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 208 | return 4096; |
| 209 | default: |
| 210 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 211 | } |
| 212 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 214 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 215 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 216 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 217 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | mbedtls_ssl_session_free( dst ); |
| 219 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 220 | |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 221 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 222 | dst->ticket = NULL; |
| 223 | #endif |
| 224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 226 | |
| 227 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 228 | if( src->peer_cert != NULL ) |
| 229 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 230 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 231 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 232 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 233 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 234 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | 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] | 239 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 240 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 242 | dst->peer_cert = NULL; |
| 243 | return( ret ); |
| 244 | } |
| 245 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 246 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 247 | if( src->peer_cert_digest != NULL ) |
| 248 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 249 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 250 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 251 | if( dst->peer_cert_digest == NULL ) |
| 252 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 253 | |
| 254 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 255 | src->peer_cert_digest_len ); |
| 256 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 257 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 258 | } |
| 259 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 261 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 262 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 263 | #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] | 264 | if( src->ticket != NULL ) |
| 265 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 266 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 267 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 268 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 269 | |
| 270 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 271 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 272 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 273 | |
| 274 | return( 0 ); |
| 275 | } |
| 276 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 277 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 278 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 279 | { |
| 280 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 281 | if( resized_buffer == NULL ) |
| 282 | return -1; |
| 283 | |
| 284 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 285 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 286 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 287 | * lost, are done outside of this function. */ |
| 288 | memcpy( resized_buffer, *buffer, |
| 289 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 290 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 291 | mbedtls_free( *buffer ); |
| 292 | |
| 293 | *buffer = resized_buffer; |
| 294 | *len_old = len_new; |
| 295 | |
| 296 | return 0; |
| 297 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 298 | |
| 299 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 300 | size_t in_buf_new_len, |
| 301 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 302 | { |
| 303 | int modified = 0; |
| 304 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 305 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 306 | if( ssl->in_buf != NULL ) |
| 307 | { |
| 308 | written_in = ssl->in_msg - ssl->in_buf; |
| 309 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 310 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 311 | if( downsizing ? |
| 312 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 313 | ssl->in_buf_len < in_buf_new_len ) |
| 314 | { |
| 315 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 316 | { |
| 317 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 318 | } |
| 319 | else |
| 320 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 321 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 322 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 323 | modified = 1; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if( ssl->out_buf != NULL ) |
| 329 | { |
| 330 | written_out = ssl->out_msg - ssl->out_buf; |
| 331 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 332 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 333 | if( downsizing ? |
| 334 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 335 | ssl->out_buf_len < out_buf_new_len ) |
| 336 | { |
| 337 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 338 | { |
| 339 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 340 | } |
| 341 | else |
| 342 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 343 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 344 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 345 | modified = 1; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | if( modified ) |
| 350 | { |
| 351 | /* Update pointers here to avoid doing it twice. */ |
| 352 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 353 | /* Fields below might not be properly updated with record |
| 354 | * splitting or with CID, so they are manually updated here. */ |
| 355 | ssl->out_msg = ssl->out_buf + written_out; |
| 356 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 357 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 358 | |
| 359 | ssl->in_msg = ssl->in_buf + written_in; |
| 360 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 361 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 362 | } |
| 363 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 364 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 365 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 366 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 367 | |
| 368 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 369 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 370 | const char *label, |
| 371 | const unsigned char *random, size_t rlen, |
| 372 | unsigned char *dstbuf, size_t dlen ); |
| 373 | |
| 374 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ); |
| 375 | |
| 376 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 377 | |
| 378 | /* Type for the TLS PRF */ |
| 379 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 380 | const unsigned char *, size_t, |
| 381 | unsigned char *, size_t); |
| 382 | |
| 383 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 384 | int ciphersuite, |
| 385 | const unsigned char master[48], |
| 386 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 387 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 388 | int encrypt_then_mac, |
| 389 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 390 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 391 | ssl_tls_prf_t tls_prf, |
| 392 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 393 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 394 | unsigned endpoint, |
| 395 | const mbedtls_ssl_context *ssl ); |
| 396 | |
| 397 | #if defined(MBEDTLS_SHA256_C) |
| 398 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 399 | const char *label, |
| 400 | const unsigned char *random, size_t rlen, |
| 401 | unsigned char *dstbuf, size_t dlen ); |
| 402 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
| 403 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
| 404 | |
| 405 | #endif /* MBEDTLS_SHA256_C */ |
| 406 | |
| 407 | #if defined(MBEDTLS_SHA384_C) |
| 408 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 409 | const char *label, |
| 410 | const unsigned char *random, size_t rlen, |
| 411 | unsigned char *dstbuf, size_t dlen ); |
| 412 | |
| 413 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
| 414 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
| 415 | #endif /* MBEDTLS_SHA384_C */ |
| 416 | |
| 417 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 418 | unsigned char *buf, |
| 419 | size_t buf_len ); |
| 420 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 421 | const unsigned char *buf, |
| 422 | size_t len ); |
| 423 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 424 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 425 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 426 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 427 | #if defined(MBEDTLS_SHA256_C) |
| 428 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 429 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 430 | |
| 431 | #if defined(MBEDTLS_SHA384_C) |
| 432 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 433 | #endif /* MBEDTLS_SHA384_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 434 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 435 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 436 | const unsigned char *secret, size_t slen, |
| 437 | const char *label, |
| 438 | const unsigned char *random, size_t rlen, |
| 439 | unsigned char *dstbuf, size_t dlen ) |
| 440 | { |
| 441 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 442 | |
| 443 | switch( prf ) |
| 444 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 445 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 446 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 447 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 448 | tls_prf = tls_prf_sha384; |
| 449 | break; |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 450 | #endif /* MBEDTLS_SHA384_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 451 | #if defined(MBEDTLS_SHA256_C) |
| 452 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 453 | tls_prf = tls_prf_sha256; |
| 454 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 455 | #endif /* MBEDTLS_SHA256_C */ |
| 456 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 457 | default: |
| 458 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 459 | } |
| 460 | |
| 461 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 462 | } |
| 463 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 464 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 465 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 466 | { |
| 467 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 468 | if( session->peer_cert != NULL ) |
| 469 | { |
| 470 | mbedtls_x509_crt_free( session->peer_cert ); |
| 471 | mbedtls_free( session->peer_cert ); |
| 472 | session->peer_cert = NULL; |
| 473 | } |
| 474 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 475 | if( session->peer_cert_digest != NULL ) |
| 476 | { |
| 477 | /* Zeroization is not necessary. */ |
| 478 | mbedtls_free( session->peer_cert_digest ); |
| 479 | session->peer_cert_digest = NULL; |
| 480 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 481 | session->peer_cert_digest_len = 0; |
| 482 | } |
| 483 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 484 | } |
| 485 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 486 | |
| 487 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 488 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 489 | { |
| 490 | ((void) ciphersuite_info); |
| 491 | |
| 492 | #if defined(MBEDTLS_SHA384_C) |
| 493 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 494 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 495 | else |
| 496 | #endif |
| 497 | #if defined(MBEDTLS_SHA256_C) |
| 498 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 499 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 500 | else |
| 501 | #endif |
| 502 | { |
| 503 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 504 | return; |
| 505 | } |
| 506 | } |
| 507 | |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 508 | static void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
| 509 | unsigned hs_type, |
| 510 | size_t total_hs_len ) |
| 511 | { |
| 512 | unsigned char hs_hdr[4]; |
| 513 | |
| 514 | /* Build HS header for checksum update. */ |
| 515 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 516 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 517 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 518 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
| 519 | |
| 520 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 521 | } |
| 522 | |
| 523 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, |
| 524 | unsigned hs_type, |
| 525 | unsigned char const *msg, |
| 526 | size_t msg_len ) |
| 527 | { |
| 528 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); |
| 529 | ssl->handshake->update_checksum( ssl, msg, msg_len ); |
| 530 | } |
| 531 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 532 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 533 | { |
| 534 | ((void) ssl); |
| 535 | #if defined(MBEDTLS_SHA256_C) |
| 536 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 537 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 538 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 539 | #else |
| 540 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 541 | #endif |
| 542 | #endif |
| 543 | #if defined(MBEDTLS_SHA384_C) |
| 544 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 545 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 546 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 547 | #else |
| 548 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); |
| 549 | #endif |
| 550 | #endif |
| 551 | } |
| 552 | |
| 553 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 554 | const unsigned char *buf, size_t len ) |
| 555 | { |
| 556 | #if defined(MBEDTLS_SHA256_C) |
| 557 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 558 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 559 | #else |
| 560 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 561 | #endif |
| 562 | #endif |
| 563 | #if defined(MBEDTLS_SHA384_C) |
| 564 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 565 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 566 | #else |
| 567 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 568 | #endif |
| 569 | #endif |
| 570 | } |
| 571 | |
| 572 | #if defined(MBEDTLS_SHA256_C) |
| 573 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 574 | const unsigned char *buf, size_t len ) |
| 575 | { |
| 576 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 577 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 578 | #else |
| 579 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 580 | #endif |
| 581 | } |
| 582 | #endif |
| 583 | |
| 584 | #if defined(MBEDTLS_SHA384_C) |
| 585 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 586 | const unsigned char *buf, size_t len ) |
| 587 | { |
| 588 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 589 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 590 | #else |
| 591 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 592 | #endif |
| 593 | } |
| 594 | #endif |
| 595 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 597 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 598 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 599 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 601 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 602 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 603 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 604 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 606 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 607 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 608 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 609 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 610 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 611 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 612 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 613 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 614 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 615 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 616 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 617 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 618 | |
| 619 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 620 | |
| 621 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 622 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 623 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 624 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 625 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | #if defined(MBEDTLS_DHM_C) |
| 627 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 628 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 629 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 631 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 632 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 633 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 634 | #if defined(MBEDTLS_SSL_CLI_C) |
| 635 | handshake->ecjpake_cache = NULL; |
| 636 | handshake->ecjpake_cache_len = 0; |
| 637 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 638 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 639 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 640 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 641 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 642 | #endif |
| 643 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 644 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 645 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 646 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 647 | |
| 648 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 649 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 650 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 651 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 654 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 655 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 657 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 658 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 659 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 660 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 661 | #else |
| 662 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 663 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 664 | #endif |
| 665 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 666 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 667 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 668 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 669 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 670 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 672 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 673 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 674 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 678 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 683 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 684 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 685 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 687 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 689 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 690 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 694 | * Now allocate missing structures. |
| 695 | */ |
| 696 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 697 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 698 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 699 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 700 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 701 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 702 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 703 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 704 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 705 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 706 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 707 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 708 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 709 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 710 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 711 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 712 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 713 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 714 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 715 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 716 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 717 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 718 | if( ssl->handshake == NULL || |
| 719 | ssl->transform_negotiate == NULL || |
| 720 | ssl->session_negotiate == NULL ) |
| 721 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 722 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | mbedtls_free( ssl->handshake ); |
| 725 | mbedtls_free( ssl->transform_negotiate ); |
| 726 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 727 | |
| 728 | ssl->handshake = NULL; |
| 729 | ssl->transform_negotiate = NULL; |
| 730 | ssl->session_negotiate = NULL; |
| 731 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 732 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 735 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 737 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 738 | ssl_handshake_params_init( ssl->handshake ); |
| 739 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 740 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 741 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 742 | { |
| 743 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 744 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 745 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 746 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 747 | else |
| 748 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 749 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 750 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 751 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 752 | #endif |
| 753 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 754 | /* |
| 755 | * curve_list is translated to IANA TLS group identifiers here because |
| 756 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 757 | * any error codes. |
| 758 | */ |
| 759 | #if defined(MBEDTLS_ECP_C) |
| 760 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 761 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 762 | if ( ssl->conf->curve_list != NULL ) |
| 763 | { |
| 764 | size_t length; |
| 765 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 766 | |
| 767 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 768 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 769 | |
| 770 | /* Leave room for zero termination */ |
| 771 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 772 | if ( group_list == NULL ) |
| 773 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 774 | |
| 775 | for( size_t i = 0; i < length; i++ ) |
| 776 | { |
| 777 | const mbedtls_ecp_curve_info *info = |
| 778 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 779 | if ( info == NULL ) |
| 780 | { |
| 781 | mbedtls_free( group_list ); |
| 782 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 783 | } |
| 784 | group_list[i] = info->tls_id; |
| 785 | } |
| 786 | |
| 787 | group_list[length] = 0; |
| 788 | |
| 789 | ssl->handshake->group_list = group_list; |
| 790 | ssl->handshake->group_list_heap_allocated = 1; |
| 791 | } |
| 792 | else |
| 793 | { |
| 794 | ssl->handshake->group_list = ssl->conf->group_list; |
| 795 | ssl->handshake->group_list_heap_allocated = 0; |
| 796 | } |
| 797 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 798 | #endif /* MBEDTLS_ECP_C */ |
| 799 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 800 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 801 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 802 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 803 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 804 | signature algorithms IANA identifiers. */ |
| 805 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 806 | ssl->conf->sig_hashes != NULL ) |
| 807 | { |
| 808 | const int *md; |
| 809 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 810 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 811 | uint16_t *p; |
| 812 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 813 | #if defined(static_assert) |
| 814 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 815 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 816 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 817 | #endif |
| 818 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 819 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 820 | { |
| 821 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 822 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 823 | #if defined(MBEDTLS_ECDSA_C) |
| 824 | sig_algs_len += sizeof( uint16_t ); |
| 825 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 826 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 827 | #if defined(MBEDTLS_RSA_C) |
| 828 | sig_algs_len += sizeof( uint16_t ); |
| 829 | #endif |
| 830 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 831 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 832 | } |
| 833 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 834 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 835 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 836 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 837 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 838 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 839 | if( ssl->handshake->sig_algs == NULL ) |
| 840 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 841 | |
| 842 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 843 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 844 | { |
| 845 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 846 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 847 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 848 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 849 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 850 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 851 | #endif |
| 852 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 853 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 854 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 855 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 856 | } |
| 857 | *p = MBEDTLS_TLS1_3_SIG_NONE; |
| 858 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 859 | } |
| 860 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 861 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 862 | { |
| 863 | ssl->handshake->sig_algs = ssl->conf->sig_algs; |
| 864 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 865 | } |
| 866 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 867 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 868 | return( 0 ); |
| 869 | } |
| 870 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 871 | #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] | 872 | /* Dummy cookie callbacks for defaults */ |
| 873 | static int ssl_cookie_write_dummy( void *ctx, |
| 874 | unsigned char **p, unsigned char *end, |
| 875 | const unsigned char *cli_id, size_t cli_id_len ) |
| 876 | { |
| 877 | ((void) ctx); |
| 878 | ((void) p); |
| 879 | ((void) end); |
| 880 | ((void) cli_id); |
| 881 | ((void) cli_id_len); |
| 882 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 883 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static int ssl_cookie_check_dummy( void *ctx, |
| 887 | const unsigned char *cookie, size_t cookie_len, |
| 888 | const unsigned char *cli_id, size_t cli_id_len ) |
| 889 | { |
| 890 | ((void) ctx); |
| 891 | ((void) cookie); |
| 892 | ((void) cookie_len); |
| 893 | ((void) cli_id); |
| 894 | ((void) cli_id_len); |
| 895 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 896 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 897 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 898 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 899 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 900 | /* |
| 901 | * Initialize an SSL context |
| 902 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 903 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 904 | { |
| 905 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 906 | } |
| 907 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 908 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 909 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 910 | const mbedtls_ssl_config *conf = ssl->conf; |
| 911 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 912 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 913 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 914 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 915 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 916 | { |
| 917 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 918 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 919 | } |
| 920 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 921 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 922 | return( 0 ); |
| 923 | } |
| 924 | #endif |
| 925 | |
| 926 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 927 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 928 | { |
| 929 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 930 | return( 0 ); |
| 931 | } |
| 932 | #endif |
| 933 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 934 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 935 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 936 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 937 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 938 | { |
| 939 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 940 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 941 | } |
| 942 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 943 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 944 | { |
| 945 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 946 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 947 | } |
| 948 | |
| 949 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 950 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 951 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 952 | } |
| 953 | #endif |
| 954 | |
| 955 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 956 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 957 | } |
| 958 | |
| 959 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 960 | { |
| 961 | int ret; |
| 962 | ret = ssl_conf_version_check( ssl ); |
| 963 | if( ret != 0 ) |
| 964 | return( ret ); |
| 965 | |
| 966 | /* Space for further checks */ |
| 967 | |
| 968 | return( 0 ); |
| 969 | } |
| 970 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 971 | /* |
| 972 | * Setup an SSL context |
| 973 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 974 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 975 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 976 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 977 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 978 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 979 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 980 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 981 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 982 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 983 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 984 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 985 | return( ret ); |
| 986 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 987 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 988 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 989 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 990 | |
| 991 | /* Set to NULL in case of an error condition */ |
| 992 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 993 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 994 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 995 | ssl->in_buf_len = in_buf_len; |
| 996 | #endif |
| 997 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 998 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 999 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1000 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len ) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1001 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1002 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1003 | } |
| 1004 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1005 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1006 | ssl->out_buf_len = out_buf_len; |
| 1007 | #endif |
| 1008 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1009 | if( ssl->out_buf == NULL ) |
| 1010 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1011 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len ) ); |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1012 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1013 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1016 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1017 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1018 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1019 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1020 | #endif |
| 1021 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1022 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1023 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1024 | |
| 1025 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1026 | |
| 1027 | error: |
| 1028 | mbedtls_free( ssl->in_buf ); |
| 1029 | mbedtls_free( ssl->out_buf ); |
| 1030 | |
| 1031 | ssl->conf = NULL; |
| 1032 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1033 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1034 | ssl->in_buf_len = 0; |
| 1035 | ssl->out_buf_len = 0; |
| 1036 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1037 | ssl->in_buf = NULL; |
| 1038 | ssl->out_buf = NULL; |
| 1039 | |
| 1040 | ssl->in_hdr = NULL; |
| 1041 | ssl->in_ctr = NULL; |
| 1042 | ssl->in_len = NULL; |
| 1043 | ssl->in_iv = NULL; |
| 1044 | ssl->in_msg = NULL; |
| 1045 | |
| 1046 | ssl->out_hdr = NULL; |
| 1047 | ssl->out_ctr = NULL; |
| 1048 | ssl->out_len = NULL; |
| 1049 | ssl->out_iv = NULL; |
| 1050 | ssl->out_msg = NULL; |
| 1051 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1052 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1056 | * Reset an initialized and used SSL context for re-use while retaining |
| 1057 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1058 | * |
| 1059 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1060 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1061 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1062 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1063 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1064 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1065 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1066 | size_t in_buf_len = ssl->in_buf_len; |
| 1067 | size_t out_buf_len = ssl->out_buf_len; |
| 1068 | #else |
| 1069 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1070 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1071 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1072 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1073 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1074 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1075 | #endif |
| 1076 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1077 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1078 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1079 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1080 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1081 | |
| 1082 | /* Reset incoming message parsing */ |
| 1083 | ssl->in_offt = NULL; |
| 1084 | ssl->nb_zero = 0; |
| 1085 | ssl->in_msgtype = 0; |
| 1086 | ssl->in_msglen = 0; |
| 1087 | ssl->in_hslen = 0; |
| 1088 | ssl->keep_current_message = 0; |
| 1089 | ssl->transform_in = NULL; |
| 1090 | |
| 1091 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1092 | ssl->next_record_offset = 0; |
| 1093 | ssl->in_epoch = 0; |
| 1094 | #endif |
| 1095 | |
| 1096 | /* Keep current datagram if partial == 1 */ |
| 1097 | if( partial == 0 ) |
| 1098 | { |
| 1099 | ssl->in_left = 0; |
| 1100 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1101 | } |
| 1102 | |
| 1103 | /* Reset outgoing message writing */ |
| 1104 | ssl->out_msgtype = 0; |
| 1105 | ssl->out_msglen = 0; |
| 1106 | ssl->out_left = 0; |
| 1107 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1108 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1109 | ssl->transform_out = NULL; |
| 1110 | |
| 1111 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1112 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1113 | #endif |
| 1114 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1115 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1116 | if( ssl->transform ) |
| 1117 | { |
| 1118 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1119 | mbedtls_free( ssl->transform ); |
| 1120 | ssl->transform = NULL; |
| 1121 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1122 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1123 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1124 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1125 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1126 | mbedtls_free( ssl->transform_application ); |
| 1127 | ssl->transform_application = NULL; |
| 1128 | |
| 1129 | if( ssl->handshake != NULL ) |
| 1130 | { |
| 1131 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1132 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1133 | ssl->handshake->transform_earlydata = NULL; |
| 1134 | |
| 1135 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1136 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1137 | ssl->handshake->transform_handshake = NULL; |
| 1138 | } |
| 1139 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1140 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1144 | { |
| 1145 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1146 | |
| 1147 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1148 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1149 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1150 | |
| 1151 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1152 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1153 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1154 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1155 | |
| 1156 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1157 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1158 | 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] | 1159 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1160 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1161 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1162 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1163 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1164 | if( ssl->session ) |
| 1165 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1166 | mbedtls_ssl_session_free( ssl->session ); |
| 1167 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1168 | ssl->session = NULL; |
| 1169 | } |
| 1170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1171 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1172 | ssl->alpn_chosen = NULL; |
| 1173 | #endif |
| 1174 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1175 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1176 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1177 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1178 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1179 | { |
| 1180 | mbedtls_free( ssl->cli_id ); |
| 1181 | ssl->cli_id = NULL; |
| 1182 | ssl->cli_id_len = 0; |
| 1183 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1184 | #endif |
| 1185 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1186 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1187 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1188 | |
| 1189 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1192 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1193 | * Reset an initialized and used SSL context for re-use while retaining |
| 1194 | * all application-set variables, function pointers and data. |
| 1195 | */ |
| 1196 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1197 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1198 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1202 | * SSL set accessors |
| 1203 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1204 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1205 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1206 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1209 | 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] | 1210 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1211 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1214 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1215 | 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] | 1216 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1217 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1218 | } |
| 1219 | #endif |
| 1220 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1221 | 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] | 1222 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1223 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1224 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1225 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1226 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1227 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1228 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1229 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1230 | { |
| 1231 | ssl->disable_datagram_packing = !allow_packing; |
| 1232 | } |
| 1233 | |
| 1234 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1235 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1236 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1237 | conf->hs_timeout_min = min; |
| 1238 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1239 | } |
| 1240 | #endif |
| 1241 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1242 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1243 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1244 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1247 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1248 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1249 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1250 | void *p_vrfy ) |
| 1251 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1252 | conf->f_vrfy = f_vrfy; |
| 1253 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1254 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1255 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1256 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1257 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1258 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1259 | void *p_rng ) |
| 1260 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1261 | conf->f_rng = f_rng; |
| 1262 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1265 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1266 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1267 | void *p_dbg ) |
| 1268 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1269 | conf->f_dbg = f_dbg; |
| 1270 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1273 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1274 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1275 | mbedtls_ssl_send_t *f_send, |
| 1276 | mbedtls_ssl_recv_t *f_recv, |
| 1277 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1278 | { |
| 1279 | ssl->p_bio = p_bio; |
| 1280 | ssl->f_send = f_send; |
| 1281 | ssl->f_recv = f_recv; |
| 1282 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1285 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1286 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1287 | { |
| 1288 | ssl->mtu = mtu; |
| 1289 | } |
| 1290 | #endif |
| 1291 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1292 | 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] | 1293 | { |
| 1294 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1295 | } |
| 1296 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1297 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1298 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1299 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1300 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1301 | { |
| 1302 | ssl->p_timer = p_timer; |
| 1303 | ssl->f_set_timer = f_set_timer; |
| 1304 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1305 | |
| 1306 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1307 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1310 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1311 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1312 | void *p_cache, |
| 1313 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1314 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1315 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1316 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1317 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1318 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1319 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1320 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1322 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1323 | 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] | 1324 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1325 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1326 | |
| 1327 | if( ssl == NULL || |
| 1328 | session == NULL || |
| 1329 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1330 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1331 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1332 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1335 | if( ssl->handshake->resume == 1 ) |
| 1336 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1337 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1338 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1339 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1340 | return( ret ); |
| 1341 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1342 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1343 | |
| 1344 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1345 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1346 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1347 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1348 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1349 | const int *ciphersuites ) |
| 1350 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1351 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1354 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1355 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1356 | const int kex_modes ) |
| 1357 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1358 | conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1359 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1360 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1362 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1363 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1364 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1365 | { |
| 1366 | conf->cert_profile = profile; |
| 1367 | } |
| 1368 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1369 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1370 | { |
| 1371 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1372 | |
| 1373 | while( cur != NULL ) |
| 1374 | { |
| 1375 | next = cur->next; |
| 1376 | mbedtls_free( cur ); |
| 1377 | cur = next; |
| 1378 | } |
| 1379 | } |
| 1380 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1381 | /* Append a new keycert entry to a (possibly empty) list */ |
| 1382 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1383 | mbedtls_x509_crt *cert, |
| 1384 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1385 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1386 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1387 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1388 | if( cert == NULL ) |
| 1389 | { |
| 1390 | /* Free list if cert is null */ |
| 1391 | ssl_key_cert_free( *head ); |
| 1392 | *head = NULL; |
| 1393 | return( 0 ); |
| 1394 | } |
| 1395 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1396 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1397 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1398 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1399 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1400 | new_cert->cert = cert; |
| 1401 | new_cert->key = key; |
| 1402 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1403 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1404 | /* Update head if the list was null, else add to the end */ |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1405 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1406 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1407 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1408 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1409 | else |
| 1410 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1411 | mbedtls_ssl_key_cert *cur = *head; |
| 1412 | while( cur->next != NULL ) |
| 1413 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1414 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1417 | return( 0 ); |
| 1418 | } |
| 1419 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1420 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1421 | mbedtls_x509_crt *own_cert, |
| 1422 | mbedtls_pk_context *pk_key ) |
| 1423 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1424 | 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] | 1425 | } |
| 1426 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1427 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1428 | mbedtls_x509_crt *ca_chain, |
| 1429 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1430 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1431 | conf->ca_chain = ca_chain; |
| 1432 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1433 | |
| 1434 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1435 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1436 | * cannot be used together. */ |
| 1437 | conf->f_ca_cb = NULL; |
| 1438 | conf->p_ca_cb = NULL; |
| 1439 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1440 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1441 | |
| 1442 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1443 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1444 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1445 | void *p_ca_cb ) |
| 1446 | { |
| 1447 | conf->f_ca_cb = f_ca_cb; |
| 1448 | conf->p_ca_cb = p_ca_cb; |
| 1449 | |
| 1450 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1451 | * cannot be used together. */ |
| 1452 | conf->ca_chain = NULL; |
| 1453 | conf->ca_crl = NULL; |
| 1454 | } |
| 1455 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1456 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1457 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1458 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1459 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1460 | size_t *name_len ) |
| 1461 | { |
| 1462 | *name_len = ssl->handshake->sni_name_len; |
| 1463 | return( ssl->handshake->sni_name ); |
| 1464 | } |
| 1465 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1466 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1467 | mbedtls_x509_crt *own_cert, |
| 1468 | mbedtls_pk_context *pk_key ) |
| 1469 | { |
| 1470 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1471 | own_cert, pk_key ) ); |
| 1472 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1473 | |
| 1474 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1475 | mbedtls_x509_crt *ca_chain, |
| 1476 | mbedtls_x509_crl *ca_crl ) |
| 1477 | { |
| 1478 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1479 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1480 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1481 | |
| 1482 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1483 | int authmode ) |
| 1484 | { |
| 1485 | ssl->handshake->sni_authmode = authmode; |
| 1486 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1487 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1488 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1489 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1490 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1491 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1492 | void *p_vrfy ) |
| 1493 | { |
| 1494 | ssl->f_vrfy = f_vrfy; |
| 1495 | ssl->p_vrfy = p_vrfy; |
| 1496 | } |
| 1497 | #endif |
| 1498 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1499 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1500 | /* |
| 1501 | * Set EC J-PAKE password for current handshake |
| 1502 | */ |
| 1503 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1504 | const unsigned char *pw, |
| 1505 | size_t pw_len ) |
| 1506 | { |
| 1507 | mbedtls_ecjpake_role role; |
| 1508 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1509 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1511 | |
| 1512 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1513 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1514 | else |
| 1515 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1516 | |
| 1517 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1518 | role, |
| 1519 | MBEDTLS_MD_SHA256, |
| 1520 | MBEDTLS_ECP_DP_SECP256R1, |
| 1521 | pw, pw_len ) ); |
| 1522 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1523 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1524 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1525 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1526 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1527 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 1528 | { |
| 1529 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1530 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 1531 | return( 1 ); |
| 1532 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1533 | |
| 1534 | if( conf->psk != NULL ) |
| 1535 | return( 1 ); |
| 1536 | |
| 1537 | return( 0 ); |
| 1538 | } |
| 1539 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1540 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1541 | { |
| 1542 | /* Remove reference to existing PSK, if any. */ |
| 1543 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1544 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1545 | { |
| 1546 | /* The maintenance of the PSK key slot is the |
| 1547 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1548 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1549 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 1550 | /* This and the following branch should never |
| 1551 | * be taken simultaenously as we maintain the |
| 1552 | * invariant that raw and opaque PSKs are never |
| 1553 | * configured simultaneously. As a safeguard, |
| 1554 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1555 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1556 | if( conf->psk != NULL ) |
| 1557 | { |
| 1558 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1559 | |
| 1560 | mbedtls_free( conf->psk ); |
| 1561 | conf->psk = NULL; |
| 1562 | conf->psk_len = 0; |
| 1563 | } |
| 1564 | |
| 1565 | /* Remove reference to PSK identity, if any. */ |
| 1566 | if( conf->psk_identity != NULL ) |
| 1567 | { |
| 1568 | mbedtls_free( conf->psk_identity ); |
| 1569 | conf->psk_identity = NULL; |
| 1570 | conf->psk_identity_len = 0; |
| 1571 | } |
| 1572 | } |
| 1573 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1574 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1575 | * It checks that the provided identity is well-formed and attempts |
| 1576 | * to make a copy of it in the SSL config. |
| 1577 | * On failure, the PSK identity in the config remains unset. */ |
| 1578 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1579 | unsigned char const *psk_identity, |
| 1580 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1581 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1582 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1583 | if( psk_identity == NULL || |
| 1584 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1585 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1586 | { |
| 1587 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1588 | } |
| 1589 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1590 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1591 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1592 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1593 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1594 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1595 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1596 | |
| 1597 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1600 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1601 | const unsigned char *psk, size_t psk_len, |
| 1602 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1603 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1604 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1605 | |
| 1606 | /* We currently only support one PSK, raw or opaque. */ |
| 1607 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1608 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1609 | |
| 1610 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1611 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1612 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1613 | if( psk_len == 0 ) |
| 1614 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1615 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1616 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1617 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1618 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1619 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1620 | conf->psk_len = psk_len; |
| 1621 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1622 | |
| 1623 | /* Check and set PSK Identity */ |
| 1624 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1625 | if( ret != 0 ) |
| 1626 | ssl_conf_remove_psk( conf ); |
| 1627 | |
| 1628 | return( ret ); |
| 1629 | } |
| 1630 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1631 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1632 | { |
| 1633 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1634 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1635 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1636 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1637 | } |
| 1638 | else |
| 1639 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1640 | if( ssl->handshake->psk != NULL ) |
| 1641 | { |
| 1642 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1643 | ssl->handshake->psk_len ); |
| 1644 | mbedtls_free( ssl->handshake->psk ); |
| 1645 | ssl->handshake->psk_len = 0; |
| 1646 | } |
| 1647 | } |
| 1648 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1649 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1650 | const unsigned char *psk, size_t psk_len ) |
| 1651 | { |
| 1652 | if( psk == NULL || ssl->handshake == NULL ) |
| 1653 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1654 | |
| 1655 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1656 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1657 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1658 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1659 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1660 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1661 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1662 | |
| 1663 | ssl->handshake->psk_len = psk_len; |
| 1664 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1665 | |
| 1666 | return( 0 ); |
| 1667 | } |
| 1668 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1669 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1670 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1671 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1672 | const unsigned char *psk_identity, |
| 1673 | size_t psk_identity_len ) |
| 1674 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1675 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1676 | |
| 1677 | /* We currently only support one PSK, raw or opaque. */ |
| 1678 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1679 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1680 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1681 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1682 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1683 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1684 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1685 | |
| 1686 | /* Check and set PSK Identity */ |
| 1687 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1688 | psk_identity_len ); |
| 1689 | if( ret != 0 ) |
| 1690 | ssl_conf_remove_psk( conf ); |
| 1691 | |
| 1692 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1693 | } |
| 1694 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1695 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1696 | mbedtls_svc_key_id_t psk ) |
| 1697 | { |
| 1698 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1699 | ( ssl->handshake == NULL ) ) |
| 1700 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1701 | |
| 1702 | ssl_remove_psk( ssl ); |
| 1703 | ssl->handshake->psk_opaque = psk; |
| 1704 | return( 0 ); |
| 1705 | } |
| 1706 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1707 | |
| 1708 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1709 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1710 | size_t), |
| 1711 | void *p_psk ) |
| 1712 | { |
| 1713 | conf->f_psk = f_psk; |
| 1714 | conf->p_psk = p_psk; |
| 1715 | } |
| 1716 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 1717 | |
| 1718 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1719 | psa_status_t mbedtls_ssl_cipher_to_psa( mbedtls_cipher_type_t mbedtls_cipher_type, |
Przemyslaw Stekiel | 430f337 | 2022-01-10 11:55:46 +0100 | [diff] [blame] | 1720 | size_t taglen, |
| 1721 | psa_algorithm_t *alg, |
| 1722 | psa_key_type_t *key_type, |
| 1723 | size_t *key_size ) |
| 1724 | { |
| 1725 | switch ( mbedtls_cipher_type ) |
| 1726 | { |
| 1727 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 1728 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1729 | *key_type = PSA_KEY_TYPE_AES; |
| 1730 | *key_size = 128; |
| 1731 | break; |
| 1732 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 1733 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1734 | *key_type = PSA_KEY_TYPE_AES; |
| 1735 | *key_size = 128; |
| 1736 | break; |
| 1737 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 1738 | *alg = PSA_ALG_GCM; |
| 1739 | *key_type = PSA_KEY_TYPE_AES; |
| 1740 | *key_size = 128; |
| 1741 | break; |
| 1742 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 1743 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1744 | *key_type = PSA_KEY_TYPE_AES; |
| 1745 | *key_size = 192; |
| 1746 | break; |
| 1747 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 1748 | *alg = PSA_ALG_GCM; |
| 1749 | *key_type = PSA_KEY_TYPE_AES; |
| 1750 | *key_size = 192; |
| 1751 | break; |
| 1752 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 1753 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1754 | *key_type = PSA_KEY_TYPE_AES; |
| 1755 | *key_size = 256; |
| 1756 | break; |
| 1757 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 1758 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1759 | *key_type = PSA_KEY_TYPE_AES; |
| 1760 | *key_size = 256; |
| 1761 | break; |
| 1762 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 1763 | *alg = PSA_ALG_GCM; |
| 1764 | *key_type = PSA_KEY_TYPE_AES; |
| 1765 | *key_size = 256; |
| 1766 | break; |
| 1767 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 1768 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1769 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1770 | *key_size = 128; |
| 1771 | break; |
| 1772 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 1773 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1774 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1775 | *key_size = 128; |
| 1776 | break; |
| 1777 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 1778 | *alg = PSA_ALG_GCM; |
| 1779 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1780 | *key_size = 128; |
| 1781 | break; |
| 1782 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 1783 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1784 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1785 | *key_size = 192; |
| 1786 | break; |
| 1787 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 1788 | *alg = PSA_ALG_GCM; |
| 1789 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1790 | *key_size = 192; |
| 1791 | break; |
| 1792 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 1793 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1794 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1795 | *key_size = 256; |
| 1796 | break; |
| 1797 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 1798 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1799 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1800 | *key_size = 256; |
| 1801 | break; |
| 1802 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 1803 | *alg = PSA_ALG_GCM; |
| 1804 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1805 | *key_size = 256; |
| 1806 | break; |
| 1807 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 1808 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1809 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1810 | *key_size = 128; |
| 1811 | break; |
| 1812 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 1813 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1814 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1815 | *key_size = 128; |
| 1816 | break; |
| 1817 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 1818 | *alg = PSA_ALG_GCM; |
| 1819 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1820 | *key_size = 128; |
| 1821 | break; |
| 1822 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 1823 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1824 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1825 | *key_size = 192; |
| 1826 | break; |
| 1827 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 1828 | *alg = PSA_ALG_GCM; |
| 1829 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1830 | *key_size = 192; |
| 1831 | break; |
| 1832 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 1833 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1834 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1835 | *key_size = 256; |
| 1836 | break; |
| 1837 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 1838 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1839 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1840 | *key_size = 256; |
| 1841 | break; |
| 1842 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 1843 | *alg = PSA_ALG_GCM; |
| 1844 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1845 | *key_size = 256; |
| 1846 | break; |
| 1847 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 1848 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 1849 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 1850 | *key_size = 256; |
| 1851 | break; |
| 1852 | case MBEDTLS_CIPHER_NULL: |
| 1853 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 1854 | *key_type = 0; |
| 1855 | *key_size = 0; |
| 1856 | break; |
| 1857 | default: |
| 1858 | return PSA_ERROR_NOT_SUPPORTED; |
| 1859 | } |
| 1860 | |
| 1861 | return PSA_SUCCESS; |
| 1862 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1863 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1864 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1865 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1866 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 1867 | const unsigned char *dhm_P, size_t P_len, |
| 1868 | const unsigned char *dhm_G, size_t G_len ) |
| 1869 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1870 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1871 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1872 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1873 | mbedtls_mpi_free( &conf->dhm_G ); |
| 1874 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1875 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 1876 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 1877 | { |
| 1878 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1879 | mbedtls_mpi_free( &conf->dhm_G ); |
| 1880 | return( ret ); |
| 1881 | } |
| 1882 | |
| 1883 | return( 0 ); |
| 1884 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1885 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1886 | 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] | 1887 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1888 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1889 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1890 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1891 | mbedtls_mpi_free( &conf->dhm_G ); |
| 1892 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 1893 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 1894 | &conf->dhm_P ) ) != 0 || |
| 1895 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 1896 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 1897 | { |
| 1898 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1899 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1900 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 1901 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1902 | |
| 1903 | return( 0 ); |
| 1904 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1905 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1906 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 1907 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 1908 | /* |
| 1909 | * Set the minimum length for Diffie-Hellman parameters |
| 1910 | */ |
| 1911 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 1912 | unsigned int bitlen ) |
| 1913 | { |
| 1914 | conf->dhm_min_bitlen = bitlen; |
| 1915 | } |
| 1916 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 1917 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1918 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 1919 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 1920 | /* |
| 1921 | * Set allowed/preferred hashes for handshake signatures |
| 1922 | */ |
| 1923 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 1924 | const int *hashes ) |
| 1925 | { |
| 1926 | conf->sig_hashes = hashes; |
| 1927 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 1928 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1929 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1930 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1931 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 1932 | const uint16_t* sig_algs ) |
| 1933 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1934 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1935 | conf->sig_hashes = NULL; |
| 1936 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 1937 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1938 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1939 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 1940 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 1941 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1942 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1943 | /* |
| 1944 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1945 | * |
| 1946 | * mbedtls_ssl_setup() takes the provided list |
| 1947 | * and translates it to a list of IANA TLS group identifiers, |
| 1948 | * stored in ssl->handshake->group_list. |
| 1949 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1950 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1951 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1952 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1953 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1954 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1955 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1956 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1957 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1958 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1959 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1960 | /* |
| 1961 | * Set the allowed groups |
| 1962 | */ |
| 1963 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 1964 | const uint16_t *group_list ) |
| 1965 | { |
| 1966 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1967 | conf->curve_list = NULL; |
| 1968 | #endif |
| 1969 | conf->group_list = group_list; |
| 1970 | } |
| 1971 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1972 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1973 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1974 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1975 | /* Initialize to suppress unnecessary compiler warning */ |
| 1976 | size_t hostname_len = 0; |
| 1977 | |
| 1978 | /* Check if new hostname is valid before |
| 1979 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1980 | if( hostname != NULL ) |
| 1981 | { |
| 1982 | hostname_len = strlen( hostname ); |
| 1983 | |
| 1984 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 1985 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1986 | } |
| 1987 | |
| 1988 | /* Now it's clear that we will overwrite the old hostname, |
| 1989 | * so we can free it safely */ |
| 1990 | |
| 1991 | if( ssl->hostname != NULL ) |
| 1992 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1993 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1994 | mbedtls_free( ssl->hostname ); |
| 1995 | } |
| 1996 | |
| 1997 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 1998 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1999 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2000 | { |
| 2001 | ssl->hostname = NULL; |
| 2002 | } |
| 2003 | else |
| 2004 | { |
| 2005 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2006 | if( ssl->hostname == NULL ) |
| 2007 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2008 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2009 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2010 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2011 | ssl->hostname[hostname_len] = '\0'; |
| 2012 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2013 | |
| 2014 | return( 0 ); |
| 2015 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2016 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2017 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2018 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2019 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2020 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2021 | const unsigned char *, size_t), |
| 2022 | void *p_sni ) |
| 2023 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2024 | conf->f_sni = f_sni; |
| 2025 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2026 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2027 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2028 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2029 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2030 | 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] | 2031 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2032 | size_t cur_len, tot_len; |
| 2033 | const char **p; |
| 2034 | |
| 2035 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2036 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2037 | * MUST NOT be truncated." |
| 2038 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2039 | */ |
| 2040 | tot_len = 0; |
| 2041 | for( p = protos; *p != NULL; p++ ) |
| 2042 | { |
| 2043 | cur_len = strlen( *p ); |
| 2044 | tot_len += cur_len; |
| 2045 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2046 | if( ( cur_len == 0 ) || |
| 2047 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2048 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2049 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2052 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2053 | |
| 2054 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2055 | } |
| 2056 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2057 | 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] | 2058 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2059 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2060 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2061 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2062 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2063 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2064 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2065 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2066 | { |
| 2067 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2068 | } |
| 2069 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2070 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2071 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2072 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2073 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2074 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2075 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2076 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2077 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2078 | |
| 2079 | if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2080 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2081 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2082 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2083 | |
| 2084 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2085 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2086 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2087 | } |
| 2088 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2089 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2090 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2091 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2092 | const mbedtls_ssl_srtp_profile *p; |
| 2093 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2094 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2095 | /* check the profiles list: all entry must be valid, |
| 2096 | * its size cannot be more than the total number of supported profiles, currently 4 */ |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2097 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2098 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2099 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2100 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2101 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2102 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2103 | list_size++; |
| 2104 | } |
| 2105 | else |
| 2106 | { |
| 2107 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2108 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2109 | } |
| 2110 | } |
| 2111 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2112 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2113 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2114 | conf->dtls_srtp_profile_list = NULL; |
| 2115 | conf->dtls_srtp_profile_list_len = 0; |
| 2116 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2117 | } |
| 2118 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2119 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2120 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2121 | |
| 2122 | return( 0 ); |
| 2123 | } |
| 2124 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2125 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2126 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2127 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2128 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2129 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2130 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2131 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2132 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2133 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2134 | else |
| 2135 | { |
| 2136 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2137 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2138 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2139 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2140 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2141 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2142 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2143 | 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] | 2144 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2145 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2148 | 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] | 2149 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2150 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2153 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2154 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2155 | char cert_req_ca_list ) |
| 2156 | { |
| 2157 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2158 | } |
| 2159 | #endif |
| 2160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2161 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2162 | 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] | 2163 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2164 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2165 | } |
| 2166 | #endif |
| 2167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2168 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2169 | 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] | 2170 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2171 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2172 | } |
| 2173 | #endif |
| 2174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2175 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2176 | 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] | 2177 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2178 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2179 | 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] | 2180 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2181 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2182 | } |
| 2183 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2184 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2185 | |
| 2186 | return( 0 ); |
| 2187 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2188 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2189 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2190 | 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] | 2191 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2192 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2195 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2196 | 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] | 2197 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2198 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2199 | } |
| 2200 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2201 | 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] | 2202 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2203 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2204 | } |
| 2205 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2206 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2207 | const unsigned char period[8] ) |
| 2208 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2209 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2210 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2213 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2214 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2215 | 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] | 2216 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2217 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2218 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2219 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2220 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2221 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2222 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2223 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2224 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2225 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2226 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2227 | conf->f_ticket_write = f_ticket_write; |
| 2228 | conf->f_ticket_parse = f_ticket_parse; |
| 2229 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2230 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2231 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2232 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2233 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2234 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2235 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2236 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2237 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2238 | ssl->f_export_keys = f_export_keys; |
| 2239 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2240 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2241 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2242 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2243 | void mbedtls_ssl_conf_async_private_cb( |
| 2244 | mbedtls_ssl_config *conf, |
| 2245 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2246 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2247 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2248 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2249 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2250 | { |
| 2251 | conf->f_async_sign_start = f_async_sign; |
| 2252 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2253 | conf->f_async_resume = f_async_resume; |
| 2254 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2255 | conf->p_async_config_data = async_config_data; |
| 2256 | } |
| 2257 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2258 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2259 | { |
| 2260 | return( conf->p_async_config_data ); |
| 2261 | } |
| 2262 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2263 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2264 | { |
| 2265 | if( ssl->handshake == NULL ) |
| 2266 | return( NULL ); |
| 2267 | else |
| 2268 | return( ssl->handshake->user_async_ctx ); |
| 2269 | } |
| 2270 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2271 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2272 | void *ctx ) |
| 2273 | { |
| 2274 | if( ssl->handshake != NULL ) |
| 2275 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2276 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2277 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2278 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2279 | /* |
| 2280 | * SSL get accessors |
| 2281 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2282 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2283 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2284 | if( ssl->session != NULL ) |
| 2285 | return( ssl->session->verify_result ); |
| 2286 | |
| 2287 | if( ssl->session_negotiate != NULL ) |
| 2288 | return( ssl->session_negotiate->verify_result ); |
| 2289 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2290 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2293 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2294 | { |
| 2295 | if( ssl == NULL || ssl->session == NULL ) |
| 2296 | return( 0 ); |
| 2297 | |
| 2298 | return( ssl->session->ciphersuite ); |
| 2299 | } |
| 2300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2301 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2302 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2303 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2304 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2305 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2306 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2309 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2310 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2311 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2312 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2313 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2314 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2315 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2316 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2317 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2318 | default: |
| 2319 | return( "unknown (DTLS)" ); |
| 2320 | } |
| 2321 | } |
| 2322 | #endif |
| 2323 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2324 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2325 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2326 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2327 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2328 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2329 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2330 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2331 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2332 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2335 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2336 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2337 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2338 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2339 | size_t read_mfl; |
| 2340 | |
| 2341 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2342 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2343 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2344 | { |
| 2345 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2346 | } |
| 2347 | |
| 2348 | /* Check if a smaller max length was negotiated */ |
| 2349 | if( ssl->session_out != NULL ) |
| 2350 | { |
| 2351 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2352 | if( read_mfl < max_len ) |
| 2353 | { |
| 2354 | max_len = read_mfl; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | // During a handshake, use the value being negotiated |
| 2359 | if( ssl->session_negotiate != NULL ) |
| 2360 | { |
| 2361 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2362 | if( read_mfl < max_len ) |
| 2363 | { |
| 2364 | max_len = read_mfl; |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | return( max_len ); |
| 2369 | } |
| 2370 | |
| 2371 | size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2372 | { |
| 2373 | size_t max_len; |
| 2374 | |
| 2375 | /* |
| 2376 | * Assume mfl_code is correct since it was checked when set |
| 2377 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2378 | 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] | 2379 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2380 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2381 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2382 | 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] | 2383 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2384 | 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] | 2385 | } |
| 2386 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2387 | /* During a handshake, use the value being negotiated */ |
| 2388 | if( ssl->session_negotiate != NULL && |
| 2389 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2390 | { |
| 2391 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2392 | } |
| 2393 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2394 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2395 | } |
| 2396 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2397 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2398 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2399 | size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2400 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2401 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2402 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2403 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 2404 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 2405 | return ( 0 ); |
| 2406 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2407 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 2408 | return( ssl->mtu ); |
| 2409 | |
| 2410 | if( ssl->mtu == 0 ) |
| 2411 | return( ssl->handshake->mtu ); |
| 2412 | |
| 2413 | return( ssl->mtu < ssl->handshake->mtu ? |
| 2414 | ssl->mtu : ssl->handshake->mtu ); |
| 2415 | } |
| 2416 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2417 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2418 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 2419 | { |
| 2420 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2421 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2422 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2423 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2424 | (void) ssl; |
| 2425 | #endif |
| 2426 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2427 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2428 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2429 | |
| 2430 | if( max_len > mfl ) |
| 2431 | max_len = mfl; |
| 2432 | #endif |
| 2433 | |
| 2434 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2435 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2436 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2437 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2438 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 2439 | const size_t overhead = (size_t) ret; |
| 2440 | |
| 2441 | if( ret < 0 ) |
| 2442 | return( ret ); |
| 2443 | |
| 2444 | if( mtu <= overhead ) |
| 2445 | { |
| 2446 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 2447 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2448 | } |
| 2449 | |
| 2450 | if( max_len > mtu - overhead ) |
| 2451 | max_len = mtu - overhead; |
| 2452 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2453 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2454 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2455 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2456 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2457 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2458 | #endif |
| 2459 | |
| 2460 | return( (int) max_len ); |
| 2461 | } |
| 2462 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2463 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 2464 | { |
| 2465 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 2466 | |
| 2467 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2468 | (void) ssl; |
| 2469 | #endif |
| 2470 | |
| 2471 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2472 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 2473 | |
| 2474 | if( max_len > mfl ) |
| 2475 | max_len = mfl; |
| 2476 | #endif |
| 2477 | |
| 2478 | return( (int) max_len ); |
| 2479 | } |
| 2480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2481 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2482 | 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] | 2483 | { |
| 2484 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2485 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2486 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2487 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2488 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2489 | #else |
| 2490 | return( NULL ); |
| 2491 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2492 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2493 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2494 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2496 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 2497 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2498 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2499 | int ret; |
| 2500 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2501 | if( ssl == NULL || |
| 2502 | dst == NULL || |
| 2503 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2504 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2505 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2506 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2507 | } |
| 2508 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2509 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 2510 | * idempotent: Each session can only be exported once. |
| 2511 | * |
| 2512 | * (This is in preparation for TLS 1.3 support where we will |
| 2513 | * need the ability to export multiple sessions (aka tickets), |
| 2514 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 2515 | * multiple times until it fails.) |
| 2516 | * |
| 2517 | * Check whether we have already exported the current session, |
| 2518 | * and fail if so. |
| 2519 | */ |
| 2520 | if( ssl->session->exported == 1 ) |
| 2521 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2522 | |
| 2523 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 2524 | if( ret != 0 ) |
| 2525 | return( ret ); |
| 2526 | |
| 2527 | /* Remember that we've exported the session. */ |
| 2528 | ssl->session->exported = 1; |
| 2529 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2530 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2531 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2532 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2533 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2534 | * Define ticket header determining Mbed TLS version |
| 2535 | * and structure of the ticket. |
| 2536 | */ |
| 2537 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2538 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2539 | * Define bitflag determining compile-time settings influencing |
| 2540 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2541 | */ |
| 2542 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2543 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2544 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2545 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2546 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2547 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2548 | |
| 2549 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2550 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2551 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2552 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2553 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2554 | |
| 2555 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2556 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2557 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2558 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2559 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 2560 | |
| 2561 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2562 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2563 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2564 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2565 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2566 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2567 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2568 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2569 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2570 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2571 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 2572 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2573 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2574 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 2575 | #else |
| 2576 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 2577 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 2578 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2579 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 2580 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 2581 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 2582 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 2583 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 2584 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2585 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2586 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2587 | ( (uint16_t) ( \ |
| 2588 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 2589 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 2590 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 2591 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2592 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 2593 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2594 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2595 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2596 | MBEDTLS_VERSION_MAJOR, |
| 2597 | MBEDTLS_VERSION_MINOR, |
| 2598 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 2599 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 2600 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2601 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2602 | |
| 2603 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2604 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 2605 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2606 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2607 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2608 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 2609 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 2610 | * opaque session_format[2]; // library-version specific 16-bit field |
| 2611 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2612 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2613 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2614 | * Note: When updating the format, remember to keep |
| 2615 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2616 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2617 | * // In this version, `session_format` determines |
| 2618 | * // the setting of those compile-time |
| 2619 | * // configuration options which influence |
| 2620 | * // the structure of mbedtls_ssl_session. |
| 2621 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2622 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 2623 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2624 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2625 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2626 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2627 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2628 | * serialized_session_tls12 data; |
| 2629 | * |
| 2630 | * }; |
| 2631 | * |
| 2632 | * } serialized_session; |
| 2633 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2634 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2635 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2636 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 2637 | unsigned char omit_header, |
| 2638 | unsigned char *buf, |
| 2639 | size_t buf_len, |
| 2640 | size_t *olen ) |
| 2641 | { |
| 2642 | unsigned char *p = buf; |
| 2643 | size_t used = 0; |
| 2644 | |
| 2645 | if( !omit_header ) |
| 2646 | { |
| 2647 | /* |
| 2648 | * Add Mbed TLS version identifier |
| 2649 | */ |
| 2650 | |
| 2651 | used += sizeof( ssl_serialized_session_header ); |
| 2652 | |
| 2653 | if( used <= buf_len ) |
| 2654 | { |
| 2655 | memcpy( p, ssl_serialized_session_header, |
| 2656 | sizeof( ssl_serialized_session_header ) ); |
| 2657 | p += sizeof( ssl_serialized_session_header ); |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | /* |
| 2662 | * TLS version identifier |
| 2663 | */ |
| 2664 | used += 1; |
| 2665 | if( used <= buf_len ) |
| 2666 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2667 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | /* Forward to version-specific serialization routine. */ |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2671 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2672 | { |
| 2673 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2674 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2675 | { |
| 2676 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 2677 | used += ssl_session_save_tls12( session, p, remaining_len ); |
| 2678 | break; |
| 2679 | } |
| 2680 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2681 | |
| 2682 | default: |
| 2683 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2684 | } |
| 2685 | |
| 2686 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 2687 | if( used > buf_len ) |
| 2688 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2689 | |
| 2690 | return( 0 ); |
| 2691 | } |
| 2692 | |
| 2693 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2694 | * Public wrapper for ssl_session_save() |
| 2695 | */ |
| 2696 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 2697 | unsigned char *buf, |
| 2698 | size_t buf_len, |
| 2699 | size_t *olen ) |
| 2700 | { |
| 2701 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 2702 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2703 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 2704 | /* |
| 2705 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 2706 | * |
| 2707 | * This internal version is wrapped by a public function that cleans up in |
| 2708 | * case of error, and has an extra option omit_header. |
| 2709 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2710 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 2711 | unsigned char omit_header, |
| 2712 | const unsigned char *buf, |
| 2713 | size_t len ) |
| 2714 | { |
| 2715 | const unsigned char *p = buf; |
| 2716 | const unsigned char * const end = buf + len; |
| 2717 | |
| 2718 | if( !omit_header ) |
| 2719 | { |
| 2720 | /* |
| 2721 | * Check Mbed TLS version identifier |
| 2722 | */ |
| 2723 | |
| 2724 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 2725 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2726 | |
| 2727 | if( memcmp( p, ssl_serialized_session_header, |
| 2728 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 2729 | { |
| 2730 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 2731 | } |
| 2732 | p += sizeof( ssl_serialized_session_header ); |
| 2733 | } |
| 2734 | |
| 2735 | /* |
| 2736 | * TLS version identifier |
| 2737 | */ |
| 2738 | if( 1 > (size_t)( end - p ) ) |
| 2739 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2740 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2741 | |
| 2742 | /* Dispatch according to TLS version. */ |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2743 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2744 | { |
| 2745 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2746 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2747 | { |
| 2748 | size_t remaining_len = ( end - p ); |
| 2749 | return( ssl_session_load_tls12( session, p, remaining_len ) ); |
| 2750 | } |
| 2751 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2752 | |
| 2753 | default: |
| 2754 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2755 | } |
| 2756 | } |
| 2757 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2758 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 2759 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2760 | */ |
| 2761 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 2762 | const unsigned char *buf, |
| 2763 | size_t len ) |
| 2764 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2765 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2766 | |
| 2767 | if( ret != 0 ) |
| 2768 | mbedtls_ssl_session_free( session ); |
| 2769 | |
| 2770 | return( ret ); |
| 2771 | } |
| 2772 | |
| 2773 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2774 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2775 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2776 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 2777 | { |
| 2778 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2779 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2780 | /* |
| 2781 | * We may have not been able to send to the peer all the handshake data |
Ronald Cron | 3f20b77 | 2022-03-08 16:00:02 +0100 | [diff] [blame] | 2782 | * that were written into the output buffer by the previous handshake step, |
| 2783 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2784 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 2785 | * We proceed to the next handshake step only when all data from the |
| 2786 | * previous one have been sent to the peer, thus we make sure that this is |
| 2787 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 2788 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 2789 | * we have to wait before to go ahead. |
| 2790 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 2791 | * peer. Data are only sent here and through |
| 2792 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
| 2793 | * alert occured. |
| 2794 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2795 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 2796 | return( ret ); |
| 2797 | |
| 2798 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2799 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2800 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 2801 | { |
| 2802 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2803 | return( ret ); |
| 2804 | } |
| 2805 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2806 | |
| 2807 | return( ret ); |
| 2808 | } |
| 2809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2810 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2811 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2812 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2813 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2814 | if( ssl == NULL || |
| 2815 | ssl->conf == NULL || |
| 2816 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2817 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2818 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2819 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | ret = ssl_prepare_handshake_step( ssl ); |
| 2823 | if( ret != 0 ) |
| 2824 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2825 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2826 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 2827 | if( ret != 0 ) |
| 2828 | goto cleanup; |
| 2829 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2830 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2831 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2832 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 2833 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 2834 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2835 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2836 | switch( ssl->state ) |
| 2837 | { |
| 2838 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 2839 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 2840 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2841 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2842 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 2843 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 2844 | break; |
| 2845 | |
| 2846 | default: |
| 2847 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2848 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2849 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 2850 | else |
| 2851 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 2852 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2853 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 2854 | #else |
| 2855 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 2856 | #endif |
| 2857 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2858 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2859 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2860 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2861 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2862 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2863 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2864 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 2865 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2866 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2867 | |
| 2868 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2869 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 2870 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 2871 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2872 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2873 | #endif |
| 2874 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2875 | if( ret != 0 ) |
| 2876 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 2877 | /* handshake_step return error. And it is same |
| 2878 | * with alert_reason. |
| 2879 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 2880 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2881 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 2882 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2883 | goto cleanup; |
| 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2888 | return( ret ); |
| 2889 | } |
| 2890 | |
| 2891 | /* |
| 2892 | * Perform the SSL handshake |
| 2893 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2894 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2895 | { |
| 2896 | int ret = 0; |
| 2897 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2898 | /* Sanity checks */ |
| 2899 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2900 | if( ssl == NULL || ssl->conf == NULL ) |
| 2901 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2902 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2903 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2904 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2905 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 2906 | { |
| 2907 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 2908 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 2909 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2910 | } |
| 2911 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2913 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2914 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2915 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2916 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2917 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2918 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2919 | |
| 2920 | if( ret != 0 ) |
| 2921 | break; |
| 2922 | } |
| 2923 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2924 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2925 | |
| 2926 | return( ret ); |
| 2927 | } |
| 2928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2929 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2930 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2931 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2932 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2933 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2934 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2935 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2936 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2937 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2938 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2939 | |
| 2940 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2941 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2942 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2943 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2944 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2945 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2946 | 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] | 2947 | return( ret ); |
| 2948 | } |
| 2949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2950 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2951 | |
| 2952 | return( 0 ); |
| 2953 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2954 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2955 | |
| 2956 | /* |
| 2957 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2958 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 2959 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 2960 | * - 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] | 2961 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2962 | * 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] | 2963 | * 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] | 2964 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 2965 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2966 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2967 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2968 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2969 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2970 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2971 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 2972 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2973 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2974 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 2975 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2976 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2977 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2978 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2979 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2980 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2981 | ssl->handshake->out_msg_seq = 1; |
| 2982 | else |
| 2983 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2984 | } |
| 2985 | #endif |
| 2986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 2988 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2989 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2990 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2991 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2992 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2993 | return( ret ); |
| 2994 | } |
| 2995 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2996 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2997 | |
| 2998 | return( 0 ); |
| 2999 | } |
| 3000 | |
| 3001 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3002 | * Renegotiate current connection on client, |
| 3003 | * or request renegotiation on server |
| 3004 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3005 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3006 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3007 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3008 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3009 | if( ssl == NULL || ssl->conf == NULL ) |
| 3010 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3012 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3013 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3014 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3015 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3016 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3017 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3018 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3019 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3020 | |
| 3021 | /* Did we already try/start sending HelloRequest? */ |
| 3022 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3023 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3024 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3025 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3026 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3027 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3028 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3029 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3030 | /* |
| 3031 | * On client, either start the renegotiation process or, |
| 3032 | * if already in progress, continue the handshake |
| 3033 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3034 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3035 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3036 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3037 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3038 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3039 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3040 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3041 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3042 | return( ret ); |
| 3043 | } |
| 3044 | } |
| 3045 | else |
| 3046 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3047 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3048 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3049 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3050 | return( ret ); |
| 3051 | } |
| 3052 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3053 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3054 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3055 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3056 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3057 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3058 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3059 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3060 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3061 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3062 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3063 | if( handshake == NULL ) |
| 3064 | return; |
| 3065 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3066 | #if defined(MBEDTLS_ECP_C) |
| 3067 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3068 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3069 | mbedtls_free( (void*) handshake->group_list ); |
| 3070 | handshake->group_list = NULL; |
| 3071 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3072 | #endif /* MBEDTLS_ECP_C */ |
| 3073 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3074 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 3075 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3076 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3077 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3078 | handshake->sig_algs = NULL; |
| 3079 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3080 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3081 | if( ssl->handshake->certificate_request_context ) |
| 3082 | { |
| 3083 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3084 | } |
| 3085 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3086 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 3087 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3088 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3089 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3090 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3091 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3092 | handshake->async_in_progress = 0; |
| 3093 | } |
| 3094 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3095 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3096 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3097 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3098 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3099 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3100 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3101 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3102 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 3103 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3104 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3105 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3106 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3107 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 3108 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3109 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3111 | #if defined(MBEDTLS_DHM_C) |
| 3112 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3113 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3114 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3115 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3116 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3117 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3118 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3119 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3120 | mbedtls_free( handshake->ecjpake_cache ); |
| 3121 | handshake->ecjpake_cache = NULL; |
| 3122 | handshake->ecjpake_cache_len = 0; |
| 3123 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3124 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3125 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3126 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3127 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3128 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3129 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3130 | #endif |
| 3131 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3132 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3133 | if( handshake->psk != NULL ) |
| 3134 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3135 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3136 | mbedtls_free( handshake->psk ); |
| 3137 | } |
| 3138 | #endif |
| 3139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3140 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3141 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3142 | /* |
| 3143 | * Free only the linked list wrapper, not the keys themselves |
| 3144 | * since the belong to the SNI callback |
| 3145 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3146 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3147 | #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] | 3148 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3149 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3150 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3151 | if( handshake->ecrs_peer_cert != NULL ) |
| 3152 | { |
| 3153 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3154 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3155 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3156 | #endif |
| 3157 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3158 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3159 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3160 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3161 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3162 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3163 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3164 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3165 | mbedtls_free( handshake->cookie ); |
| 3166 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3167 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3168 | |
| 3169 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3170 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3171 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3172 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3173 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3174 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3175 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3176 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3177 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3178 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3179 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3180 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3181 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3182 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3183 | mbedtls_free( handshake->transform_earlydata ); |
| 3184 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3185 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3186 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3187 | |
| 3188 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3189 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3190 | * processes datagrams and the fact that a datagram is allowed to have |
| 3191 | * several records in it, it is possible that the I/O buffers are not |
| 3192 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3193 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3194 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3195 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3196 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3197 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3198 | mbedtls_platform_zeroize( handshake, |
| 3199 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3202 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3203 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3204 | if( session == NULL ) |
| 3205 | return; |
| 3206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3207 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3208 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3209 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3210 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3211 | #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] | 3212 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3213 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3214 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3215 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3218 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3219 | |
| 3220 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3221 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3222 | #else |
| 3223 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3224 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3225 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3226 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3227 | |
| 3228 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3229 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3230 | #else |
| 3231 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3232 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3233 | |
| 3234 | #if defined(MBEDTLS_SSL_ALPN) |
| 3235 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3236 | #else |
| 3237 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3238 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3239 | |
| 3240 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3241 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3242 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3243 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3244 | |
| 3245 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3246 | ( (uint32_t) ( \ |
| 3247 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3248 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3249 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3250 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3251 | 0u ) ) |
| 3252 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3253 | static unsigned char ssl_serialized_context_header[] = { |
| 3254 | MBEDTLS_VERSION_MAJOR, |
| 3255 | MBEDTLS_VERSION_MINOR, |
| 3256 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3257 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3258 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3259 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3260 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3261 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3262 | }; |
| 3263 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3264 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3265 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3266 | * |
| 3267 | * The format of the serialized data is: |
| 3268 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3269 | * |
| 3270 | * // header |
| 3271 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3272 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3273 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3274 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3275 | * Note: When updating the format, remember to keep these |
| 3276 | * version+format bytes. (We may make their size part of the API.) |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3277 | * |
| 3278 | * // session sub-structure |
| 3279 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3280 | * // transform sub-structure |
| 3281 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3282 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3283 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3284 | * // fields from ssl_context |
| 3285 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3286 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3287 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3288 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3289 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3290 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3291 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3292 | * |
| 3293 | * Note that many fields of the ssl_context or sub-structures are not |
| 3294 | * serialized, as they fall in one of the following categories: |
| 3295 | * |
| 3296 | * 1. forced value (eg in_left must be 0) |
| 3297 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3298 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3299 | * 4. value was temporary (eg content of input buffer) |
| 3300 | * 5. value will be provided by the user again (eg I/O callbacks and context) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3301 | */ |
| 3302 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3303 | unsigned char *buf, |
| 3304 | size_t buf_len, |
| 3305 | size_t *olen ) |
| 3306 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3307 | unsigned char *p = buf; |
| 3308 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3309 | size_t session_len; |
| 3310 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3311 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3312 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3313 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3314 | * this function's documentation. |
| 3315 | * |
| 3316 | * These are due to assumptions/limitations in the implementation. Some of |
| 3317 | * them are likely to stay (no handshake in progress) some might go away |
| 3318 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3319 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3320 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3321 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3322 | { |
| 3323 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3324 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3325 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3326 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3327 | { |
| 3328 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3329 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3330 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3331 | /* Double-check that sub-structures are indeed ready */ |
| 3332 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3333 | { |
| 3334 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3335 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3336 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3337 | /* There must be no pending incoming or outgoing data */ |
| 3338 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3339 | { |
| 3340 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3341 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3342 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3343 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3344 | { |
| 3345 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3346 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3347 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3348 | /* Protocol must be DLTS, not TLS */ |
| 3349 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3350 | { |
| 3351 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3352 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3353 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3354 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3355 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3356 | { |
| 3357 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3358 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3359 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3360 | /* We must be using an AEAD ciphersuite */ |
| 3361 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3362 | { |
| 3363 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3364 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3365 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3366 | /* Renegotiation must not be enabled */ |
| 3367 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3368 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3369 | { |
| 3370 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3371 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3372 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3373 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3374 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3375 | /* |
| 3376 | * Version and format identifier |
| 3377 | */ |
| 3378 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3379 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3380 | if( used <= buf_len ) |
| 3381 | { |
| 3382 | memcpy( p, ssl_serialized_context_header, |
| 3383 | sizeof( ssl_serialized_context_header ) ); |
| 3384 | p += sizeof( ssl_serialized_context_header ); |
| 3385 | } |
| 3386 | |
| 3387 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3388 | * Session (length + data) |
| 3389 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3390 | ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3391 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 3392 | return( ret ); |
| 3393 | |
| 3394 | used += 4 + session_len; |
| 3395 | if( used <= buf_len ) |
| 3396 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3397 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 3398 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3399 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3400 | ret = ssl_session_save( ssl->session, 1, |
| 3401 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3402 | if( ret != 0 ) |
| 3403 | return( ret ); |
| 3404 | |
| 3405 | p += session_len; |
| 3406 | } |
| 3407 | |
| 3408 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3409 | * Transform |
| 3410 | */ |
| 3411 | used += sizeof( ssl->transform->randbytes ); |
| 3412 | if( used <= buf_len ) |
| 3413 | { |
| 3414 | memcpy( p, ssl->transform->randbytes, |
| 3415 | sizeof( ssl->transform->randbytes ) ); |
| 3416 | p += sizeof( ssl->transform->randbytes ); |
| 3417 | } |
| 3418 | |
| 3419 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3420 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 3421 | if( used <= buf_len ) |
| 3422 | { |
| 3423 | *p++ = ssl->transform->in_cid_len; |
| 3424 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 3425 | p += ssl->transform->in_cid_len; |
| 3426 | |
| 3427 | *p++ = ssl->transform->out_cid_len; |
| 3428 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 3429 | p += ssl->transform->out_cid_len; |
| 3430 | } |
| 3431 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3432 | |
| 3433 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3434 | * Saved fields from top-level ssl_context structure |
| 3435 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3436 | used += 4; |
| 3437 | if( used <= buf_len ) |
| 3438 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3439 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 3440 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3441 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3442 | |
| 3443 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3444 | used += 16; |
| 3445 | if( used <= buf_len ) |
| 3446 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3447 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 3448 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3449 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3450 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 3451 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3452 | } |
| 3453 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3454 | |
| 3455 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3456 | used += 1; |
| 3457 | if( used <= buf_len ) |
| 3458 | { |
| 3459 | *p++ = ssl->disable_datagram_packing; |
| 3460 | } |
| 3461 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3462 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3463 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3464 | if( used <= buf_len ) |
| 3465 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3466 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 3467 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3471 | used += 2; |
| 3472 | if( used <= buf_len ) |
| 3473 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3474 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 3475 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3476 | } |
| 3477 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3478 | |
| 3479 | #if defined(MBEDTLS_SSL_ALPN) |
| 3480 | { |
| 3481 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 3482 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3483 | : 0; |
| 3484 | |
| 3485 | used += 1 + alpn_len; |
| 3486 | if( used <= buf_len ) |
| 3487 | { |
| 3488 | *p++ = alpn_len; |
| 3489 | |
| 3490 | if( ssl->alpn_chosen != NULL ) |
| 3491 | { |
| 3492 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 3493 | p += alpn_len; |
| 3494 | } |
| 3495 | } |
| 3496 | } |
| 3497 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3498 | |
| 3499 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3500 | * Done |
| 3501 | */ |
| 3502 | *olen = used; |
| 3503 | |
| 3504 | if( used > buf_len ) |
| 3505 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3506 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3507 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 3508 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3509 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3513 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3514 | * |
| 3515 | * This internal version is wrapped by a public function that cleans up in |
| 3516 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3517 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3518 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 3519 | const unsigned char *buf, |
| 3520 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3521 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3522 | const unsigned char *p = buf; |
| 3523 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3524 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3525 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3526 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3527 | /* |
| 3528 | * The context should have been freshly setup or reset. |
| 3529 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 3530 | * (Checking session is useful because it won't be NULL if we're |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3531 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 3532 | */ |
| 3533 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 3534 | ssl->session != NULL ) |
| 3535 | { |
| 3536 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3537 | } |
| 3538 | |
| 3539 | /* |
| 3540 | * We can't check that the config matches the initial one, but we can at |
| 3541 | * least check it matches the requirements for serializing. |
| 3542 | */ |
| 3543 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3544 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 3545 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3546 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3547 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3548 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3549 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3550 | { |
| 3551 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3552 | } |
| 3553 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3554 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 3555 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3556 | /* |
| 3557 | * Check version identifier |
| 3558 | */ |
| 3559 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 3560 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3561 | |
| 3562 | if( memcmp( p, ssl_serialized_context_header, |
| 3563 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 3564 | { |
| 3565 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3566 | } |
| 3567 | p += sizeof( ssl_serialized_context_header ); |
| 3568 | |
| 3569 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3570 | * Session |
| 3571 | */ |
| 3572 | if( (size_t)( end - p ) < 4 ) |
| 3573 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3574 | |
| 3575 | session_len = ( (size_t) p[0] << 24 ) | |
| 3576 | ( (size_t) p[1] << 16 ) | |
| 3577 | ( (size_t) p[2] << 8 ) | |
| 3578 | ( (size_t) p[3] ); |
| 3579 | p += 4; |
| 3580 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3581 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3582 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3583 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3584 | ssl->session_in = ssl->session; |
| 3585 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3586 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3587 | |
| 3588 | if( (size_t)( end - p ) < session_len ) |
| 3589 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3590 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3591 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3592 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3593 | { |
| 3594 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3595 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3596 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3597 | |
| 3598 | p += session_len; |
| 3599 | |
| 3600 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3601 | * Transform |
| 3602 | */ |
| 3603 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3604 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3605 | * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */ |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3606 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3607 | ssl->transform_in = ssl->transform; |
| 3608 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3609 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3610 | |
| 3611 | /* Read random bytes and populate structure */ |
| 3612 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 3613 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3614 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3615 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3616 | ssl->session->ciphersuite, |
| 3617 | ssl->session->master, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3618 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 3619 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3620 | ssl->session->encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3621 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 3622 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3623 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 3624 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 3625 | MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3626 | ssl->conf->endpoint, |
| 3627 | ssl ); |
| 3628 | if( ret != 0 ) |
| 3629 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3630 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3631 | p += sizeof( ssl->transform->randbytes ); |
| 3632 | |
| 3633 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3634 | /* Read connection IDs and store them */ |
| 3635 | if( (size_t)( end - p ) < 1 ) |
| 3636 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3637 | |
| 3638 | ssl->transform->in_cid_len = *p++; |
| 3639 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 3640 | if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u ) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3641 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3642 | |
| 3643 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 3644 | p += ssl->transform->in_cid_len; |
| 3645 | |
| 3646 | ssl->transform->out_cid_len = *p++; |
| 3647 | |
| 3648 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 3649 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3650 | |
| 3651 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 3652 | p += ssl->transform->out_cid_len; |
| 3653 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3654 | |
| 3655 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3656 | * Saved fields from top-level ssl_context structure |
| 3657 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3658 | if( (size_t)( end - p ) < 4 ) |
| 3659 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3660 | |
| 3661 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 3662 | ( (uint32_t) p[1] << 16 ) | |
| 3663 | ( (uint32_t) p[2] << 8 ) | |
| 3664 | ( (uint32_t) p[3] ); |
| 3665 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3666 | |
| 3667 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3668 | if( (size_t)( end - p ) < 16 ) |
| 3669 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3670 | |
| 3671 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 3672 | ( (uint64_t) p[1] << 48 ) | |
| 3673 | ( (uint64_t) p[2] << 40 ) | |
| 3674 | ( (uint64_t) p[3] << 32 ) | |
| 3675 | ( (uint64_t) p[4] << 24 ) | |
| 3676 | ( (uint64_t) p[5] << 16 ) | |
| 3677 | ( (uint64_t) p[6] << 8 ) | |
| 3678 | ( (uint64_t) p[7] ); |
| 3679 | p += 8; |
| 3680 | |
| 3681 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 3682 | ( (uint64_t) p[1] << 48 ) | |
| 3683 | ( (uint64_t) p[2] << 40 ) | |
| 3684 | ( (uint64_t) p[3] << 32 ) | |
| 3685 | ( (uint64_t) p[4] << 24 ) | |
| 3686 | ( (uint64_t) p[5] << 16 ) | |
| 3687 | ( (uint64_t) p[6] << 8 ) | |
| 3688 | ( (uint64_t) p[7] ); |
| 3689 | p += 8; |
| 3690 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3691 | |
| 3692 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3693 | if( (size_t)( end - p ) < 1 ) |
| 3694 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3695 | |
| 3696 | ssl->disable_datagram_packing = *p++; |
| 3697 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3698 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3699 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3700 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3701 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 3702 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3703 | |
| 3704 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3705 | if( (size_t)( end - p ) < 2 ) |
| 3706 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3707 | |
| 3708 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 3709 | p += 2; |
| 3710 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3711 | |
| 3712 | #if defined(MBEDTLS_SSL_ALPN) |
| 3713 | { |
| 3714 | uint8_t alpn_len; |
| 3715 | const char **cur; |
| 3716 | |
| 3717 | if( (size_t)( end - p ) < 1 ) |
| 3718 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3719 | |
| 3720 | alpn_len = *p++; |
| 3721 | |
| 3722 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 3723 | { |
| 3724 | /* alpn_chosen should point to an item in the configured list */ |
| 3725 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 3726 | { |
| 3727 | if( strlen( *cur ) == alpn_len && |
| 3728 | memcmp( p, cur, alpn_len ) == 0 ) |
| 3729 | { |
| 3730 | ssl->alpn_chosen = *cur; |
| 3731 | break; |
| 3732 | } |
| 3733 | } |
| 3734 | } |
| 3735 | |
| 3736 | /* can only happen on conf mismatch */ |
| 3737 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 3738 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3739 | |
| 3740 | p += alpn_len; |
| 3741 | } |
| 3742 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3743 | |
| 3744 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3745 | * Forced fields from top-level ssl_context structure |
| 3746 | * |
| 3747 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 3748 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 3749 | */ |
| 3750 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3751 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3752 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3753 | /* Adjust pointers for header fields of outgoing records to |
| 3754 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3755 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3756 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3757 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3758 | ssl->in_epoch = 1; |
| 3759 | #endif |
| 3760 | |
| 3761 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 3762 | * which we don't want - otherwise we'd end up freeing the wrong transform |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 3763 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 3764 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3765 | if( ssl->handshake != NULL ) |
| 3766 | { |
| 3767 | mbedtls_ssl_handshake_free( ssl ); |
| 3768 | mbedtls_free( ssl->handshake ); |
| 3769 | ssl->handshake = NULL; |
| 3770 | } |
| 3771 | |
| 3772 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3773 | * Done - should have consumed entire buffer |
| 3774 | */ |
| 3775 | if( p != end ) |
| 3776 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3777 | |
| 3778 | return( 0 ); |
| 3779 | } |
| 3780 | |
| 3781 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3782 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3783 | */ |
| 3784 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 3785 | const unsigned char *buf, |
| 3786 | size_t len ) |
| 3787 | { |
| 3788 | int ret = ssl_context_load( context, buf, len ); |
| 3789 | |
| 3790 | if( ret != 0 ) |
| 3791 | mbedtls_ssl_free( context ); |
| 3792 | |
| 3793 | return( ret ); |
| 3794 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3795 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3796 | |
| 3797 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3798 | * Free an SSL context |
| 3799 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3800 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3801 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3802 | if( ssl == NULL ) |
| 3803 | return; |
| 3804 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3805 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3806 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3807 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3808 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3809 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3810 | size_t out_buf_len = ssl->out_buf_len; |
| 3811 | #else |
| 3812 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3813 | #endif |
| 3814 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3815 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3816 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3817 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3820 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3821 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3822 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3823 | size_t in_buf_len = ssl->in_buf_len; |
| 3824 | #else |
| 3825 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3826 | #endif |
| 3827 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3828 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3829 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3830 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3833 | if( ssl->transform ) |
| 3834 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3835 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3836 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
| 3839 | if( ssl->handshake ) |
| 3840 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3841 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3842 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 3843 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3844 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3845 | mbedtls_free( ssl->handshake ); |
| 3846 | mbedtls_free( ssl->transform_negotiate ); |
| 3847 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3850 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3851 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 3852 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3853 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3854 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3855 | if( ssl->session ) |
| 3856 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3857 | mbedtls_ssl_session_free( ssl->session ); |
| 3858 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3859 | } |
| 3860 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 3861 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3862 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3863 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3864 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3865 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3866 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 3867 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3868 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3869 | #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] | 3870 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3871 | #endif |
| 3872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3873 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 3874 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 3875 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3876 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 3879 | /* |
| 3880 | * Initialze mbedtls_ssl_config |
| 3881 | */ |
| 3882 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 3883 | { |
| 3884 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 3885 | } |
| 3886 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3887 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3888 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3889 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
Gilles Peskine | a28f0f5 | 2021-06-02 15:29:38 +0200 | [diff] [blame] | 3890 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 3891 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3892 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 3893 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3894 | static int ssl_preset_default_hashes[] = { |
| 3895 | #if defined(MBEDTLS_SHA512_C) |
| 3896 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 3897 | #endif |
| 3898 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3899 | MBEDTLS_MD_SHA384, |
| 3900 | #endif |
| 3901 | #if defined(MBEDTLS_SHA256_C) |
| 3902 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 3903 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3904 | MBEDTLS_MD_NONE |
| 3905 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3906 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 3907 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3908 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3909 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 3910 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3911 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3912 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3913 | * about this list. |
| 3914 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3915 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3916 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3917 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3918 | #endif |
| 3919 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3920 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3921 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3922 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3923 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3924 | #endif |
| 3925 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3926 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3927 | #endif |
| 3928 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3929 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3930 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3931 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3932 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3933 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3934 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3935 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3936 | #endif |
| 3937 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3938 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3939 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3940 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3941 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3942 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3943 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 3944 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 3945 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 3946 | 0 |
| 3947 | }; |
| 3948 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3949 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3950 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3951 | static int ssl_preset_suiteb_hashes[] = { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3952 | #if defined(MBEDTLS_SHA256_C) |
| 3953 | MBEDTLS_MD_SHA256, |
| 3954 | #endif |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3955 | #if defined(MBEDTLS_SHA384_C) |
| 3956 | MBEDTLS_MD_SHA384, |
| 3957 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3958 | MBEDTLS_MD_NONE |
| 3959 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3960 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3961 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3962 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 3963 | * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 3964 | * rules SHOULD be upheld. |
| 3965 | * - No duplicate entries. |
| 3966 | * - But if there is a good reason, do not change the order of the algorithms. |
| 3967 | * - ssl_tls12_present* is for TLS 1.2 use only. |
| 3968 | * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes. |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3969 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3970 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3971 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 3972 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 3973 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 3974 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 3975 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 3976 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3977 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3978 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 3979 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 3980 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 3981 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 3982 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 3983 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 3984 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ |
| 3985 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 3986 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
| 3987 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 3988 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3989 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3990 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 3991 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 3992 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
| 3993 | |
Ronald Cron | 8540cf6 | 2022-03-16 08:01:09 +0100 | [diff] [blame] | 3994 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C) |
| 3995 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 3996 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 3997 | |
| 3998 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C) |
| 3999 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4000 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4001 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4002 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 4003 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4004 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4005 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4006 | MBEDTLS_TLS1_3_SIG_NONE |
| 4007 | }; |
| 4008 | |
| 4009 | /* NOTICE: see above */ |
| 4010 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4011 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4012 | #if defined(MBEDTLS_SHA512_C) |
| 4013 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) |
| 4014 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4015 | #if defined(MBEDTLS_SHA384_C) |
| 4016 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 4017 | #endif |
| 4018 | #if defined(MBEDTLS_SHA256_C) |
| 4019 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 4020 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4021 | MBEDTLS_TLS1_3_SIG_NONE |
| 4022 | }; |
| 4023 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4024 | /* NOTICE: see above */ |
| 4025 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4026 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4027 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 4028 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4029 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 4030 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 4031 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4032 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4033 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 4034 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4035 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 4036 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 4037 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4038 | |
| 4039 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 4040 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4041 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4042 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4043 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 4044 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4045 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4046 | |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4047 | MBEDTLS_TLS1_3_SIG_NONE |
| 4048 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4049 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4050 | /* NOTICE: see above */ |
| 4051 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4052 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4053 | #if defined(MBEDTLS_SHA256_C) |
| 4054 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 4055 | #endif |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 4056 | #if defined(MBEDTLS_SHA384_C) |
| 4057 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 4058 | #endif |
Xiaofei Bai | 9539501 | 2021-11-25 08:51:30 +0000 | [diff] [blame] | 4059 | MBEDTLS_TLS1_3_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4060 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4061 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4062 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4063 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4064 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4065 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4066 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4067 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4068 | #endif |
| 4069 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4070 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4071 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4072 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4073 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4074 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4075 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4076 | /* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs` |
Jerry Yu | 370e146 | 2022-01-25 10:36:53 +0800 | [diff] [blame] | 4077 | * to make sure there are no duplicated signature algorithm entries. */ |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4078 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4079 | { |
| 4080 | size_t i, j; |
| 4081 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4082 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4083 | for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4084 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4085 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4086 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4087 | if( sig_algs[i] != sig_algs[j] ) |
| 4088 | continue; |
| 4089 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4090 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4091 | sig_algs[i], j, i ); |
| 4092 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4093 | } |
| 4094 | } |
| 4095 | return( ret ); |
| 4096 | } |
| 4097 | |
| 4098 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4099 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4100 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4101 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4102 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4103 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4104 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4105 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4106 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4107 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4108 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4109 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4110 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4111 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4112 | { |
| 4113 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4114 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4115 | } |
| 4116 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4117 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4118 | { |
| 4119 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4120 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4121 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4122 | |
| 4123 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4124 | if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_suiteb_sig_algs ) ) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4125 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4126 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4127 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4128 | } |
| 4129 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4130 | if( ssl_check_no_sig_alg_duplication( ssl_tls12_preset_default_sig_algs ) ) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4131 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4132 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4133 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4134 | } |
| 4135 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4136 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4137 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4138 | /* Use the functions here so that they are covered in tests, |
| 4139 | * but otherwise access member directly for efficiency */ |
| 4140 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4141 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4142 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4143 | /* |
| 4144 | * Things that are common to all presets |
| 4145 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4146 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4147 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4148 | { |
| 4149 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4150 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4151 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4152 | #endif |
| 4153 | } |
| 4154 | #endif |
| 4155 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4156 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4157 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4158 | #endif |
| 4159 | |
| 4160 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4161 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4162 | #endif |
| 4163 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4164 | #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] | 4165 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4166 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4167 | #endif |
| 4168 | |
| 4169 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4170 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4171 | #endif |
| 4172 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4173 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4174 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4175 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4176 | #endif |
| 4177 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4178 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4179 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4180 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4181 | #endif |
| 4182 | |
| 4183 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4184 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4185 | memset( conf->renego_period, 0x00, 2 ); |
| 4186 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4187 | #endif |
| 4188 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4189 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4190 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4191 | { |
| 4192 | const unsigned char dhm_p[] = |
| 4193 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4194 | const unsigned char dhm_g[] = |
| 4195 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4196 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4197 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4198 | dhm_p, sizeof( dhm_p ), |
| 4199 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4200 | { |
| 4201 | return( ret ); |
| 4202 | } |
| 4203 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4204 | #endif |
| 4205 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4206 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4207 | /* |
| 4208 | * Allow all TLS 1.3 key exchange modes by default. |
| 4209 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4210 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4211 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4212 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4213 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4214 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4215 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4216 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4217 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4218 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4219 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4220 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4221 | } |
| 4222 | else |
| 4223 | { |
| 4224 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4225 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4226 | { |
| 4227 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4228 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4229 | } |
| 4230 | else |
| 4231 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4232 | { |
| 4233 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4234 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4235 | } |
| 4236 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4237 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4238 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4239 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4240 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4241 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4242 | #else |
| 4243 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4244 | #endif |
| 4245 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4246 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4247 | /* |
| 4248 | * Preset-specific defaults |
| 4249 | */ |
| 4250 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4251 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4252 | /* |
| 4253 | * NSA Suite B |
| 4254 | */ |
| 4255 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4256 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4257 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4258 | |
| 4259 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4260 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4261 | #endif |
| 4262 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4263 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4264 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4265 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4266 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4267 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4268 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4269 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4270 | else |
| 4271 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4272 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4273 | #endif |
| 4274 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4275 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4276 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4277 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4278 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4279 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4280 | |
| 4281 | /* |
| 4282 | * Default |
| 4283 | */ |
| 4284 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4285 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4286 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4287 | |
| 4288 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4289 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4290 | #endif |
| 4291 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4292 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4293 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4294 | conf->sig_hashes = ssl_preset_default_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4295 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4296 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4297 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4298 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4299 | else |
| 4300 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4301 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4302 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4303 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4304 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4305 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4306 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4307 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4308 | |
| 4309 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4310 | conf->dhm_min_bitlen = 1024; |
| 4311 | #endif |
| 4312 | } |
| 4313 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4314 | return( 0 ); |
| 4315 | } |
| 4316 | |
| 4317 | /* |
| 4318 | * Free mbedtls_ssl_config |
| 4319 | */ |
| 4320 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4321 | { |
| 4322 | #if defined(MBEDTLS_DHM_C) |
| 4323 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4324 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4325 | #endif |
| 4326 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4327 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4328 | if( conf->psk != NULL ) |
| 4329 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4330 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4331 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4332 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4333 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | if( conf->psk_identity != NULL ) |
| 4337 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4338 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4339 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4340 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4341 | conf->psk_identity_len = 0; |
| 4342 | } |
| 4343 | #endif |
| 4344 | |
| 4345 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4346 | ssl_key_cert_free( conf->key_cert ); |
| 4347 | #endif |
| 4348 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4349 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4350 | } |
| 4351 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4352 | #if defined(MBEDTLS_PK_C) && \ |
| 4353 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4354 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4355 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4356 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4357 | 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] | 4358 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4359 | #if defined(MBEDTLS_RSA_C) |
| 4360 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 4361 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4362 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4363 | #if defined(MBEDTLS_ECDSA_C) |
| 4364 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 4365 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4366 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4367 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4368 | } |
| 4369 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4370 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 4371 | { |
| 4372 | switch( type ) { |
| 4373 | case MBEDTLS_PK_RSA: |
| 4374 | return( MBEDTLS_SSL_SIG_RSA ); |
| 4375 | case MBEDTLS_PK_ECDSA: |
| 4376 | case MBEDTLS_PK_ECKEY: |
| 4377 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 4378 | default: |
| 4379 | return( MBEDTLS_SSL_SIG_ANON ); |
| 4380 | } |
| 4381 | } |
| 4382 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4383 | 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] | 4384 | { |
| 4385 | switch( sig ) |
| 4386 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4387 | #if defined(MBEDTLS_RSA_C) |
| 4388 | case MBEDTLS_SSL_SIG_RSA: |
| 4389 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4390 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4391 | #if defined(MBEDTLS_ECDSA_C) |
| 4392 | case MBEDTLS_SSL_SIG_ECDSA: |
| 4393 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4394 | #endif |
| 4395 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4396 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4397 | } |
| 4398 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4399 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4400 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4401 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4402 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4403 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4404 | 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] | 4405 | { |
| 4406 | switch( hash ) |
| 4407 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4408 | #if defined(MBEDTLS_MD5_C) |
| 4409 | case MBEDTLS_SSL_HASH_MD5: |
| 4410 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4411 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4412 | #if defined(MBEDTLS_SHA1_C) |
| 4413 | case MBEDTLS_SSL_HASH_SHA1: |
| 4414 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4415 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4416 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4417 | case MBEDTLS_SSL_HASH_SHA224: |
| 4418 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4419 | #endif |
| 4420 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4421 | case MBEDTLS_SSL_HASH_SHA256: |
| 4422 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4423 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4424 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4425 | case MBEDTLS_SSL_HASH_SHA384: |
| 4426 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4427 | #endif |
| 4428 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4429 | case MBEDTLS_SSL_HASH_SHA512: |
| 4430 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4431 | #endif |
| 4432 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4433 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4434 | } |
| 4435 | } |
| 4436 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4437 | /* |
| 4438 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 4439 | */ |
| 4440 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 4441 | { |
| 4442 | switch( md ) |
| 4443 | { |
| 4444 | #if defined(MBEDTLS_MD5_C) |
| 4445 | case MBEDTLS_MD_MD5: |
| 4446 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 4447 | #endif |
| 4448 | #if defined(MBEDTLS_SHA1_C) |
| 4449 | case MBEDTLS_MD_SHA1: |
| 4450 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 4451 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4452 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4453 | case MBEDTLS_MD_SHA224: |
| 4454 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4455 | #endif |
| 4456 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4457 | case MBEDTLS_MD_SHA256: |
| 4458 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 4459 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4460 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4461 | case MBEDTLS_MD_SHA384: |
| 4462 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4463 | #endif |
| 4464 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4465 | case MBEDTLS_MD_SHA512: |
| 4466 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 4467 | #endif |
| 4468 | default: |
| 4469 | return( MBEDTLS_SSL_HASH_NONE ); |
| 4470 | } |
| 4471 | } |
| 4472 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4473 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4474 | * 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] | 4475 | * 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] | 4476 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4477 | int mbedtls_ssl_check_curve_tls_id( const mbedtls_ssl_context *ssl, uint16_t tls_id ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4478 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4479 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4480 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4481 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4482 | return( -1 ); |
| 4483 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4484 | for( ; *group_list != 0; group_list++ ) |
| 4485 | { |
| 4486 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4487 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4488 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4489 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4490 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4491 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4492 | |
| 4493 | #if defined(MBEDTLS_ECP_C) |
| 4494 | /* |
| 4495 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 4496 | */ |
| 4497 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 4498 | { |
Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 4499 | uint16_t tls_id = mbedtls_ecp_curve_info_from_grp_id( grp_id )->tls_id; |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4500 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 4501 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4502 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4504 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4505 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 4506 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4507 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4508 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4509 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4510 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4511 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4512 | const char *ext_oid; |
| 4513 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4515 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4516 | { |
| 4517 | /* Server part of the key exchange */ |
| 4518 | switch( ciphersuite->key_exchange ) |
| 4519 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4520 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 4521 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4522 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4523 | break; |
| 4524 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4525 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 4526 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 4527 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 4528 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4529 | break; |
| 4530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4531 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 4532 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4533 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4534 | break; |
| 4535 | |
| 4536 | /* 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] | 4537 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 4538 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 4539 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 4540 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 4541 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4542 | usage = 0; |
| 4543 | } |
| 4544 | } |
| 4545 | else |
| 4546 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4547 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 4548 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4549 | } |
| 4550 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4551 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4552 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4553 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4554 | ret = -1; |
| 4555 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4556 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4557 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4559 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 4560 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4561 | } |
| 4562 | else |
| 4563 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4564 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 4565 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4566 | } |
| 4567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4568 | 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] | 4569 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4570 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4571 | ret = -1; |
| 4572 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4573 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4574 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4575 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4576 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 4577 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4578 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4579 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 4580 | const mbedtls_md_type_t md, |
| 4581 | unsigned char *dst, |
| 4582 | size_t dst_len, |
| 4583 | size_t *olen ) |
| 4584 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4585 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4586 | psa_hash_operation_t *hash_operation_to_clone; |
| 4587 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 4588 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4589 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4590 | |
| 4591 | switch( md ) |
| 4592 | { |
| 4593 | #if defined(MBEDTLS_SHA384_C) |
| 4594 | case MBEDTLS_MD_SHA384: |
| 4595 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 4596 | break; |
| 4597 | #endif |
| 4598 | |
| 4599 | #if defined(MBEDTLS_SHA256_C) |
| 4600 | case MBEDTLS_MD_SHA256: |
| 4601 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 4602 | break; |
| 4603 | #endif |
| 4604 | |
| 4605 | default: |
| 4606 | goto exit; |
| 4607 | } |
| 4608 | |
| 4609 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 4610 | if( status != PSA_SUCCESS ) |
| 4611 | goto exit; |
| 4612 | |
| 4613 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 4614 | if( status != PSA_SUCCESS ) |
| 4615 | goto exit; |
| 4616 | |
| 4617 | exit: |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 4618 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4619 | } |
| 4620 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4621 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4622 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4623 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 4624 | unsigned char *dst, |
| 4625 | size_t dst_len, |
| 4626 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4627 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4628 | int ret; |
| 4629 | mbedtls_sha512_context sha512; |
| 4630 | |
| 4631 | if( dst_len < 48 ) |
| 4632 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4633 | |
| 4634 | mbedtls_sha512_init( &sha512 ); |
| 4635 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 4636 | |
| 4637 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 4638 | { |
| 4639 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 4640 | goto exit; |
| 4641 | } |
| 4642 | |
| 4643 | *olen = 48; |
| 4644 | |
| 4645 | exit: |
| 4646 | |
| 4647 | mbedtls_sha512_free( &sha512 ); |
| 4648 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4649 | } |
| 4650 | #endif /* MBEDTLS_SHA384_C */ |
| 4651 | |
| 4652 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4653 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 4654 | unsigned char *dst, |
| 4655 | size_t dst_len, |
| 4656 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4657 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4658 | int ret; |
| 4659 | mbedtls_sha256_context sha256; |
| 4660 | |
| 4661 | if( dst_len < 32 ) |
| 4662 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4663 | |
| 4664 | mbedtls_sha256_init( &sha256 ); |
| 4665 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4666 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4667 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 4668 | { |
| 4669 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 4670 | goto exit; |
| 4671 | } |
| 4672 | |
| 4673 | *olen = 32; |
| 4674 | |
| 4675 | exit: |
| 4676 | |
| 4677 | mbedtls_sha256_free( &sha256 ); |
| 4678 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4679 | } |
| 4680 | #endif /* MBEDTLS_SHA256_C */ |
| 4681 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4682 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 4683 | const mbedtls_md_type_t md, |
| 4684 | unsigned char *dst, |
| 4685 | size_t dst_len, |
| 4686 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4687 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4688 | switch( md ) |
| 4689 | { |
| 4690 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4691 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4692 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4693 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4694 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4695 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4696 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4697 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4698 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4699 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4700 | |
| 4701 | default: |
| 4702 | break; |
| 4703 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4704 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4705 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 4706 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4707 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4708 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4709 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4710 | |
| 4711 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4712 | |
| 4713 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 4714 | mbedtls_svc_key_id_t key, |
| 4715 | psa_algorithm_t alg, |
| 4716 | const unsigned char* seed, size_t seed_length, |
| 4717 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 4718 | const unsigned char* salt, size_t salt_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4719 | size_t capacity ) |
| 4720 | { |
| 4721 | psa_status_t status; |
| 4722 | |
| 4723 | status = psa_key_derivation_setup( derivation, alg ); |
| 4724 | if( status != PSA_SUCCESS ) |
| 4725 | return( status ); |
| 4726 | |
| 4727 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 4728 | { |
| 4729 | status = psa_key_derivation_input_bytes( derivation, |
| 4730 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 4731 | seed, seed_length ); |
| 4732 | if( status != PSA_SUCCESS ) |
| 4733 | return( status ); |
| 4734 | |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 4735 | if ( salt != NULL ) |
| 4736 | { |
| 4737 | status = psa_key_derivation_input_bytes( derivation, |
| 4738 | PSA_KEY_DERIVATION_INPUT_SALT, |
| 4739 | salt, salt_length ); |
| 4740 | if( status != PSA_SUCCESS ) |
| 4741 | return( status ); |
| 4742 | } |
| 4743 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4744 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 4745 | { |
| 4746 | status = psa_key_derivation_input_bytes( |
| 4747 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4748 | NULL, 0 ); |
| 4749 | } |
| 4750 | else |
| 4751 | { |
| 4752 | status = psa_key_derivation_input_key( |
| 4753 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 4754 | } |
| 4755 | if( status != PSA_SUCCESS ) |
| 4756 | return( status ); |
| 4757 | |
| 4758 | status = psa_key_derivation_input_bytes( derivation, |
| 4759 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 4760 | label, label_length ); |
| 4761 | if( status != PSA_SUCCESS ) |
| 4762 | return( status ); |
| 4763 | } |
| 4764 | else |
| 4765 | { |
| 4766 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4767 | } |
| 4768 | |
| 4769 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 4770 | if( status != PSA_SUCCESS ) |
| 4771 | return( status ); |
| 4772 | |
| 4773 | return( PSA_SUCCESS ); |
| 4774 | } |
| 4775 | |
| 4776 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 4777 | const unsigned char *secret, size_t slen, |
| 4778 | const char *label, |
| 4779 | const unsigned char *random, size_t rlen, |
| 4780 | unsigned char *dstbuf, size_t dlen ) |
| 4781 | { |
| 4782 | psa_status_t status; |
| 4783 | psa_algorithm_t alg; |
| 4784 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4785 | psa_key_derivation_operation_t derivation = |
| 4786 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4787 | |
| 4788 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 4789 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 4790 | else |
| 4791 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 4792 | |
| 4793 | /* Normally a "secret" should be long enough to be impossible to |
| 4794 | * find by brute force, and in particular should not be empty. But |
| 4795 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 4796 | * and for this use case it makes sense to have a 0-length "secret". |
| 4797 | * Since the key API doesn't allow importing a key of length 0, |
| 4798 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 4799 | * to mean a 0-length "secret" input. */ |
| 4800 | if( slen != 0 ) |
| 4801 | { |
| 4802 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 4803 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4804 | psa_set_key_algorithm( &key_attributes, alg ); |
| 4805 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4806 | |
| 4807 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 4808 | if( status != PSA_SUCCESS ) |
| 4809 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4810 | } |
| 4811 | |
| 4812 | status = setup_psa_key_derivation( &derivation, |
| 4813 | master_key, alg, |
| 4814 | random, rlen, |
| 4815 | (unsigned char const *) label, |
| 4816 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 4817 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4818 | dlen ); |
| 4819 | if( status != PSA_SUCCESS ) |
| 4820 | { |
| 4821 | psa_key_derivation_abort( &derivation ); |
| 4822 | psa_destroy_key( master_key ); |
| 4823 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4824 | } |
| 4825 | |
| 4826 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 4827 | if( status != PSA_SUCCESS ) |
| 4828 | { |
| 4829 | psa_key_derivation_abort( &derivation ); |
| 4830 | psa_destroy_key( master_key ); |
| 4831 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4832 | } |
| 4833 | |
| 4834 | status = psa_key_derivation_abort( &derivation ); |
| 4835 | if( status != PSA_SUCCESS ) |
| 4836 | { |
| 4837 | psa_destroy_key( master_key ); |
| 4838 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4839 | } |
| 4840 | |
| 4841 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 4842 | status = psa_destroy_key( master_key ); |
| 4843 | if( status != PSA_SUCCESS ) |
| 4844 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4845 | |
| 4846 | return( 0 ); |
| 4847 | } |
| 4848 | |
| 4849 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4850 | |
| 4851 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 4852 | const unsigned char *secret, size_t slen, |
| 4853 | const char *label, |
| 4854 | const unsigned char *random, size_t rlen, |
| 4855 | unsigned char *dstbuf, size_t dlen ) |
| 4856 | { |
| 4857 | size_t nb; |
| 4858 | size_t i, j, k, md_len; |
| 4859 | unsigned char *tmp; |
| 4860 | size_t tmp_len = 0; |
| 4861 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 4862 | const mbedtls_md_info_t *md_info; |
| 4863 | mbedtls_md_context_t md_ctx; |
| 4864 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 4865 | |
| 4866 | mbedtls_md_init( &md_ctx ); |
| 4867 | |
| 4868 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 4869 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4870 | |
| 4871 | md_len = mbedtls_md_get_size( md_info ); |
| 4872 | |
| 4873 | tmp_len = md_len + strlen( label ) + rlen; |
| 4874 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 4875 | if( tmp == NULL ) |
| 4876 | { |
| 4877 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 4878 | goto exit; |
| 4879 | } |
| 4880 | |
| 4881 | nb = strlen( label ); |
| 4882 | memcpy( tmp + md_len, label, nb ); |
| 4883 | memcpy( tmp + md_len + nb, random, rlen ); |
| 4884 | nb += rlen; |
| 4885 | |
| 4886 | /* |
| 4887 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 4888 | */ |
| 4889 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 4890 | goto exit; |
| 4891 | |
| 4892 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 4893 | if( ret != 0 ) |
| 4894 | goto exit; |
| 4895 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 4896 | if( ret != 0 ) |
| 4897 | goto exit; |
| 4898 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 4899 | if( ret != 0 ) |
| 4900 | goto exit; |
| 4901 | |
| 4902 | for( i = 0; i < dlen; i += md_len ) |
| 4903 | { |
| 4904 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 4905 | if( ret != 0 ) |
| 4906 | goto exit; |
| 4907 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 4908 | if( ret != 0 ) |
| 4909 | goto exit; |
| 4910 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 4911 | if( ret != 0 ) |
| 4912 | goto exit; |
| 4913 | |
| 4914 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 4915 | if( ret != 0 ) |
| 4916 | goto exit; |
| 4917 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 4918 | if( ret != 0 ) |
| 4919 | goto exit; |
| 4920 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 4921 | if( ret != 0 ) |
| 4922 | goto exit; |
| 4923 | |
| 4924 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 4925 | |
| 4926 | for( j = 0; j < k; j++ ) |
| 4927 | dstbuf[i + j] = h_i[j]; |
| 4928 | } |
| 4929 | |
| 4930 | exit: |
| 4931 | mbedtls_md_free( &md_ctx ); |
| 4932 | |
| 4933 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 4934 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 4935 | |
| 4936 | mbedtls_free( tmp ); |
| 4937 | |
| 4938 | return( ret ); |
| 4939 | } |
| 4940 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4941 | |
| 4942 | #if defined(MBEDTLS_SHA256_C) |
| 4943 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 4944 | const char *label, |
| 4945 | const unsigned char *random, size_t rlen, |
| 4946 | unsigned char *dstbuf, size_t dlen ) |
| 4947 | { |
| 4948 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 4949 | label, random, rlen, dstbuf, dlen ) ); |
| 4950 | } |
| 4951 | #endif /* MBEDTLS_SHA256_C */ |
| 4952 | |
| 4953 | #if defined(MBEDTLS_SHA384_C) |
| 4954 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 4955 | const char *label, |
| 4956 | const unsigned char *random, size_t rlen, |
| 4957 | unsigned char *dstbuf, size_t dlen ) |
| 4958 | { |
| 4959 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 4960 | label, random, rlen, dstbuf, dlen ) ); |
| 4961 | } |
| 4962 | #endif /* MBEDTLS_SHA384_C */ |
| 4963 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4964 | /* |
| 4965 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 4966 | * |
| 4967 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4968 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 4969 | * |
| 4970 | * Outputs: |
| 4971 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 4972 | */ |
| 4973 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4974 | mbedtls_md_type_t hash ) |
| 4975 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4976 | #if defined(MBEDTLS_SHA384_C) |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 4977 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4978 | { |
| 4979 | handshake->tls_prf = tls_prf_sha384; |
| 4980 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 4981 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 4982 | } |
| 4983 | else |
| 4984 | #endif |
| 4985 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4986 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 4987 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4988 | handshake->tls_prf = tls_prf_sha256; |
| 4989 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 4990 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 4991 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 4992 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4993 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4994 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 4995 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4996 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4997 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 4998 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 4999 | |
| 5000 | return( 0 ); |
| 5001 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5002 | |
| 5003 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
| 5004 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5005 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 5006 | { |
| 5007 | if( ssl->conf->f_psk != NULL ) |
| 5008 | { |
| 5009 | /* If we've used a callback to select the PSK, |
| 5010 | * the static configuration is irrelevant. */ |
| 5011 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 5012 | return( 1 ); |
| 5013 | |
| 5014 | return( 0 ); |
| 5015 | } |
| 5016 | |
| 5017 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) |
| 5018 | return( 1 ); |
| 5019 | |
| 5020 | return( 0 ); |
| 5021 | } |
| 5022 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
| 5023 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 5024 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5025 | /* |
| 5026 | * Compute master secret if needed |
| 5027 | * |
| 5028 | * Parameters: |
| 5029 | * [in/out] handshake |
| 5030 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5031 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5032 | * [out] premaster (cleared) |
| 5033 | * [out] master |
| 5034 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5035 | * debug: conf->f_dbg, conf->p_dbg |
| 5036 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5037 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5038 | */ |
| 5039 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5040 | unsigned char *master, |
| 5041 | const mbedtls_ssl_context *ssl ) |
| 5042 | { |
| 5043 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5044 | |
| 5045 | /* cf. RFC 5246, Section 8.1: |
| 5046 | * "The master secret is always exactly 48 bytes in length." */ |
| 5047 | size_t const master_secret_len = 48; |
| 5048 | |
| 5049 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5050 | unsigned char session_hash[48]; |
| 5051 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5052 | |
| 5053 | /* The label for the KDF used for key expansion. |
| 5054 | * This is either "master secret" or "extended master secret" |
| 5055 | * depending on whether the Extended Master Secret extension |
| 5056 | * is used. */ |
| 5057 | char const *lbl = "master secret"; |
| 5058 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame^] | 5059 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5060 | * - If the Extended Master Secret extension is not used, |
| 5061 | * this is ClientHello.Random + ServerHello.Random |
| 5062 | * (see Sect. 8.1 in RFC 5246). |
| 5063 | * - If the Extended Master Secret extension is used, |
| 5064 | * this is the transcript of the handshake so far. |
| 5065 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame^] | 5066 | unsigned char const *seed = handshake->randbytes; |
| 5067 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5068 | |
| 5069 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5070 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5071 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5072 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5073 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5074 | (void) ssl; |
| 5075 | #endif |
| 5076 | |
| 5077 | if( handshake->resume != 0 ) |
| 5078 | { |
| 5079 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5080 | return( 0 ); |
| 5081 | } |
| 5082 | |
| 5083 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5084 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5085 | { |
| 5086 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame^] | 5087 | seed = session_hash; |
| 5088 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5089 | |
| 5090 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame^] | 5091 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5092 | } |
| 5093 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 5094 | |
| 5095 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5096 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 5097 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5098 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 5099 | { |
| 5100 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5101 | psa_status_t status; |
| 5102 | psa_algorithm_t alg; |
| 5103 | mbedtls_svc_key_id_t psk; |
| 5104 | psa_key_derivation_operation_t derivation = |
| 5105 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5106 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5107 | |
| 5108 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5109 | |
| 5110 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5111 | |
| 5112 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5113 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5114 | else |
| 5115 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5116 | |
| 5117 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame^] | 5118 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5119 | (unsigned char const *) lbl, |
| 5120 | (size_t) strlen( lbl ), |
| 5121 | master_secret_len ); |
| 5122 | if( status != PSA_SUCCESS ) |
| 5123 | { |
| 5124 | psa_key_derivation_abort( &derivation ); |
| 5125 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5126 | } |
| 5127 | |
| 5128 | status = psa_key_derivation_output_bytes( &derivation, |
| 5129 | master, |
| 5130 | master_secret_len ); |
| 5131 | if( status != PSA_SUCCESS ) |
| 5132 | { |
| 5133 | psa_key_derivation_abort( &derivation ); |
| 5134 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5135 | } |
| 5136 | |
| 5137 | status = psa_key_derivation_abort( &derivation ); |
| 5138 | if( status != PSA_SUCCESS ) |
| 5139 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5140 | } |
| 5141 | else |
| 5142 | #endif |
| 5143 | { |
| 5144 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame^] | 5145 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5146 | master, |
| 5147 | master_secret_len ); |
| 5148 | if( ret != 0 ) |
| 5149 | { |
| 5150 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5151 | return( ret ); |
| 5152 | } |
| 5153 | |
| 5154 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5155 | handshake->premaster, |
| 5156 | handshake->pmslen ); |
| 5157 | |
| 5158 | mbedtls_platform_zeroize( handshake->premaster, |
| 5159 | sizeof(handshake->premaster) ); |
| 5160 | } |
| 5161 | |
| 5162 | return( 0 ); |
| 5163 | } |
| 5164 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5165 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5166 | { |
| 5167 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5168 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5169 | ssl->handshake->ciphersuite_info; |
| 5170 | |
| 5171 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5172 | |
| 5173 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5174 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5175 | ciphersuite_info->mac ); |
| 5176 | if( ret != 0 ) |
| 5177 | { |
| 5178 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5179 | return( ret ); |
| 5180 | } |
| 5181 | |
| 5182 | /* Compute master secret if needed */ |
| 5183 | ret = ssl_compute_master( ssl->handshake, |
| 5184 | ssl->session_negotiate->master, |
| 5185 | ssl ); |
| 5186 | if( ret != 0 ) |
| 5187 | { |
| 5188 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5189 | return( ret ); |
| 5190 | } |
| 5191 | |
| 5192 | /* Swap the client and server random values: |
| 5193 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 5194 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 5195 | { |
| 5196 | unsigned char tmp[64]; |
| 5197 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 5198 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 5199 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 5200 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 5201 | } |
| 5202 | |
| 5203 | /* Populate transform structure */ |
| 5204 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 5205 | ssl->session_negotiate->ciphersuite, |
| 5206 | ssl->session_negotiate->master, |
| 5207 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 5208 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5209 | ssl->session_negotiate->encrypt_then_mac, |
| 5210 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 5211 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 5212 | ssl->handshake->tls_prf, |
| 5213 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5214 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5215 | ssl->conf->endpoint, |
| 5216 | ssl ); |
| 5217 | if( ret != 0 ) |
| 5218 | { |
| 5219 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 5220 | return( ret ); |
| 5221 | } |
| 5222 | |
| 5223 | /* We no longer need Server/ClientHello.random values */ |
| 5224 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 5225 | sizeof( ssl->handshake->randbytes ) ); |
| 5226 | |
| 5227 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 5228 | |
| 5229 | return( 0 ); |
| 5230 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5231 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5232 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 5233 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5234 | switch( md ) |
| 5235 | { |
| 5236 | #if defined(MBEDTLS_SHA384_C) |
| 5237 | case MBEDTLS_SSL_HASH_SHA384: |
| 5238 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5239 | break; |
| 5240 | #endif |
| 5241 | #if defined(MBEDTLS_SHA256_C) |
| 5242 | case MBEDTLS_SSL_HASH_SHA256: |
| 5243 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5244 | break; |
| 5245 | #endif |
| 5246 | default: |
| 5247 | return( -1 ); |
| 5248 | } |
| 5249 | |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5250 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5251 | } |
| 5252 | |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5253 | #if defined(MBEDTLS_SHA256_C) |
| 5254 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 5255 | unsigned char *hash, |
| 5256 | size_t *hlen ) |
| 5257 | { |
| 5258 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5259 | size_t hash_size; |
| 5260 | psa_status_t status; |
| 5261 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 5262 | |
| 5263 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 5264 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 5265 | if( status != PSA_SUCCESS ) |
| 5266 | { |
| 5267 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5268 | return; |
| 5269 | } |
| 5270 | |
| 5271 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 5272 | if( status != PSA_SUCCESS ) |
| 5273 | { |
| 5274 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5275 | return; |
| 5276 | } |
| 5277 | |
| 5278 | *hlen = 32; |
| 5279 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5280 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5281 | #else |
| 5282 | mbedtls_sha256_context sha256; |
| 5283 | |
| 5284 | mbedtls_sha256_init( &sha256 ); |
| 5285 | |
| 5286 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 5287 | |
| 5288 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 5289 | mbedtls_sha256_finish( &sha256, hash ); |
| 5290 | |
| 5291 | *hlen = 32; |
| 5292 | |
| 5293 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5294 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5295 | |
| 5296 | mbedtls_sha256_free( &sha256 ); |
| 5297 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5298 | return; |
| 5299 | } |
| 5300 | #endif /* MBEDTLS_SHA256_C */ |
| 5301 | |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5302 | #if defined(MBEDTLS_SHA384_C) |
| 5303 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 5304 | unsigned char *hash, |
| 5305 | size_t *hlen ) |
| 5306 | { |
| 5307 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5308 | size_t hash_size; |
| 5309 | psa_status_t status; |
| 5310 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 5311 | |
| 5312 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 5313 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 5314 | if( status != PSA_SUCCESS ) |
| 5315 | { |
| 5316 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5317 | return; |
| 5318 | } |
| 5319 | |
| 5320 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 5321 | if( status != PSA_SUCCESS ) |
| 5322 | { |
| 5323 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5324 | return; |
| 5325 | } |
| 5326 | |
| 5327 | *hlen = 48; |
| 5328 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5329 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5330 | #else |
| 5331 | mbedtls_sha512_context sha512; |
| 5332 | |
| 5333 | mbedtls_sha512_init( &sha512 ); |
| 5334 | |
| 5335 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 5336 | |
| 5337 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 5338 | mbedtls_sha512_finish( &sha512, hash ); |
| 5339 | |
| 5340 | *hlen = 48; |
| 5341 | |
| 5342 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5343 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5344 | |
| 5345 | mbedtls_sha512_free( &sha512 ); |
| 5346 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5347 | return; |
| 5348 | } |
| 5349 | #endif /* MBEDTLS_SHA384_C */ |
| 5350 | |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5351 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 5352 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 5353 | { |
| 5354 | unsigned char *p = ssl->handshake->premaster; |
| 5355 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 5356 | const unsigned char *psk = NULL; |
| 5357 | size_t psk_len = 0; |
| 5358 | |
Neil Armstrong | a33a255 | 2022-04-12 14:40:47 +0200 | [diff] [blame] | 5359 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5360 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 5361 | (void) key_ex; |
| 5362 | #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
| 5363 | |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5364 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) |
| 5365 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
| 5366 | { |
| 5367 | /* |
| 5368 | * This should never happen because the existence of a PSK is always |
| 5369 | * checked before calling this function |
| 5370 | */ |
| 5371 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5372 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5373 | } |
| 5374 | |
| 5375 | /* |
| 5376 | * PMS = struct { |
| 5377 | * opaque other_secret<0..2^16-1>; |
| 5378 | * opaque psk<0..2^16-1>; |
| 5379 | * }; |
| 5380 | * with "other_secret" depending on the particular key exchange |
| 5381 | */ |
| 5382 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 5383 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 5384 | { |
| 5385 | if( end - p < 2 ) |
| 5386 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5387 | |
| 5388 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5389 | p += 2; |
| 5390 | |
| 5391 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5392 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5393 | |
| 5394 | memset( p, 0, psk_len ); |
| 5395 | p += psk_len; |
| 5396 | } |
| 5397 | else |
| 5398 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 5399 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 5400 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 5401 | { |
| 5402 | /* |
| 5403 | * other_secret already set by the ClientKeyExchange message, |
| 5404 | * and is 48 bytes long |
| 5405 | */ |
| 5406 | if( end - p < 2 ) |
| 5407 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5408 | |
| 5409 | *p++ = 0; |
| 5410 | *p++ = 48; |
| 5411 | p += 48; |
| 5412 | } |
| 5413 | else |
| 5414 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 5415 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 5416 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 5417 | { |
| 5418 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5419 | size_t len; |
| 5420 | |
| 5421 | /* Write length only when we know the actual value */ |
| 5422 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 5423 | p + 2, end - ( p + 2 ), &len, |
| 5424 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5425 | { |
| 5426 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 5427 | return( ret ); |
| 5428 | } |
| 5429 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 5430 | p += 2 + len; |
| 5431 | |
| 5432 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 5433 | } |
| 5434 | else |
| 5435 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | a33a255 | 2022-04-12 14:40:47 +0200 | [diff] [blame] | 5436 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5437 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5438 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 5439 | { |
| 5440 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5441 | size_t zlen; |
| 5442 | |
| 5443 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 5444 | p + 2, end - ( p + 2 ), |
| 5445 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5446 | { |
| 5447 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 5448 | return( ret ); |
| 5449 | } |
| 5450 | |
| 5451 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 5452 | p += 2 + zlen; |
| 5453 | |
| 5454 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 5455 | MBEDTLS_DEBUG_ECDH_Z ); |
| 5456 | } |
| 5457 | else |
Neil Armstrong | a33a255 | 2022-04-12 14:40:47 +0200 | [diff] [blame] | 5458 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5459 | { |
| 5460 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5461 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5462 | } |
| 5463 | |
| 5464 | /* opaque psk<0..2^16-1>; */ |
| 5465 | if( end - p < 2 ) |
| 5466 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5467 | |
| 5468 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5469 | p += 2; |
| 5470 | |
| 5471 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5472 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5473 | |
| 5474 | memcpy( p, psk, psk_len ); |
| 5475 | p += psk_len; |
| 5476 | |
| 5477 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 5478 | |
| 5479 | return( 0 ); |
| 5480 | } |
| 5481 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 5482 | |
| 5483 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5484 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 5485 | |
| 5486 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5487 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 5488 | { |
| 5489 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 5490 | * timeout if we were using the usual handshake doubling scheme */ |
| 5491 | if( ssl->conf->renego_max_records < 0 ) |
| 5492 | { |
| 5493 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 5494 | unsigned char doublings = 1; |
| 5495 | |
| 5496 | while( ratio != 0 ) |
| 5497 | { |
| 5498 | ++doublings; |
| 5499 | ratio >>= 1; |
| 5500 | } |
| 5501 | |
| 5502 | if( ++ssl->renego_records_seen > doublings ) |
| 5503 | { |
| 5504 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 5505 | return( 0 ); |
| 5506 | } |
| 5507 | } |
| 5508 | |
| 5509 | return( ssl_write_hello_request( ssl ) ); |
| 5510 | } |
| 5511 | #endif |
| 5512 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5513 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5514 | /* |
| 5515 | * Handshake functions |
| 5516 | */ |
| 5517 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5518 | /* No certificate support -> dummy functions */ |
| 5519 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 5520 | { |
| 5521 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5522 | ssl->handshake->ciphersuite_info; |
| 5523 | |
| 5524 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 5525 | |
| 5526 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5527 | { |
| 5528 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5529 | ssl->state++; |
| 5530 | return( 0 ); |
| 5531 | } |
| 5532 | |
| 5533 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5534 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5535 | } |
| 5536 | |
| 5537 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 5538 | { |
| 5539 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5540 | ssl->handshake->ciphersuite_info; |
| 5541 | |
| 5542 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 5543 | |
| 5544 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5545 | { |
| 5546 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 5547 | ssl->state++; |
| 5548 | return( 0 ); |
| 5549 | } |
| 5550 | |
| 5551 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5552 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5553 | } |
| 5554 | |
| 5555 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5556 | /* Some certificate support -> implement write and parse */ |
| 5557 | |
| 5558 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 5559 | { |
| 5560 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 5561 | size_t i, n; |
| 5562 | const mbedtls_x509_crt *crt; |
| 5563 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5564 | ssl->handshake->ciphersuite_info; |
| 5565 | |
| 5566 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 5567 | |
| 5568 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5569 | { |
| 5570 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5571 | ssl->state++; |
| 5572 | return( 0 ); |
| 5573 | } |
| 5574 | |
| 5575 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5576 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5577 | { |
| 5578 | if( ssl->handshake->client_auth == 0 ) |
| 5579 | { |
| 5580 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5581 | ssl->state++; |
| 5582 | return( 0 ); |
| 5583 | } |
| 5584 | } |
| 5585 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 5586 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5587 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 5588 | { |
| 5589 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 5590 | { |
| 5591 | /* Should never happen because we shouldn't have picked the |
| 5592 | * ciphersuite if we don't have a certificate. */ |
| 5593 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5594 | } |
| 5595 | } |
| 5596 | #endif |
| 5597 | |
| 5598 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 5599 | |
| 5600 | /* |
| 5601 | * 0 . 0 handshake type |
| 5602 | * 1 . 3 handshake length |
| 5603 | * 4 . 6 length of all certs |
| 5604 | * 7 . 9 length of cert. 1 |
| 5605 | * 10 . n-1 peer certificate |
| 5606 | * n . n+2 length of cert. 2 |
| 5607 | * n+3 . ... upper level cert, etc. |
| 5608 | */ |
| 5609 | i = 7; |
| 5610 | crt = mbedtls_ssl_own_cert( ssl ); |
| 5611 | |
| 5612 | while( crt != NULL ) |
| 5613 | { |
| 5614 | n = crt->raw.len; |
| 5615 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 5616 | { |
| 5617 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 5618 | " > %" MBEDTLS_PRINTF_SIZET, |
| 5619 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 5620 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 5621 | } |
| 5622 | |
| 5623 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 5624 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 5625 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 5626 | |
| 5627 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 5628 | i += n; crt = crt->next; |
| 5629 | } |
| 5630 | |
| 5631 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 5632 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 5633 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 5634 | |
| 5635 | ssl->out_msglen = i; |
| 5636 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5637 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 5638 | |
| 5639 | ssl->state++; |
| 5640 | |
| 5641 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 5642 | { |
| 5643 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 5644 | return( ret ); |
| 5645 | } |
| 5646 | |
| 5647 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 5648 | |
| 5649 | return( ret ); |
| 5650 | } |
| 5651 | |
| 5652 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5653 | |
| 5654 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5655 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 5656 | unsigned char *crt_buf, |
| 5657 | size_t crt_buf_len ) |
| 5658 | { |
| 5659 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 5660 | |
| 5661 | if( peer_crt == NULL ) |
| 5662 | return( -1 ); |
| 5663 | |
| 5664 | if( peer_crt->raw.len != crt_buf_len ) |
| 5665 | return( -1 ); |
| 5666 | |
| 5667 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 5668 | } |
| 5669 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5670 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 5671 | unsigned char *crt_buf, |
| 5672 | size_t crt_buf_len ) |
| 5673 | { |
| 5674 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5675 | unsigned char const * const peer_cert_digest = |
| 5676 | ssl->session->peer_cert_digest; |
| 5677 | mbedtls_md_type_t const peer_cert_digest_type = |
| 5678 | ssl->session->peer_cert_digest_type; |
| 5679 | mbedtls_md_info_t const * const digest_info = |
| 5680 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 5681 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 5682 | size_t digest_len; |
| 5683 | |
| 5684 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 5685 | return( -1 ); |
| 5686 | |
| 5687 | digest_len = mbedtls_md_get_size( digest_info ); |
| 5688 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 5689 | return( -1 ); |
| 5690 | |
| 5691 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 5692 | if( ret != 0 ) |
| 5693 | return( -1 ); |
| 5694 | |
| 5695 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 5696 | } |
| 5697 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5698 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 5699 | |
| 5700 | /* |
| 5701 | * Once the certificate message is read, parse it into a cert chain and |
| 5702 | * perform basic checks, but leave actual verification to the caller |
| 5703 | */ |
| 5704 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 5705 | mbedtls_x509_crt *chain ) |
| 5706 | { |
| 5707 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5708 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5709 | int crt_cnt=0; |
| 5710 | #endif |
| 5711 | size_t i, n; |
| 5712 | uint8_t alert; |
| 5713 | |
| 5714 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 5715 | { |
| 5716 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5717 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5718 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 5719 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5720 | } |
| 5721 | |
| 5722 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 5723 | { |
| 5724 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5725 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 5726 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5727 | } |
| 5728 | |
| 5729 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 5730 | { |
| 5731 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5732 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5733 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5734 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5735 | } |
| 5736 | |
| 5737 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 5738 | |
| 5739 | /* |
| 5740 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 5741 | */ |
| 5742 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 5743 | |
| 5744 | if( ssl->in_msg[i] != 0 || |
| 5745 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 5746 | { |
| 5747 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5748 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5749 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5750 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5751 | } |
| 5752 | |
| 5753 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 5754 | i += 3; |
| 5755 | |
| 5756 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 5757 | while( i < ssl->in_hslen ) |
| 5758 | { |
| 5759 | /* Check that there's room for the next CRT's length fields. */ |
| 5760 | if ( i + 3 > ssl->in_hslen ) { |
| 5761 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5762 | mbedtls_ssl_send_alert_message( ssl, |
| 5763 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5764 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5765 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5766 | } |
| 5767 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 5768 | * anything beyond 2**16 ~ 64K. */ |
| 5769 | if( ssl->in_msg[i] != 0 ) |
| 5770 | { |
| 5771 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5772 | mbedtls_ssl_send_alert_message( ssl, |
| 5773 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5774 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 5775 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 5776 | } |
| 5777 | |
| 5778 | /* Read length of the next CRT in the chain. */ |
| 5779 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 5780 | | (unsigned int) ssl->in_msg[i + 2]; |
| 5781 | i += 3; |
| 5782 | |
| 5783 | if( n < 128 || i + n > ssl->in_hslen ) |
| 5784 | { |
| 5785 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5786 | mbedtls_ssl_send_alert_message( ssl, |
| 5787 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5788 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5789 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5790 | } |
| 5791 | |
| 5792 | /* Check if we're handling the first CRT in the chain. */ |
| 5793 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5794 | if( crt_cnt++ == 0 && |
| 5795 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 5796 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 5797 | { |
| 5798 | /* During client-side renegotiation, check that the server's |
| 5799 | * end-CRTs hasn't changed compared to the initial handshake, |
| 5800 | * mitigating the triple handshake attack. On success, reuse |
| 5801 | * the original end-CRT instead of parsing it again. */ |
| 5802 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 5803 | if( ssl_check_peer_crt_unchanged( ssl, |
| 5804 | &ssl->in_msg[i], |
| 5805 | n ) != 0 ) |
| 5806 | { |
| 5807 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 5808 | mbedtls_ssl_send_alert_message( ssl, |
| 5809 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5810 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 5811 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 5812 | } |
| 5813 | |
| 5814 | /* Now we can safely free the original chain. */ |
| 5815 | ssl_clear_peer_cert( ssl->session ); |
| 5816 | } |
| 5817 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 5818 | |
| 5819 | /* Parse the next certificate in the chain. */ |
| 5820 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5821 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 5822 | #else |
| 5823 | /* If we don't need to store the CRT chain permanently, parse |
| 5824 | * it in-place from the input buffer instead of making a copy. */ |
| 5825 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 5826 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5827 | switch( ret ) |
| 5828 | { |
| 5829 | case 0: /*ok*/ |
| 5830 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 5831 | /* Ignore certificate with an unknown algorithm: maybe a |
| 5832 | prior certificate was already trusted. */ |
| 5833 | break; |
| 5834 | |
| 5835 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 5836 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 5837 | goto crt_parse_der_failed; |
| 5838 | |
| 5839 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 5840 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 5841 | goto crt_parse_der_failed; |
| 5842 | |
| 5843 | default: |
| 5844 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 5845 | crt_parse_der_failed: |
| 5846 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 5847 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 5848 | return( ret ); |
| 5849 | } |
| 5850 | |
| 5851 | i += n; |
| 5852 | } |
| 5853 | |
| 5854 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 5855 | return( 0 ); |
| 5856 | } |
| 5857 | |
| 5858 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5859 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 5860 | { |
| 5861 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5862 | return( -1 ); |
| 5863 | |
| 5864 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 5865 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 5866 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 5867 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 5868 | { |
| 5869 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 5870 | return( 0 ); |
| 5871 | } |
| 5872 | return( -1 ); |
| 5873 | } |
| 5874 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 5875 | |
| 5876 | /* Check if a certificate message is expected. |
| 5877 | * Return either |
| 5878 | * - SSL_CERTIFICATE_EXPECTED, or |
| 5879 | * - SSL_CERTIFICATE_SKIP |
| 5880 | * indicating whether a Certificate message is expected or not. |
| 5881 | */ |
| 5882 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 5883 | #define SSL_CERTIFICATE_SKIP 1 |
| 5884 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 5885 | int authmode ) |
| 5886 | { |
| 5887 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5888 | ssl->handshake->ciphersuite_info; |
| 5889 | |
| 5890 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5891 | return( SSL_CERTIFICATE_SKIP ); |
| 5892 | |
| 5893 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5894 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 5895 | { |
| 5896 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 5897 | return( SSL_CERTIFICATE_SKIP ); |
| 5898 | |
| 5899 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 5900 | { |
| 5901 | ssl->session_negotiate->verify_result = |
| 5902 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 5903 | return( SSL_CERTIFICATE_SKIP ); |
| 5904 | } |
| 5905 | } |
| 5906 | #else |
| 5907 | ((void) authmode); |
| 5908 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 5909 | |
| 5910 | return( SSL_CERTIFICATE_EXPECTED ); |
| 5911 | } |
| 5912 | |
| 5913 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 5914 | int authmode, |
| 5915 | mbedtls_x509_crt *chain, |
| 5916 | void *rs_ctx ) |
| 5917 | { |
| 5918 | int ret = 0; |
| 5919 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5920 | ssl->handshake->ciphersuite_info; |
| 5921 | int have_ca_chain = 0; |
| 5922 | |
| 5923 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 5924 | void *p_vrfy; |
| 5925 | |
| 5926 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 5927 | return( 0 ); |
| 5928 | |
| 5929 | if( ssl->f_vrfy != NULL ) |
| 5930 | { |
| 5931 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 5932 | f_vrfy = ssl->f_vrfy; |
| 5933 | p_vrfy = ssl->p_vrfy; |
| 5934 | } |
| 5935 | else |
| 5936 | { |
| 5937 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 5938 | f_vrfy = ssl->conf->f_vrfy; |
| 5939 | p_vrfy = ssl->conf->p_vrfy; |
| 5940 | } |
| 5941 | |
| 5942 | /* |
| 5943 | * Main check: verify certificate |
| 5944 | */ |
| 5945 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 5946 | if( ssl->conf->f_ca_cb != NULL ) |
| 5947 | { |
| 5948 | ((void) rs_ctx); |
| 5949 | have_ca_chain = 1; |
| 5950 | |
| 5951 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 5952 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 5953 | chain, |
| 5954 | ssl->conf->f_ca_cb, |
| 5955 | ssl->conf->p_ca_cb, |
| 5956 | ssl->conf->cert_profile, |
| 5957 | ssl->hostname, |
| 5958 | &ssl->session_negotiate->verify_result, |
| 5959 | f_vrfy, p_vrfy ); |
| 5960 | } |
| 5961 | else |
| 5962 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 5963 | { |
| 5964 | mbedtls_x509_crt *ca_chain; |
| 5965 | mbedtls_x509_crl *ca_crl; |
| 5966 | |
| 5967 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 5968 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 5969 | { |
| 5970 | ca_chain = ssl->handshake->sni_ca_chain; |
| 5971 | ca_crl = ssl->handshake->sni_ca_crl; |
| 5972 | } |
| 5973 | else |
| 5974 | #endif |
| 5975 | { |
| 5976 | ca_chain = ssl->conf->ca_chain; |
| 5977 | ca_crl = ssl->conf->ca_crl; |
| 5978 | } |
| 5979 | |
| 5980 | if( ca_chain != NULL ) |
| 5981 | have_ca_chain = 1; |
| 5982 | |
| 5983 | ret = mbedtls_x509_crt_verify_restartable( |
| 5984 | chain, |
| 5985 | ca_chain, ca_crl, |
| 5986 | ssl->conf->cert_profile, |
| 5987 | ssl->hostname, |
| 5988 | &ssl->session_negotiate->verify_result, |
| 5989 | f_vrfy, p_vrfy, rs_ctx ); |
| 5990 | } |
| 5991 | |
| 5992 | if( ret != 0 ) |
| 5993 | { |
| 5994 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 5995 | } |
| 5996 | |
| 5997 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 5998 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 5999 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6000 | #endif |
| 6001 | |
| 6002 | /* |
| 6003 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6004 | */ |
| 6005 | |
| 6006 | #if defined(MBEDTLS_ECP_C) |
| 6007 | { |
| 6008 | const mbedtls_pk_context *pk = &chain->pk; |
| 6009 | |
| 6010 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 6011 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 6012 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 6013 | { |
| 6014 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6015 | |
| 6016 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6017 | if( ret == 0 ) |
| 6018 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6019 | } |
| 6020 | } |
| 6021 | #endif /* MBEDTLS_ECP_C */ |
| 6022 | |
| 6023 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6024 | ciphersuite_info, |
| 6025 | ! ssl->conf->endpoint, |
| 6026 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6027 | { |
| 6028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6029 | if( ret == 0 ) |
| 6030 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6031 | } |
| 6032 | |
| 6033 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6034 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6035 | * with details encoded in the verification flags. All other kinds |
| 6036 | * of error codes, including those from the user provided f_vrfy |
| 6037 | * functions, are treated as fatal and lead to a failure of |
| 6038 | * ssl_parse_certificate even if verification was optional. */ |
| 6039 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6040 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6041 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6042 | { |
| 6043 | ret = 0; |
| 6044 | } |
| 6045 | |
| 6046 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6047 | { |
| 6048 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6049 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6050 | } |
| 6051 | |
| 6052 | if( ret != 0 ) |
| 6053 | { |
| 6054 | uint8_t alert; |
| 6055 | |
| 6056 | /* The certificate may have been rejected for several reasons. |
| 6057 | Pick one and send the corresponding alert. Which alert to send |
| 6058 | may be a subject of debate in some cases. */ |
| 6059 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6060 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6061 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6062 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6063 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6064 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6065 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6066 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6067 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6068 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6069 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6070 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6071 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6072 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6073 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6074 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6075 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6076 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6077 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6078 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6079 | else |
| 6080 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6081 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6082 | alert ); |
| 6083 | } |
| 6084 | |
| 6085 | #if defined(MBEDTLS_DEBUG_C) |
| 6086 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6087 | { |
| 6088 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6089 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6090 | } |
| 6091 | else |
| 6092 | { |
| 6093 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6094 | } |
| 6095 | #endif /* MBEDTLS_DEBUG_C */ |
| 6096 | |
| 6097 | return( ret ); |
| 6098 | } |
| 6099 | |
| 6100 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6101 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6102 | unsigned char *start, size_t len ) |
| 6103 | { |
| 6104 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6105 | /* Remember digest of the peer's end-CRT. */ |
| 6106 | ssl->session_negotiate->peer_cert_digest = |
| 6107 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6108 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6109 | { |
| 6110 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6111 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6112 | mbedtls_ssl_send_alert_message( ssl, |
| 6113 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6114 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6115 | |
| 6116 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6117 | } |
| 6118 | |
| 6119 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6120 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6121 | start, len, |
| 6122 | ssl->session_negotiate->peer_cert_digest ); |
| 6123 | |
| 6124 | ssl->session_negotiate->peer_cert_digest_type = |
| 6125 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6126 | ssl->session_negotiate->peer_cert_digest_len = |
| 6127 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6128 | |
| 6129 | return( ret ); |
| 6130 | } |
| 6131 | |
| 6132 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6133 | unsigned char *start, size_t len ) |
| 6134 | { |
| 6135 | unsigned char *end = start + len; |
| 6136 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6137 | |
| 6138 | /* Make a copy of the peer's raw public key. */ |
| 6139 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6140 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6141 | &ssl->handshake->peer_pubkey ); |
| 6142 | if( ret != 0 ) |
| 6143 | { |
| 6144 | /* We should have parsed the public key before. */ |
| 6145 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6146 | } |
| 6147 | |
| 6148 | return( 0 ); |
| 6149 | } |
| 6150 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6151 | |
| 6152 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6153 | { |
| 6154 | int ret = 0; |
| 6155 | int crt_expected; |
| 6156 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6157 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6158 | ? ssl->handshake->sni_authmode |
| 6159 | : ssl->conf->authmode; |
| 6160 | #else |
| 6161 | const int authmode = ssl->conf->authmode; |
| 6162 | #endif |
| 6163 | void *rs_ctx = NULL; |
| 6164 | mbedtls_x509_crt *chain = NULL; |
| 6165 | |
| 6166 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6167 | |
| 6168 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6169 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 6170 | { |
| 6171 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6172 | goto exit; |
| 6173 | } |
| 6174 | |
| 6175 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6176 | if( ssl->handshake->ecrs_enabled && |
| 6177 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 6178 | { |
| 6179 | chain = ssl->handshake->ecrs_peer_cert; |
| 6180 | ssl->handshake->ecrs_peer_cert = NULL; |
| 6181 | goto crt_verify; |
| 6182 | } |
| 6183 | #endif |
| 6184 | |
| 6185 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6186 | { |
| 6187 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6188 | let it decide whether to alert. */ |
| 6189 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6190 | goto exit; |
| 6191 | } |
| 6192 | |
| 6193 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6194 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6195 | { |
| 6196 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 6197 | |
| 6198 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6199 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 6200 | |
| 6201 | goto exit; |
| 6202 | } |
| 6203 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6204 | |
| 6205 | /* Clear existing peer CRT structure in case we tried to |
| 6206 | * reuse a session but it failed, and allocate a new one. */ |
| 6207 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 6208 | |
| 6209 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6210 | if( chain == NULL ) |
| 6211 | { |
| 6212 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 6213 | sizeof( mbedtls_x509_crt ) ) ); |
| 6214 | mbedtls_ssl_send_alert_message( ssl, |
| 6215 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6216 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6217 | |
| 6218 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6219 | goto exit; |
| 6220 | } |
| 6221 | mbedtls_x509_crt_init( chain ); |
| 6222 | |
| 6223 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 6224 | if( ret != 0 ) |
| 6225 | goto exit; |
| 6226 | |
| 6227 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6228 | if( ssl->handshake->ecrs_enabled) |
| 6229 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 6230 | |
| 6231 | crt_verify: |
| 6232 | if( ssl->handshake->ecrs_enabled) |
| 6233 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6234 | #endif |
| 6235 | |
| 6236 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 6237 | chain, rs_ctx ); |
| 6238 | if( ret != 0 ) |
| 6239 | goto exit; |
| 6240 | |
| 6241 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6242 | { |
| 6243 | unsigned char *crt_start, *pk_start; |
| 6244 | size_t crt_len, pk_len; |
| 6245 | |
| 6246 | /* We parse the CRT chain without copying, so |
| 6247 | * these pointers point into the input buffer, |
| 6248 | * and are hence still valid after freeing the |
| 6249 | * CRT chain. */ |
| 6250 | |
| 6251 | crt_start = chain->raw.p; |
| 6252 | crt_len = chain->raw.len; |
| 6253 | |
| 6254 | pk_start = chain->pk_raw.p; |
| 6255 | pk_len = chain->pk_raw.len; |
| 6256 | |
| 6257 | /* Free the CRT structures before computing |
| 6258 | * digest and copying the peer's public key. */ |
| 6259 | mbedtls_x509_crt_free( chain ); |
| 6260 | mbedtls_free( chain ); |
| 6261 | chain = NULL; |
| 6262 | |
| 6263 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 6264 | if( ret != 0 ) |
| 6265 | goto exit; |
| 6266 | |
| 6267 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 6268 | if( ret != 0 ) |
| 6269 | goto exit; |
| 6270 | } |
| 6271 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6272 | /* Pass ownership to session structure. */ |
| 6273 | ssl->session_negotiate->peer_cert = chain; |
| 6274 | chain = NULL; |
| 6275 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6276 | |
| 6277 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 6278 | |
| 6279 | exit: |
| 6280 | |
| 6281 | if( ret == 0 ) |
| 6282 | ssl->state++; |
| 6283 | |
| 6284 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6285 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 6286 | { |
| 6287 | ssl->handshake->ecrs_peer_cert = chain; |
| 6288 | chain = NULL; |
| 6289 | } |
| 6290 | #endif |
| 6291 | |
| 6292 | if( chain != NULL ) |
| 6293 | { |
| 6294 | mbedtls_x509_crt_free( chain ); |
| 6295 | mbedtls_free( chain ); |
| 6296 | } |
| 6297 | |
| 6298 | return( ret ); |
| 6299 | } |
| 6300 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6301 | |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6302 | #if defined(MBEDTLS_SHA256_C) |
| 6303 | static void ssl_calc_finished_tls_sha256( |
| 6304 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6305 | { |
| 6306 | int len = 12; |
| 6307 | const char *sender; |
| 6308 | unsigned char padbuf[32]; |
| 6309 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6310 | size_t hash_size; |
| 6311 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 6312 | psa_status_t status; |
| 6313 | #else |
| 6314 | mbedtls_sha256_context sha256; |
| 6315 | #endif |
| 6316 | |
| 6317 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6318 | if( !session ) |
| 6319 | session = ssl->session; |
| 6320 | |
| 6321 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6322 | ? "client finished" |
| 6323 | : "server finished"; |
| 6324 | |
| 6325 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6326 | sha256_psa = psa_hash_operation_init(); |
| 6327 | |
| 6328 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 6329 | |
| 6330 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6331 | if( status != PSA_SUCCESS ) |
| 6332 | { |
| 6333 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6334 | return; |
| 6335 | } |
| 6336 | |
| 6337 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6338 | if( status != PSA_SUCCESS ) |
| 6339 | { |
| 6340 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6341 | return; |
| 6342 | } |
| 6343 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 6344 | #else |
| 6345 | |
| 6346 | mbedtls_sha256_init( &sha256 ); |
| 6347 | |
| 6348 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 6349 | |
| 6350 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6351 | |
| 6352 | /* |
| 6353 | * TLSv1.2: |
| 6354 | * hash = PRF( master, finished_label, |
| 6355 | * Hash( handshake ) )[0.11] |
| 6356 | */ |
| 6357 | |
| 6358 | #if !defined(MBEDTLS_SHA256_ALT) |
| 6359 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 6360 | sha256.state, sizeof( sha256.state ) ); |
| 6361 | #endif |
| 6362 | |
| 6363 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 6364 | mbedtls_sha256_free( &sha256 ); |
| 6365 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6366 | |
| 6367 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6368 | padbuf, 32, buf, len ); |
| 6369 | |
| 6370 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6371 | |
| 6372 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6373 | |
| 6374 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6375 | } |
| 6376 | #endif /* MBEDTLS_SHA256_C */ |
| 6377 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6378 | |
| 6379 | #if defined(MBEDTLS_SHA384_C) |
| 6380 | |
| 6381 | static void ssl_calc_finished_tls_sha384( |
| 6382 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6383 | { |
| 6384 | int len = 12; |
| 6385 | const char *sender; |
| 6386 | unsigned char padbuf[48]; |
| 6387 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6388 | size_t hash_size; |
| 6389 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 6390 | psa_status_t status; |
| 6391 | #else |
| 6392 | mbedtls_sha512_context sha512; |
| 6393 | #endif |
| 6394 | |
| 6395 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6396 | if( !session ) |
| 6397 | session = ssl->session; |
| 6398 | |
| 6399 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6400 | ? "client finished" |
| 6401 | : "server finished"; |
| 6402 | |
| 6403 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6404 | sha384_psa = psa_hash_operation_init(); |
| 6405 | |
| 6406 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 6407 | |
| 6408 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6409 | if( status != PSA_SUCCESS ) |
| 6410 | { |
| 6411 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6412 | return; |
| 6413 | } |
| 6414 | |
| 6415 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6416 | if( status != PSA_SUCCESS ) |
| 6417 | { |
| 6418 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6419 | return; |
| 6420 | } |
| 6421 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 6422 | #else |
| 6423 | mbedtls_sha512_init( &sha512 ); |
| 6424 | |
| 6425 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 6426 | |
| 6427 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 6428 | |
| 6429 | /* |
| 6430 | * TLSv1.2: |
| 6431 | * hash = PRF( master, finished_label, |
| 6432 | * Hash( handshake ) )[0.11] |
| 6433 | */ |
| 6434 | |
| 6435 | #if !defined(MBEDTLS_SHA512_ALT) |
| 6436 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 6437 | sha512.state, sizeof( sha512.state ) ); |
| 6438 | #endif |
| 6439 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 6440 | |
| 6441 | mbedtls_sha512_free( &sha512 ); |
| 6442 | #endif |
| 6443 | |
| 6444 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6445 | padbuf, 48, buf, len ); |
| 6446 | |
| 6447 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6448 | |
| 6449 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6450 | |
| 6451 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6452 | } |
| 6453 | #endif /* MBEDTLS_SHA384_C */ |
| 6454 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 6455 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 6456 | { |
| 6457 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 6458 | |
| 6459 | /* |
| 6460 | * Free our handshake params |
| 6461 | */ |
| 6462 | mbedtls_ssl_handshake_free( ssl ); |
| 6463 | mbedtls_free( ssl->handshake ); |
| 6464 | ssl->handshake = NULL; |
| 6465 | |
| 6466 | /* |
| 6467 | * Free the previous transform and swith in the current one |
| 6468 | */ |
| 6469 | if( ssl->transform ) |
| 6470 | { |
| 6471 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6472 | mbedtls_free( ssl->transform ); |
| 6473 | } |
| 6474 | ssl->transform = ssl->transform_negotiate; |
| 6475 | ssl->transform_negotiate = NULL; |
| 6476 | |
| 6477 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 6478 | } |
| 6479 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 6480 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 6481 | { |
| 6482 | int resume = ssl->handshake->resume; |
| 6483 | |
| 6484 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 6485 | |
| 6486 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6487 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6488 | { |
| 6489 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 6490 | ssl->renego_records_seen = 0; |
| 6491 | } |
| 6492 | #endif |
| 6493 | |
| 6494 | /* |
| 6495 | * Free the previous session and switch in the current one |
| 6496 | */ |
| 6497 | if( ssl->session ) |
| 6498 | { |
| 6499 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6500 | /* RFC 7366 3.1: keep the EtM state */ |
| 6501 | ssl->session_negotiate->encrypt_then_mac = |
| 6502 | ssl->session->encrypt_then_mac; |
| 6503 | #endif |
| 6504 | |
| 6505 | mbedtls_ssl_session_free( ssl->session ); |
| 6506 | mbedtls_free( ssl->session ); |
| 6507 | } |
| 6508 | ssl->session = ssl->session_negotiate; |
| 6509 | ssl->session_negotiate = NULL; |
| 6510 | |
| 6511 | /* |
| 6512 | * Add cache entry |
| 6513 | */ |
| 6514 | if( ssl->conf->f_set_cache != NULL && |
| 6515 | ssl->session->id_len != 0 && |
| 6516 | resume == 0 ) |
| 6517 | { |
| 6518 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 6519 | ssl->session->id, |
| 6520 | ssl->session->id_len, |
| 6521 | ssl->session ) != 0 ) |
| 6522 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 6523 | } |
| 6524 | |
| 6525 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6526 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6527 | ssl->handshake->flight != NULL ) |
| 6528 | { |
| 6529 | /* Cancel handshake timer */ |
| 6530 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 6531 | |
| 6532 | /* Keep last flight around in case we need to resend it: |
| 6533 | * we need the handshake and transform structures for that */ |
| 6534 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 6535 | } |
| 6536 | else |
| 6537 | #endif |
| 6538 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6539 | |
| 6540 | ssl->state++; |
| 6541 | |
| 6542 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 6543 | } |
| 6544 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 6545 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 6546 | { |
| 6547 | int ret, hash_len; |
| 6548 | |
| 6549 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 6550 | |
| 6551 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 6552 | |
| 6553 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 6554 | |
| 6555 | /* |
| 6556 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 6557 | * may define some other value. Currently (early 2016), no defined |
| 6558 | * ciphersuite does this (and this is unlikely to change as activity has |
| 6559 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 6560 | */ |
| 6561 | hash_len = 12; |
| 6562 | |
| 6563 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6564 | ssl->verify_data_len = hash_len; |
| 6565 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 6566 | #endif |
| 6567 | |
| 6568 | ssl->out_msglen = 4 + hash_len; |
| 6569 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6570 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 6571 | |
| 6572 | /* |
| 6573 | * In case of session resuming, invert the client and server |
| 6574 | * ChangeCipherSpec messages order. |
| 6575 | */ |
| 6576 | if( ssl->handshake->resume != 0 ) |
| 6577 | { |
| 6578 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6579 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6580 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 6581 | #endif |
| 6582 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6583 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6584 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 6585 | #endif |
| 6586 | } |
| 6587 | else |
| 6588 | ssl->state++; |
| 6589 | |
| 6590 | /* |
| 6591 | * Switch to our negotiated transform and session parameters for outbound |
| 6592 | * data. |
| 6593 | */ |
| 6594 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 6595 | |
| 6596 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6597 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6598 | { |
| 6599 | unsigned char i; |
| 6600 | |
| 6601 | /* Remember current epoch settings for resending */ |
| 6602 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 6603 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 6604 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 6605 | |
| 6606 | /* Set sequence_number to zero */ |
| 6607 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 6608 | |
| 6609 | |
| 6610 | /* Increment epoch */ |
| 6611 | for( i = 2; i > 0; i-- ) |
| 6612 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 6613 | break; |
| 6614 | |
| 6615 | /* The loop goes to its end iff the counter is wrapping */ |
| 6616 | if( i == 0 ) |
| 6617 | { |
| 6618 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 6619 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 6620 | } |
| 6621 | } |
| 6622 | else |
| 6623 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6624 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 6625 | |
| 6626 | ssl->transform_out = ssl->transform_negotiate; |
| 6627 | ssl->session_out = ssl->session_negotiate; |
| 6628 | |
| 6629 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6630 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6631 | mbedtls_ssl_send_flight_completed( ssl ); |
| 6632 | #endif |
| 6633 | |
| 6634 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6635 | { |
| 6636 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6637 | return( ret ); |
| 6638 | } |
| 6639 | |
| 6640 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6641 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6642 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 6643 | { |
| 6644 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 6645 | return( ret ); |
| 6646 | } |
| 6647 | #endif |
| 6648 | |
| 6649 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 6650 | |
| 6651 | return( 0 ); |
| 6652 | } |
| 6653 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 6654 | #define SSL_MAX_HASH_LEN 12 |
| 6655 | |
| 6656 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 6657 | { |
| 6658 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6659 | unsigned int hash_len = 12; |
| 6660 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 6661 | |
| 6662 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 6663 | |
| 6664 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 6665 | |
| 6666 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6667 | { |
| 6668 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6669 | goto exit; |
| 6670 | } |
| 6671 | |
| 6672 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6673 | { |
| 6674 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6675 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6676 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6677 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 6678 | goto exit; |
| 6679 | } |
| 6680 | |
| 6681 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 6682 | { |
| 6683 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6684 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6685 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 6686 | goto exit; |
| 6687 | } |
| 6688 | |
| 6689 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 6690 | { |
| 6691 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6692 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6693 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6694 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 6695 | goto exit; |
| 6696 | } |
| 6697 | |
| 6698 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 6699 | buf, hash_len ) != 0 ) |
| 6700 | { |
| 6701 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6702 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6703 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 6704 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 6705 | goto exit; |
| 6706 | } |
| 6707 | |
| 6708 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6709 | ssl->verify_data_len = hash_len; |
| 6710 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 6711 | #endif |
| 6712 | |
| 6713 | if( ssl->handshake->resume != 0 ) |
| 6714 | { |
| 6715 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6716 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6717 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 6718 | #endif |
| 6719 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6720 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6721 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 6722 | #endif |
| 6723 | } |
| 6724 | else |
| 6725 | ssl->state++; |
| 6726 | |
| 6727 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6728 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6729 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 6730 | #endif |
| 6731 | |
| 6732 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 6733 | |
| 6734 | exit: |
| 6735 | mbedtls_platform_zeroize( buf, hash_len ); |
| 6736 | return( ret ); |
| 6737 | } |
| 6738 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 6739 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 6740 | /* |
| 6741 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 6742 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 6743 | */ |
| 6744 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 6745 | { |
| 6746 | #if defined(MBEDTLS_SHA384_C) |
| 6747 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6748 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 6749 | |
| 6750 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 6751 | return( tls_prf_sha384 ); |
| 6752 | #else |
| 6753 | (void) ciphersuite_id; |
| 6754 | #endif |
| 6755 | return( tls_prf_sha256 ); |
| 6756 | } |
| 6757 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 6758 | |
| 6759 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 6760 | { |
| 6761 | ((void) tls_prf); |
| 6762 | #if defined(MBEDTLS_SHA384_C) |
| 6763 | if( tls_prf == tls_prf_sha384 ) |
| 6764 | { |
| 6765 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 6766 | } |
| 6767 | else |
| 6768 | #endif |
| 6769 | #if defined(MBEDTLS_SHA256_C) |
| 6770 | if( tls_prf == tls_prf_sha256 ) |
| 6771 | { |
| 6772 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 6773 | } |
| 6774 | else |
| 6775 | #endif |
| 6776 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 6777 | } |
| 6778 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6779 | /* |
| 6780 | * Populate a transform structure with session keys and all the other |
| 6781 | * necessary information. |
| 6782 | * |
| 6783 | * Parameters: |
| 6784 | * - [in/out]: transform: structure to populate |
| 6785 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 6786 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 6787 | * - [in] ciphersuite |
| 6788 | * - [in] master |
| 6789 | * - [in] encrypt_then_mac |
| 6790 | * - [in] compression |
| 6791 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 6792 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 6793 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6794 | * - [in] endpoint: client or server |
| 6795 | * - [in] ssl: used for: |
| 6796 | * - ssl->conf->{f,p}_export_keys |
| 6797 | * [in] optionally used for: |
| 6798 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 6799 | */ |
| 6800 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 6801 | int ciphersuite, |
| 6802 | const unsigned char master[48], |
| 6803 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 6804 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6805 | int encrypt_then_mac, |
| 6806 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 6807 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 6808 | ssl_tls_prf_t tls_prf, |
| 6809 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 6810 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6811 | unsigned endpoint, |
| 6812 | const mbedtls_ssl_context *ssl ) |
| 6813 | { |
| 6814 | int ret = 0; |
| 6815 | unsigned char keyblk[256]; |
| 6816 | unsigned char *key1; |
| 6817 | unsigned char *key2; |
| 6818 | unsigned char *mac_enc; |
| 6819 | unsigned char *mac_dec; |
| 6820 | size_t mac_key_len = 0; |
| 6821 | size_t iv_copy_len; |
| 6822 | size_t keylen; |
| 6823 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 6824 | const mbedtls_cipher_info_t *cipher_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6825 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6826 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6827 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6828 | |
| 6829 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6830 | psa_key_type_t key_type; |
| 6831 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6832 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6833 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6834 | size_t key_bits; |
| 6835 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6836 | #endif |
| 6837 | |
| 6838 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6839 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6840 | if( ssl->f_export_keys == NULL ) |
| 6841 | { |
| 6842 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 6843 | (void) ssl; |
| 6844 | } |
| 6845 | #endif |
| 6846 | |
| 6847 | /* |
| 6848 | * Some data just needs copying into the structure |
| 6849 | */ |
| 6850 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 6851 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 6852 | transform->encrypt_then_mac = encrypt_then_mac; |
| 6853 | #endif |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 6854 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6855 | |
| 6856 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 6857 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 6858 | #endif |
| 6859 | |
| 6860 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 6861 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6862 | { |
| 6863 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 6864 | * generation separate. This should never happen. */ |
| 6865 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6866 | } |
| 6867 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 6868 | |
| 6869 | /* |
| 6870 | * Get various info structures |
| 6871 | */ |
| 6872 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 6873 | if( ciphersuite_info == NULL ) |
| 6874 | { |
| 6875 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 6876 | ciphersuite ) ); |
| 6877 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6878 | } |
| 6879 | |
| 6880 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 6881 | if( cipher_info == NULL ) |
| 6882 | { |
| 6883 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 6884 | ciphersuite_info->cipher ) ); |
| 6885 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6886 | } |
| 6887 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6888 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6889 | mac_alg = mbedtls_psa_translate_md( ciphersuite_info->mac ); |
| 6890 | if( mac_alg == 0 ) |
| 6891 | { |
| 6892 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_psa_translate_md for %u not found", |
| 6893 | (unsigned) ciphersuite_info->mac ) ); |
| 6894 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6895 | } |
| 6896 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6897 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 6898 | if( md_info == NULL ) |
| 6899 | { |
| 6900 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 6901 | (unsigned) ciphersuite_info->mac ) ); |
| 6902 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6903 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6904 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6905 | |
| 6906 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6907 | /* Copy own and peer's CID if the use of the CID |
| 6908 | * extension has been negotiated. */ |
| 6909 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 6910 | { |
| 6911 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 6912 | |
| 6913 | transform->in_cid_len = ssl->own_cid_len; |
| 6914 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 6915 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 6916 | transform->in_cid_len ); |
| 6917 | |
| 6918 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 6919 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 6920 | ssl->handshake->peer_cid_len ); |
| 6921 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 6922 | transform->out_cid_len ); |
| 6923 | } |
| 6924 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6925 | |
| 6926 | /* |
| 6927 | * Compute key block using the PRF |
| 6928 | */ |
| 6929 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 6930 | if( ret != 0 ) |
| 6931 | { |
| 6932 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 6933 | return( ret ); |
| 6934 | } |
| 6935 | |
| 6936 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 6937 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 6938 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 6939 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 6940 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 6941 | |
| 6942 | /* |
| 6943 | * Determine the appropriate key, IV and MAC length. |
| 6944 | */ |
| 6945 | |
| 6946 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
| 6947 | |
| 6948 | #if defined(MBEDTLS_GCM_C) || \ |
| 6949 | defined(MBEDTLS_CCM_C) || \ |
| 6950 | defined(MBEDTLS_CHACHAPOLY_C) |
| 6951 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM || |
| 6952 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM || |
| 6953 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
| 6954 | { |
| 6955 | size_t explicit_ivlen; |
| 6956 | |
| 6957 | transform->maclen = 0; |
| 6958 | mac_key_len = 0; |
| 6959 | transform->taglen = |
| 6960 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 6961 | |
| 6962 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 6963 | * with mode and version: |
| 6964 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 6965 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 6966 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 6967 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 6968 | * sequence number). |
| 6969 | */ |
| 6970 | transform->ivlen = 12; |
| 6971 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
| 6972 | transform->fixed_ivlen = 12; |
| 6973 | else |
| 6974 | transform->fixed_ivlen = 4; |
| 6975 | |
| 6976 | /* Minimum length of encrypted record */ |
| 6977 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 6978 | transform->minlen = explicit_ivlen + transform->taglen; |
| 6979 | } |
| 6980 | else |
| 6981 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 6982 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 6983 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM || |
| 6984 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 6985 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6986 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6987 | /* Get MAC length */ |
| 6988 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 6989 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6990 | /* Initialize HMAC contexts */ |
| 6991 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 6992 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 6993 | { |
| 6994 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 6995 | goto end; |
| 6996 | } |
| 6997 | |
| 6998 | /* Get MAC length */ |
| 6999 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7000 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7001 | transform->maclen = mac_key_len; |
| 7002 | |
| 7003 | /* IV length */ |
| 7004 | transform->ivlen = cipher_info->iv_size; |
| 7005 | |
| 7006 | /* Minimum length */ |
| 7007 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ) |
| 7008 | transform->minlen = transform->maclen; |
| 7009 | else |
| 7010 | { |
| 7011 | /* |
| 7012 | * GenericBlockCipher: |
| 7013 | * 1. if EtM is in use: one block plus MAC |
| 7014 | * otherwise: * first multiple of blocklen greater than maclen |
| 7015 | * 2. IV |
| 7016 | */ |
| 7017 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7018 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
| 7019 | { |
| 7020 | transform->minlen = transform->maclen |
| 7021 | + cipher_info->block_size; |
| 7022 | } |
| 7023 | else |
| 7024 | #endif |
| 7025 | { |
| 7026 | transform->minlen = transform->maclen |
| 7027 | + cipher_info->block_size |
| 7028 | - transform->maclen % cipher_info->block_size; |
| 7029 | } |
| 7030 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7031 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7032 | { |
| 7033 | transform->minlen += transform->ivlen; |
| 7034 | } |
| 7035 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7036 | { |
| 7037 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7038 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7039 | goto end; |
| 7040 | } |
| 7041 | } |
| 7042 | } |
| 7043 | else |
| 7044 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7045 | { |
| 7046 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7047 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7048 | } |
| 7049 | |
| 7050 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7051 | (unsigned) keylen, |
| 7052 | (unsigned) transform->minlen, |
| 7053 | (unsigned) transform->ivlen, |
| 7054 | (unsigned) transform->maclen ) ); |
| 7055 | |
| 7056 | /* |
| 7057 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7058 | */ |
| 7059 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7060 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7061 | { |
| 7062 | key1 = keyblk + mac_key_len * 2; |
| 7063 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7064 | |
| 7065 | mac_enc = keyblk; |
| 7066 | mac_dec = keyblk + mac_key_len; |
| 7067 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7068 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7069 | transform->fixed_ivlen : transform->ivlen; |
| 7070 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7071 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7072 | iv_copy_len ); |
| 7073 | } |
| 7074 | else |
| 7075 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7076 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7077 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7078 | { |
| 7079 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7080 | key2 = keyblk + mac_key_len * 2; |
| 7081 | |
| 7082 | mac_enc = keyblk + mac_key_len; |
| 7083 | mac_dec = keyblk; |
| 7084 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7085 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7086 | transform->fixed_ivlen : transform->ivlen; |
| 7087 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7088 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7089 | iv_copy_len ); |
| 7090 | } |
| 7091 | else |
| 7092 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7093 | { |
| 7094 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7095 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7096 | goto end; |
| 7097 | } |
| 7098 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7099 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7100 | { |
| 7101 | ssl->f_export_keys( ssl->p_export_keys, |
| 7102 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7103 | master, 48, |
| 7104 | randbytes + 32, |
| 7105 | randbytes, |
| 7106 | tls_prf_get_type( tls_prf ) ); |
| 7107 | } |
| 7108 | |
| 7109 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7110 | if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type, |
| 7111 | transform->taglen, |
| 7112 | &alg, |
| 7113 | &key_type, |
| 7114 | &key_bits ) ) != PSA_SUCCESS ) |
| 7115 | { |
| 7116 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7117 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7118 | goto end; |
| 7119 | } |
| 7120 | |
| 7121 | transform->psa_alg = alg; |
| 7122 | |
| 7123 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7124 | { |
| 7125 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7126 | psa_set_key_algorithm( &attributes, alg ); |
| 7127 | psa_set_key_type( &attributes, key_type ); |
| 7128 | |
| 7129 | if( ( status = psa_import_key( &attributes, |
| 7130 | key1, |
| 7131 | PSA_BITS_TO_BYTES( key_bits ), |
| 7132 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 7133 | { |
| 7134 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 7135 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7136 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7137 | goto end; |
| 7138 | } |
| 7139 | |
| 7140 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7141 | |
| 7142 | if( ( status = psa_import_key( &attributes, |
| 7143 | key2, |
| 7144 | PSA_BITS_TO_BYTES( key_bits ), |
| 7145 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 7146 | { |
| 7147 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7148 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7149 | goto end; |
| 7150 | } |
| 7151 | } |
| 7152 | #else |
| 7153 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 7154 | cipher_info ) ) != 0 ) |
| 7155 | { |
| 7156 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7157 | goto end; |
| 7158 | } |
| 7159 | |
| 7160 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 7161 | cipher_info ) ) != 0 ) |
| 7162 | { |
| 7163 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7164 | goto end; |
| 7165 | } |
| 7166 | |
| 7167 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 7168 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7169 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 7170 | { |
| 7171 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7172 | goto end; |
| 7173 | } |
| 7174 | |
| 7175 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 7176 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7177 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 7178 | { |
| 7179 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7180 | goto end; |
| 7181 | } |
| 7182 | |
| 7183 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 7184 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7185 | { |
| 7186 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 7187 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7188 | { |
| 7189 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7190 | goto end; |
| 7191 | } |
| 7192 | |
| 7193 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 7194 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7195 | { |
| 7196 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7197 | goto end; |
| 7198 | } |
| 7199 | } |
| 7200 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 7201 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7202 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7203 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7204 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 7205 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 7206 | if( mac_key_len != 0 ) |
| 7207 | { |
| 7208 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7209 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7210 | |
| 7211 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7212 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7213 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 7214 | |
| 7215 | if( ( status = psa_import_key( &attributes, |
| 7216 | mac_enc, mac_key_len, |
| 7217 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 7218 | { |
| 7219 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7220 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7221 | goto end; |
| 7222 | } |
| 7223 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 7224 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
| 7225 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) && |
| 7226 | ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7227 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 7228 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 7229 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 7230 | else |
| 7231 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 7232 | |
| 7233 | if( ( status = psa_import_key( &attributes, |
| 7234 | mac_dec, mac_key_len, |
| 7235 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 7236 | { |
| 7237 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7238 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7239 | goto end; |
| 7240 | } |
| 7241 | #else |
| 7242 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 7243 | if( ret != 0 ) |
| 7244 | goto end; |
| 7245 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 7246 | if( ret != 0 ) |
| 7247 | goto end; |
| 7248 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7249 | } |
| 7250 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7251 | |
| 7252 | ((void) mac_dec); |
| 7253 | ((void) mac_enc); |
| 7254 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7255 | end: |
| 7256 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 7257 | return( ret ); |
| 7258 | } |
| 7259 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7260 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7261 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7262 | unsigned char *hash, size_t *hashlen, |
| 7263 | unsigned char *data, size_t data_len, |
| 7264 | mbedtls_md_type_t md_alg ) |
| 7265 | { |
| 7266 | psa_status_t status; |
| 7267 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
| 7268 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7269 | |
| 7270 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 7271 | |
| 7272 | if( ( status = psa_hash_setup( &hash_operation, |
| 7273 | hash_alg ) ) != PSA_SUCCESS ) |
| 7274 | { |
| 7275 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 7276 | goto exit; |
| 7277 | } |
| 7278 | |
| 7279 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7280 | 64 ) ) != PSA_SUCCESS ) |
| 7281 | { |
| 7282 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7283 | goto exit; |
| 7284 | } |
| 7285 | |
| 7286 | if( ( status = psa_hash_update( &hash_operation, |
| 7287 | data, data_len ) ) != PSA_SUCCESS ) |
| 7288 | { |
| 7289 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7290 | goto exit; |
| 7291 | } |
| 7292 | |
| 7293 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 7294 | hashlen ) ) != PSA_SUCCESS ) |
| 7295 | { |
| 7296 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 7297 | goto exit; |
| 7298 | } |
| 7299 | |
| 7300 | exit: |
| 7301 | if( status != PSA_SUCCESS ) |
| 7302 | { |
| 7303 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7304 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7305 | switch( status ) |
| 7306 | { |
| 7307 | case PSA_ERROR_NOT_SUPPORTED: |
| 7308 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 7309 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 7310 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7311 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7312 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7313 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7314 | default: |
| 7315 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 7316 | } |
| 7317 | } |
| 7318 | return( 0 ); |
| 7319 | } |
| 7320 | |
| 7321 | #else |
| 7322 | |
| 7323 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7324 | unsigned char *hash, size_t *hashlen, |
| 7325 | unsigned char *data, size_t data_len, |
| 7326 | mbedtls_md_type_t md_alg ) |
| 7327 | { |
| 7328 | int ret = 0; |
| 7329 | mbedtls_md_context_t ctx; |
| 7330 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 7331 | *hashlen = mbedtls_md_get_size( md_info ); |
| 7332 | |
| 7333 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 7334 | |
| 7335 | mbedtls_md_init( &ctx ); |
| 7336 | |
| 7337 | /* |
| 7338 | * digitally-signed struct { |
| 7339 | * opaque client_random[32]; |
| 7340 | * opaque server_random[32]; |
| 7341 | * ServerDHParams params; |
| 7342 | * }; |
| 7343 | */ |
| 7344 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7345 | { |
| 7346 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7347 | goto exit; |
| 7348 | } |
| 7349 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7350 | { |
| 7351 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7352 | goto exit; |
| 7353 | } |
| 7354 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7355 | { |
| 7356 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7357 | goto exit; |
| 7358 | } |
| 7359 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7360 | { |
| 7361 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7362 | goto exit; |
| 7363 | } |
| 7364 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 7365 | { |
| 7366 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7367 | goto exit; |
| 7368 | } |
| 7369 | |
| 7370 | exit: |
| 7371 | mbedtls_md_free( &ctx ); |
| 7372 | |
| 7373 | if( ret != 0 ) |
| 7374 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7375 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7376 | |
| 7377 | return( ret ); |
| 7378 | } |
| 7379 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7380 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7381 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7382 | |
| 7383 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 7384 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 7385 | mbedtls_pk_type_t sig_alg ) |
| 7386 | { |
| 7387 | switch( sig_alg ) |
| 7388 | { |
| 7389 | case MBEDTLS_PK_RSA: |
| 7390 | return( set->rsa ); |
| 7391 | case MBEDTLS_PK_ECDSA: |
| 7392 | return( set->ecdsa ); |
| 7393 | default: |
| 7394 | return( MBEDTLS_MD_NONE ); |
| 7395 | } |
| 7396 | } |
| 7397 | |
| 7398 | /* Add a signature-hash-pair to a signature-hash set */ |
| 7399 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 7400 | mbedtls_pk_type_t sig_alg, |
| 7401 | mbedtls_md_type_t md_alg ) |
| 7402 | { |
| 7403 | switch( sig_alg ) |
| 7404 | { |
| 7405 | case MBEDTLS_PK_RSA: |
| 7406 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 7407 | set->rsa = md_alg; |
| 7408 | break; |
| 7409 | |
| 7410 | case MBEDTLS_PK_ECDSA: |
| 7411 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 7412 | set->ecdsa = md_alg; |
| 7413 | break; |
| 7414 | |
| 7415 | default: |
| 7416 | break; |
| 7417 | } |
| 7418 | } |
| 7419 | |
| 7420 | /* Allow exactly one hash algorithm for each signature. */ |
| 7421 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 7422 | mbedtls_md_type_t md_alg ) |
| 7423 | { |
| 7424 | set->rsa = md_alg; |
| 7425 | set->ecdsa = md_alg; |
| 7426 | } |
| 7427 | |
| 7428 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7429 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7430 | /* Serialization of TLS 1.2 sessions: |
| 7431 | * |
| 7432 | * struct { |
| 7433 | * uint64 start_time; |
| 7434 | * uint8 ciphersuite[2]; // defined by the standard |
| 7435 | * uint8 compression; // 0 or 1 |
| 7436 | * uint8 session_id_len; // at most 32 |
| 7437 | * opaque session_id[32]; |
| 7438 | * opaque master[48]; // fixed length in the standard |
| 7439 | * uint32 verify_result; |
| 7440 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 7441 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 7442 | * uint32 ticket_lifetime; |
| 7443 | * uint8 mfl_code; // up to 255 according to standard |
| 7444 | * uint8 encrypt_then_mac; // 0 or 1 |
| 7445 | * } serialized_session_tls12; |
| 7446 | * |
| 7447 | */ |
| 7448 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 7449 | unsigned char *buf, |
| 7450 | size_t buf_len ) |
| 7451 | { |
| 7452 | unsigned char *p = buf; |
| 7453 | size_t used = 0; |
| 7454 | |
| 7455 | #if defined(MBEDTLS_HAVE_TIME) |
| 7456 | uint64_t start; |
| 7457 | #endif |
| 7458 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7459 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7460 | size_t cert_len; |
| 7461 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7462 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7463 | |
| 7464 | /* |
| 7465 | * Time |
| 7466 | */ |
| 7467 | #if defined(MBEDTLS_HAVE_TIME) |
| 7468 | used += 8; |
| 7469 | |
| 7470 | if( used <= buf_len ) |
| 7471 | { |
| 7472 | start = (uint64_t) session->start; |
| 7473 | |
| 7474 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 7475 | p += 8; |
| 7476 | } |
| 7477 | #endif /* MBEDTLS_HAVE_TIME */ |
| 7478 | |
| 7479 | /* |
| 7480 | * Basic mandatory fields |
| 7481 | */ |
| 7482 | used += 2 /* ciphersuite */ |
| 7483 | + 1 /* compression */ |
| 7484 | + 1 /* id_len */ |
| 7485 | + sizeof( session->id ) |
| 7486 | + sizeof( session->master ) |
| 7487 | + 4; /* verify_result */ |
| 7488 | |
| 7489 | if( used <= buf_len ) |
| 7490 | { |
| 7491 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 7492 | p += 2; |
| 7493 | |
| 7494 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
| 7495 | |
| 7496 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 7497 | memcpy( p, session->id, 32 ); |
| 7498 | p += 32; |
| 7499 | |
| 7500 | memcpy( p, session->master, 48 ); |
| 7501 | p += 48; |
| 7502 | |
| 7503 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 7504 | p += 4; |
| 7505 | } |
| 7506 | |
| 7507 | /* |
| 7508 | * Peer's end-entity certificate |
| 7509 | */ |
| 7510 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7511 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7512 | if( session->peer_cert == NULL ) |
| 7513 | cert_len = 0; |
| 7514 | else |
| 7515 | cert_len = session->peer_cert->raw.len; |
| 7516 | |
| 7517 | used += 3 + cert_len; |
| 7518 | |
| 7519 | if( used <= buf_len ) |
| 7520 | { |
| 7521 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 7522 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 7523 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 7524 | |
| 7525 | if( session->peer_cert != NULL ) |
| 7526 | { |
| 7527 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 7528 | p += cert_len; |
| 7529 | } |
| 7530 | } |
| 7531 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7532 | if( session->peer_cert_digest != NULL ) |
| 7533 | { |
| 7534 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 7535 | if( used <= buf_len ) |
| 7536 | { |
| 7537 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 7538 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 7539 | memcpy( p, session->peer_cert_digest, |
| 7540 | session->peer_cert_digest_len ); |
| 7541 | p += session->peer_cert_digest_len; |
| 7542 | } |
| 7543 | } |
| 7544 | else |
| 7545 | { |
| 7546 | used += 2; |
| 7547 | if( used <= buf_len ) |
| 7548 | { |
| 7549 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 7550 | *p++ = 0; |
| 7551 | } |
| 7552 | } |
| 7553 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7554 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7555 | |
| 7556 | /* |
| 7557 | * Session ticket if any, plus associated data |
| 7558 | */ |
| 7559 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7560 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 7561 | |
| 7562 | if( used <= buf_len ) |
| 7563 | { |
| 7564 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 7565 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 7566 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 7567 | |
| 7568 | if( session->ticket != NULL ) |
| 7569 | { |
| 7570 | memcpy( p, session->ticket, session->ticket_len ); |
| 7571 | p += session->ticket_len; |
| 7572 | } |
| 7573 | |
| 7574 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 7575 | p += 4; |
| 7576 | } |
| 7577 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7578 | |
| 7579 | /* |
| 7580 | * Misc extension-related info |
| 7581 | */ |
| 7582 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 7583 | used += 1; |
| 7584 | |
| 7585 | if( used <= buf_len ) |
| 7586 | *p++ = session->mfl_code; |
| 7587 | #endif |
| 7588 | |
| 7589 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7590 | used += 1; |
| 7591 | |
| 7592 | if( used <= buf_len ) |
| 7593 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 7594 | #endif |
| 7595 | |
| 7596 | return( used ); |
| 7597 | } |
| 7598 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7599 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 7600 | const unsigned char *buf, |
| 7601 | size_t len ) |
| 7602 | { |
| 7603 | #if defined(MBEDTLS_HAVE_TIME) |
| 7604 | uint64_t start; |
| 7605 | #endif |
| 7606 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7607 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7608 | size_t cert_len; |
| 7609 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7610 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7611 | |
| 7612 | const unsigned char *p = buf; |
| 7613 | const unsigned char * const end = buf + len; |
| 7614 | |
| 7615 | /* |
| 7616 | * Time |
| 7617 | */ |
| 7618 | #if defined(MBEDTLS_HAVE_TIME) |
| 7619 | if( 8 > (size_t)( end - p ) ) |
| 7620 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7621 | |
| 7622 | start = ( (uint64_t) p[0] << 56 ) | |
| 7623 | ( (uint64_t) p[1] << 48 ) | |
| 7624 | ( (uint64_t) p[2] << 40 ) | |
| 7625 | ( (uint64_t) p[3] << 32 ) | |
| 7626 | ( (uint64_t) p[4] << 24 ) | |
| 7627 | ( (uint64_t) p[5] << 16 ) | |
| 7628 | ( (uint64_t) p[6] << 8 ) | |
| 7629 | ( (uint64_t) p[7] ); |
| 7630 | p += 8; |
| 7631 | |
| 7632 | session->start = (time_t) start; |
| 7633 | #endif /* MBEDTLS_HAVE_TIME */ |
| 7634 | |
| 7635 | /* |
| 7636 | * Basic mandatory fields |
| 7637 | */ |
| 7638 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 7639 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7640 | |
| 7641 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 7642 | p += 2; |
| 7643 | |
| 7644 | session->compression = *p++; |
| 7645 | |
| 7646 | session->id_len = *p++; |
| 7647 | memcpy( session->id, p, 32 ); |
| 7648 | p += 32; |
| 7649 | |
| 7650 | memcpy( session->master, p, 48 ); |
| 7651 | p += 48; |
| 7652 | |
| 7653 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 7654 | ( (uint32_t) p[1] << 16 ) | |
| 7655 | ( (uint32_t) p[2] << 8 ) | |
| 7656 | ( (uint32_t) p[3] ); |
| 7657 | p += 4; |
| 7658 | |
| 7659 | /* Immediately clear invalid pointer values that have been read, in case |
| 7660 | * we exit early before we replaced them with valid ones. */ |
| 7661 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7662 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7663 | session->peer_cert = NULL; |
| 7664 | #else |
| 7665 | session->peer_cert_digest = NULL; |
| 7666 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7667 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7668 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7669 | session->ticket = NULL; |
| 7670 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7671 | |
| 7672 | /* |
| 7673 | * Peer certificate |
| 7674 | */ |
| 7675 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7676 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7677 | /* Deserialize CRT from the end of the ticket. */ |
| 7678 | if( 3 > (size_t)( end - p ) ) |
| 7679 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7680 | |
| 7681 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 7682 | p += 3; |
| 7683 | |
| 7684 | if( cert_len != 0 ) |
| 7685 | { |
| 7686 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7687 | |
| 7688 | if( cert_len > (size_t)( end - p ) ) |
| 7689 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7690 | |
| 7691 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7692 | |
| 7693 | if( session->peer_cert == NULL ) |
| 7694 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7695 | |
| 7696 | mbedtls_x509_crt_init( session->peer_cert ); |
| 7697 | |
| 7698 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 7699 | p, cert_len ) ) != 0 ) |
| 7700 | { |
| 7701 | mbedtls_x509_crt_free( session->peer_cert ); |
| 7702 | mbedtls_free( session->peer_cert ); |
| 7703 | session->peer_cert = NULL; |
| 7704 | return( ret ); |
| 7705 | } |
| 7706 | |
| 7707 | p += cert_len; |
| 7708 | } |
| 7709 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7710 | /* Deserialize CRT digest from the end of the ticket. */ |
| 7711 | if( 2 > (size_t)( end - p ) ) |
| 7712 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7713 | |
| 7714 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 7715 | session->peer_cert_digest_len = (size_t) *p++; |
| 7716 | |
| 7717 | if( session->peer_cert_digest_len != 0 ) |
| 7718 | { |
| 7719 | const mbedtls_md_info_t *md_info = |
| 7720 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 7721 | if( md_info == NULL ) |
| 7722 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7723 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 7724 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7725 | |
| 7726 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 7727 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7728 | |
| 7729 | session->peer_cert_digest = |
| 7730 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 7731 | if( session->peer_cert_digest == NULL ) |
| 7732 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7733 | |
| 7734 | memcpy( session->peer_cert_digest, p, |
| 7735 | session->peer_cert_digest_len ); |
| 7736 | p += session->peer_cert_digest_len; |
| 7737 | } |
| 7738 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7739 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7740 | |
| 7741 | /* |
| 7742 | * Session ticket and associated data |
| 7743 | */ |
| 7744 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7745 | if( 3 > (size_t)( end - p ) ) |
| 7746 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7747 | |
| 7748 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 7749 | p += 3; |
| 7750 | |
| 7751 | if( session->ticket_len != 0 ) |
| 7752 | { |
| 7753 | if( session->ticket_len > (size_t)( end - p ) ) |
| 7754 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7755 | |
| 7756 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 7757 | if( session->ticket == NULL ) |
| 7758 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7759 | |
| 7760 | memcpy( session->ticket, p, session->ticket_len ); |
| 7761 | p += session->ticket_len; |
| 7762 | } |
| 7763 | |
| 7764 | if( 4 > (size_t)( end - p ) ) |
| 7765 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7766 | |
| 7767 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 7768 | ( (uint32_t) p[1] << 16 ) | |
| 7769 | ( (uint32_t) p[2] << 8 ) | |
| 7770 | ( (uint32_t) p[3] ); |
| 7771 | p += 4; |
| 7772 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7773 | |
| 7774 | /* |
| 7775 | * Misc extension-related info |
| 7776 | */ |
| 7777 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 7778 | if( 1 > (size_t)( end - p ) ) |
| 7779 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7780 | |
| 7781 | session->mfl_code = *p++; |
| 7782 | #endif |
| 7783 | |
| 7784 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7785 | if( 1 > (size_t)( end - p ) ) |
| 7786 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7787 | |
| 7788 | session->encrypt_then_mac = *p++; |
| 7789 | #endif |
| 7790 | |
| 7791 | /* Done, should have consumed entire buffer */ |
| 7792 | if( p != end ) |
| 7793 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7794 | |
| 7795 | return( 0 ); |
| 7796 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 7797 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7798 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 7799 | int mbedtls_ssl_validate_ciphersuite( |
| 7800 | const mbedtls_ssl_context *ssl, |
| 7801 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 7802 | mbedtls_ssl_protocol_version min_tls_version, |
| 7803 | mbedtls_ssl_protocol_version max_tls_version ) |
| 7804 | { |
| 7805 | (void) ssl; |
| 7806 | |
| 7807 | if( suite_info == NULL ) |
| 7808 | return( -1 ); |
| 7809 | |
| 7810 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 7811 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 7812 | { |
| 7813 | return( -1 ); |
| 7814 | } |
| 7815 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 7816 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 7817 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 7818 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 7819 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 7820 | { |
| 7821 | return( -1 ); |
| 7822 | } |
| 7823 | #endif |
| 7824 | |
| 7825 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 7826 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 7827 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 7828 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 7829 | { |
| 7830 | return( -1 ); |
| 7831 | } |
| 7832 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 7833 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7834 | |
| 7835 | return( 0 ); |
| 7836 | } |
| 7837 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7838 | #endif /* MBEDTLS_SSL_TLS_C */ |