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], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 386 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 387 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 388 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 389 | ssl_tls_prf_t tls_prf, |
| 390 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 391 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 392 | unsigned endpoint, |
| 393 | const mbedtls_ssl_context *ssl ); |
| 394 | |
| 395 | #if defined(MBEDTLS_SHA256_C) |
| 396 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 397 | const char *label, |
| 398 | const unsigned char *random, size_t rlen, |
| 399 | unsigned char *dstbuf, size_t dlen ); |
| 400 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
| 401 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
| 402 | |
| 403 | #endif /* MBEDTLS_SHA256_C */ |
| 404 | |
| 405 | #if defined(MBEDTLS_SHA384_C) |
| 406 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 407 | const char *label, |
| 408 | const unsigned char *random, size_t rlen, |
| 409 | unsigned char *dstbuf, size_t dlen ); |
| 410 | |
| 411 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
| 412 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
| 413 | #endif /* MBEDTLS_SHA384_C */ |
| 414 | |
| 415 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 416 | unsigned char *buf, |
| 417 | size_t buf_len ); |
| 418 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 419 | const unsigned char *buf, |
| 420 | size_t len ); |
| 421 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 422 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 423 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 424 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 425 | #if defined(MBEDTLS_SHA256_C) |
| 426 | 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] | 427 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 428 | |
| 429 | #if defined(MBEDTLS_SHA384_C) |
| 430 | 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] | 431 | #endif /* MBEDTLS_SHA384_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 432 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 433 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 434 | const unsigned char *secret, size_t slen, |
| 435 | const char *label, |
| 436 | const unsigned char *random, size_t rlen, |
| 437 | unsigned char *dstbuf, size_t dlen ) |
| 438 | { |
| 439 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 440 | |
| 441 | switch( prf ) |
| 442 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 443 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 444 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 445 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 446 | tls_prf = tls_prf_sha384; |
| 447 | break; |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 448 | #endif /* MBEDTLS_SHA384_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 449 | #if defined(MBEDTLS_SHA256_C) |
| 450 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 451 | tls_prf = tls_prf_sha256; |
| 452 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 453 | #endif /* MBEDTLS_SHA256_C */ |
| 454 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 455 | default: |
| 456 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 457 | } |
| 458 | |
| 459 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 460 | } |
| 461 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 462 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 463 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 464 | { |
| 465 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 466 | if( session->peer_cert != NULL ) |
| 467 | { |
| 468 | mbedtls_x509_crt_free( session->peer_cert ); |
| 469 | mbedtls_free( session->peer_cert ); |
| 470 | session->peer_cert = NULL; |
| 471 | } |
| 472 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 473 | if( session->peer_cert_digest != NULL ) |
| 474 | { |
| 475 | /* Zeroization is not necessary. */ |
| 476 | mbedtls_free( session->peer_cert_digest ); |
| 477 | session->peer_cert_digest = NULL; |
| 478 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 479 | session->peer_cert_digest_len = 0; |
| 480 | } |
| 481 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 482 | } |
| 483 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 484 | |
| 485 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 486 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 487 | { |
| 488 | ((void) ciphersuite_info); |
| 489 | |
| 490 | #if defined(MBEDTLS_SHA384_C) |
| 491 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 492 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 493 | else |
| 494 | #endif |
| 495 | #if defined(MBEDTLS_SHA256_C) |
| 496 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 497 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 498 | else |
| 499 | #endif |
| 500 | { |
| 501 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 502 | return; |
| 503 | } |
| 504 | } |
| 505 | |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 506 | static void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, |
| 507 | unsigned hs_type, |
| 508 | size_t total_hs_len ) |
| 509 | { |
| 510 | unsigned char hs_hdr[4]; |
| 511 | |
| 512 | /* Build HS header for checksum update. */ |
| 513 | hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); |
| 514 | hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); |
| 515 | hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); |
| 516 | hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); |
| 517 | |
| 518 | ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); |
| 519 | } |
| 520 | |
| 521 | void mbedtls_ssl_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl, |
| 522 | unsigned hs_type, |
| 523 | unsigned char const *msg, |
| 524 | size_t msg_len ) |
| 525 | { |
| 526 | mbedtls_ssl_add_hs_hdr_to_checksum( ssl, hs_type, msg_len ); |
| 527 | ssl->handshake->update_checksum( ssl, msg, msg_len ); |
| 528 | } |
| 529 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 530 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 531 | { |
| 532 | ((void) ssl); |
| 533 | #if defined(MBEDTLS_SHA256_C) |
| 534 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 535 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 536 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 537 | #else |
| 538 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 539 | #endif |
| 540 | #endif |
| 541 | #if defined(MBEDTLS_SHA384_C) |
| 542 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 543 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 544 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 545 | #else |
| 546 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); |
| 547 | #endif |
| 548 | #endif |
| 549 | } |
| 550 | |
| 551 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 552 | const unsigned char *buf, size_t len ) |
| 553 | { |
| 554 | #if defined(MBEDTLS_SHA256_C) |
| 555 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 556 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 557 | #else |
| 558 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 559 | #endif |
| 560 | #endif |
| 561 | #if defined(MBEDTLS_SHA384_C) |
| 562 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 563 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 564 | #else |
| 565 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 566 | #endif |
| 567 | #endif |
| 568 | } |
| 569 | |
| 570 | #if defined(MBEDTLS_SHA256_C) |
| 571 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 572 | const unsigned char *buf, size_t len ) |
| 573 | { |
| 574 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 575 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 576 | #else |
| 577 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 578 | #endif |
| 579 | } |
| 580 | #endif |
| 581 | |
| 582 | #if defined(MBEDTLS_SHA384_C) |
| 583 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 584 | const unsigned char *buf, size_t len ) |
| 585 | { |
| 586 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 587 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 588 | #else |
| 589 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 590 | #endif |
| 591 | } |
| 592 | #endif |
| 593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 595 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
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 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 599 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 600 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 601 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 602 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 603 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 604 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 605 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 606 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 607 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 608 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 609 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 610 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 611 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 613 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 614 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 615 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 616 | |
| 617 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 619 | #if defined(MBEDTLS_DHM_C) |
| 620 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 621 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 622 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 624 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 625 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 626 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 627 | #if defined(MBEDTLS_SSL_CLI_C) |
| 628 | handshake->ecjpake_cache = NULL; |
| 629 | handshake->ecjpake_cache_len = 0; |
| 630 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 631 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 632 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 633 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 634 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 635 | #endif |
| 636 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 637 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 638 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 639 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 640 | |
| 641 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 642 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 643 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 644 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 645 | } |
| 646 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 647 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 650 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 651 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 652 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 653 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 654 | #else |
| 655 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 656 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 657 | #endif |
| 658 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 659 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 660 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 661 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 662 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 663 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 665 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 666 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 667 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 668 | } |
| 669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 670 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 671 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 672 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 676 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 677 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 678 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 680 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 682 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 683 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 684 | |
| 685 | /* |
| 686 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 687 | * Now allocate missing structures. |
| 688 | */ |
| 689 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 690 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 691 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 692 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 693 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 694 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 695 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 696 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 697 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 698 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 699 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 700 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 701 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 702 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 703 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 704 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 705 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 706 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 707 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 708 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 709 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 710 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 711 | if( ssl->handshake == NULL || |
| 712 | ssl->transform_negotiate == NULL || |
| 713 | ssl->session_negotiate == NULL ) |
| 714 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 715 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | mbedtls_free( ssl->handshake ); |
| 718 | mbedtls_free( ssl->transform_negotiate ); |
| 719 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 720 | |
| 721 | ssl->handshake = NULL; |
| 722 | ssl->transform_negotiate = NULL; |
| 723 | ssl->session_negotiate = NULL; |
| 724 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 725 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 728 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 729 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 730 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 731 | ssl_handshake_params_init( ssl->handshake ); |
| 732 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 733 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 734 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 735 | { |
| 736 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 737 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 738 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 739 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 740 | else |
| 741 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 742 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 743 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 744 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 745 | #endif |
| 746 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 747 | /* |
| 748 | * curve_list is translated to IANA TLS group identifiers here because |
| 749 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 750 | * any error codes. |
| 751 | */ |
| 752 | #if defined(MBEDTLS_ECP_C) |
| 753 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 754 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 755 | if ( ssl->conf->curve_list != NULL ) |
| 756 | { |
| 757 | size_t length; |
| 758 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 759 | |
| 760 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 761 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 762 | |
| 763 | /* Leave room for zero termination */ |
| 764 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 765 | if ( group_list == NULL ) |
| 766 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 767 | |
| 768 | for( size_t i = 0; i < length; i++ ) |
| 769 | { |
| 770 | const mbedtls_ecp_curve_info *info = |
| 771 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 772 | if ( info == NULL ) |
| 773 | { |
| 774 | mbedtls_free( group_list ); |
| 775 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 776 | } |
| 777 | group_list[i] = info->tls_id; |
| 778 | } |
| 779 | |
| 780 | group_list[length] = 0; |
| 781 | |
| 782 | ssl->handshake->group_list = group_list; |
| 783 | ssl->handshake->group_list_heap_allocated = 1; |
| 784 | } |
| 785 | else |
| 786 | { |
| 787 | ssl->handshake->group_list = ssl->conf->group_list; |
| 788 | ssl->handshake->group_list_heap_allocated = 0; |
| 789 | } |
| 790 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 791 | #endif /* MBEDTLS_ECP_C */ |
| 792 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 793 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 794 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 795 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 796 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 797 | signature algorithms IANA identifiers. */ |
| 798 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 799 | ssl->conf->sig_hashes != NULL ) |
| 800 | { |
| 801 | const int *md; |
| 802 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 803 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 804 | uint16_t *p; |
| 805 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 806 | #if defined(static_assert) |
| 807 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 808 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 809 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 810 | #endif |
| 811 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 812 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 813 | { |
| 814 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 815 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 816 | #if defined(MBEDTLS_ECDSA_C) |
| 817 | sig_algs_len += sizeof( uint16_t ); |
| 818 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 819 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 820 | #if defined(MBEDTLS_RSA_C) |
| 821 | sig_algs_len += sizeof( uint16_t ); |
| 822 | #endif |
| 823 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 824 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 825 | } |
| 826 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 827 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 828 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 829 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 830 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 831 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 832 | if( ssl->handshake->sig_algs == NULL ) |
| 833 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 834 | |
| 835 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 836 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 837 | { |
| 838 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 839 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 840 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 841 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 842 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 843 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 844 | #endif |
| 845 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 846 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 847 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 848 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 849 | } |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 850 | *p = MBEDTLS_TLS_SIG_NONE; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 851 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 852 | } |
| 853 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 854 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 855 | { |
| 856 | ssl->handshake->sig_algs = ssl->conf->sig_algs; |
| 857 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 858 | } |
| 859 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 860 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 861 | return( 0 ); |
| 862 | } |
| 863 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 864 | #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] | 865 | /* Dummy cookie callbacks for defaults */ |
| 866 | static int ssl_cookie_write_dummy( void *ctx, |
| 867 | unsigned char **p, unsigned char *end, |
| 868 | const unsigned char *cli_id, size_t cli_id_len ) |
| 869 | { |
| 870 | ((void) ctx); |
| 871 | ((void) p); |
| 872 | ((void) end); |
| 873 | ((void) cli_id); |
| 874 | ((void) cli_id_len); |
| 875 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 876 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | static int ssl_cookie_check_dummy( void *ctx, |
| 880 | const unsigned char *cookie, size_t cookie_len, |
| 881 | const unsigned char *cli_id, size_t cli_id_len ) |
| 882 | { |
| 883 | ((void) ctx); |
| 884 | ((void) cookie); |
| 885 | ((void) cookie_len); |
| 886 | ((void) cli_id); |
| 887 | ((void) cli_id_len); |
| 888 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 889 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 890 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 891 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 892 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 893 | /* |
| 894 | * Initialize an SSL context |
| 895 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 896 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 897 | { |
| 898 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 899 | } |
| 900 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 901 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 902 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 903 | const mbedtls_ssl_config *conf = ssl->conf; |
| 904 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 905 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 906 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 907 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 908 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 909 | { |
| 910 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 911 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 912 | } |
| 913 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 914 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 915 | return( 0 ); |
| 916 | } |
| 917 | #endif |
| 918 | |
| 919 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 920 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 921 | { |
| 922 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 923 | return( 0 ); |
| 924 | } |
| 925 | #endif |
| 926 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 927 | #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] | 928 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 929 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 930 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 931 | { |
| 932 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 933 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 934 | } |
| 935 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 936 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 937 | { |
| 938 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 939 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 940 | } |
| 941 | |
| 942 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 943 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 944 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 945 | } |
| 946 | #endif |
| 947 | |
| 948 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 949 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 950 | } |
| 951 | |
| 952 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 953 | { |
| 954 | int ret; |
| 955 | ret = ssl_conf_version_check( ssl ); |
| 956 | if( ret != 0 ) |
| 957 | return( ret ); |
| 958 | |
| 959 | /* Space for further checks */ |
| 960 | |
| 961 | return( 0 ); |
| 962 | } |
| 963 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 964 | /* |
| 965 | * Setup an SSL context |
| 966 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 967 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 968 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 969 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 970 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 971 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 972 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 973 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 974 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 975 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 976 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 977 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 978 | return( ret ); |
| 979 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 980 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 981 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 982 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 983 | |
| 984 | /* Set to NULL in case of an error condition */ |
| 985 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 986 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 987 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 988 | ssl->in_buf_len = in_buf_len; |
| 989 | #endif |
| 990 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 991 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 992 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 993 | 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] | 994 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 995 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 996 | } |
| 997 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 998 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 999 | ssl->out_buf_len = out_buf_len; |
| 1000 | #endif |
| 1001 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1002 | if( ssl->out_buf == NULL ) |
| 1003 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1004 | 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] | 1005 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1006 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1009 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1010 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1011 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1012 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1013 | #endif |
| 1014 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1015 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1016 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1017 | |
| 1018 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1019 | |
| 1020 | error: |
| 1021 | mbedtls_free( ssl->in_buf ); |
| 1022 | mbedtls_free( ssl->out_buf ); |
| 1023 | |
| 1024 | ssl->conf = NULL; |
| 1025 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1026 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1027 | ssl->in_buf_len = 0; |
| 1028 | ssl->out_buf_len = 0; |
| 1029 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1030 | ssl->in_buf = NULL; |
| 1031 | ssl->out_buf = NULL; |
| 1032 | |
| 1033 | ssl->in_hdr = NULL; |
| 1034 | ssl->in_ctr = NULL; |
| 1035 | ssl->in_len = NULL; |
| 1036 | ssl->in_iv = NULL; |
| 1037 | ssl->in_msg = NULL; |
| 1038 | |
| 1039 | ssl->out_hdr = NULL; |
| 1040 | ssl->out_ctr = NULL; |
| 1041 | ssl->out_len = NULL; |
| 1042 | ssl->out_iv = NULL; |
| 1043 | ssl->out_msg = NULL; |
| 1044 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1045 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1049 | * Reset an initialized and used SSL context for re-use while retaining |
| 1050 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1051 | * |
| 1052 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1053 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1054 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1055 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1056 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1057 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1058 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1059 | size_t in_buf_len = ssl->in_buf_len; |
| 1060 | size_t out_buf_len = ssl->out_buf_len; |
| 1061 | #else |
| 1062 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1063 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1064 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1065 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1066 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1067 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1068 | #endif |
| 1069 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1070 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1071 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1072 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1073 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1074 | |
| 1075 | /* Reset incoming message parsing */ |
| 1076 | ssl->in_offt = NULL; |
| 1077 | ssl->nb_zero = 0; |
| 1078 | ssl->in_msgtype = 0; |
| 1079 | ssl->in_msglen = 0; |
| 1080 | ssl->in_hslen = 0; |
| 1081 | ssl->keep_current_message = 0; |
| 1082 | ssl->transform_in = NULL; |
| 1083 | |
| 1084 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1085 | ssl->next_record_offset = 0; |
| 1086 | ssl->in_epoch = 0; |
| 1087 | #endif |
| 1088 | |
| 1089 | /* Keep current datagram if partial == 1 */ |
| 1090 | if( partial == 0 ) |
| 1091 | { |
| 1092 | ssl->in_left = 0; |
| 1093 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1094 | } |
| 1095 | |
| 1096 | /* Reset outgoing message writing */ |
| 1097 | ssl->out_msgtype = 0; |
| 1098 | ssl->out_msglen = 0; |
| 1099 | ssl->out_left = 0; |
| 1100 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1101 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1102 | ssl->transform_out = NULL; |
| 1103 | |
| 1104 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1105 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1106 | #endif |
| 1107 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1108 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1109 | if( ssl->transform ) |
| 1110 | { |
| 1111 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1112 | mbedtls_free( ssl->transform ); |
| 1113 | ssl->transform = NULL; |
| 1114 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1115 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1116 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1117 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1118 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1119 | mbedtls_free( ssl->transform_application ); |
| 1120 | ssl->transform_application = NULL; |
| 1121 | |
| 1122 | if( ssl->handshake != NULL ) |
| 1123 | { |
| 1124 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1125 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1126 | ssl->handshake->transform_earlydata = NULL; |
| 1127 | |
| 1128 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1129 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1130 | ssl->handshake->transform_handshake = NULL; |
| 1131 | } |
| 1132 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1133 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1137 | { |
| 1138 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1139 | |
| 1140 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1141 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1142 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1143 | |
| 1144 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1145 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1146 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1147 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1148 | |
| 1149 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1150 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1151 | 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] | 1152 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1153 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1154 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1155 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1156 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1157 | if( ssl->session ) |
| 1158 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1159 | mbedtls_ssl_session_free( ssl->session ); |
| 1160 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1161 | ssl->session = NULL; |
| 1162 | } |
| 1163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1164 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1165 | ssl->alpn_chosen = NULL; |
| 1166 | #endif |
| 1167 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1168 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1169 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1170 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1171 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1172 | { |
| 1173 | mbedtls_free( ssl->cli_id ); |
| 1174 | ssl->cli_id = NULL; |
| 1175 | ssl->cli_id_len = 0; |
| 1176 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1177 | #endif |
| 1178 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1179 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1180 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1181 | |
| 1182 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1185 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1186 | * Reset an initialized and used SSL context for re-use while retaining |
| 1187 | * all application-set variables, function pointers and data. |
| 1188 | */ |
| 1189 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1190 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1191 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1195 | * SSL set accessors |
| 1196 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1197 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1198 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1199 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1202 | 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] | 1203 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1204 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1205 | } |
| 1206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1207 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1208 | 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] | 1209 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1210 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1211 | } |
| 1212 | #endif |
| 1213 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1214 | 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] | 1215 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1216 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1217 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1218 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1219 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1220 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1221 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1222 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1223 | { |
| 1224 | ssl->disable_datagram_packing = !allow_packing; |
| 1225 | } |
| 1226 | |
| 1227 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1228 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1229 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1230 | conf->hs_timeout_min = min; |
| 1231 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1232 | } |
| 1233 | #endif |
| 1234 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1235 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1236 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1237 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1240 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1241 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1242 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1243 | void *p_vrfy ) |
| 1244 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1245 | conf->f_vrfy = f_vrfy; |
| 1246 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1247 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1248 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1249 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1250 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1251 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1252 | void *p_rng ) |
| 1253 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1254 | conf->f_rng = f_rng; |
| 1255 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1258 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1259 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1260 | void *p_dbg ) |
| 1261 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1262 | conf->f_dbg = f_dbg; |
| 1263 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1266 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1267 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1268 | mbedtls_ssl_send_t *f_send, |
| 1269 | mbedtls_ssl_recv_t *f_recv, |
| 1270 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1271 | { |
| 1272 | ssl->p_bio = p_bio; |
| 1273 | ssl->f_send = f_send; |
| 1274 | ssl->f_recv = f_recv; |
| 1275 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1278 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1279 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1280 | { |
| 1281 | ssl->mtu = mtu; |
| 1282 | } |
| 1283 | #endif |
| 1284 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1285 | 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] | 1286 | { |
| 1287 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1288 | } |
| 1289 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1290 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1291 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1292 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1293 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1294 | { |
| 1295 | ssl->p_timer = p_timer; |
| 1296 | ssl->f_set_timer = f_set_timer; |
| 1297 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1298 | |
| 1299 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1300 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1301 | } |
| 1302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1303 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1304 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1305 | void *p_cache, |
| 1306 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1307 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1308 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1309 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1310 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1311 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1312 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1313 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1315 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1316 | 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] | 1317 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1318 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1319 | |
| 1320 | if( ssl == NULL || |
| 1321 | session == NULL || |
| 1322 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1323 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1324 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1326 | } |
| 1327 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1328 | if( ssl->handshake->resume == 1 ) |
| 1329 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1330 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1331 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1332 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1333 | return( ret ); |
| 1334 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1335 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1336 | |
| 1337 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1338 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1339 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1340 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1341 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1342 | const int *ciphersuites ) |
| 1343 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1344 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1347 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1348 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1349 | const int kex_modes ) |
| 1350 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1351 | 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] | 1352 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1353 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1355 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1356 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1357 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1358 | { |
| 1359 | conf->cert_profile = profile; |
| 1360 | } |
| 1361 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1362 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1363 | { |
| 1364 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1365 | |
| 1366 | while( cur != NULL ) |
| 1367 | { |
| 1368 | next = cur->next; |
| 1369 | mbedtls_free( cur ); |
| 1370 | cur = next; |
| 1371 | } |
| 1372 | } |
| 1373 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1374 | /* Append a new keycert entry to a (possibly empty) list */ |
| 1375 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1376 | mbedtls_x509_crt *cert, |
| 1377 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1378 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1379 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1380 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1381 | if( cert == NULL ) |
| 1382 | { |
| 1383 | /* Free list if cert is null */ |
| 1384 | ssl_key_cert_free( *head ); |
| 1385 | *head = NULL; |
| 1386 | return( 0 ); |
| 1387 | } |
| 1388 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1389 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1390 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1391 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1392 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1393 | new_cert->cert = cert; |
| 1394 | new_cert->key = key; |
| 1395 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1396 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1397 | /* 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] | 1398 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1399 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1400 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1401 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1402 | else |
| 1403 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1404 | mbedtls_ssl_key_cert *cur = *head; |
| 1405 | while( cur->next != NULL ) |
| 1406 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1407 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1408 | } |
| 1409 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1410 | return( 0 ); |
| 1411 | } |
| 1412 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1413 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1414 | mbedtls_x509_crt *own_cert, |
| 1415 | mbedtls_pk_context *pk_key ) |
| 1416 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1417 | 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] | 1418 | } |
| 1419 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1420 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1421 | mbedtls_x509_crt *ca_chain, |
| 1422 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1423 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1424 | conf->ca_chain = ca_chain; |
| 1425 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1426 | |
| 1427 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1428 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1429 | * cannot be used together. */ |
| 1430 | conf->f_ca_cb = NULL; |
| 1431 | conf->p_ca_cb = NULL; |
| 1432 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1433 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1434 | |
| 1435 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1436 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1437 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1438 | void *p_ca_cb ) |
| 1439 | { |
| 1440 | conf->f_ca_cb = f_ca_cb; |
| 1441 | conf->p_ca_cb = p_ca_cb; |
| 1442 | |
| 1443 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1444 | * cannot be used together. */ |
| 1445 | conf->ca_chain = NULL; |
| 1446 | conf->ca_crl = NULL; |
| 1447 | } |
| 1448 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1450 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1451 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1452 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1453 | size_t *name_len ) |
| 1454 | { |
| 1455 | *name_len = ssl->handshake->sni_name_len; |
| 1456 | return( ssl->handshake->sni_name ); |
| 1457 | } |
| 1458 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1459 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1460 | mbedtls_x509_crt *own_cert, |
| 1461 | mbedtls_pk_context *pk_key ) |
| 1462 | { |
| 1463 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1464 | own_cert, pk_key ) ); |
| 1465 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1466 | |
| 1467 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1468 | mbedtls_x509_crt *ca_chain, |
| 1469 | mbedtls_x509_crl *ca_crl ) |
| 1470 | { |
| 1471 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1472 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1473 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1474 | |
| 1475 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1476 | int authmode ) |
| 1477 | { |
| 1478 | ssl->handshake->sni_authmode = authmode; |
| 1479 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1480 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1481 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1482 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1483 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1484 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1485 | void *p_vrfy ) |
| 1486 | { |
| 1487 | ssl->f_vrfy = f_vrfy; |
| 1488 | ssl->p_vrfy = p_vrfy; |
| 1489 | } |
| 1490 | #endif |
| 1491 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1492 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1493 | /* |
| 1494 | * Set EC J-PAKE password for current handshake |
| 1495 | */ |
| 1496 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1497 | const unsigned char *pw, |
| 1498 | size_t pw_len ) |
| 1499 | { |
| 1500 | mbedtls_ecjpake_role role; |
| 1501 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1502 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1503 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1504 | |
| 1505 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1506 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1507 | else |
| 1508 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1509 | |
| 1510 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1511 | role, |
| 1512 | MBEDTLS_MD_SHA256, |
| 1513 | MBEDTLS_ECP_DP_SECP256R1, |
| 1514 | pw, pw_len ) ); |
| 1515 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1516 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1517 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1518 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1519 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1520 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 1521 | { |
| 1522 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1523 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 1524 | return( 1 ); |
| 1525 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1526 | if( conf->psk != NULL ) |
| 1527 | return( 1 ); |
| 1528 | |
| 1529 | return( 0 ); |
| 1530 | } |
| 1531 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1532 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1533 | { |
| 1534 | /* Remove reference to existing PSK, if any. */ |
| 1535 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1536 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1537 | { |
| 1538 | /* The maintenance of the PSK key slot is the |
| 1539 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1540 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1541 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1542 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1543 | if( conf->psk != NULL ) |
| 1544 | { |
| 1545 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1546 | |
| 1547 | mbedtls_free( conf->psk ); |
| 1548 | conf->psk = NULL; |
| 1549 | conf->psk_len = 0; |
| 1550 | } |
| 1551 | |
| 1552 | /* Remove reference to PSK identity, if any. */ |
| 1553 | if( conf->psk_identity != NULL ) |
| 1554 | { |
| 1555 | mbedtls_free( conf->psk_identity ); |
| 1556 | conf->psk_identity = NULL; |
| 1557 | conf->psk_identity_len = 0; |
| 1558 | } |
| 1559 | } |
| 1560 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1561 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1562 | * It checks that the provided identity is well-formed and attempts |
| 1563 | * to make a copy of it in the SSL config. |
| 1564 | * On failure, the PSK identity in the config remains unset. */ |
| 1565 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1566 | unsigned char const *psk_identity, |
| 1567 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1568 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1569 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1570 | if( psk_identity == NULL || |
| 1571 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1572 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1573 | { |
| 1574 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1575 | } |
| 1576 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1577 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1578 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1579 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1580 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1581 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1582 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1583 | |
| 1584 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1585 | } |
| 1586 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1587 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1588 | const unsigned char *psk, size_t psk_len, |
| 1589 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1590 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1591 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1592 | |
| 1593 | /* We currently only support one PSK, raw or opaque. */ |
| 1594 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1595 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1596 | |
| 1597 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1598 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1599 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1600 | if( psk_len == 0 ) |
| 1601 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1602 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1603 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1604 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1605 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1606 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1607 | conf->psk_len = psk_len; |
| 1608 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1609 | |
| 1610 | /* Check and set PSK Identity */ |
| 1611 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1612 | if( ret != 0 ) |
| 1613 | ssl_conf_remove_psk( conf ); |
| 1614 | |
| 1615 | return( ret ); |
| 1616 | } |
| 1617 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1618 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1619 | { |
| 1620 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1621 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1622 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1623 | /* The maintenance of the external PSK key slot is the |
| 1624 | * user's responsibility. */ |
| 1625 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1626 | { |
| 1627 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1628 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1629 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1630 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1631 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1632 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1633 | if( ssl->handshake->psk != NULL ) |
| 1634 | { |
| 1635 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1636 | ssl->handshake->psk_len ); |
| 1637 | mbedtls_free( ssl->handshake->psk ); |
| 1638 | ssl->handshake->psk_len = 0; |
| 1639 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1640 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1641 | } |
| 1642 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1643 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1644 | const unsigned char *psk, size_t psk_len ) |
| 1645 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1646 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1647 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 1648 | psa_status_t status; |
| 1649 | psa_algorithm_t alg; |
| 1650 | mbedtls_svc_key_id_t key; |
| 1651 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1652 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1653 | if( psk == NULL || ssl->handshake == NULL ) |
| 1654 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1655 | |
| 1656 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1657 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1658 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1659 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1660 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1661 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1662 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) |
| 1663 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 1664 | else |
| 1665 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1666 | |
| 1667 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 1668 | psa_set_key_algorithm( &key_attributes, alg ); |
| 1669 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 1670 | |
| 1671 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 1672 | if( status != PSA_SUCCESS ) |
| 1673 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1674 | |
| 1675 | /* Allow calling psa_destroy_key() on psk remove */ |
| 1676 | ssl->handshake->psk_opaque_is_internal = 1; |
| 1677 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 1678 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1679 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1680 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1681 | |
| 1682 | ssl->handshake->psk_len = psk_len; |
| 1683 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1684 | |
| 1685 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1686 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1689 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1690 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1691 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1692 | const unsigned char *psk_identity, |
| 1693 | size_t psk_identity_len ) |
| 1694 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1695 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1696 | |
| 1697 | /* We currently only support one PSK, raw or opaque. */ |
| 1698 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1699 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1700 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1701 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1702 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1703 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1704 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1705 | |
| 1706 | /* Check and set PSK Identity */ |
| 1707 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1708 | psk_identity_len ); |
| 1709 | if( ret != 0 ) |
| 1710 | ssl_conf_remove_psk( conf ); |
| 1711 | |
| 1712 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1715 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1716 | mbedtls_svc_key_id_t psk ) |
| 1717 | { |
| 1718 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1719 | ( ssl->handshake == NULL ) ) |
| 1720 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1721 | |
| 1722 | ssl_remove_psk( ssl ); |
| 1723 | ssl->handshake->psk_opaque = psk; |
| 1724 | return( 0 ); |
| 1725 | } |
| 1726 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1727 | |
| 1728 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1729 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1730 | size_t), |
| 1731 | void *p_psk ) |
| 1732 | { |
| 1733 | conf->f_psk = f_psk; |
| 1734 | conf->p_psk = p_psk; |
| 1735 | } |
| 1736 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 1737 | |
| 1738 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1739 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1740 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1741 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1742 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1743 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1744 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1745 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1746 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1747 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1748 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1749 | } |
| 1750 | |
| 1751 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1752 | |
| 1753 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1754 | mbedtls_cipher_mode_t mode ) |
| 1755 | { |
| 1756 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1757 | if( mode == MBEDTLS_MODE_CBC ) |
| 1758 | return( MBEDTLS_SSL_MODE_CBC ); |
| 1759 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1760 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1761 | #if defined(MBEDTLS_GCM_C) || \ |
| 1762 | defined(MBEDTLS_CCM_C) || \ |
| 1763 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1764 | if( mode == MBEDTLS_MODE_GCM || |
| 1765 | mode == MBEDTLS_MODE_CCM || |
| 1766 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1767 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 1768 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1769 | |
| 1770 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1771 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1772 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1773 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1774 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 1775 | mbedtls_ssl_mode_t base_mode, |
| 1776 | int encrypt_then_mac ) |
| 1777 | { |
| 1778 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1779 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 1780 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 1781 | { |
| 1782 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 1783 | } |
| 1784 | #else |
| 1785 | (void) encrypt_then_mac; |
| 1786 | #endif |
| 1787 | return( base_mode ); |
| 1788 | } |
| 1789 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1790 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1791 | const mbedtls_ssl_transform *transform ) |
| 1792 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1793 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1794 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1795 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1796 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1797 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 1798 | #endif |
| 1799 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1800 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1801 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1802 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1803 | encrypt_then_mac = transform->encrypt_then_mac; |
| 1804 | #endif |
| 1805 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1808 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1809 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1810 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1811 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1812 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 1813 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1814 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 1815 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1816 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1817 | psa_status_t status; |
| 1818 | psa_algorithm_t alg; |
| 1819 | psa_key_type_t type; |
| 1820 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1821 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 1822 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1823 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1824 | #else |
| 1825 | const mbedtls_cipher_info_t *cipher = |
| 1826 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1827 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1828 | { |
| 1829 | base_mode = |
| 1830 | mbedtls_ssl_get_base_mode( |
| 1831 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 1832 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1833 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1834 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1835 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1836 | int encrypt_then_mac = 0; |
| 1837 | #endif |
| 1838 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1839 | } |
| 1840 | |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 1841 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1842 | 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] | 1843 | size_t taglen, |
| 1844 | psa_algorithm_t *alg, |
| 1845 | psa_key_type_t *key_type, |
| 1846 | size_t *key_size ) |
| 1847 | { |
| 1848 | switch ( mbedtls_cipher_type ) |
| 1849 | { |
| 1850 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 1851 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1852 | *key_type = PSA_KEY_TYPE_AES; |
| 1853 | *key_size = 128; |
| 1854 | break; |
| 1855 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 1856 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1857 | *key_type = PSA_KEY_TYPE_AES; |
| 1858 | *key_size = 128; |
| 1859 | break; |
| 1860 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 1861 | *alg = PSA_ALG_GCM; |
| 1862 | *key_type = PSA_KEY_TYPE_AES; |
| 1863 | *key_size = 128; |
| 1864 | break; |
| 1865 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 1866 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1867 | *key_type = PSA_KEY_TYPE_AES; |
| 1868 | *key_size = 192; |
| 1869 | break; |
| 1870 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 1871 | *alg = PSA_ALG_GCM; |
| 1872 | *key_type = PSA_KEY_TYPE_AES; |
| 1873 | *key_size = 192; |
| 1874 | break; |
| 1875 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 1876 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1877 | *key_type = PSA_KEY_TYPE_AES; |
| 1878 | *key_size = 256; |
| 1879 | break; |
| 1880 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 1881 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1882 | *key_type = PSA_KEY_TYPE_AES; |
| 1883 | *key_size = 256; |
| 1884 | break; |
| 1885 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 1886 | *alg = PSA_ALG_GCM; |
| 1887 | *key_type = PSA_KEY_TYPE_AES; |
| 1888 | *key_size = 256; |
| 1889 | break; |
| 1890 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 1891 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1892 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1893 | *key_size = 128; |
| 1894 | break; |
| 1895 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 1896 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1897 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1898 | *key_size = 128; |
| 1899 | break; |
| 1900 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 1901 | *alg = PSA_ALG_GCM; |
| 1902 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1903 | *key_size = 128; |
| 1904 | break; |
| 1905 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 1906 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1907 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1908 | *key_size = 192; |
| 1909 | break; |
| 1910 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 1911 | *alg = PSA_ALG_GCM; |
| 1912 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1913 | *key_size = 192; |
| 1914 | break; |
| 1915 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 1916 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1917 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1918 | *key_size = 256; |
| 1919 | break; |
| 1920 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 1921 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1922 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1923 | *key_size = 256; |
| 1924 | break; |
| 1925 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 1926 | *alg = PSA_ALG_GCM; |
| 1927 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1928 | *key_size = 256; |
| 1929 | break; |
| 1930 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 1931 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1932 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1933 | *key_size = 128; |
| 1934 | break; |
| 1935 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 1936 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1937 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1938 | *key_size = 128; |
| 1939 | break; |
| 1940 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 1941 | *alg = PSA_ALG_GCM; |
| 1942 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1943 | *key_size = 128; |
| 1944 | break; |
| 1945 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 1946 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1947 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1948 | *key_size = 192; |
| 1949 | break; |
| 1950 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 1951 | *alg = PSA_ALG_GCM; |
| 1952 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1953 | *key_size = 192; |
| 1954 | break; |
| 1955 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 1956 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1957 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1958 | *key_size = 256; |
| 1959 | break; |
| 1960 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 1961 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1962 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1963 | *key_size = 256; |
| 1964 | break; |
| 1965 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 1966 | *alg = PSA_ALG_GCM; |
| 1967 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1968 | *key_size = 256; |
| 1969 | break; |
| 1970 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 1971 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 1972 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 1973 | *key_size = 256; |
| 1974 | break; |
| 1975 | case MBEDTLS_CIPHER_NULL: |
| 1976 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 1977 | *key_type = 0; |
| 1978 | *key_size = 0; |
| 1979 | break; |
| 1980 | default: |
| 1981 | return PSA_ERROR_NOT_SUPPORTED; |
| 1982 | } |
| 1983 | |
| 1984 | return PSA_SUCCESS; |
| 1985 | } |
Neil Armstrong | 8395d7a | 2022-05-18 11:44:56 +0200 | [diff] [blame] | 1986 | #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1987 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1988 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1989 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 1990 | const unsigned char *dhm_P, size_t P_len, |
| 1991 | const unsigned char *dhm_G, size_t G_len ) |
| 1992 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1993 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1994 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1995 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1996 | mbedtls_mpi_free( &conf->dhm_G ); |
| 1997 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1998 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 1999 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2000 | { |
| 2001 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2002 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2003 | return( ret ); |
| 2004 | } |
| 2005 | |
| 2006 | return( 0 ); |
| 2007 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2008 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2009 | 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] | 2010 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2011 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2012 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2013 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2014 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2015 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2016 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2017 | &conf->dhm_P ) ) != 0 || |
| 2018 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2019 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2020 | { |
| 2021 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2022 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2023 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2024 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2025 | |
| 2026 | return( 0 ); |
| 2027 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2028 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2029 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2030 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2031 | /* |
| 2032 | * Set the minimum length for Diffie-Hellman parameters |
| 2033 | */ |
| 2034 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2035 | unsigned int bitlen ) |
| 2036 | { |
| 2037 | conf->dhm_min_bitlen = bitlen; |
| 2038 | } |
| 2039 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2040 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2041 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2042 | #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] | 2043 | /* |
| 2044 | * Set allowed/preferred hashes for handshake signatures |
| 2045 | */ |
| 2046 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2047 | const int *hashes ) |
| 2048 | { |
| 2049 | conf->sig_hashes = hashes; |
| 2050 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2051 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2052 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2053 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2054 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2055 | const uint16_t* sig_algs ) |
| 2056 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2057 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2058 | conf->sig_hashes = NULL; |
| 2059 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2060 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2061 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2062 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2063 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2064 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2065 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2066 | /* |
| 2067 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2068 | * |
| 2069 | * mbedtls_ssl_setup() takes the provided list |
| 2070 | * and translates it to a list of IANA TLS group identifiers, |
| 2071 | * stored in ssl->handshake->group_list. |
| 2072 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2073 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2074 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2075 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2076 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2077 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2078 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2079 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2080 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2081 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2082 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2083 | /* |
| 2084 | * Set the allowed groups |
| 2085 | */ |
| 2086 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2087 | const uint16_t *group_list ) |
| 2088 | { |
| 2089 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2090 | conf->curve_list = NULL; |
| 2091 | #endif |
| 2092 | conf->group_list = group_list; |
| 2093 | } |
| 2094 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2095 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2097 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2098 | /* Initialize to suppress unnecessary compiler warning */ |
| 2099 | size_t hostname_len = 0; |
| 2100 | |
| 2101 | /* Check if new hostname is valid before |
| 2102 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2103 | if( hostname != NULL ) |
| 2104 | { |
| 2105 | hostname_len = strlen( hostname ); |
| 2106 | |
| 2107 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2108 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2109 | } |
| 2110 | |
| 2111 | /* Now it's clear that we will overwrite the old hostname, |
| 2112 | * so we can free it safely */ |
| 2113 | |
| 2114 | if( ssl->hostname != NULL ) |
| 2115 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2116 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2117 | mbedtls_free( ssl->hostname ); |
| 2118 | } |
| 2119 | |
| 2120 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2121 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2122 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2123 | { |
| 2124 | ssl->hostname = NULL; |
| 2125 | } |
| 2126 | else |
| 2127 | { |
| 2128 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2129 | if( ssl->hostname == NULL ) |
| 2130 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2131 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2132 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2133 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2134 | ssl->hostname[hostname_len] = '\0'; |
| 2135 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2136 | |
| 2137 | return( 0 ); |
| 2138 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2139 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2140 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2141 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2142 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2143 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2144 | const unsigned char *, size_t), |
| 2145 | void *p_sni ) |
| 2146 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2147 | conf->f_sni = f_sni; |
| 2148 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2149 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2150 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2152 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2153 | 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] | 2154 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2155 | size_t cur_len, tot_len; |
| 2156 | const char **p; |
| 2157 | |
| 2158 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2159 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2160 | * MUST NOT be truncated." |
| 2161 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2162 | */ |
| 2163 | tot_len = 0; |
| 2164 | for( p = protos; *p != NULL; p++ ) |
| 2165 | { |
| 2166 | cur_len = strlen( *p ); |
| 2167 | tot_len += cur_len; |
| 2168 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2169 | if( ( cur_len == 0 ) || |
| 2170 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2171 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2172 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2173 | } |
| 2174 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2175 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2176 | |
| 2177 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2178 | } |
| 2179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2180 | 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] | 2181 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2182 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2183 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2184 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2185 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2186 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2187 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2188 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2189 | { |
| 2190 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2191 | } |
| 2192 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2193 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2194 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2195 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2196 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2197 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2198 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2199 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2200 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2201 | |
| 2202 | 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] | 2203 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2204 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2205 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2206 | |
| 2207 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2208 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2209 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2212 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2213 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2214 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2215 | const mbedtls_ssl_srtp_profile *p; |
| 2216 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2217 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2218 | /* check the profiles list: all entry must be valid, |
| 2219 | * 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] | 2220 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2221 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2222 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2223 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2224 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2225 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2226 | list_size++; |
| 2227 | } |
| 2228 | else |
| 2229 | { |
| 2230 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2231 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2232 | } |
| 2233 | } |
| 2234 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2235 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2236 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2237 | conf->dtls_srtp_profile_list = NULL; |
| 2238 | conf->dtls_srtp_profile_list_len = 0; |
| 2239 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2240 | } |
| 2241 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2242 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2243 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2244 | |
| 2245 | return( 0 ); |
| 2246 | } |
| 2247 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2248 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2249 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2250 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2251 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2252 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2253 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2254 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2255 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2256 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2257 | else |
| 2258 | { |
| 2259 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2260 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2261 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2262 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2263 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2264 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2265 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2266 | 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] | 2267 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2268 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2271 | 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] | 2272 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2273 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2276 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2277 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2278 | char cert_req_ca_list ) |
| 2279 | { |
| 2280 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2281 | } |
| 2282 | #endif |
| 2283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2284 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2285 | 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] | 2286 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2287 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2288 | } |
| 2289 | #endif |
| 2290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2291 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2292 | 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] | 2293 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2294 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2295 | } |
| 2296 | #endif |
| 2297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2298 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2299 | 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] | 2300 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2301 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2302 | 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] | 2303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2304 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2305 | } |
| 2306 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2307 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2308 | |
| 2309 | return( 0 ); |
| 2310 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2311 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2312 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2313 | 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] | 2314 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2315 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2318 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2319 | 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] | 2320 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2321 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2322 | } |
| 2323 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2324 | 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] | 2325 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2326 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2327 | } |
| 2328 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2329 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2330 | const unsigned char period[8] ) |
| 2331 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2332 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2333 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2334 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2336 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2337 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2338 | 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] | 2339 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2340 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2341 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2342 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2343 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2344 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2345 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2346 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2347 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2348 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2349 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2350 | conf->f_ticket_write = f_ticket_write; |
| 2351 | conf->f_ticket_parse = f_ticket_parse; |
| 2352 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2353 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2354 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2355 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2356 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2357 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2358 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2359 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2360 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2361 | ssl->f_export_keys = f_export_keys; |
| 2362 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2363 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2364 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2365 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2366 | void mbedtls_ssl_conf_async_private_cb( |
| 2367 | mbedtls_ssl_config *conf, |
| 2368 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2369 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2370 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2371 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2372 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2373 | { |
| 2374 | conf->f_async_sign_start = f_async_sign; |
| 2375 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2376 | conf->f_async_resume = f_async_resume; |
| 2377 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2378 | conf->p_async_config_data = async_config_data; |
| 2379 | } |
| 2380 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2381 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2382 | { |
| 2383 | return( conf->p_async_config_data ); |
| 2384 | } |
| 2385 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2386 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2387 | { |
| 2388 | if( ssl->handshake == NULL ) |
| 2389 | return( NULL ); |
| 2390 | else |
| 2391 | return( ssl->handshake->user_async_ctx ); |
| 2392 | } |
| 2393 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2394 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2395 | void *ctx ) |
| 2396 | { |
| 2397 | if( ssl->handshake != NULL ) |
| 2398 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2399 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2400 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2401 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2402 | /* |
| 2403 | * SSL get accessors |
| 2404 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2405 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2406 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2407 | if( ssl->session != NULL ) |
| 2408 | return( ssl->session->verify_result ); |
| 2409 | |
| 2410 | if( ssl->session_negotiate != NULL ) |
| 2411 | return( ssl->session_negotiate->verify_result ); |
| 2412 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2413 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2416 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2417 | { |
| 2418 | if( ssl == NULL || ssl->session == NULL ) |
| 2419 | return( 0 ); |
| 2420 | |
| 2421 | return( ssl->session->ciphersuite ); |
| 2422 | } |
| 2423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2424 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2425 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2426 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2427 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2429 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2432 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2433 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2434 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2435 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2436 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2437 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2438 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2439 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2440 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2441 | default: |
| 2442 | return( "unknown (DTLS)" ); |
| 2443 | } |
| 2444 | } |
| 2445 | #endif |
| 2446 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2447 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2448 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2449 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2450 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2451 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2452 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2453 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2454 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2455 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2458 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2459 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2460 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2461 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2462 | size_t read_mfl; |
| 2463 | |
| 2464 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2465 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2466 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2467 | { |
| 2468 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2469 | } |
| 2470 | |
| 2471 | /* Check if a smaller max length was negotiated */ |
| 2472 | if( ssl->session_out != NULL ) |
| 2473 | { |
| 2474 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2475 | if( read_mfl < max_len ) |
| 2476 | { |
| 2477 | max_len = read_mfl; |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | // During a handshake, use the value being negotiated |
| 2482 | if( ssl->session_negotiate != NULL ) |
| 2483 | { |
| 2484 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2485 | if( read_mfl < max_len ) |
| 2486 | { |
| 2487 | max_len = read_mfl; |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | return( max_len ); |
| 2492 | } |
| 2493 | |
| 2494 | 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] | 2495 | { |
| 2496 | size_t max_len; |
| 2497 | |
| 2498 | /* |
| 2499 | * Assume mfl_code is correct since it was checked when set |
| 2500 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2501 | 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] | 2502 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2503 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2504 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2505 | 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] | 2506 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2507 | 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] | 2508 | } |
| 2509 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2510 | /* During a handshake, use the value being negotiated */ |
| 2511 | if( ssl->session_negotiate != NULL && |
| 2512 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2513 | { |
| 2514 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2515 | } |
| 2516 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2517 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2518 | } |
| 2519 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2520 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2521 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2522 | 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] | 2523 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2524 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2525 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2526 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 2527 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 2528 | return ( 0 ); |
| 2529 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2530 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 2531 | return( ssl->mtu ); |
| 2532 | |
| 2533 | if( ssl->mtu == 0 ) |
| 2534 | return( ssl->handshake->mtu ); |
| 2535 | |
| 2536 | return( ssl->mtu < ssl->handshake->mtu ? |
| 2537 | ssl->mtu : ssl->handshake->mtu ); |
| 2538 | } |
| 2539 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2540 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2541 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 2542 | { |
| 2543 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2544 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2545 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2546 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2547 | (void) ssl; |
| 2548 | #endif |
| 2549 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2550 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2551 | 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] | 2552 | |
| 2553 | if( max_len > mfl ) |
| 2554 | max_len = mfl; |
| 2555 | #endif |
| 2556 | |
| 2557 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2558 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2559 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2560 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2561 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 2562 | const size_t overhead = (size_t) ret; |
| 2563 | |
| 2564 | if( ret < 0 ) |
| 2565 | return( ret ); |
| 2566 | |
| 2567 | if( mtu <= overhead ) |
| 2568 | { |
| 2569 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 2570 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2571 | } |
| 2572 | |
| 2573 | if( max_len > mtu - overhead ) |
| 2574 | max_len = mtu - overhead; |
| 2575 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2576 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2577 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2578 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2579 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2580 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2581 | #endif |
| 2582 | |
| 2583 | return( (int) max_len ); |
| 2584 | } |
| 2585 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2586 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 2587 | { |
| 2588 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 2589 | |
| 2590 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2591 | (void) ssl; |
| 2592 | #endif |
| 2593 | |
| 2594 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2595 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 2596 | |
| 2597 | if( max_len > mfl ) |
| 2598 | max_len = mfl; |
| 2599 | #endif |
| 2600 | |
| 2601 | return( (int) max_len ); |
| 2602 | } |
| 2603 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2604 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2605 | 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] | 2606 | { |
| 2607 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2608 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2609 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2610 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2611 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2612 | #else |
| 2613 | return( NULL ); |
| 2614 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2615 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2616 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2618 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2619 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 2620 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2621 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2622 | int ret; |
| 2623 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2624 | if( ssl == NULL || |
| 2625 | dst == NULL || |
| 2626 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2627 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2629 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2630 | } |
| 2631 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2632 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 2633 | * idempotent: Each session can only be exported once. |
| 2634 | * |
| 2635 | * (This is in preparation for TLS 1.3 support where we will |
| 2636 | * need the ability to export multiple sessions (aka tickets), |
| 2637 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 2638 | * multiple times until it fails.) |
| 2639 | * |
| 2640 | * Check whether we have already exported the current session, |
| 2641 | * and fail if so. |
| 2642 | */ |
| 2643 | if( ssl->session->exported == 1 ) |
| 2644 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2645 | |
| 2646 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 2647 | if( ret != 0 ) |
| 2648 | return( ret ); |
| 2649 | |
| 2650 | /* Remember that we've exported the session. */ |
| 2651 | ssl->session->exported = 1; |
| 2652 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2653 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2655 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2656 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2657 | * Define ticket header determining Mbed TLS version |
| 2658 | * and structure of the ticket. |
| 2659 | */ |
| 2660 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2661 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2662 | * Define bitflag determining compile-time settings influencing |
| 2663 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2664 | */ |
| 2665 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2666 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2667 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2668 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2669 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2670 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2671 | |
| 2672 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2673 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2674 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2675 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2676 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2677 | |
| 2678 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2679 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2680 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2681 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2682 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 2683 | |
| 2684 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2685 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2686 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2687 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2688 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2689 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2690 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2691 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2692 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2693 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2694 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 2695 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2696 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2697 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 2698 | #else |
| 2699 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 2700 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 2701 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2702 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 2703 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 2704 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 2705 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 2706 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 2707 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2708 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2709 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2710 | ( (uint16_t) ( \ |
| 2711 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 2712 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 2713 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 2714 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2715 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 2716 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2717 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2718 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2719 | MBEDTLS_VERSION_MAJOR, |
| 2720 | MBEDTLS_VERSION_MINOR, |
| 2721 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 2722 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 2723 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2724 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2725 | |
| 2726 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2727 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 2728 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2729 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2730 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2731 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 2732 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 2733 | * opaque session_format[2]; // library-version specific 16-bit field |
| 2734 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2735 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2736 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2737 | * Note: When updating the format, remember to keep |
| 2738 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2739 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2740 | * // In this version, `session_format` determines |
| 2741 | * // the setting of those compile-time |
| 2742 | * // configuration options which influence |
| 2743 | * // the structure of mbedtls_ssl_session. |
| 2744 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2745 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 2746 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2747 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2748 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2749 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2750 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2751 | * serialized_session_tls12 data; |
| 2752 | * |
| 2753 | * }; |
| 2754 | * |
| 2755 | * } serialized_session; |
| 2756 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2757 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2758 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2759 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 2760 | unsigned char omit_header, |
| 2761 | unsigned char *buf, |
| 2762 | size_t buf_len, |
| 2763 | size_t *olen ) |
| 2764 | { |
| 2765 | unsigned char *p = buf; |
| 2766 | size_t used = 0; |
| 2767 | |
| 2768 | if( !omit_header ) |
| 2769 | { |
| 2770 | /* |
| 2771 | * Add Mbed TLS version identifier |
| 2772 | */ |
| 2773 | |
| 2774 | used += sizeof( ssl_serialized_session_header ); |
| 2775 | |
| 2776 | if( used <= buf_len ) |
| 2777 | { |
| 2778 | memcpy( p, ssl_serialized_session_header, |
| 2779 | sizeof( ssl_serialized_session_header ) ); |
| 2780 | p += sizeof( ssl_serialized_session_header ); |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | /* |
| 2785 | * TLS version identifier |
| 2786 | */ |
| 2787 | used += 1; |
| 2788 | if( used <= buf_len ) |
| 2789 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2790 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | /* Forward to version-specific serialization routine. */ |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2794 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2795 | { |
| 2796 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2797 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2798 | { |
| 2799 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 2800 | used += ssl_session_save_tls12( session, p, remaining_len ); |
| 2801 | break; |
| 2802 | } |
| 2803 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2804 | |
| 2805 | default: |
| 2806 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2807 | } |
| 2808 | |
| 2809 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 2810 | if( used > buf_len ) |
| 2811 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2812 | |
| 2813 | return( 0 ); |
| 2814 | } |
| 2815 | |
| 2816 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2817 | * Public wrapper for ssl_session_save() |
| 2818 | */ |
| 2819 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 2820 | unsigned char *buf, |
| 2821 | size_t buf_len, |
| 2822 | size_t *olen ) |
| 2823 | { |
| 2824 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 2825 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2826 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 2827 | /* |
| 2828 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 2829 | * |
| 2830 | * This internal version is wrapped by a public function that cleans up in |
| 2831 | * case of error, and has an extra option omit_header. |
| 2832 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2833 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 2834 | unsigned char omit_header, |
| 2835 | const unsigned char *buf, |
| 2836 | size_t len ) |
| 2837 | { |
| 2838 | const unsigned char *p = buf; |
| 2839 | const unsigned char * const end = buf + len; |
| 2840 | |
| 2841 | if( !omit_header ) |
| 2842 | { |
| 2843 | /* |
| 2844 | * Check Mbed TLS version identifier |
| 2845 | */ |
| 2846 | |
| 2847 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 2848 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2849 | |
| 2850 | if( memcmp( p, ssl_serialized_session_header, |
| 2851 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 2852 | { |
| 2853 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 2854 | } |
| 2855 | p += sizeof( ssl_serialized_session_header ); |
| 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * TLS version identifier |
| 2860 | */ |
| 2861 | if( 1 > (size_t)( end - p ) ) |
| 2862 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2863 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2864 | |
| 2865 | /* Dispatch according to TLS version. */ |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2866 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2867 | { |
| 2868 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2869 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2870 | { |
| 2871 | size_t remaining_len = ( end - p ); |
| 2872 | return( ssl_session_load_tls12( session, p, remaining_len ) ); |
| 2873 | } |
| 2874 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2875 | |
| 2876 | default: |
| 2877 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2878 | } |
| 2879 | } |
| 2880 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2881 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 2882 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2883 | */ |
| 2884 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 2885 | const unsigned char *buf, |
| 2886 | size_t len ) |
| 2887 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2888 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2889 | |
| 2890 | if( ret != 0 ) |
| 2891 | mbedtls_ssl_session_free( session ); |
| 2892 | |
| 2893 | return( ret ); |
| 2894 | } |
| 2895 | |
| 2896 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2897 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2898 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2899 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 2900 | { |
| 2901 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2902 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2903 | /* |
| 2904 | * 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] | 2905 | * that were written into the output buffer by the previous handshake step, |
| 2906 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2907 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 2908 | * We proceed to the next handshake step only when all data from the |
| 2909 | * previous one have been sent to the peer, thus we make sure that this is |
| 2910 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 2911 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 2912 | * we have to wait before to go ahead. |
| 2913 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 2914 | * peer. Data are only sent here and through |
| 2915 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 2916 | * alert occurred. |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2917 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2918 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 2919 | return( ret ); |
| 2920 | |
| 2921 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2922 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2923 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 2924 | { |
| 2925 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2926 | return( ret ); |
| 2927 | } |
| 2928 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2929 | |
| 2930 | return( ret ); |
| 2931 | } |
| 2932 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2933 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2934 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2935 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2936 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2937 | if( ssl == NULL || |
| 2938 | ssl->conf == NULL || |
| 2939 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2940 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2941 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2942 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | ret = ssl_prepare_handshake_step( ssl ); |
| 2946 | if( ret != 0 ) |
| 2947 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2948 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2949 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 2950 | if( ret != 0 ) |
| 2951 | goto cleanup; |
| 2952 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2953 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2954 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2955 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 2956 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 2957 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2958 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2959 | switch( ssl->state ) |
| 2960 | { |
| 2961 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 2962 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 2963 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2964 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2965 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 2966 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 2967 | break; |
| 2968 | |
| 2969 | default: |
| 2970 | #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] | 2971 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2972 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 2973 | else |
| 2974 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 2975 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2976 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 2977 | #else |
| 2978 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 2979 | #endif |
| 2980 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2981 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2982 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2983 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2984 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2985 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2986 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2987 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 2988 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2989 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2990 | |
| 2991 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2992 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 2993 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 2994 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2995 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2996 | #endif |
| 2997 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2998 | if( ret != 0 ) |
| 2999 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3000 | /* handshake_step return error. And it is same |
| 3001 | * with alert_reason. |
| 3002 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3003 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3004 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3005 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3006 | goto cleanup; |
| 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3011 | return( ret ); |
| 3012 | } |
| 3013 | |
| 3014 | /* |
| 3015 | * Perform the SSL handshake |
| 3016 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3017 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3018 | { |
| 3019 | int ret = 0; |
| 3020 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3021 | /* Sanity checks */ |
| 3022 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3023 | if( ssl == NULL || ssl->conf == NULL ) |
| 3024 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3025 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3026 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3027 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3028 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3029 | { |
| 3030 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3031 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3032 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3033 | } |
| 3034 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3035 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3036 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3037 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3038 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3039 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3040 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3041 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3042 | |
| 3043 | if( ret != 0 ) |
| 3044 | break; |
| 3045 | } |
| 3046 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3047 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3048 | |
| 3049 | return( ret ); |
| 3050 | } |
| 3051 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3052 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3053 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3054 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3055 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3056 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3057 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3058 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3059 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3061 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3062 | |
| 3063 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3064 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3065 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3066 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3067 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3068 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3069 | 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] | 3070 | return( ret ); |
| 3071 | } |
| 3072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3073 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3074 | |
| 3075 | return( 0 ); |
| 3076 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3077 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3078 | |
| 3079 | /* |
| 3080 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3081 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3082 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3083 | * - 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] | 3084 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3085 | * 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] | 3086 | * 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] | 3087 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3088 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3089 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3090 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3093 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3094 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3095 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3096 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3097 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3098 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3099 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3100 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3101 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3102 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3103 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3104 | ssl->handshake->out_msg_seq = 1; |
| 3105 | else |
| 3106 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3107 | } |
| 3108 | #endif |
| 3109 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3110 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3111 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3113 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3114 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3115 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3116 | return( ret ); |
| 3117 | } |
| 3118 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3119 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3120 | |
| 3121 | return( 0 ); |
| 3122 | } |
| 3123 | |
| 3124 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3125 | * Renegotiate current connection on client, |
| 3126 | * or request renegotiation on server |
| 3127 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3128 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3129 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3130 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3131 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3132 | if( ssl == NULL || ssl->conf == NULL ) |
| 3133 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3134 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3135 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3136 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3137 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3138 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3139 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3140 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3142 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3143 | |
| 3144 | /* Did we already try/start sending HelloRequest? */ |
| 3145 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3146 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3147 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3148 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3149 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3150 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3152 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3153 | /* |
| 3154 | * On client, either start the renegotiation process or, |
| 3155 | * if already in progress, continue the handshake |
| 3156 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3157 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3158 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3159 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3160 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3161 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3162 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3163 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3164 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3165 | return( ret ); |
| 3166 | } |
| 3167 | } |
| 3168 | else |
| 3169 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3170 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3171 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3172 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3173 | return( ret ); |
| 3174 | } |
| 3175 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3176 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3177 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3178 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3179 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3180 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3181 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3182 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3183 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3184 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3185 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3186 | if( handshake == NULL ) |
| 3187 | return; |
| 3188 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3189 | #if defined(MBEDTLS_ECP_C) |
| 3190 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3191 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3192 | mbedtls_free( (void*) handshake->group_list ); |
| 3193 | handshake->group_list = NULL; |
| 3194 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3195 | #endif /* MBEDTLS_ECP_C */ |
| 3196 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3197 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 3198 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3199 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3200 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3201 | handshake->sig_algs = NULL; |
| 3202 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3203 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3204 | if( ssl->handshake->certificate_request_context ) |
| 3205 | { |
| 3206 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3207 | } |
| 3208 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3209 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 3210 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3211 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3212 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3213 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3214 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3215 | handshake->async_in_progress = 0; |
| 3216 | } |
| 3217 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3218 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3219 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3220 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3221 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3222 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3223 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3224 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3225 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 3226 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3227 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3228 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3229 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3230 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 3231 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3232 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3234 | #if defined(MBEDTLS_DHM_C) |
| 3235 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3236 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3237 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3238 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3239 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3240 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3241 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3242 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3243 | mbedtls_free( handshake->ecjpake_cache ); |
| 3244 | handshake->ecjpake_cache = NULL; |
| 3245 | handshake->ecjpake_cache_len = 0; |
| 3246 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3247 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3248 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3249 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3250 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3251 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3252 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3253 | #endif |
| 3254 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3255 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3256 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3257 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3258 | { |
| 3259 | /* The maintenance of the external PSK key slot is the |
| 3260 | * user's responsibility. */ |
| 3261 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3262 | { |
| 3263 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3264 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3265 | } |
| 3266 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3267 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3268 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3269 | if( handshake->psk != NULL ) |
| 3270 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3271 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3272 | mbedtls_free( handshake->psk ); |
| 3273 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3274 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3275 | #endif |
| 3276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3277 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3278 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3279 | /* |
| 3280 | * Free only the linked list wrapper, not the keys themselves |
| 3281 | * since the belong to the SNI callback |
| 3282 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3283 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3284 | #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] | 3285 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3286 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3287 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3288 | if( handshake->ecrs_peer_cert != NULL ) |
| 3289 | { |
| 3290 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3291 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3292 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3293 | #endif |
| 3294 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3295 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3296 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3297 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3298 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3299 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3300 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3301 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3302 | mbedtls_free( handshake->cookie ); |
| 3303 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3304 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3305 | |
| 3306 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3307 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3308 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3309 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3310 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3311 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3312 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3313 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3314 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3315 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3316 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3317 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3318 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3319 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3320 | mbedtls_free( handshake->transform_earlydata ); |
| 3321 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3322 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3323 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3324 | |
| 3325 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3326 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3327 | * processes datagrams and the fact that a datagram is allowed to have |
| 3328 | * several records in it, it is possible that the I/O buffers are not |
| 3329 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3330 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3331 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3332 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3333 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3334 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3335 | mbedtls_platform_zeroize( handshake, |
| 3336 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3339 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3340 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3341 | if( session == NULL ) |
| 3342 | return; |
| 3343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3344 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3345 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3346 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3347 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3348 | #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] | 3349 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3350 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3351 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3352 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3355 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3356 | |
| 3357 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3358 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3359 | #else |
| 3360 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3361 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3362 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3363 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3364 | |
| 3365 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3366 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3367 | #else |
| 3368 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3369 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3370 | |
| 3371 | #if defined(MBEDTLS_SSL_ALPN) |
| 3372 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3373 | #else |
| 3374 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3375 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3376 | |
| 3377 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3378 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3379 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3380 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3381 | |
| 3382 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3383 | ( (uint32_t) ( \ |
| 3384 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3385 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3386 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3387 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3388 | 0u ) ) |
| 3389 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3390 | static unsigned char ssl_serialized_context_header[] = { |
| 3391 | MBEDTLS_VERSION_MAJOR, |
| 3392 | MBEDTLS_VERSION_MINOR, |
| 3393 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3394 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3395 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3396 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3397 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3398 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3399 | }; |
| 3400 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3401 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3402 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3403 | * |
| 3404 | * The format of the serialized data is: |
| 3405 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3406 | * |
| 3407 | * // header |
| 3408 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3409 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3410 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3411 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3412 | * Note: When updating the format, remember to keep these |
| 3413 | * 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] | 3414 | * |
| 3415 | * // session sub-structure |
| 3416 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3417 | * // transform sub-structure |
| 3418 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3419 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3420 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3421 | * // fields from ssl_context |
| 3422 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3423 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3424 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3425 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3426 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3427 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3428 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3429 | * |
| 3430 | * Note that many fields of the ssl_context or sub-structures are not |
| 3431 | * serialized, as they fall in one of the following categories: |
| 3432 | * |
| 3433 | * 1. forced value (eg in_left must be 0) |
| 3434 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3435 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3436 | * 4. value was temporary (eg content of input buffer) |
| 3437 | * 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] | 3438 | */ |
| 3439 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3440 | unsigned char *buf, |
| 3441 | size_t buf_len, |
| 3442 | size_t *olen ) |
| 3443 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3444 | unsigned char *p = buf; |
| 3445 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3446 | size_t session_len; |
| 3447 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3448 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3449 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3450 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3451 | * this function's documentation. |
| 3452 | * |
| 3453 | * These are due to assumptions/limitations in the implementation. Some of |
| 3454 | * them are likely to stay (no handshake in progress) some might go away |
| 3455 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3456 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3457 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3458 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3459 | { |
| 3460 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3461 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3462 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3463 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3464 | { |
| 3465 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3466 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3467 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3468 | /* Double-check that sub-structures are indeed ready */ |
| 3469 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3470 | { |
| 3471 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3472 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3473 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3474 | /* There must be no pending incoming or outgoing data */ |
| 3475 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3476 | { |
| 3477 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3478 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3479 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3480 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3481 | { |
| 3482 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3483 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3484 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3485 | /* Protocol must be DLTS, not TLS */ |
| 3486 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3487 | { |
| 3488 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3489 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3490 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3491 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3492 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3493 | { |
| 3494 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3495 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3496 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3497 | /* We must be using an AEAD ciphersuite */ |
| 3498 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3499 | { |
| 3500 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3501 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3502 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3503 | /* Renegotiation must not be enabled */ |
| 3504 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3505 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3506 | { |
| 3507 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3508 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3509 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3510 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3511 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3512 | /* |
| 3513 | * Version and format identifier |
| 3514 | */ |
| 3515 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3516 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3517 | if( used <= buf_len ) |
| 3518 | { |
| 3519 | memcpy( p, ssl_serialized_context_header, |
| 3520 | sizeof( ssl_serialized_context_header ) ); |
| 3521 | p += sizeof( ssl_serialized_context_header ); |
| 3522 | } |
| 3523 | |
| 3524 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3525 | * Session (length + data) |
| 3526 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3527 | 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] | 3528 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 3529 | return( ret ); |
| 3530 | |
| 3531 | used += 4 + session_len; |
| 3532 | if( used <= buf_len ) |
| 3533 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3534 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 3535 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3536 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3537 | ret = ssl_session_save( ssl->session, 1, |
| 3538 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3539 | if( ret != 0 ) |
| 3540 | return( ret ); |
| 3541 | |
| 3542 | p += session_len; |
| 3543 | } |
| 3544 | |
| 3545 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3546 | * Transform |
| 3547 | */ |
| 3548 | used += sizeof( ssl->transform->randbytes ); |
| 3549 | if( used <= buf_len ) |
| 3550 | { |
| 3551 | memcpy( p, ssl->transform->randbytes, |
| 3552 | sizeof( ssl->transform->randbytes ) ); |
| 3553 | p += sizeof( ssl->transform->randbytes ); |
| 3554 | } |
| 3555 | |
| 3556 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3557 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 3558 | if( used <= buf_len ) |
| 3559 | { |
| 3560 | *p++ = ssl->transform->in_cid_len; |
| 3561 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 3562 | p += ssl->transform->in_cid_len; |
| 3563 | |
| 3564 | *p++ = ssl->transform->out_cid_len; |
| 3565 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 3566 | p += ssl->transform->out_cid_len; |
| 3567 | } |
| 3568 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3569 | |
| 3570 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3571 | * Saved fields from top-level ssl_context structure |
| 3572 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3573 | used += 4; |
| 3574 | if( used <= buf_len ) |
| 3575 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3576 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 3577 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3578 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3579 | |
| 3580 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3581 | used += 16; |
| 3582 | if( used <= buf_len ) |
| 3583 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3584 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 3585 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3586 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3587 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 3588 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3589 | } |
| 3590 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3591 | |
| 3592 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3593 | used += 1; |
| 3594 | if( used <= buf_len ) |
| 3595 | { |
| 3596 | *p++ = ssl->disable_datagram_packing; |
| 3597 | } |
| 3598 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3599 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3600 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3601 | if( used <= buf_len ) |
| 3602 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3603 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 3604 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3608 | used += 2; |
| 3609 | if( used <= buf_len ) |
| 3610 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3611 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 3612 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3613 | } |
| 3614 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3615 | |
| 3616 | #if defined(MBEDTLS_SSL_ALPN) |
| 3617 | { |
| 3618 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 3619 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3620 | : 0; |
| 3621 | |
| 3622 | used += 1 + alpn_len; |
| 3623 | if( used <= buf_len ) |
| 3624 | { |
| 3625 | *p++ = alpn_len; |
| 3626 | |
| 3627 | if( ssl->alpn_chosen != NULL ) |
| 3628 | { |
| 3629 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 3630 | p += alpn_len; |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3635 | |
| 3636 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3637 | * Done |
| 3638 | */ |
| 3639 | *olen = used; |
| 3640 | |
| 3641 | if( used > buf_len ) |
| 3642 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3643 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3644 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 3645 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3646 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3650 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3651 | * |
| 3652 | * This internal version is wrapped by a public function that cleans up in |
| 3653 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3654 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3655 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 3656 | const unsigned char *buf, |
| 3657 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3658 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3659 | const unsigned char *p = buf; |
| 3660 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3661 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3662 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3663 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3664 | /* |
| 3665 | * The context should have been freshly setup or reset. |
| 3666 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 3667 | * (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] | 3668 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 3669 | */ |
| 3670 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 3671 | ssl->session != NULL ) |
| 3672 | { |
| 3673 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3674 | } |
| 3675 | |
| 3676 | /* |
| 3677 | * We can't check that the config matches the initial one, but we can at |
| 3678 | * least check it matches the requirements for serializing. |
| 3679 | */ |
| 3680 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3681 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 3682 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3683 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3684 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3685 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3686 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3687 | { |
| 3688 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3689 | } |
| 3690 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3691 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 3692 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3693 | /* |
| 3694 | * Check version identifier |
| 3695 | */ |
| 3696 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 3697 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3698 | |
| 3699 | if( memcmp( p, ssl_serialized_context_header, |
| 3700 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 3701 | { |
| 3702 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3703 | } |
| 3704 | p += sizeof( ssl_serialized_context_header ); |
| 3705 | |
| 3706 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3707 | * Session |
| 3708 | */ |
| 3709 | if( (size_t)( end - p ) < 4 ) |
| 3710 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3711 | |
| 3712 | session_len = ( (size_t) p[0] << 24 ) | |
| 3713 | ( (size_t) p[1] << 16 ) | |
| 3714 | ( (size_t) p[2] << 8 ) | |
| 3715 | ( (size_t) p[3] ); |
| 3716 | p += 4; |
| 3717 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3718 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3719 | * 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] | 3720 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3721 | ssl->session_in = ssl->session; |
| 3722 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3723 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3724 | |
| 3725 | if( (size_t)( end - p ) < session_len ) |
| 3726 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3727 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3728 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3729 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3730 | { |
| 3731 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3732 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3733 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3734 | |
| 3735 | p += session_len; |
| 3736 | |
| 3737 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3738 | * Transform |
| 3739 | */ |
| 3740 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3741 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3742 | * 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] | 3743 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3744 | ssl->transform_in = ssl->transform; |
| 3745 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3746 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3747 | |
| 3748 | /* Read random bytes and populate structure */ |
| 3749 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 3750 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3751 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3752 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3753 | ssl->session->ciphersuite, |
| 3754 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 3755 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3756 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 3757 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3758 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 3759 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 3760 | 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] | 3761 | ssl->conf->endpoint, |
| 3762 | ssl ); |
| 3763 | if( ret != 0 ) |
| 3764 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3765 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3766 | p += sizeof( ssl->transform->randbytes ); |
| 3767 | |
| 3768 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3769 | /* Read connection IDs and store them */ |
| 3770 | if( (size_t)( end - p ) < 1 ) |
| 3771 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3772 | |
| 3773 | ssl->transform->in_cid_len = *p++; |
| 3774 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 3775 | 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] | 3776 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3777 | |
| 3778 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 3779 | p += ssl->transform->in_cid_len; |
| 3780 | |
| 3781 | ssl->transform->out_cid_len = *p++; |
| 3782 | |
| 3783 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 3784 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3785 | |
| 3786 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 3787 | p += ssl->transform->out_cid_len; |
| 3788 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3789 | |
| 3790 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3791 | * Saved fields from top-level ssl_context structure |
| 3792 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3793 | if( (size_t)( end - p ) < 4 ) |
| 3794 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3795 | |
| 3796 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 3797 | ( (uint32_t) p[1] << 16 ) | |
| 3798 | ( (uint32_t) p[2] << 8 ) | |
| 3799 | ( (uint32_t) p[3] ); |
| 3800 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3801 | |
| 3802 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3803 | if( (size_t)( end - p ) < 16 ) |
| 3804 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3805 | |
| 3806 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 3807 | ( (uint64_t) p[1] << 48 ) | |
| 3808 | ( (uint64_t) p[2] << 40 ) | |
| 3809 | ( (uint64_t) p[3] << 32 ) | |
| 3810 | ( (uint64_t) p[4] << 24 ) | |
| 3811 | ( (uint64_t) p[5] << 16 ) | |
| 3812 | ( (uint64_t) p[6] << 8 ) | |
| 3813 | ( (uint64_t) p[7] ); |
| 3814 | p += 8; |
| 3815 | |
| 3816 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 3817 | ( (uint64_t) p[1] << 48 ) | |
| 3818 | ( (uint64_t) p[2] << 40 ) | |
| 3819 | ( (uint64_t) p[3] << 32 ) | |
| 3820 | ( (uint64_t) p[4] << 24 ) | |
| 3821 | ( (uint64_t) p[5] << 16 ) | |
| 3822 | ( (uint64_t) p[6] << 8 ) | |
| 3823 | ( (uint64_t) p[7] ); |
| 3824 | p += 8; |
| 3825 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3826 | |
| 3827 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3828 | if( (size_t)( end - p ) < 1 ) |
| 3829 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3830 | |
| 3831 | ssl->disable_datagram_packing = *p++; |
| 3832 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3833 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3834 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3835 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3836 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 3837 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3838 | |
| 3839 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3840 | if( (size_t)( end - p ) < 2 ) |
| 3841 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3842 | |
| 3843 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 3844 | p += 2; |
| 3845 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3846 | |
| 3847 | #if defined(MBEDTLS_SSL_ALPN) |
| 3848 | { |
| 3849 | uint8_t alpn_len; |
| 3850 | const char **cur; |
| 3851 | |
| 3852 | if( (size_t)( end - p ) < 1 ) |
| 3853 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3854 | |
| 3855 | alpn_len = *p++; |
| 3856 | |
| 3857 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 3858 | { |
| 3859 | /* alpn_chosen should point to an item in the configured list */ |
| 3860 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 3861 | { |
| 3862 | if( strlen( *cur ) == alpn_len && |
| 3863 | memcmp( p, cur, alpn_len ) == 0 ) |
| 3864 | { |
| 3865 | ssl->alpn_chosen = *cur; |
| 3866 | break; |
| 3867 | } |
| 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | /* can only happen on conf mismatch */ |
| 3872 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 3873 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3874 | |
| 3875 | p += alpn_len; |
| 3876 | } |
| 3877 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3878 | |
| 3879 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3880 | * Forced fields from top-level ssl_context structure |
| 3881 | * |
| 3882 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 3883 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 3884 | */ |
| 3885 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3886 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3887 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3888 | /* Adjust pointers for header fields of outgoing records to |
| 3889 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3890 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3891 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3892 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3893 | ssl->in_epoch = 1; |
| 3894 | #endif |
| 3895 | |
| 3896 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 3897 | * 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] | 3898 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 3899 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3900 | if( ssl->handshake != NULL ) |
| 3901 | { |
| 3902 | mbedtls_ssl_handshake_free( ssl ); |
| 3903 | mbedtls_free( ssl->handshake ); |
| 3904 | ssl->handshake = NULL; |
| 3905 | } |
| 3906 | |
| 3907 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3908 | * Done - should have consumed entire buffer |
| 3909 | */ |
| 3910 | if( p != end ) |
| 3911 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3912 | |
| 3913 | return( 0 ); |
| 3914 | } |
| 3915 | |
| 3916 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3917 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3918 | */ |
| 3919 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 3920 | const unsigned char *buf, |
| 3921 | size_t len ) |
| 3922 | { |
| 3923 | int ret = ssl_context_load( context, buf, len ); |
| 3924 | |
| 3925 | if( ret != 0 ) |
| 3926 | mbedtls_ssl_free( context ); |
| 3927 | |
| 3928 | return( ret ); |
| 3929 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3930 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3931 | |
| 3932 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3933 | * Free an SSL context |
| 3934 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3935 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3936 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3937 | if( ssl == NULL ) |
| 3938 | return; |
| 3939 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3940 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3941 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3942 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3943 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3944 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3945 | size_t out_buf_len = ssl->out_buf_len; |
| 3946 | #else |
| 3947 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3948 | #endif |
| 3949 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3950 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3951 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3952 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3955 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3956 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3957 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3958 | size_t in_buf_len = ssl->in_buf_len; |
| 3959 | #else |
| 3960 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3961 | #endif |
| 3962 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3963 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3964 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3965 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3968 | if( ssl->transform ) |
| 3969 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3970 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3971 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | if( ssl->handshake ) |
| 3975 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3976 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3977 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 3978 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3979 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3980 | mbedtls_free( ssl->handshake ); |
| 3981 | mbedtls_free( ssl->transform_negotiate ); |
| 3982 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3985 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3986 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 3987 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3988 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3989 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3990 | if( ssl->session ) |
| 3991 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3992 | mbedtls_ssl_session_free( ssl->session ); |
| 3993 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 3996 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3997 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3998 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3999 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4000 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4001 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4002 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4003 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4004 | #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] | 4005 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4006 | #endif |
| 4007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4008 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4009 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4010 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4011 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4014 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4015 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4016 | */ |
| 4017 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4018 | { |
| 4019 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4020 | } |
| 4021 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4022 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4023 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4024 | /* 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] | 4025 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 4026 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4027 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 4028 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4029 | static int ssl_preset_default_hashes[] = { |
| 4030 | #if defined(MBEDTLS_SHA512_C) |
| 4031 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4032 | #endif |
| 4033 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4034 | MBEDTLS_MD_SHA384, |
| 4035 | #endif |
| 4036 | #if defined(MBEDTLS_SHA256_C) |
| 4037 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4038 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4039 | MBEDTLS_MD_NONE |
| 4040 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4041 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 4042 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4043 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4044 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4045 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4046 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4047 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4048 | * about this list. |
| 4049 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4050 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4051 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4052 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4053 | #endif |
| 4054 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4055 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4056 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4057 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4058 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4059 | #endif |
| 4060 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4061 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4062 | #endif |
| 4063 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4064 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4065 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4066 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4067 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4068 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4069 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4070 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4071 | #endif |
| 4072 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4073 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4074 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4075 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4076 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4077 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4078 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4079 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4080 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4081 | 0 |
| 4082 | }; |
| 4083 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4084 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4085 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4086 | static int ssl_preset_suiteb_hashes[] = { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4087 | #if defined(MBEDTLS_SHA256_C) |
| 4088 | MBEDTLS_MD_SHA256, |
| 4089 | #endif |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4090 | #if defined(MBEDTLS_SHA384_C) |
| 4091 | MBEDTLS_MD_SHA384, |
| 4092 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4093 | MBEDTLS_MD_NONE |
| 4094 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4095 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4096 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4097 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4098 | * 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] | 4099 | * rules SHOULD be upheld. |
| 4100 | * - No duplicate entries. |
| 4101 | * - But if there is a good reason, do not change the order of the algorithms. |
| 4102 | * - ssl_tls12_present* is for TLS 1.2 use only. |
| 4103 | * - 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] | 4104 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4105 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4106 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4107 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 4108 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4109 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 4110 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 4111 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4112 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4113 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 4114 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4115 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 4116 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 4117 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4118 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4119 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ |
| 4120 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4121 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
| 4122 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 4123 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4124 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4125 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 4126 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4127 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
| 4128 | |
Ronald Cron | 8540cf6 | 2022-03-16 08:01:09 +0100 | [diff] [blame] | 4129 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C) |
| 4130 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4131 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4132 | |
| 4133 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C) |
| 4134 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4135 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4136 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4137 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 4138 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4139 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4140 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4141 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4142 | }; |
| 4143 | |
| 4144 | /* NOTICE: see above */ |
| 4145 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4146 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4147 | #if defined(MBEDTLS_SHA512_C) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4148 | #if defined(MBEDTLS_ECDSA_C) |
| 4149 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512 ), |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4150 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4151 | #if defined(MBEDTLS_RSA_C) |
| 4152 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512 ), |
| 4153 | #endif |
| 4154 | #endif /* MBEDTLS_SHA512_C */ |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4155 | #if defined(MBEDTLS_SHA384_C) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4156 | #if defined(MBEDTLS_ECDSA_C) |
| 4157 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ), |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4158 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4159 | #if defined(MBEDTLS_RSA_C) |
| 4160 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4161 | #endif |
| 4162 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4163 | #if defined(MBEDTLS_SHA256_C) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4164 | #if defined(MBEDTLS_ECDSA_C) |
| 4165 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ), |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4166 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4167 | #if defined(MBEDTLS_RSA_C) |
| 4168 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4169 | #endif |
| 4170 | #endif /* MBEDTLS_SHA256_C */ |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4171 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4172 | }; |
| 4173 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4174 | /* NOTICE: see above */ |
| 4175 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4176 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4177 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 4178 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4179 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 4180 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 4181 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4182 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4183 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 4184 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4185 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 4186 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 4187 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4188 | |
| 4189 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 4190 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4191 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4192 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4193 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 4194 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4195 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4196 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4197 | MBEDTLS_TLS_SIG_NONE |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4198 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4199 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4200 | /* NOTICE: see above */ |
| 4201 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4202 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4203 | #if defined(MBEDTLS_SHA256_C) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4204 | #if defined(MBEDTLS_ECDSA_C) |
| 4205 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256 ), |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4206 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4207 | #if defined(MBEDTLS_RSA_C) |
| 4208 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256 ), |
| 4209 | #endif |
| 4210 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 4211 | #if defined(MBEDTLS_SHA384_C) |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4212 | #if defined(MBEDTLS_ECDSA_C) |
| 4213 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384 ), |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 4214 | #endif |
Gabor Mezei | c1051b6 | 2022-05-10 13:13:58 +0200 | [diff] [blame] | 4215 | #if defined(MBEDTLS_RSA_C) |
| 4216 | MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG( MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384 ), |
| 4217 | #endif |
| 4218 | #endif /* MBEDTLS_SHA256_C */ |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4219 | MBEDTLS_TLS_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4220 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4221 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4222 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4223 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4224 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4225 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4226 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4227 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4228 | #endif |
| 4229 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4230 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4231 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4232 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4233 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4234 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4235 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4236 | /* 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] | 4237 | * to make sure there are no duplicated signature algorithm entries. */ |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4238 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4239 | { |
| 4240 | size_t i, j; |
| 4241 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4242 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4243 | for( i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4244 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4245 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4246 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4247 | if( sig_algs[i] != sig_algs[j] ) |
| 4248 | continue; |
| 4249 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4250 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4251 | sig_algs[i], j, i ); |
| 4252 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4253 | } |
| 4254 | } |
| 4255 | return( ret ); |
| 4256 | } |
| 4257 | |
| 4258 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4259 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4260 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4261 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4262 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4263 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4264 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4265 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4266 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4267 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4268 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4269 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4270 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4271 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4272 | { |
| 4273 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4274 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4275 | } |
| 4276 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4277 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4278 | { |
| 4279 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4280 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4281 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4282 | |
| 4283 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4284 | 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] | 4285 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4286 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4287 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4288 | } |
| 4289 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4290 | 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] | 4291 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4292 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4293 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4294 | } |
| 4295 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4296 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4297 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4298 | /* Use the functions here so that they are covered in tests, |
| 4299 | * but otherwise access member directly for efficiency */ |
| 4300 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4301 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4302 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4303 | /* |
| 4304 | * Things that are common to all presets |
| 4305 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4306 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4307 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4308 | { |
| 4309 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4310 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4311 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4312 | #endif |
| 4313 | } |
| 4314 | #endif |
| 4315 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4316 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4317 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4318 | #endif |
| 4319 | |
| 4320 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4321 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4322 | #endif |
| 4323 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4324 | #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] | 4325 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4326 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4327 | #endif |
| 4328 | |
| 4329 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4330 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4331 | #endif |
| 4332 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4333 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4334 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4335 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4336 | #endif |
| 4337 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4338 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4339 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4340 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4341 | #endif |
| 4342 | |
| 4343 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4344 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4345 | memset( conf->renego_period, 0x00, 2 ); |
| 4346 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4347 | #endif |
| 4348 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4349 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4350 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4351 | { |
| 4352 | const unsigned char dhm_p[] = |
| 4353 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4354 | const unsigned char dhm_g[] = |
| 4355 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4356 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4357 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4358 | dhm_p, sizeof( dhm_p ), |
| 4359 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4360 | { |
| 4361 | return( ret ); |
| 4362 | } |
| 4363 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4364 | #endif |
| 4365 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4366 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4367 | /* |
| 4368 | * Allow all TLS 1.3 key exchange modes by default. |
| 4369 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4370 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4371 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4372 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4373 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4374 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4375 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4376 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4377 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4378 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4379 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4380 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4381 | } |
| 4382 | else |
| 4383 | { |
| 4384 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4385 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4386 | { |
| 4387 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4388 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4389 | } |
| 4390 | else |
| 4391 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4392 | { |
| 4393 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4394 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4395 | } |
| 4396 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4397 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4398 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4399 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4400 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4401 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4402 | #else |
| 4403 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4404 | #endif |
| 4405 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4406 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4407 | /* |
| 4408 | * Preset-specific defaults |
| 4409 | */ |
| 4410 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4411 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4412 | /* |
| 4413 | * NSA Suite B |
| 4414 | */ |
| 4415 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4416 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4417 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4418 | |
| 4419 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4420 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4421 | #endif |
| 4422 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4423 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4424 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4425 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4426 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4427 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4428 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4429 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4430 | else |
| 4431 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4432 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4433 | #endif |
| 4434 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4435 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4436 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4437 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4438 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4439 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4440 | |
| 4441 | /* |
| 4442 | * Default |
| 4443 | */ |
| 4444 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4445 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4446 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4447 | |
| 4448 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4449 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4450 | #endif |
| 4451 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4452 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4453 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4454 | conf->sig_hashes = ssl_preset_default_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4455 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4456 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4457 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4458 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4459 | else |
| 4460 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4461 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4462 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4463 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4464 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4465 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4466 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4467 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4468 | |
| 4469 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4470 | conf->dhm_min_bitlen = 1024; |
| 4471 | #endif |
| 4472 | } |
| 4473 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4474 | return( 0 ); |
| 4475 | } |
| 4476 | |
| 4477 | /* |
| 4478 | * Free mbedtls_ssl_config |
| 4479 | */ |
| 4480 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4481 | { |
| 4482 | #if defined(MBEDTLS_DHM_C) |
| 4483 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4484 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4485 | #endif |
| 4486 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4487 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4488 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4489 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 4490 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4491 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4492 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4493 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4494 | if( conf->psk != NULL ) |
| 4495 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4496 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4497 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4498 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4499 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | if( conf->psk_identity != NULL ) |
| 4503 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4504 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4505 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4506 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4507 | conf->psk_identity_len = 0; |
| 4508 | } |
| 4509 | #endif |
| 4510 | |
| 4511 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4512 | ssl_key_cert_free( conf->key_cert ); |
| 4513 | #endif |
| 4514 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4515 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4518 | #if defined(MBEDTLS_PK_C) && \ |
| 4519 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4520 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4521 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4522 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4523 | 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] | 4524 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4525 | #if defined(MBEDTLS_RSA_C) |
| 4526 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 4527 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4528 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4529 | #if defined(MBEDTLS_ECDSA_C) |
| 4530 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 4531 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4532 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4533 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4534 | } |
| 4535 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4536 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 4537 | { |
| 4538 | switch( type ) { |
| 4539 | case MBEDTLS_PK_RSA: |
| 4540 | return( MBEDTLS_SSL_SIG_RSA ); |
| 4541 | case MBEDTLS_PK_ECDSA: |
| 4542 | case MBEDTLS_PK_ECKEY: |
| 4543 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 4544 | default: |
| 4545 | return( MBEDTLS_SSL_SIG_ANON ); |
| 4546 | } |
| 4547 | } |
| 4548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4549 | 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] | 4550 | { |
| 4551 | switch( sig ) |
| 4552 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4553 | #if defined(MBEDTLS_RSA_C) |
| 4554 | case MBEDTLS_SSL_SIG_RSA: |
| 4555 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4556 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4557 | #if defined(MBEDTLS_ECDSA_C) |
| 4558 | case MBEDTLS_SSL_SIG_ECDSA: |
| 4559 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4560 | #endif |
| 4561 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4562 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4563 | } |
| 4564 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4565 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4566 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4567 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4568 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4569 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4570 | 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] | 4571 | { |
| 4572 | switch( hash ) |
| 4573 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4574 | #if defined(MBEDTLS_MD5_C) |
| 4575 | case MBEDTLS_SSL_HASH_MD5: |
| 4576 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4577 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4578 | #if defined(MBEDTLS_SHA1_C) |
| 4579 | case MBEDTLS_SSL_HASH_SHA1: |
| 4580 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4581 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4582 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4583 | case MBEDTLS_SSL_HASH_SHA224: |
| 4584 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4585 | #endif |
| 4586 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4587 | case MBEDTLS_SSL_HASH_SHA256: |
| 4588 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4589 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4590 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4591 | case MBEDTLS_SSL_HASH_SHA384: |
| 4592 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4593 | #endif |
| 4594 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4595 | case MBEDTLS_SSL_HASH_SHA512: |
| 4596 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4597 | #endif |
| 4598 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4599 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4600 | } |
| 4601 | } |
| 4602 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4603 | /* |
| 4604 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 4605 | */ |
| 4606 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 4607 | { |
| 4608 | switch( md ) |
| 4609 | { |
| 4610 | #if defined(MBEDTLS_MD5_C) |
| 4611 | case MBEDTLS_MD_MD5: |
| 4612 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 4613 | #endif |
| 4614 | #if defined(MBEDTLS_SHA1_C) |
| 4615 | case MBEDTLS_MD_SHA1: |
| 4616 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 4617 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4618 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4619 | case MBEDTLS_MD_SHA224: |
| 4620 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4621 | #endif |
| 4622 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4623 | case MBEDTLS_MD_SHA256: |
| 4624 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 4625 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4626 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4627 | case MBEDTLS_MD_SHA384: |
| 4628 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4629 | #endif |
| 4630 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4631 | case MBEDTLS_MD_SHA512: |
| 4632 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 4633 | #endif |
| 4634 | default: |
| 4635 | return( MBEDTLS_SSL_HASH_NONE ); |
| 4636 | } |
| 4637 | } |
| 4638 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4639 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4640 | * 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] | 4641 | * 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] | 4642 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4643 | 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] | 4644 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4645 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4646 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4647 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4648 | return( -1 ); |
| 4649 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4650 | for( ; *group_list != 0; group_list++ ) |
| 4651 | { |
| 4652 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4653 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4654 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4655 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4656 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4657 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4658 | |
| 4659 | #if defined(MBEDTLS_ECP_C) |
| 4660 | /* |
| 4661 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 4662 | */ |
| 4663 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 4664 | { |
Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 4665 | 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] | 4666 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 4667 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4668 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4670 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4671 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 4672 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4673 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4674 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4675 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4676 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4677 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4678 | const char *ext_oid; |
| 4679 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4681 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4682 | { |
| 4683 | /* Server part of the key exchange */ |
| 4684 | switch( ciphersuite->key_exchange ) |
| 4685 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4686 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 4687 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4688 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4689 | break; |
| 4690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4691 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 4692 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 4693 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 4694 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4695 | break; |
| 4696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4697 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 4698 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4699 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4700 | break; |
| 4701 | |
| 4702 | /* 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] | 4703 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 4704 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 4705 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 4706 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 4707 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4708 | usage = 0; |
| 4709 | } |
| 4710 | } |
| 4711 | else |
| 4712 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4713 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 4714 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4715 | } |
| 4716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4717 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4718 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4719 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4720 | ret = -1; |
| 4721 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4722 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4723 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4724 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4725 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 4726 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4727 | } |
| 4728 | else |
| 4729 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4730 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 4731 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4732 | } |
| 4733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4734 | 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] | 4735 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4736 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4737 | ret = -1; |
| 4738 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4739 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4740 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4741 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4742 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 4743 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4744 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4745 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 4746 | const mbedtls_md_type_t md, |
| 4747 | unsigned char *dst, |
| 4748 | size_t dst_len, |
| 4749 | size_t *olen ) |
| 4750 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4751 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4752 | psa_hash_operation_t *hash_operation_to_clone; |
| 4753 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 4754 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4755 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4756 | |
| 4757 | switch( md ) |
| 4758 | { |
| 4759 | #if defined(MBEDTLS_SHA384_C) |
| 4760 | case MBEDTLS_MD_SHA384: |
| 4761 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 4762 | break; |
| 4763 | #endif |
| 4764 | |
| 4765 | #if defined(MBEDTLS_SHA256_C) |
| 4766 | case MBEDTLS_MD_SHA256: |
| 4767 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 4768 | break; |
| 4769 | #endif |
| 4770 | |
| 4771 | default: |
| 4772 | goto exit; |
| 4773 | } |
| 4774 | |
| 4775 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 4776 | if( status != PSA_SUCCESS ) |
| 4777 | goto exit; |
| 4778 | |
| 4779 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 4780 | if( status != PSA_SUCCESS ) |
| 4781 | goto exit; |
| 4782 | |
| 4783 | exit: |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 4784 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4785 | } |
| 4786 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4787 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4788 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4789 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 4790 | unsigned char *dst, |
| 4791 | size_t dst_len, |
| 4792 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4793 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4794 | int ret; |
| 4795 | mbedtls_sha512_context sha512; |
| 4796 | |
| 4797 | if( dst_len < 48 ) |
| 4798 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4799 | |
| 4800 | mbedtls_sha512_init( &sha512 ); |
| 4801 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 4802 | |
| 4803 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 4804 | { |
| 4805 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 4806 | goto exit; |
| 4807 | } |
| 4808 | |
| 4809 | *olen = 48; |
| 4810 | |
| 4811 | exit: |
| 4812 | |
| 4813 | mbedtls_sha512_free( &sha512 ); |
| 4814 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4815 | } |
| 4816 | #endif /* MBEDTLS_SHA384_C */ |
| 4817 | |
| 4818 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4819 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 4820 | unsigned char *dst, |
| 4821 | size_t dst_len, |
| 4822 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4823 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4824 | int ret; |
| 4825 | mbedtls_sha256_context sha256; |
| 4826 | |
| 4827 | if( dst_len < 32 ) |
| 4828 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4829 | |
| 4830 | mbedtls_sha256_init( &sha256 ); |
| 4831 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4832 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4833 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 4834 | { |
| 4835 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 4836 | goto exit; |
| 4837 | } |
| 4838 | |
| 4839 | *olen = 32; |
| 4840 | |
| 4841 | exit: |
| 4842 | |
| 4843 | mbedtls_sha256_free( &sha256 ); |
| 4844 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4845 | } |
| 4846 | #endif /* MBEDTLS_SHA256_C */ |
| 4847 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4848 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 4849 | const mbedtls_md_type_t md, |
| 4850 | unsigned char *dst, |
| 4851 | size_t dst_len, |
| 4852 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4853 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4854 | switch( md ) |
| 4855 | { |
| 4856 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4857 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4858 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4859 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4860 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4861 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4862 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4863 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4864 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4865 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4866 | |
| 4867 | default: |
| 4868 | break; |
| 4869 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4870 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4871 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 4872 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4873 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4874 | |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 4875 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 4876 | /* mbedtls_ssl_parse_sig_alg_ext() |
| 4877 | * |
| 4878 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 4879 | * value (TLS 1.3 RFC8446): |
| 4880 | * enum { |
| 4881 | * .... |
| 4882 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 4883 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 4884 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 4885 | * .... |
| 4886 | * } SignatureScheme; |
| 4887 | * |
| 4888 | * struct { |
| 4889 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 4890 | * } SignatureSchemeList; |
| 4891 | * |
| 4892 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 4893 | * value (TLS 1.2 RFC5246): |
| 4894 | * enum { |
| 4895 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 4896 | * sha512(6), (255) |
| 4897 | * } HashAlgorithm; |
| 4898 | * |
| 4899 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 4900 | * SignatureAlgorithm; |
| 4901 | * |
| 4902 | * struct { |
| 4903 | * HashAlgorithm hash; |
| 4904 | * SignatureAlgorithm signature; |
| 4905 | * } SignatureAndHashAlgorithm; |
| 4906 | * |
| 4907 | * SignatureAndHashAlgorithm |
| 4908 | * supported_signature_algorithms<2..2^16-2>; |
| 4909 | * |
| 4910 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 4911 | * generalization of the TLS 1.2 signature algorithm extension. |
| 4912 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 4913 | * `SignatureScheme` field of TLS 1.3 |
| 4914 | * |
| 4915 | */ |
| 4916 | int mbedtls_ssl_parse_sig_alg_ext( mbedtls_ssl_context *ssl, |
| 4917 | const unsigned char *buf, |
| 4918 | const unsigned char *end ) |
| 4919 | { |
| 4920 | const unsigned char *p = buf; |
| 4921 | size_t supported_sig_algs_len = 0; |
| 4922 | const unsigned char *supported_sig_algs_end; |
| 4923 | uint16_t sig_alg; |
| 4924 | uint32_t common_idx = 0; |
| 4925 | |
| 4926 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
| 4927 | supported_sig_algs_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 4928 | p += 2; |
| 4929 | |
| 4930 | memset( ssl->handshake->received_sig_algs, 0, |
| 4931 | sizeof(ssl->handshake->received_sig_algs) ); |
| 4932 | |
| 4933 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, supported_sig_algs_len ); |
| 4934 | supported_sig_algs_end = p + supported_sig_algs_len; |
| 4935 | while( p < supported_sig_algs_end ) |
| 4936 | { |
| 4937 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, supported_sig_algs_end, 2 ); |
| 4938 | sig_alg = MBEDTLS_GET_UINT16_BE( p, 0 ); |
| 4939 | p += 2; |
| 4940 | |
| 4941 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "received signature algorithm: 0x%x", |
| 4942 | sig_alg ) ); |
| 4943 | |
| 4944 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, sig_alg ) || |
| 4945 | ! mbedtls_ssl_sig_alg_is_offered( ssl, sig_alg ) ) |
| 4946 | continue; |
| 4947 | |
| 4948 | if( common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE ) |
| 4949 | { |
| 4950 | ssl->handshake->received_sig_algs[common_idx] = sig_alg; |
| 4951 | common_idx += 1; |
| 4952 | } |
| 4953 | } |
| 4954 | /* Check that we consumed all the message. */ |
| 4955 | if( p != end ) |
| 4956 | { |
| 4957 | MBEDTLS_SSL_DEBUG_MSG( 1, |
| 4958 | ( "Signature algorithms extension length misaligned" ) ); |
| 4959 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, |
| 4960 | MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 4961 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 4962 | } |
| 4963 | |
| 4964 | if( common_idx == 0 ) |
| 4965 | { |
| 4966 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no signature algorithm in common" ) ); |
| 4967 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, |
| 4968 | MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 4969 | return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE ); |
| 4970 | } |
| 4971 | |
Gabor Mezei | 15b95a6 | 2022-05-09 16:37:58 +0200 | [diff] [blame] | 4972 | ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 4973 | return( 0 ); |
| 4974 | } |
| 4975 | |
| 4976 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4977 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4978 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4979 | |
| 4980 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4981 | |
| 4982 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 4983 | mbedtls_svc_key_id_t key, |
| 4984 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4985 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4986 | const unsigned char* seed, size_t seed_length, |
| 4987 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 4988 | const unsigned char* other_secret, |
| 4989 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4990 | size_t capacity ) |
| 4991 | { |
| 4992 | psa_status_t status; |
| 4993 | |
| 4994 | status = psa_key_derivation_setup( derivation, alg ); |
| 4995 | if( status != PSA_SUCCESS ) |
| 4996 | return( status ); |
| 4997 | |
| 4998 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 4999 | { |
| 5000 | status = psa_key_derivation_input_bytes( derivation, |
| 5001 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 5002 | seed, seed_length ); |
| 5003 | if( status != PSA_SUCCESS ) |
| 5004 | return( status ); |
| 5005 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5006 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5007 | { |
| 5008 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5009 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 5010 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5011 | if( status != PSA_SUCCESS ) |
| 5012 | return( status ); |
| 5013 | } |
| 5014 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5015 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 5016 | { |
| 5017 | status = psa_key_derivation_input_bytes( |
| 5018 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5019 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5020 | } |
| 5021 | else |
| 5022 | { |
| 5023 | status = psa_key_derivation_input_key( |
| 5024 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 5025 | } |
| 5026 | if( status != PSA_SUCCESS ) |
| 5027 | return( status ); |
| 5028 | |
| 5029 | status = psa_key_derivation_input_bytes( derivation, |
| 5030 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 5031 | label, label_length ); |
| 5032 | if( status != PSA_SUCCESS ) |
| 5033 | return( status ); |
| 5034 | } |
| 5035 | else |
| 5036 | { |
| 5037 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5038 | } |
| 5039 | |
| 5040 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5041 | if( status != PSA_SUCCESS ) |
| 5042 | return( status ); |
| 5043 | |
| 5044 | return( PSA_SUCCESS ); |
| 5045 | } |
| 5046 | |
| 5047 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5048 | const unsigned char *secret, size_t slen, |
| 5049 | const char *label, |
| 5050 | const unsigned char *random, size_t rlen, |
| 5051 | unsigned char *dstbuf, size_t dlen ) |
| 5052 | { |
| 5053 | psa_status_t status; |
| 5054 | psa_algorithm_t alg; |
| 5055 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5056 | psa_key_derivation_operation_t derivation = |
| 5057 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5058 | |
| 5059 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5060 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5061 | else |
| 5062 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5063 | |
| 5064 | /* Normally a "secret" should be long enough to be impossible to |
| 5065 | * find by brute force, and in particular should not be empty. But |
| 5066 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5067 | * and for this use case it makes sense to have a 0-length "secret". |
| 5068 | * Since the key API doesn't allow importing a key of length 0, |
| 5069 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5070 | * to mean a 0-length "secret" input. */ |
| 5071 | if( slen != 0 ) |
| 5072 | { |
| 5073 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5074 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5075 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5076 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5077 | |
| 5078 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5079 | if( status != PSA_SUCCESS ) |
| 5080 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5081 | } |
| 5082 | |
| 5083 | status = setup_psa_key_derivation( &derivation, |
| 5084 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5085 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5086 | random, rlen, |
| 5087 | (unsigned char const *) label, |
| 5088 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 5089 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 5090 | dlen ); |
| 5091 | if( status != PSA_SUCCESS ) |
| 5092 | { |
| 5093 | psa_key_derivation_abort( &derivation ); |
| 5094 | psa_destroy_key( master_key ); |
| 5095 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5096 | } |
| 5097 | |
| 5098 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5099 | if( status != PSA_SUCCESS ) |
| 5100 | { |
| 5101 | psa_key_derivation_abort( &derivation ); |
| 5102 | psa_destroy_key( master_key ); |
| 5103 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5104 | } |
| 5105 | |
| 5106 | status = psa_key_derivation_abort( &derivation ); |
| 5107 | if( status != PSA_SUCCESS ) |
| 5108 | { |
| 5109 | psa_destroy_key( master_key ); |
| 5110 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5111 | } |
| 5112 | |
| 5113 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5114 | status = psa_destroy_key( master_key ); |
| 5115 | if( status != PSA_SUCCESS ) |
| 5116 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5117 | |
| 5118 | return( 0 ); |
| 5119 | } |
| 5120 | |
| 5121 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5122 | |
| 5123 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5124 | const unsigned char *secret, size_t slen, |
| 5125 | const char *label, |
| 5126 | const unsigned char *random, size_t rlen, |
| 5127 | unsigned char *dstbuf, size_t dlen ) |
| 5128 | { |
| 5129 | size_t nb; |
| 5130 | size_t i, j, k, md_len; |
| 5131 | unsigned char *tmp; |
| 5132 | size_t tmp_len = 0; |
| 5133 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5134 | const mbedtls_md_info_t *md_info; |
| 5135 | mbedtls_md_context_t md_ctx; |
| 5136 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5137 | |
| 5138 | mbedtls_md_init( &md_ctx ); |
| 5139 | |
| 5140 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5141 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5142 | |
| 5143 | md_len = mbedtls_md_get_size( md_info ); |
| 5144 | |
| 5145 | tmp_len = md_len + strlen( label ) + rlen; |
| 5146 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5147 | if( tmp == NULL ) |
| 5148 | { |
| 5149 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5150 | goto exit; |
| 5151 | } |
| 5152 | |
| 5153 | nb = strlen( label ); |
| 5154 | memcpy( tmp + md_len, label, nb ); |
| 5155 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5156 | nb += rlen; |
| 5157 | |
| 5158 | /* |
| 5159 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5160 | */ |
| 5161 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5162 | goto exit; |
| 5163 | |
| 5164 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5165 | if( ret != 0 ) |
| 5166 | goto exit; |
| 5167 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5168 | if( ret != 0 ) |
| 5169 | goto exit; |
| 5170 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5171 | if( ret != 0 ) |
| 5172 | goto exit; |
| 5173 | |
| 5174 | for( i = 0; i < dlen; i += md_len ) |
| 5175 | { |
| 5176 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5177 | if( ret != 0 ) |
| 5178 | goto exit; |
| 5179 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5180 | if( ret != 0 ) |
| 5181 | goto exit; |
| 5182 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5183 | if( ret != 0 ) |
| 5184 | goto exit; |
| 5185 | |
| 5186 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5187 | if( ret != 0 ) |
| 5188 | goto exit; |
| 5189 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5190 | if( ret != 0 ) |
| 5191 | goto exit; |
| 5192 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5193 | if( ret != 0 ) |
| 5194 | goto exit; |
| 5195 | |
| 5196 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5197 | |
| 5198 | for( j = 0; j < k; j++ ) |
| 5199 | dstbuf[i + j] = h_i[j]; |
| 5200 | } |
| 5201 | |
| 5202 | exit: |
| 5203 | mbedtls_md_free( &md_ctx ); |
| 5204 | |
| 5205 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5206 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5207 | |
| 5208 | mbedtls_free( tmp ); |
| 5209 | |
| 5210 | return( ret ); |
| 5211 | } |
| 5212 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5213 | |
| 5214 | #if defined(MBEDTLS_SHA256_C) |
| 5215 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5216 | const char *label, |
| 5217 | const unsigned char *random, size_t rlen, |
| 5218 | unsigned char *dstbuf, size_t dlen ) |
| 5219 | { |
| 5220 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5221 | label, random, rlen, dstbuf, dlen ) ); |
| 5222 | } |
| 5223 | #endif /* MBEDTLS_SHA256_C */ |
| 5224 | |
| 5225 | #if defined(MBEDTLS_SHA384_C) |
| 5226 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5227 | const char *label, |
| 5228 | const unsigned char *random, size_t rlen, |
| 5229 | unsigned char *dstbuf, size_t dlen ) |
| 5230 | { |
| 5231 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5232 | label, random, rlen, dstbuf, dlen ) ); |
| 5233 | } |
| 5234 | #endif /* MBEDTLS_SHA384_C */ |
| 5235 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5236 | /* |
| 5237 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5238 | * |
| 5239 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5240 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5241 | * |
| 5242 | * Outputs: |
| 5243 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5244 | */ |
| 5245 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5246 | mbedtls_md_type_t hash ) |
| 5247 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5248 | #if defined(MBEDTLS_SHA384_C) |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5249 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5250 | { |
| 5251 | handshake->tls_prf = tls_prf_sha384; |
| 5252 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5253 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5254 | } |
| 5255 | else |
| 5256 | #endif |
| 5257 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5258 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5259 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5260 | handshake->tls_prf = tls_prf_sha256; |
| 5261 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5262 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5263 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5264 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5265 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5266 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5267 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5268 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5269 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5270 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5271 | |
| 5272 | return( 0 ); |
| 5273 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5274 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5275 | /* |
| 5276 | * Compute master secret if needed |
| 5277 | * |
| 5278 | * Parameters: |
| 5279 | * [in/out] handshake |
| 5280 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5281 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5282 | * [out] premaster (cleared) |
| 5283 | * [out] master |
| 5284 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5285 | * debug: conf->f_dbg, conf->p_dbg |
| 5286 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5287 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5288 | */ |
| 5289 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5290 | unsigned char *master, |
| 5291 | const mbedtls_ssl_context *ssl ) |
| 5292 | { |
| 5293 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5294 | |
| 5295 | /* cf. RFC 5246, Section 8.1: |
| 5296 | * "The master secret is always exactly 48 bytes in length." */ |
| 5297 | size_t const master_secret_len = 48; |
| 5298 | |
| 5299 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5300 | unsigned char session_hash[48]; |
| 5301 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5302 | |
| 5303 | /* The label for the KDF used for key expansion. |
| 5304 | * This is either "master secret" or "extended master secret" |
| 5305 | * depending on whether the Extended Master Secret extension |
| 5306 | * is used. */ |
| 5307 | char const *lbl = "master secret"; |
| 5308 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5309 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5310 | * - If the Extended Master Secret extension is not used, |
| 5311 | * this is ClientHello.Random + ServerHello.Random |
| 5312 | * (see Sect. 8.1 in RFC 5246). |
| 5313 | * - If the Extended Master Secret extension is used, |
| 5314 | * this is the transcript of the handshake so far. |
| 5315 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5316 | unsigned char const *seed = handshake->randbytes; |
| 5317 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5318 | |
| 5319 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5320 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5321 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5322 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5323 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5324 | (void) ssl; |
| 5325 | #endif |
| 5326 | |
| 5327 | if( handshake->resume != 0 ) |
| 5328 | { |
| 5329 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5330 | return( 0 ); |
| 5331 | } |
| 5332 | |
| 5333 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5334 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5335 | { |
| 5336 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5337 | seed = session_hash; |
| 5338 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5339 | |
| 5340 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5341 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5342 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5343 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5344 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5345 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5346 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5347 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5348 | { |
| 5349 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5350 | psa_status_t status; |
| 5351 | psa_algorithm_t alg; |
| 5352 | mbedtls_svc_key_id_t psk; |
| 5353 | psa_key_derivation_operation_t derivation = |
| 5354 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5355 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5356 | |
| 5357 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5358 | |
| 5359 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5360 | |
| 5361 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5362 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5363 | else |
| 5364 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5365 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5366 | size_t other_secret_len = 0; |
| 5367 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5368 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5369 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5370 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5371 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5372 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5373 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5374 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5375 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5376 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5377 | other_secret_len = 48; |
| 5378 | other_secret = handshake->premaster + 2; |
| 5379 | break; |
| 5380 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5381 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5382 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 5383 | other_secret = handshake->premaster + 2; |
| 5384 | break; |
| 5385 | default: |
| 5386 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5387 | } |
| 5388 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5389 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5390 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5391 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5392 | (unsigned char const *) lbl, |
| 5393 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5394 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5395 | master_secret_len ); |
| 5396 | if( status != PSA_SUCCESS ) |
| 5397 | { |
| 5398 | psa_key_derivation_abort( &derivation ); |
| 5399 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5400 | } |
| 5401 | |
| 5402 | status = psa_key_derivation_output_bytes( &derivation, |
| 5403 | master, |
| 5404 | master_secret_len ); |
| 5405 | if( status != PSA_SUCCESS ) |
| 5406 | { |
| 5407 | psa_key_derivation_abort( &derivation ); |
| 5408 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5409 | } |
| 5410 | |
| 5411 | status = psa_key_derivation_abort( &derivation ); |
| 5412 | if( status != PSA_SUCCESS ) |
| 5413 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5414 | } |
| 5415 | else |
| 5416 | #endif |
| 5417 | { |
| 5418 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5419 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5420 | master, |
| 5421 | master_secret_len ); |
| 5422 | if( ret != 0 ) |
| 5423 | { |
| 5424 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5425 | return( ret ); |
| 5426 | } |
| 5427 | |
| 5428 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5429 | handshake->premaster, |
| 5430 | handshake->pmslen ); |
| 5431 | |
| 5432 | mbedtls_platform_zeroize( handshake->premaster, |
| 5433 | sizeof(handshake->premaster) ); |
| 5434 | } |
| 5435 | |
| 5436 | return( 0 ); |
| 5437 | } |
| 5438 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5439 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5440 | { |
| 5441 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5442 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5443 | ssl->handshake->ciphersuite_info; |
| 5444 | |
| 5445 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5446 | |
| 5447 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5448 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5449 | ciphersuite_info->mac ); |
| 5450 | if( ret != 0 ) |
| 5451 | { |
| 5452 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5453 | return( ret ); |
| 5454 | } |
| 5455 | |
| 5456 | /* Compute master secret if needed */ |
| 5457 | ret = ssl_compute_master( ssl->handshake, |
| 5458 | ssl->session_negotiate->master, |
| 5459 | ssl ); |
| 5460 | if( ret != 0 ) |
| 5461 | { |
| 5462 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5463 | return( ret ); |
| 5464 | } |
| 5465 | |
| 5466 | /* Swap the client and server random values: |
| 5467 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 5468 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 5469 | { |
| 5470 | unsigned char tmp[64]; |
| 5471 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 5472 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 5473 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 5474 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 5475 | } |
| 5476 | |
| 5477 | /* Populate transform structure */ |
| 5478 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 5479 | ssl->session_negotiate->ciphersuite, |
| 5480 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5481 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5482 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5483 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5484 | ssl->handshake->tls_prf, |
| 5485 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5486 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5487 | ssl->conf->endpoint, |
| 5488 | ssl ); |
| 5489 | if( ret != 0 ) |
| 5490 | { |
| 5491 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 5492 | return( ret ); |
| 5493 | } |
| 5494 | |
| 5495 | /* We no longer need Server/ClientHello.random values */ |
| 5496 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 5497 | sizeof( ssl->handshake->randbytes ) ); |
| 5498 | |
| 5499 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 5500 | |
| 5501 | return( 0 ); |
| 5502 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5503 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5504 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 5505 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5506 | switch( md ) |
| 5507 | { |
| 5508 | #if defined(MBEDTLS_SHA384_C) |
| 5509 | case MBEDTLS_SSL_HASH_SHA384: |
| 5510 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5511 | break; |
| 5512 | #endif |
| 5513 | #if defined(MBEDTLS_SHA256_C) |
| 5514 | case MBEDTLS_SSL_HASH_SHA256: |
| 5515 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5516 | break; |
| 5517 | #endif |
| 5518 | default: |
| 5519 | return( -1 ); |
| 5520 | } |
| 5521 | |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5522 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5523 | } |
| 5524 | |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5525 | #if defined(MBEDTLS_SHA256_C) |
| 5526 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 5527 | unsigned char *hash, |
| 5528 | size_t *hlen ) |
| 5529 | { |
| 5530 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5531 | size_t hash_size; |
| 5532 | psa_status_t status; |
| 5533 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 5534 | |
| 5535 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 5536 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 5537 | if( status != PSA_SUCCESS ) |
| 5538 | { |
| 5539 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5540 | return; |
| 5541 | } |
| 5542 | |
| 5543 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 5544 | if( status != PSA_SUCCESS ) |
| 5545 | { |
| 5546 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5547 | return; |
| 5548 | } |
| 5549 | |
| 5550 | *hlen = 32; |
| 5551 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5552 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5553 | #else |
| 5554 | mbedtls_sha256_context sha256; |
| 5555 | |
| 5556 | mbedtls_sha256_init( &sha256 ); |
| 5557 | |
| 5558 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 5559 | |
| 5560 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 5561 | mbedtls_sha256_finish( &sha256, hash ); |
| 5562 | |
| 5563 | *hlen = 32; |
| 5564 | |
| 5565 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5566 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5567 | |
| 5568 | mbedtls_sha256_free( &sha256 ); |
| 5569 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5570 | return; |
| 5571 | } |
| 5572 | #endif /* MBEDTLS_SHA256_C */ |
| 5573 | |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5574 | #if defined(MBEDTLS_SHA384_C) |
| 5575 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 5576 | unsigned char *hash, |
| 5577 | size_t *hlen ) |
| 5578 | { |
| 5579 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5580 | size_t hash_size; |
| 5581 | psa_status_t status; |
| 5582 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 5583 | |
| 5584 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 5585 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 5586 | if( status != PSA_SUCCESS ) |
| 5587 | { |
| 5588 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5589 | return; |
| 5590 | } |
| 5591 | |
| 5592 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 5593 | if( status != PSA_SUCCESS ) |
| 5594 | { |
| 5595 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5596 | return; |
| 5597 | } |
| 5598 | |
| 5599 | *hlen = 48; |
| 5600 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5601 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5602 | #else |
| 5603 | mbedtls_sha512_context sha512; |
| 5604 | |
| 5605 | mbedtls_sha512_init( &sha512 ); |
| 5606 | |
| 5607 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 5608 | |
| 5609 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 5610 | mbedtls_sha512_finish( &sha512, hash ); |
| 5611 | |
| 5612 | *hlen = 48; |
| 5613 | |
| 5614 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5615 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5616 | |
| 5617 | mbedtls_sha512_free( &sha512 ); |
| 5618 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5619 | return; |
| 5620 | } |
| 5621 | #endif /* MBEDTLS_SHA384_C */ |
| 5622 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5623 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5624 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5625 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 5626 | { |
| 5627 | unsigned char *p = ssl->handshake->premaster; |
| 5628 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 5629 | const unsigned char *psk = NULL; |
| 5630 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5631 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5632 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5633 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5634 | { |
| 5635 | /* |
| 5636 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5637 | * checked before calling this function. |
| 5638 | * |
| 5639 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5640 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5641 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5642 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 5643 | { |
| 5644 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5645 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5646 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | /* |
| 5650 | * PMS = struct { |
| 5651 | * opaque other_secret<0..2^16-1>; |
| 5652 | * opaque psk<0..2^16-1>; |
| 5653 | * }; |
| 5654 | * with "other_secret" depending on the particular key exchange |
| 5655 | */ |
| 5656 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 5657 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 5658 | { |
| 5659 | if( end - p < 2 ) |
| 5660 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5661 | |
| 5662 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5663 | p += 2; |
| 5664 | |
| 5665 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5666 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5667 | |
| 5668 | memset( p, 0, psk_len ); |
| 5669 | p += psk_len; |
| 5670 | } |
| 5671 | else |
| 5672 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 5673 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 5674 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 5675 | { |
| 5676 | /* |
| 5677 | * other_secret already set by the ClientKeyExchange message, |
| 5678 | * and is 48 bytes long |
| 5679 | */ |
| 5680 | if( end - p < 2 ) |
| 5681 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5682 | |
| 5683 | *p++ = 0; |
| 5684 | *p++ = 48; |
| 5685 | p += 48; |
| 5686 | } |
| 5687 | else |
| 5688 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 5689 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 5690 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 5691 | { |
| 5692 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5693 | size_t len; |
| 5694 | |
| 5695 | /* Write length only when we know the actual value */ |
| 5696 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 5697 | p + 2, end - ( p + 2 ), &len, |
| 5698 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5699 | { |
| 5700 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 5701 | return( ret ); |
| 5702 | } |
| 5703 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 5704 | p += 2 + len; |
| 5705 | |
| 5706 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 5707 | } |
| 5708 | else |
| 5709 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5710 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5711 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 5712 | { |
| 5713 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5714 | size_t zlen; |
| 5715 | |
| 5716 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 5717 | p + 2, end - ( p + 2 ), |
| 5718 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5719 | { |
| 5720 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 5721 | return( ret ); |
| 5722 | } |
| 5723 | |
| 5724 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 5725 | p += 2 + zlen; |
| 5726 | |
| 5727 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 5728 | MBEDTLS_DEBUG_ECDH_Z ); |
| 5729 | } |
| 5730 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5731 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5732 | { |
| 5733 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5734 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5735 | } |
| 5736 | |
| 5737 | /* opaque psk<0..2^16-1>; */ |
| 5738 | if( end - p < 2 ) |
| 5739 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5740 | |
| 5741 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5742 | p += 2; |
| 5743 | |
| 5744 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5745 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5746 | |
| 5747 | memcpy( p, psk, psk_len ); |
| 5748 | p += psk_len; |
| 5749 | |
| 5750 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 5751 | |
| 5752 | return( 0 ); |
| 5753 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5754 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 5755 | |
| 5756 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5757 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 5758 | |
| 5759 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5760 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 5761 | { |
| 5762 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 5763 | * timeout if we were using the usual handshake doubling scheme */ |
| 5764 | if( ssl->conf->renego_max_records < 0 ) |
| 5765 | { |
| 5766 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 5767 | unsigned char doublings = 1; |
| 5768 | |
| 5769 | while( ratio != 0 ) |
| 5770 | { |
| 5771 | ++doublings; |
| 5772 | ratio >>= 1; |
| 5773 | } |
| 5774 | |
| 5775 | if( ++ssl->renego_records_seen > doublings ) |
| 5776 | { |
| 5777 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 5778 | return( 0 ); |
| 5779 | } |
| 5780 | } |
| 5781 | |
| 5782 | return( ssl_write_hello_request( ssl ) ); |
| 5783 | } |
| 5784 | #endif |
| 5785 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5786 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5787 | /* |
| 5788 | * Handshake functions |
| 5789 | */ |
| 5790 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5791 | /* No certificate support -> dummy functions */ |
| 5792 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 5793 | { |
| 5794 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5795 | ssl->handshake->ciphersuite_info; |
| 5796 | |
| 5797 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 5798 | |
| 5799 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5800 | { |
| 5801 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5802 | ssl->state++; |
| 5803 | return( 0 ); |
| 5804 | } |
| 5805 | |
| 5806 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5807 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5808 | } |
| 5809 | |
| 5810 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 5811 | { |
| 5812 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5813 | ssl->handshake->ciphersuite_info; |
| 5814 | |
| 5815 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 5816 | |
| 5817 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5818 | { |
| 5819 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 5820 | ssl->state++; |
| 5821 | return( 0 ); |
| 5822 | } |
| 5823 | |
| 5824 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5825 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5826 | } |
| 5827 | |
| 5828 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5829 | /* Some certificate support -> implement write and parse */ |
| 5830 | |
| 5831 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 5832 | { |
| 5833 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 5834 | size_t i, n; |
| 5835 | const mbedtls_x509_crt *crt; |
| 5836 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5837 | ssl->handshake->ciphersuite_info; |
| 5838 | |
| 5839 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 5840 | |
| 5841 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5842 | { |
| 5843 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5844 | ssl->state++; |
| 5845 | return( 0 ); |
| 5846 | } |
| 5847 | |
| 5848 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5849 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5850 | { |
| 5851 | if( ssl->handshake->client_auth == 0 ) |
| 5852 | { |
| 5853 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5854 | ssl->state++; |
| 5855 | return( 0 ); |
| 5856 | } |
| 5857 | } |
| 5858 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 5859 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5860 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 5861 | { |
| 5862 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 5863 | { |
| 5864 | /* Should never happen because we shouldn't have picked the |
| 5865 | * ciphersuite if we don't have a certificate. */ |
| 5866 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5867 | } |
| 5868 | } |
| 5869 | #endif |
| 5870 | |
| 5871 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 5872 | |
| 5873 | /* |
| 5874 | * 0 . 0 handshake type |
| 5875 | * 1 . 3 handshake length |
| 5876 | * 4 . 6 length of all certs |
| 5877 | * 7 . 9 length of cert. 1 |
| 5878 | * 10 . n-1 peer certificate |
| 5879 | * n . n+2 length of cert. 2 |
| 5880 | * n+3 . ... upper level cert, etc. |
| 5881 | */ |
| 5882 | i = 7; |
| 5883 | crt = mbedtls_ssl_own_cert( ssl ); |
| 5884 | |
| 5885 | while( crt != NULL ) |
| 5886 | { |
| 5887 | n = crt->raw.len; |
| 5888 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 5889 | { |
| 5890 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 5891 | " > %" MBEDTLS_PRINTF_SIZET, |
| 5892 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 5893 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 5894 | } |
| 5895 | |
| 5896 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 5897 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 5898 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 5899 | |
| 5900 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 5901 | i += n; crt = crt->next; |
| 5902 | } |
| 5903 | |
| 5904 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 5905 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 5906 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 5907 | |
| 5908 | ssl->out_msglen = i; |
| 5909 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5910 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 5911 | |
| 5912 | ssl->state++; |
| 5913 | |
| 5914 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 5915 | { |
| 5916 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 5917 | return( ret ); |
| 5918 | } |
| 5919 | |
| 5920 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 5921 | |
| 5922 | return( ret ); |
| 5923 | } |
| 5924 | |
| 5925 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5926 | |
| 5927 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5928 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 5929 | unsigned char *crt_buf, |
| 5930 | size_t crt_buf_len ) |
| 5931 | { |
| 5932 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 5933 | |
| 5934 | if( peer_crt == NULL ) |
| 5935 | return( -1 ); |
| 5936 | |
| 5937 | if( peer_crt->raw.len != crt_buf_len ) |
| 5938 | return( -1 ); |
| 5939 | |
| 5940 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 5941 | } |
| 5942 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5943 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 5944 | unsigned char *crt_buf, |
| 5945 | size_t crt_buf_len ) |
| 5946 | { |
| 5947 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5948 | unsigned char const * const peer_cert_digest = |
| 5949 | ssl->session->peer_cert_digest; |
| 5950 | mbedtls_md_type_t const peer_cert_digest_type = |
| 5951 | ssl->session->peer_cert_digest_type; |
| 5952 | mbedtls_md_info_t const * const digest_info = |
| 5953 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 5954 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 5955 | size_t digest_len; |
| 5956 | |
| 5957 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 5958 | return( -1 ); |
| 5959 | |
| 5960 | digest_len = mbedtls_md_get_size( digest_info ); |
| 5961 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 5962 | return( -1 ); |
| 5963 | |
| 5964 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 5965 | if( ret != 0 ) |
| 5966 | return( -1 ); |
| 5967 | |
| 5968 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 5969 | } |
| 5970 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5971 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 5972 | |
| 5973 | /* |
| 5974 | * Once the certificate message is read, parse it into a cert chain and |
| 5975 | * perform basic checks, but leave actual verification to the caller |
| 5976 | */ |
| 5977 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 5978 | mbedtls_x509_crt *chain ) |
| 5979 | { |
| 5980 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5981 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5982 | int crt_cnt=0; |
| 5983 | #endif |
| 5984 | size_t i, n; |
| 5985 | uint8_t alert; |
| 5986 | |
| 5987 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 5988 | { |
| 5989 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5990 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5991 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 5992 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5993 | } |
| 5994 | |
| 5995 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 5996 | { |
| 5997 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5998 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 5999 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 6000 | } |
| 6001 | |
| 6002 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 6003 | { |
| 6004 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6005 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6006 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6007 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6008 | } |
| 6009 | |
| 6010 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 6011 | |
| 6012 | /* |
| 6013 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 6014 | */ |
| 6015 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 6016 | |
| 6017 | if( ssl->in_msg[i] != 0 || |
| 6018 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 6019 | { |
| 6020 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6021 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6022 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6023 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6024 | } |
| 6025 | |
| 6026 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 6027 | i += 3; |
| 6028 | |
| 6029 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 6030 | while( i < ssl->in_hslen ) |
| 6031 | { |
| 6032 | /* Check that there's room for the next CRT's length fields. */ |
| 6033 | if ( i + 3 > ssl->in_hslen ) { |
| 6034 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6035 | mbedtls_ssl_send_alert_message( ssl, |
| 6036 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6037 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6038 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6039 | } |
| 6040 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 6041 | * anything beyond 2**16 ~ 64K. */ |
| 6042 | if( ssl->in_msg[i] != 0 ) |
| 6043 | { |
| 6044 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6045 | mbedtls_ssl_send_alert_message( ssl, |
| 6046 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6047 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 6048 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6049 | } |
| 6050 | |
| 6051 | /* Read length of the next CRT in the chain. */ |
| 6052 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 6053 | | (unsigned int) ssl->in_msg[i + 2]; |
| 6054 | i += 3; |
| 6055 | |
| 6056 | if( n < 128 || i + n > ssl->in_hslen ) |
| 6057 | { |
| 6058 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6059 | mbedtls_ssl_send_alert_message( ssl, |
| 6060 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6061 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6062 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6063 | } |
| 6064 | |
| 6065 | /* Check if we're handling the first CRT in the chain. */ |
| 6066 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6067 | if( crt_cnt++ == 0 && |
| 6068 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6069 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6070 | { |
| 6071 | /* During client-side renegotiation, check that the server's |
| 6072 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6073 | * mitigating the triple handshake attack. On success, reuse |
| 6074 | * the original end-CRT instead of parsing it again. */ |
| 6075 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6076 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6077 | &ssl->in_msg[i], |
| 6078 | n ) != 0 ) |
| 6079 | { |
| 6080 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6081 | mbedtls_ssl_send_alert_message( ssl, |
| 6082 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6083 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6084 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6085 | } |
| 6086 | |
| 6087 | /* Now we can safely free the original chain. */ |
| 6088 | ssl_clear_peer_cert( ssl->session ); |
| 6089 | } |
| 6090 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6091 | |
| 6092 | /* Parse the next certificate in the chain. */ |
| 6093 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6094 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6095 | #else |
| 6096 | /* If we don't need to store the CRT chain permanently, parse |
| 6097 | * it in-place from the input buffer instead of making a copy. */ |
| 6098 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6099 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6100 | switch( ret ) |
| 6101 | { |
| 6102 | case 0: /*ok*/ |
| 6103 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6104 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6105 | prior certificate was already trusted. */ |
| 6106 | break; |
| 6107 | |
| 6108 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6109 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6110 | goto crt_parse_der_failed; |
| 6111 | |
| 6112 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6113 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6114 | goto crt_parse_der_failed; |
| 6115 | |
| 6116 | default: |
| 6117 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6118 | crt_parse_der_failed: |
| 6119 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6120 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6121 | return( ret ); |
| 6122 | } |
| 6123 | |
| 6124 | i += n; |
| 6125 | } |
| 6126 | |
| 6127 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6128 | return( 0 ); |
| 6129 | } |
| 6130 | |
| 6131 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6132 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6133 | { |
| 6134 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6135 | return( -1 ); |
| 6136 | |
| 6137 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6138 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6139 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6140 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6141 | { |
Ronald Cron | 1938588 | 2022-06-15 16:26:13 +0200 | [diff] [blame^] | 6142 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) ); |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 6143 | return( 0 ); |
| 6144 | } |
| 6145 | return( -1 ); |
| 6146 | } |
| 6147 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6148 | |
| 6149 | /* Check if a certificate message is expected. |
| 6150 | * Return either |
| 6151 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6152 | * - SSL_CERTIFICATE_SKIP |
| 6153 | * indicating whether a Certificate message is expected or not. |
| 6154 | */ |
| 6155 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6156 | #define SSL_CERTIFICATE_SKIP 1 |
| 6157 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6158 | int authmode ) |
| 6159 | { |
| 6160 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6161 | ssl->handshake->ciphersuite_info; |
| 6162 | |
| 6163 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6164 | return( SSL_CERTIFICATE_SKIP ); |
| 6165 | |
| 6166 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6167 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6168 | { |
| 6169 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6170 | return( SSL_CERTIFICATE_SKIP ); |
| 6171 | |
| 6172 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6173 | { |
| 6174 | ssl->session_negotiate->verify_result = |
| 6175 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6176 | return( SSL_CERTIFICATE_SKIP ); |
| 6177 | } |
| 6178 | } |
| 6179 | #else |
| 6180 | ((void) authmode); |
| 6181 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6182 | |
| 6183 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6184 | } |
| 6185 | |
| 6186 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6187 | int authmode, |
| 6188 | mbedtls_x509_crt *chain, |
| 6189 | void *rs_ctx ) |
| 6190 | { |
| 6191 | int ret = 0; |
| 6192 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6193 | ssl->handshake->ciphersuite_info; |
| 6194 | int have_ca_chain = 0; |
| 6195 | |
| 6196 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6197 | void *p_vrfy; |
| 6198 | |
| 6199 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6200 | return( 0 ); |
| 6201 | |
| 6202 | if( ssl->f_vrfy != NULL ) |
| 6203 | { |
| 6204 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6205 | f_vrfy = ssl->f_vrfy; |
| 6206 | p_vrfy = ssl->p_vrfy; |
| 6207 | } |
| 6208 | else |
| 6209 | { |
| 6210 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6211 | f_vrfy = ssl->conf->f_vrfy; |
| 6212 | p_vrfy = ssl->conf->p_vrfy; |
| 6213 | } |
| 6214 | |
| 6215 | /* |
| 6216 | * Main check: verify certificate |
| 6217 | */ |
| 6218 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6219 | if( ssl->conf->f_ca_cb != NULL ) |
| 6220 | { |
| 6221 | ((void) rs_ctx); |
| 6222 | have_ca_chain = 1; |
| 6223 | |
| 6224 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6225 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6226 | chain, |
| 6227 | ssl->conf->f_ca_cb, |
| 6228 | ssl->conf->p_ca_cb, |
| 6229 | ssl->conf->cert_profile, |
| 6230 | ssl->hostname, |
| 6231 | &ssl->session_negotiate->verify_result, |
| 6232 | f_vrfy, p_vrfy ); |
| 6233 | } |
| 6234 | else |
| 6235 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6236 | { |
| 6237 | mbedtls_x509_crt *ca_chain; |
| 6238 | mbedtls_x509_crl *ca_crl; |
| 6239 | |
| 6240 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6241 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6242 | { |
| 6243 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6244 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6245 | } |
| 6246 | else |
| 6247 | #endif |
| 6248 | { |
| 6249 | ca_chain = ssl->conf->ca_chain; |
| 6250 | ca_crl = ssl->conf->ca_crl; |
| 6251 | } |
| 6252 | |
| 6253 | if( ca_chain != NULL ) |
| 6254 | have_ca_chain = 1; |
| 6255 | |
| 6256 | ret = mbedtls_x509_crt_verify_restartable( |
| 6257 | chain, |
| 6258 | ca_chain, ca_crl, |
| 6259 | ssl->conf->cert_profile, |
| 6260 | ssl->hostname, |
| 6261 | &ssl->session_negotiate->verify_result, |
| 6262 | f_vrfy, p_vrfy, rs_ctx ); |
| 6263 | } |
| 6264 | |
| 6265 | if( ret != 0 ) |
| 6266 | { |
| 6267 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6268 | } |
| 6269 | |
| 6270 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6271 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6272 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6273 | #endif |
| 6274 | |
| 6275 | /* |
| 6276 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6277 | */ |
| 6278 | |
| 6279 | #if defined(MBEDTLS_ECP_C) |
| 6280 | { |
| 6281 | const mbedtls_pk_context *pk = &chain->pk; |
| 6282 | |
| 6283 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 6284 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 6285 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 6286 | { |
| 6287 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6288 | |
| 6289 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6290 | if( ret == 0 ) |
| 6291 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6292 | } |
| 6293 | } |
| 6294 | #endif /* MBEDTLS_ECP_C */ |
| 6295 | |
| 6296 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6297 | ciphersuite_info, |
| 6298 | ! ssl->conf->endpoint, |
| 6299 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6300 | { |
| 6301 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6302 | if( ret == 0 ) |
| 6303 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6304 | } |
| 6305 | |
| 6306 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6307 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6308 | * with details encoded in the verification flags. All other kinds |
| 6309 | * of error codes, including those from the user provided f_vrfy |
| 6310 | * functions, are treated as fatal and lead to a failure of |
| 6311 | * ssl_parse_certificate even if verification was optional. */ |
| 6312 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6313 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6314 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6315 | { |
| 6316 | ret = 0; |
| 6317 | } |
| 6318 | |
| 6319 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6320 | { |
| 6321 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6322 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6323 | } |
| 6324 | |
| 6325 | if( ret != 0 ) |
| 6326 | { |
| 6327 | uint8_t alert; |
| 6328 | |
| 6329 | /* The certificate may have been rejected for several reasons. |
| 6330 | Pick one and send the corresponding alert. Which alert to send |
| 6331 | may be a subject of debate in some cases. */ |
| 6332 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6333 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6334 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6335 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6336 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6337 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6338 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6339 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6340 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6341 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6342 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6343 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6344 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6345 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6346 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6347 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6348 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6349 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6350 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6351 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6352 | else |
| 6353 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6354 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6355 | alert ); |
| 6356 | } |
| 6357 | |
| 6358 | #if defined(MBEDTLS_DEBUG_C) |
| 6359 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6360 | { |
| 6361 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6362 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6363 | } |
| 6364 | else |
| 6365 | { |
| 6366 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6367 | } |
| 6368 | #endif /* MBEDTLS_DEBUG_C */ |
| 6369 | |
| 6370 | return( ret ); |
| 6371 | } |
| 6372 | |
| 6373 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6374 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6375 | unsigned char *start, size_t len ) |
| 6376 | { |
| 6377 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6378 | /* Remember digest of the peer's end-CRT. */ |
| 6379 | ssl->session_negotiate->peer_cert_digest = |
| 6380 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6381 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6382 | { |
| 6383 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6384 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6385 | mbedtls_ssl_send_alert_message( ssl, |
| 6386 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6387 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6388 | |
| 6389 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6390 | } |
| 6391 | |
| 6392 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6393 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6394 | start, len, |
| 6395 | ssl->session_negotiate->peer_cert_digest ); |
| 6396 | |
| 6397 | ssl->session_negotiate->peer_cert_digest_type = |
| 6398 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6399 | ssl->session_negotiate->peer_cert_digest_len = |
| 6400 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6401 | |
| 6402 | return( ret ); |
| 6403 | } |
| 6404 | |
| 6405 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6406 | unsigned char *start, size_t len ) |
| 6407 | { |
| 6408 | unsigned char *end = start + len; |
| 6409 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6410 | |
| 6411 | /* Make a copy of the peer's raw public key. */ |
| 6412 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6413 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6414 | &ssl->handshake->peer_pubkey ); |
| 6415 | if( ret != 0 ) |
| 6416 | { |
| 6417 | /* We should have parsed the public key before. */ |
| 6418 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6419 | } |
| 6420 | |
| 6421 | return( 0 ); |
| 6422 | } |
| 6423 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6424 | |
| 6425 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6426 | { |
| 6427 | int ret = 0; |
| 6428 | int crt_expected; |
| 6429 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6430 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6431 | ? ssl->handshake->sni_authmode |
| 6432 | : ssl->conf->authmode; |
| 6433 | #else |
| 6434 | const int authmode = ssl->conf->authmode; |
| 6435 | #endif |
| 6436 | void *rs_ctx = NULL; |
| 6437 | mbedtls_x509_crt *chain = NULL; |
| 6438 | |
| 6439 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6440 | |
| 6441 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6442 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 6443 | { |
| 6444 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6445 | goto exit; |
| 6446 | } |
| 6447 | |
| 6448 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6449 | if( ssl->handshake->ecrs_enabled && |
| 6450 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 6451 | { |
| 6452 | chain = ssl->handshake->ecrs_peer_cert; |
| 6453 | ssl->handshake->ecrs_peer_cert = NULL; |
| 6454 | goto crt_verify; |
| 6455 | } |
| 6456 | #endif |
| 6457 | |
| 6458 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6459 | { |
| 6460 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6461 | let it decide whether to alert. */ |
| 6462 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6463 | goto exit; |
| 6464 | } |
| 6465 | |
| 6466 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6467 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6468 | { |
| 6469 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 6470 | |
| 6471 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6472 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 6473 | |
| 6474 | goto exit; |
| 6475 | } |
| 6476 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6477 | |
| 6478 | /* Clear existing peer CRT structure in case we tried to |
| 6479 | * reuse a session but it failed, and allocate a new one. */ |
| 6480 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 6481 | |
| 6482 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6483 | if( chain == NULL ) |
| 6484 | { |
| 6485 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 6486 | sizeof( mbedtls_x509_crt ) ) ); |
| 6487 | mbedtls_ssl_send_alert_message( ssl, |
| 6488 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6489 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6490 | |
| 6491 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6492 | goto exit; |
| 6493 | } |
| 6494 | mbedtls_x509_crt_init( chain ); |
| 6495 | |
| 6496 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 6497 | if( ret != 0 ) |
| 6498 | goto exit; |
| 6499 | |
| 6500 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6501 | if( ssl->handshake->ecrs_enabled) |
| 6502 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 6503 | |
| 6504 | crt_verify: |
| 6505 | if( ssl->handshake->ecrs_enabled) |
| 6506 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6507 | #endif |
| 6508 | |
| 6509 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 6510 | chain, rs_ctx ); |
| 6511 | if( ret != 0 ) |
| 6512 | goto exit; |
| 6513 | |
| 6514 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6515 | { |
| 6516 | unsigned char *crt_start, *pk_start; |
| 6517 | size_t crt_len, pk_len; |
| 6518 | |
| 6519 | /* We parse the CRT chain without copying, so |
| 6520 | * these pointers point into the input buffer, |
| 6521 | * and are hence still valid after freeing the |
| 6522 | * CRT chain. */ |
| 6523 | |
| 6524 | crt_start = chain->raw.p; |
| 6525 | crt_len = chain->raw.len; |
| 6526 | |
| 6527 | pk_start = chain->pk_raw.p; |
| 6528 | pk_len = chain->pk_raw.len; |
| 6529 | |
| 6530 | /* Free the CRT structures before computing |
| 6531 | * digest and copying the peer's public key. */ |
| 6532 | mbedtls_x509_crt_free( chain ); |
| 6533 | mbedtls_free( chain ); |
| 6534 | chain = NULL; |
| 6535 | |
| 6536 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 6537 | if( ret != 0 ) |
| 6538 | goto exit; |
| 6539 | |
| 6540 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 6541 | if( ret != 0 ) |
| 6542 | goto exit; |
| 6543 | } |
| 6544 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6545 | /* Pass ownership to session structure. */ |
| 6546 | ssl->session_negotiate->peer_cert = chain; |
| 6547 | chain = NULL; |
| 6548 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6549 | |
| 6550 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 6551 | |
| 6552 | exit: |
| 6553 | |
| 6554 | if( ret == 0 ) |
| 6555 | ssl->state++; |
| 6556 | |
| 6557 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6558 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 6559 | { |
| 6560 | ssl->handshake->ecrs_peer_cert = chain; |
| 6561 | chain = NULL; |
| 6562 | } |
| 6563 | #endif |
| 6564 | |
| 6565 | if( chain != NULL ) |
| 6566 | { |
| 6567 | mbedtls_x509_crt_free( chain ); |
| 6568 | mbedtls_free( chain ); |
| 6569 | } |
| 6570 | |
| 6571 | return( ret ); |
| 6572 | } |
| 6573 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6574 | |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6575 | #if defined(MBEDTLS_SHA256_C) |
| 6576 | static void ssl_calc_finished_tls_sha256( |
| 6577 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6578 | { |
| 6579 | int len = 12; |
| 6580 | const char *sender; |
| 6581 | unsigned char padbuf[32]; |
| 6582 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6583 | size_t hash_size; |
| 6584 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 6585 | psa_status_t status; |
| 6586 | #else |
| 6587 | mbedtls_sha256_context sha256; |
| 6588 | #endif |
| 6589 | |
| 6590 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6591 | if( !session ) |
| 6592 | session = ssl->session; |
| 6593 | |
| 6594 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6595 | ? "client finished" |
| 6596 | : "server finished"; |
| 6597 | |
| 6598 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6599 | sha256_psa = psa_hash_operation_init(); |
| 6600 | |
| 6601 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 6602 | |
| 6603 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6604 | if( status != PSA_SUCCESS ) |
| 6605 | { |
| 6606 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6607 | return; |
| 6608 | } |
| 6609 | |
| 6610 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6611 | if( status != PSA_SUCCESS ) |
| 6612 | { |
| 6613 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6614 | return; |
| 6615 | } |
| 6616 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 6617 | #else |
| 6618 | |
| 6619 | mbedtls_sha256_init( &sha256 ); |
| 6620 | |
| 6621 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 6622 | |
| 6623 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6624 | |
| 6625 | /* |
| 6626 | * TLSv1.2: |
| 6627 | * hash = PRF( master, finished_label, |
| 6628 | * Hash( handshake ) )[0.11] |
| 6629 | */ |
| 6630 | |
| 6631 | #if !defined(MBEDTLS_SHA256_ALT) |
| 6632 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 6633 | sha256.state, sizeof( sha256.state ) ); |
| 6634 | #endif |
| 6635 | |
| 6636 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 6637 | mbedtls_sha256_free( &sha256 ); |
| 6638 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6639 | |
| 6640 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6641 | padbuf, 32, buf, len ); |
| 6642 | |
| 6643 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6644 | |
| 6645 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6646 | |
| 6647 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6648 | } |
| 6649 | #endif /* MBEDTLS_SHA256_C */ |
| 6650 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6651 | |
| 6652 | #if defined(MBEDTLS_SHA384_C) |
| 6653 | |
| 6654 | static void ssl_calc_finished_tls_sha384( |
| 6655 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6656 | { |
| 6657 | int len = 12; |
| 6658 | const char *sender; |
| 6659 | unsigned char padbuf[48]; |
| 6660 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6661 | size_t hash_size; |
| 6662 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 6663 | psa_status_t status; |
| 6664 | #else |
| 6665 | mbedtls_sha512_context sha512; |
| 6666 | #endif |
| 6667 | |
| 6668 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6669 | if( !session ) |
| 6670 | session = ssl->session; |
| 6671 | |
| 6672 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6673 | ? "client finished" |
| 6674 | : "server finished"; |
| 6675 | |
| 6676 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6677 | sha384_psa = psa_hash_operation_init(); |
| 6678 | |
| 6679 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 6680 | |
| 6681 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6682 | if( status != PSA_SUCCESS ) |
| 6683 | { |
| 6684 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6685 | return; |
| 6686 | } |
| 6687 | |
| 6688 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6689 | if( status != PSA_SUCCESS ) |
| 6690 | { |
| 6691 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6692 | return; |
| 6693 | } |
| 6694 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 6695 | #else |
| 6696 | mbedtls_sha512_init( &sha512 ); |
| 6697 | |
| 6698 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 6699 | |
| 6700 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 6701 | |
| 6702 | /* |
| 6703 | * TLSv1.2: |
| 6704 | * hash = PRF( master, finished_label, |
| 6705 | * Hash( handshake ) )[0.11] |
| 6706 | */ |
| 6707 | |
| 6708 | #if !defined(MBEDTLS_SHA512_ALT) |
| 6709 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 6710 | sha512.state, sizeof( sha512.state ) ); |
| 6711 | #endif |
| 6712 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 6713 | |
| 6714 | mbedtls_sha512_free( &sha512 ); |
| 6715 | #endif |
| 6716 | |
| 6717 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6718 | padbuf, 48, buf, len ); |
| 6719 | |
| 6720 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6721 | |
| 6722 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6723 | |
| 6724 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6725 | } |
| 6726 | #endif /* MBEDTLS_SHA384_C */ |
| 6727 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 6728 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 6729 | { |
| 6730 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 6731 | |
| 6732 | /* |
| 6733 | * Free our handshake params |
| 6734 | */ |
| 6735 | mbedtls_ssl_handshake_free( ssl ); |
| 6736 | mbedtls_free( ssl->handshake ); |
| 6737 | ssl->handshake = NULL; |
| 6738 | |
| 6739 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6740 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 6741 | */ |
| 6742 | if( ssl->transform ) |
| 6743 | { |
| 6744 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6745 | mbedtls_free( ssl->transform ); |
| 6746 | } |
| 6747 | ssl->transform = ssl->transform_negotiate; |
| 6748 | ssl->transform_negotiate = NULL; |
| 6749 | |
| 6750 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 6751 | } |
| 6752 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 6753 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 6754 | { |
| 6755 | int resume = ssl->handshake->resume; |
| 6756 | |
| 6757 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 6758 | |
| 6759 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6760 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6761 | { |
| 6762 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 6763 | ssl->renego_records_seen = 0; |
| 6764 | } |
| 6765 | #endif |
| 6766 | |
| 6767 | /* |
| 6768 | * Free the previous session and switch in the current one |
| 6769 | */ |
| 6770 | if( ssl->session ) |
| 6771 | { |
| 6772 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6773 | /* RFC 7366 3.1: keep the EtM state */ |
| 6774 | ssl->session_negotiate->encrypt_then_mac = |
| 6775 | ssl->session->encrypt_then_mac; |
| 6776 | #endif |
| 6777 | |
| 6778 | mbedtls_ssl_session_free( ssl->session ); |
| 6779 | mbedtls_free( ssl->session ); |
| 6780 | } |
| 6781 | ssl->session = ssl->session_negotiate; |
| 6782 | ssl->session_negotiate = NULL; |
| 6783 | |
| 6784 | /* |
| 6785 | * Add cache entry |
| 6786 | */ |
| 6787 | if( ssl->conf->f_set_cache != NULL && |
| 6788 | ssl->session->id_len != 0 && |
| 6789 | resume == 0 ) |
| 6790 | { |
| 6791 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 6792 | ssl->session->id, |
| 6793 | ssl->session->id_len, |
| 6794 | ssl->session ) != 0 ) |
| 6795 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 6796 | } |
| 6797 | |
| 6798 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6799 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6800 | ssl->handshake->flight != NULL ) |
| 6801 | { |
| 6802 | /* Cancel handshake timer */ |
| 6803 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 6804 | |
| 6805 | /* Keep last flight around in case we need to resend it: |
| 6806 | * we need the handshake and transform structures for that */ |
| 6807 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 6808 | } |
| 6809 | else |
| 6810 | #endif |
| 6811 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6812 | |
| 6813 | ssl->state++; |
| 6814 | |
| 6815 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 6816 | } |
| 6817 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 6818 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 6819 | { |
| 6820 | int ret, hash_len; |
| 6821 | |
| 6822 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 6823 | |
| 6824 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 6825 | |
| 6826 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 6827 | |
| 6828 | /* |
| 6829 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 6830 | * may define some other value. Currently (early 2016), no defined |
| 6831 | * ciphersuite does this (and this is unlikely to change as activity has |
| 6832 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 6833 | */ |
| 6834 | hash_len = 12; |
| 6835 | |
| 6836 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6837 | ssl->verify_data_len = hash_len; |
| 6838 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 6839 | #endif |
| 6840 | |
| 6841 | ssl->out_msglen = 4 + hash_len; |
| 6842 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6843 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 6844 | |
| 6845 | /* |
| 6846 | * In case of session resuming, invert the client and server |
| 6847 | * ChangeCipherSpec messages order. |
| 6848 | */ |
| 6849 | if( ssl->handshake->resume != 0 ) |
| 6850 | { |
| 6851 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6852 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6853 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 6854 | #endif |
| 6855 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6856 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6857 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 6858 | #endif |
| 6859 | } |
| 6860 | else |
| 6861 | ssl->state++; |
| 6862 | |
| 6863 | /* |
| 6864 | * Switch to our negotiated transform and session parameters for outbound |
| 6865 | * data. |
| 6866 | */ |
| 6867 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 6868 | |
| 6869 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6870 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6871 | { |
| 6872 | unsigned char i; |
| 6873 | |
| 6874 | /* Remember current epoch settings for resending */ |
| 6875 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 6876 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 6877 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 6878 | |
| 6879 | /* Set sequence_number to zero */ |
| 6880 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 6881 | |
| 6882 | |
| 6883 | /* Increment epoch */ |
| 6884 | for( i = 2; i > 0; i-- ) |
| 6885 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 6886 | break; |
| 6887 | |
| 6888 | /* The loop goes to its end iff the counter is wrapping */ |
| 6889 | if( i == 0 ) |
| 6890 | { |
| 6891 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 6892 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 6893 | } |
| 6894 | } |
| 6895 | else |
| 6896 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6897 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 6898 | |
| 6899 | ssl->transform_out = ssl->transform_negotiate; |
| 6900 | ssl->session_out = ssl->session_negotiate; |
| 6901 | |
| 6902 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6903 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6904 | mbedtls_ssl_send_flight_completed( ssl ); |
| 6905 | #endif |
| 6906 | |
| 6907 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6908 | { |
| 6909 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6910 | return( ret ); |
| 6911 | } |
| 6912 | |
| 6913 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6914 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6915 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 6916 | { |
| 6917 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 6918 | return( ret ); |
| 6919 | } |
| 6920 | #endif |
| 6921 | |
| 6922 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 6923 | |
| 6924 | return( 0 ); |
| 6925 | } |
| 6926 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 6927 | #define SSL_MAX_HASH_LEN 12 |
| 6928 | |
| 6929 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 6930 | { |
| 6931 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6932 | unsigned int hash_len = 12; |
| 6933 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 6934 | |
| 6935 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 6936 | |
| 6937 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 6938 | |
| 6939 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6940 | { |
| 6941 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6942 | goto exit; |
| 6943 | } |
| 6944 | |
| 6945 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6946 | { |
| 6947 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6948 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6949 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6950 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 6951 | goto exit; |
| 6952 | } |
| 6953 | |
| 6954 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 6955 | { |
| 6956 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6957 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6958 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 6959 | goto exit; |
| 6960 | } |
| 6961 | |
| 6962 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 6963 | { |
| 6964 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6965 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6966 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6967 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 6968 | goto exit; |
| 6969 | } |
| 6970 | |
| 6971 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 6972 | buf, hash_len ) != 0 ) |
| 6973 | { |
| 6974 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6975 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6976 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 6977 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 6978 | goto exit; |
| 6979 | } |
| 6980 | |
| 6981 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6982 | ssl->verify_data_len = hash_len; |
| 6983 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 6984 | #endif |
| 6985 | |
| 6986 | if( ssl->handshake->resume != 0 ) |
| 6987 | { |
| 6988 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6989 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6990 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 6991 | #endif |
| 6992 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6993 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6994 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 6995 | #endif |
| 6996 | } |
| 6997 | else |
| 6998 | ssl->state++; |
| 6999 | |
| 7000 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7001 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 7002 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 7003 | #endif |
| 7004 | |
| 7005 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 7006 | |
| 7007 | exit: |
| 7008 | mbedtls_platform_zeroize( buf, hash_len ); |
| 7009 | return( ret ); |
| 7010 | } |
| 7011 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 7012 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7013 | /* |
| 7014 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 7015 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 7016 | */ |
| 7017 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 7018 | { |
| 7019 | #if defined(MBEDTLS_SHA384_C) |
| 7020 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 7021 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 7022 | |
| 7023 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 7024 | return( tls_prf_sha384 ); |
| 7025 | #else |
| 7026 | (void) ciphersuite_id; |
| 7027 | #endif |
| 7028 | return( tls_prf_sha256 ); |
| 7029 | } |
| 7030 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 7031 | |
| 7032 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 7033 | { |
| 7034 | ((void) tls_prf); |
| 7035 | #if defined(MBEDTLS_SHA384_C) |
| 7036 | if( tls_prf == tls_prf_sha384 ) |
| 7037 | { |
| 7038 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 7039 | } |
| 7040 | else |
| 7041 | #endif |
| 7042 | #if defined(MBEDTLS_SHA256_C) |
| 7043 | if( tls_prf == tls_prf_sha256 ) |
| 7044 | { |
| 7045 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 7046 | } |
| 7047 | else |
| 7048 | #endif |
| 7049 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 7050 | } |
| 7051 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7052 | /* |
| 7053 | * Populate a transform structure with session keys and all the other |
| 7054 | * necessary information. |
| 7055 | * |
| 7056 | * Parameters: |
| 7057 | * - [in/out]: transform: structure to populate |
| 7058 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7059 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7060 | * - [in] ciphersuite |
| 7061 | * - [in] master |
| 7062 | * - [in] encrypt_then_mac |
| 7063 | * - [in] compression |
| 7064 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7065 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7066 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7067 | * - [in] endpoint: client or server |
| 7068 | * - [in] ssl: used for: |
| 7069 | * - ssl->conf->{f,p}_export_keys |
| 7070 | * [in] optionally used for: |
| 7071 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7072 | */ |
| 7073 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7074 | int ciphersuite, |
| 7075 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7076 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7077 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7078 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7079 | ssl_tls_prf_t tls_prf, |
| 7080 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7081 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7082 | unsigned endpoint, |
| 7083 | const mbedtls_ssl_context *ssl ) |
| 7084 | { |
| 7085 | int ret = 0; |
| 7086 | unsigned char keyblk[256]; |
| 7087 | unsigned char *key1; |
| 7088 | unsigned char *key2; |
| 7089 | unsigned char *mac_enc; |
| 7090 | unsigned char *mac_dec; |
| 7091 | size_t mac_key_len = 0; |
| 7092 | size_t iv_copy_len; |
| 7093 | size_t keylen; |
| 7094 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7095 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7096 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7097 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7098 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7099 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7100 | |
| 7101 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7102 | psa_key_type_t key_type; |
| 7103 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7104 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7105 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7106 | size_t key_bits; |
| 7107 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7108 | #endif |
| 7109 | |
| 7110 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7111 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7112 | if( ssl->f_export_keys == NULL ) |
| 7113 | { |
| 7114 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7115 | (void) ssl; |
| 7116 | } |
| 7117 | #endif |
| 7118 | |
| 7119 | /* |
| 7120 | * Some data just needs copying into the structure |
| 7121 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7122 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7123 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7124 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7125 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7126 | |
| 7127 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7128 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7129 | #endif |
| 7130 | |
| 7131 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7132 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7133 | { |
| 7134 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7135 | * generation separate. This should never happen. */ |
| 7136 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7137 | } |
| 7138 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7139 | |
| 7140 | /* |
| 7141 | * Get various info structures |
| 7142 | */ |
| 7143 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7144 | if( ciphersuite_info == NULL ) |
| 7145 | { |
| 7146 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7147 | ciphersuite ) ); |
| 7148 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7149 | } |
| 7150 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7151 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7152 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7153 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7154 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7155 | ciphersuite_info ); |
| 7156 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7157 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7158 | transform->taglen = |
| 7159 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7160 | |
| 7161 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7162 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7163 | transform->taglen, |
| 7164 | &alg, |
| 7165 | &key_type, |
| 7166 | &key_bits ) ) != PSA_SUCCESS ) |
| 7167 | { |
| 7168 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7169 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7170 | goto end; |
| 7171 | } |
| 7172 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7173 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7174 | if( cipher_info == NULL ) |
| 7175 | { |
| 7176 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7177 | ciphersuite_info->cipher ) ); |
| 7178 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7179 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7180 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7181 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7182 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7183 | mac_alg = mbedtls_psa_translate_md( ciphersuite_info->mac ); |
| 7184 | if( mac_alg == 0 ) |
| 7185 | { |
| 7186 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_psa_translate_md for %u not found", |
| 7187 | (unsigned) ciphersuite_info->mac ) ); |
| 7188 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7189 | } |
| 7190 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7191 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7192 | if( md_info == NULL ) |
| 7193 | { |
| 7194 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7195 | (unsigned) ciphersuite_info->mac ) ); |
| 7196 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7197 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7198 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7199 | |
| 7200 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7201 | /* Copy own and peer's CID if the use of the CID |
| 7202 | * extension has been negotiated. */ |
| 7203 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7204 | { |
| 7205 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7206 | |
| 7207 | transform->in_cid_len = ssl->own_cid_len; |
| 7208 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7209 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7210 | transform->in_cid_len ); |
| 7211 | |
| 7212 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7213 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7214 | ssl->handshake->peer_cid_len ); |
| 7215 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7216 | transform->out_cid_len ); |
| 7217 | } |
| 7218 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7219 | |
| 7220 | /* |
| 7221 | * Compute key block using the PRF |
| 7222 | */ |
| 7223 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7224 | if( ret != 0 ) |
| 7225 | { |
| 7226 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7227 | return( ret ); |
| 7228 | } |
| 7229 | |
| 7230 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7231 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7232 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7233 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7234 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7235 | |
| 7236 | /* |
| 7237 | * Determine the appropriate key, IV and MAC length. |
| 7238 | */ |
| 7239 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7240 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7241 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7242 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7243 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7244 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7245 | |
| 7246 | #if defined(MBEDTLS_GCM_C) || \ |
| 7247 | defined(MBEDTLS_CCM_C) || \ |
| 7248 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7249 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7250 | { |
| 7251 | size_t explicit_ivlen; |
| 7252 | |
| 7253 | transform->maclen = 0; |
| 7254 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7255 | |
| 7256 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7257 | * with mode and version: |
| 7258 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7259 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7260 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7261 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7262 | * sequence number). |
| 7263 | */ |
| 7264 | transform->ivlen = 12; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7265 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7266 | if( key_type == PSA_KEY_TYPE_CHACHA20 ) |
| 7267 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7268 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7269 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7270 | transform->fixed_ivlen = 12; |
| 7271 | else |
| 7272 | transform->fixed_ivlen = 4; |
| 7273 | |
| 7274 | /* Minimum length of encrypted record */ |
| 7275 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7276 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7277 | } |
| 7278 | else |
| 7279 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7280 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7281 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 7282 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 7283 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7284 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7285 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7286 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7287 | #else |
| 7288 | size_t block_size = cipher_info->block_size; |
| 7289 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7290 | |
| 7291 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7292 | /* Get MAC length */ |
| 7293 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 7294 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7295 | /* Initialize HMAC contexts */ |
| 7296 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7297 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7298 | { |
| 7299 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7300 | goto end; |
| 7301 | } |
| 7302 | |
| 7303 | /* Get MAC length */ |
| 7304 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7305 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7306 | transform->maclen = mac_key_len; |
| 7307 | |
| 7308 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7309 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7310 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7311 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7312 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7313 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7314 | |
| 7315 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7316 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7317 | transform->minlen = transform->maclen; |
| 7318 | else |
| 7319 | { |
| 7320 | /* |
| 7321 | * GenericBlockCipher: |
| 7322 | * 1. if EtM is in use: one block plus MAC |
| 7323 | * otherwise: * first multiple of blocklen greater than maclen |
| 7324 | * 2. IV |
| 7325 | */ |
| 7326 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7327 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7328 | { |
| 7329 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7330 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7331 | } |
| 7332 | else |
| 7333 | #endif |
| 7334 | { |
| 7335 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7336 | + block_size |
| 7337 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7338 | } |
| 7339 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7340 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7341 | { |
| 7342 | transform->minlen += transform->ivlen; |
| 7343 | } |
| 7344 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7345 | { |
| 7346 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7347 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7348 | goto end; |
| 7349 | } |
| 7350 | } |
| 7351 | } |
| 7352 | else |
| 7353 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7354 | { |
| 7355 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7356 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7357 | } |
| 7358 | |
| 7359 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7360 | (unsigned) keylen, |
| 7361 | (unsigned) transform->minlen, |
| 7362 | (unsigned) transform->ivlen, |
| 7363 | (unsigned) transform->maclen ) ); |
| 7364 | |
| 7365 | /* |
| 7366 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7367 | */ |
| 7368 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7369 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7370 | { |
| 7371 | key1 = keyblk + mac_key_len * 2; |
| 7372 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7373 | |
| 7374 | mac_enc = keyblk; |
| 7375 | mac_dec = keyblk + mac_key_len; |
| 7376 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7377 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7378 | transform->fixed_ivlen : transform->ivlen; |
| 7379 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7380 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7381 | iv_copy_len ); |
| 7382 | } |
| 7383 | else |
| 7384 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7385 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7386 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7387 | { |
| 7388 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7389 | key2 = keyblk + mac_key_len * 2; |
| 7390 | |
| 7391 | mac_enc = keyblk + mac_key_len; |
| 7392 | mac_dec = keyblk; |
| 7393 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7394 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7395 | transform->fixed_ivlen : transform->ivlen; |
| 7396 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7397 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7398 | iv_copy_len ); |
| 7399 | } |
| 7400 | else |
| 7401 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7402 | { |
| 7403 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7404 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7405 | goto end; |
| 7406 | } |
| 7407 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7408 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7409 | { |
| 7410 | ssl->f_export_keys( ssl->p_export_keys, |
| 7411 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7412 | master, 48, |
| 7413 | randbytes + 32, |
| 7414 | randbytes, |
| 7415 | tls_prf_get_type( tls_prf ) ); |
| 7416 | } |
| 7417 | |
| 7418 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7419 | transform->psa_alg = alg; |
| 7420 | |
| 7421 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7422 | { |
| 7423 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7424 | psa_set_key_algorithm( &attributes, alg ); |
| 7425 | psa_set_key_type( &attributes, key_type ); |
| 7426 | |
| 7427 | if( ( status = psa_import_key( &attributes, |
| 7428 | key1, |
| 7429 | PSA_BITS_TO_BYTES( key_bits ), |
| 7430 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 7431 | { |
| 7432 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 7433 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7434 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7435 | goto end; |
| 7436 | } |
| 7437 | |
| 7438 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7439 | |
| 7440 | if( ( status = psa_import_key( &attributes, |
| 7441 | key2, |
| 7442 | PSA_BITS_TO_BYTES( key_bits ), |
| 7443 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 7444 | { |
| 7445 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7446 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7447 | goto end; |
| 7448 | } |
| 7449 | } |
| 7450 | #else |
| 7451 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 7452 | cipher_info ) ) != 0 ) |
| 7453 | { |
| 7454 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7455 | goto end; |
| 7456 | } |
| 7457 | |
| 7458 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 7459 | cipher_info ) ) != 0 ) |
| 7460 | { |
| 7461 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7462 | goto end; |
| 7463 | } |
| 7464 | |
| 7465 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 7466 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7467 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 7468 | { |
| 7469 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7470 | goto end; |
| 7471 | } |
| 7472 | |
| 7473 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 7474 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7475 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 7476 | { |
| 7477 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7478 | goto end; |
| 7479 | } |
| 7480 | |
| 7481 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 7482 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7483 | { |
| 7484 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 7485 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7486 | { |
| 7487 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7488 | goto end; |
| 7489 | } |
| 7490 | |
| 7491 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 7492 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7493 | { |
| 7494 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7495 | goto end; |
| 7496 | } |
| 7497 | } |
| 7498 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 7499 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7500 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7501 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7502 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 7503 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 7504 | if( mac_key_len != 0 ) |
| 7505 | { |
| 7506 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7507 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7508 | |
| 7509 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7510 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7511 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 7512 | |
| 7513 | if( ( status = psa_import_key( &attributes, |
| 7514 | mac_enc, mac_key_len, |
| 7515 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 7516 | { |
| 7517 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7518 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7519 | goto end; |
| 7520 | } |
| 7521 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 7522 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
| 7523 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) && |
| 7524 | ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7525 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 7526 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 7527 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 7528 | else |
| 7529 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 7530 | |
| 7531 | if( ( status = psa_import_key( &attributes, |
| 7532 | mac_dec, mac_key_len, |
| 7533 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 7534 | { |
| 7535 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7536 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7537 | goto end; |
| 7538 | } |
| 7539 | #else |
| 7540 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 7541 | if( ret != 0 ) |
| 7542 | goto end; |
| 7543 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 7544 | if( ret != 0 ) |
| 7545 | goto end; |
| 7546 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7547 | } |
| 7548 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7549 | |
| 7550 | ((void) mac_dec); |
| 7551 | ((void) mac_enc); |
| 7552 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7553 | end: |
| 7554 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 7555 | return( ret ); |
| 7556 | } |
| 7557 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7558 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7559 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7560 | unsigned char *hash, size_t *hashlen, |
| 7561 | unsigned char *data, size_t data_len, |
| 7562 | mbedtls_md_type_t md_alg ) |
| 7563 | { |
| 7564 | psa_status_t status; |
| 7565 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
| 7566 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7567 | |
| 7568 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 7569 | |
| 7570 | if( ( status = psa_hash_setup( &hash_operation, |
| 7571 | hash_alg ) ) != PSA_SUCCESS ) |
| 7572 | { |
| 7573 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 7574 | goto exit; |
| 7575 | } |
| 7576 | |
| 7577 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7578 | 64 ) ) != PSA_SUCCESS ) |
| 7579 | { |
| 7580 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7581 | goto exit; |
| 7582 | } |
| 7583 | |
| 7584 | if( ( status = psa_hash_update( &hash_operation, |
| 7585 | data, data_len ) ) != PSA_SUCCESS ) |
| 7586 | { |
| 7587 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7588 | goto exit; |
| 7589 | } |
| 7590 | |
| 7591 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 7592 | hashlen ) ) != PSA_SUCCESS ) |
| 7593 | { |
| 7594 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 7595 | goto exit; |
| 7596 | } |
| 7597 | |
| 7598 | exit: |
| 7599 | if( status != PSA_SUCCESS ) |
| 7600 | { |
| 7601 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7602 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7603 | switch( status ) |
| 7604 | { |
| 7605 | case PSA_ERROR_NOT_SUPPORTED: |
| 7606 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 7607 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 7608 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7609 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7610 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7611 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7612 | default: |
| 7613 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 7614 | } |
| 7615 | } |
| 7616 | return( 0 ); |
| 7617 | } |
| 7618 | |
| 7619 | #else |
| 7620 | |
| 7621 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7622 | unsigned char *hash, size_t *hashlen, |
| 7623 | unsigned char *data, size_t data_len, |
| 7624 | mbedtls_md_type_t md_alg ) |
| 7625 | { |
| 7626 | int ret = 0; |
| 7627 | mbedtls_md_context_t ctx; |
| 7628 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 7629 | *hashlen = mbedtls_md_get_size( md_info ); |
| 7630 | |
| 7631 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 7632 | |
| 7633 | mbedtls_md_init( &ctx ); |
| 7634 | |
| 7635 | /* |
| 7636 | * digitally-signed struct { |
| 7637 | * opaque client_random[32]; |
| 7638 | * opaque server_random[32]; |
| 7639 | * ServerDHParams params; |
| 7640 | * }; |
| 7641 | */ |
| 7642 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7643 | { |
| 7644 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7645 | goto exit; |
| 7646 | } |
| 7647 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7648 | { |
| 7649 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7650 | goto exit; |
| 7651 | } |
| 7652 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7653 | { |
| 7654 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7655 | goto exit; |
| 7656 | } |
| 7657 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7658 | { |
| 7659 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7660 | goto exit; |
| 7661 | } |
| 7662 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 7663 | { |
| 7664 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7665 | goto exit; |
| 7666 | } |
| 7667 | |
| 7668 | exit: |
| 7669 | mbedtls_md_free( &ctx ); |
| 7670 | |
| 7671 | if( ret != 0 ) |
| 7672 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7673 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7674 | |
| 7675 | return( ret ); |
| 7676 | } |
| 7677 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7678 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7679 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7680 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 7681 | /* Find the preferred hash for a given signature algorithm. */ |
| 7682 | unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg( |
| 7683 | mbedtls_ssl_context *ssl, |
| 7684 | unsigned int sig_alg ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7685 | { |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 7686 | unsigned int i; |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 7687 | uint16_t *received_sig_algs = ssl->handshake->received_sig_algs; |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 7688 | |
| 7689 | if( sig_alg == MBEDTLS_SSL_SIG_ANON ) |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 7690 | return( MBEDTLS_SSL_HASH_NONE ); |
Gabor Mezei | 078e803 | 2022-04-27 21:17:56 +0200 | [diff] [blame] | 7691 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 7692 | for( i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++ ) |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7693 | { |
Gabor Mezei | 3631cf6 | 2022-05-10 12:59:00 +0200 | [diff] [blame] | 7694 | if( sig_alg == MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG( |
| 7695 | received_sig_algs[i] ) ) |
Gabor Mezei | 696956d | 2022-05-13 16:27:29 +0200 | [diff] [blame] | 7696 | return( MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG( |
| 7697 | received_sig_algs[i] ) ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7698 | } |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7699 | |
Gabor Mezei | a3d016c | 2022-05-10 12:44:09 +0200 | [diff] [blame] | 7700 | return( MBEDTLS_SSL_HASH_NONE ); |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7701 | } |
| 7702 | |
| 7703 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7704 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7705 | /* Serialization of TLS 1.2 sessions: |
| 7706 | * |
| 7707 | * struct { |
| 7708 | * uint64 start_time; |
| 7709 | * uint8 ciphersuite[2]; // defined by the standard |
| 7710 | * uint8 compression; // 0 or 1 |
| 7711 | * uint8 session_id_len; // at most 32 |
| 7712 | * opaque session_id[32]; |
| 7713 | * opaque master[48]; // fixed length in the standard |
| 7714 | * uint32 verify_result; |
| 7715 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 7716 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 7717 | * uint32 ticket_lifetime; |
| 7718 | * uint8 mfl_code; // up to 255 according to standard |
| 7719 | * uint8 encrypt_then_mac; // 0 or 1 |
| 7720 | * } serialized_session_tls12; |
| 7721 | * |
| 7722 | */ |
| 7723 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 7724 | unsigned char *buf, |
| 7725 | size_t buf_len ) |
| 7726 | { |
| 7727 | unsigned char *p = buf; |
| 7728 | size_t used = 0; |
| 7729 | |
| 7730 | #if defined(MBEDTLS_HAVE_TIME) |
| 7731 | uint64_t start; |
| 7732 | #endif |
| 7733 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7734 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7735 | size_t cert_len; |
| 7736 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7737 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7738 | |
| 7739 | /* |
| 7740 | * Time |
| 7741 | */ |
| 7742 | #if defined(MBEDTLS_HAVE_TIME) |
| 7743 | used += 8; |
| 7744 | |
| 7745 | if( used <= buf_len ) |
| 7746 | { |
| 7747 | start = (uint64_t) session->start; |
| 7748 | |
| 7749 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 7750 | p += 8; |
| 7751 | } |
| 7752 | #endif /* MBEDTLS_HAVE_TIME */ |
| 7753 | |
| 7754 | /* |
| 7755 | * Basic mandatory fields |
| 7756 | */ |
| 7757 | used += 2 /* ciphersuite */ |
| 7758 | + 1 /* compression */ |
| 7759 | + 1 /* id_len */ |
| 7760 | + sizeof( session->id ) |
| 7761 | + sizeof( session->master ) |
| 7762 | + 4; /* verify_result */ |
| 7763 | |
| 7764 | if( used <= buf_len ) |
| 7765 | { |
| 7766 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 7767 | p += 2; |
| 7768 | |
| 7769 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
| 7770 | |
| 7771 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 7772 | memcpy( p, session->id, 32 ); |
| 7773 | p += 32; |
| 7774 | |
| 7775 | memcpy( p, session->master, 48 ); |
| 7776 | p += 48; |
| 7777 | |
| 7778 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 7779 | p += 4; |
| 7780 | } |
| 7781 | |
| 7782 | /* |
| 7783 | * Peer's end-entity certificate |
| 7784 | */ |
| 7785 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7786 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7787 | if( session->peer_cert == NULL ) |
| 7788 | cert_len = 0; |
| 7789 | else |
| 7790 | cert_len = session->peer_cert->raw.len; |
| 7791 | |
| 7792 | used += 3 + cert_len; |
| 7793 | |
| 7794 | if( used <= buf_len ) |
| 7795 | { |
| 7796 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 7797 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 7798 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 7799 | |
| 7800 | if( session->peer_cert != NULL ) |
| 7801 | { |
| 7802 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 7803 | p += cert_len; |
| 7804 | } |
| 7805 | } |
| 7806 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7807 | if( session->peer_cert_digest != NULL ) |
| 7808 | { |
| 7809 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 7810 | if( used <= buf_len ) |
| 7811 | { |
| 7812 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 7813 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 7814 | memcpy( p, session->peer_cert_digest, |
| 7815 | session->peer_cert_digest_len ); |
| 7816 | p += session->peer_cert_digest_len; |
| 7817 | } |
| 7818 | } |
| 7819 | else |
| 7820 | { |
| 7821 | used += 2; |
| 7822 | if( used <= buf_len ) |
| 7823 | { |
| 7824 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 7825 | *p++ = 0; |
| 7826 | } |
| 7827 | } |
| 7828 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7829 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7830 | |
| 7831 | /* |
| 7832 | * Session ticket if any, plus associated data |
| 7833 | */ |
| 7834 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7835 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 7836 | |
| 7837 | if( used <= buf_len ) |
| 7838 | { |
| 7839 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 7840 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 7841 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 7842 | |
| 7843 | if( session->ticket != NULL ) |
| 7844 | { |
| 7845 | memcpy( p, session->ticket, session->ticket_len ); |
| 7846 | p += session->ticket_len; |
| 7847 | } |
| 7848 | |
| 7849 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 7850 | p += 4; |
| 7851 | } |
| 7852 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7853 | |
| 7854 | /* |
| 7855 | * Misc extension-related info |
| 7856 | */ |
| 7857 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 7858 | used += 1; |
| 7859 | |
| 7860 | if( used <= buf_len ) |
| 7861 | *p++ = session->mfl_code; |
| 7862 | #endif |
| 7863 | |
| 7864 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7865 | used += 1; |
| 7866 | |
| 7867 | if( used <= buf_len ) |
| 7868 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 7869 | #endif |
| 7870 | |
| 7871 | return( used ); |
| 7872 | } |
| 7873 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7874 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 7875 | const unsigned char *buf, |
| 7876 | size_t len ) |
| 7877 | { |
| 7878 | #if defined(MBEDTLS_HAVE_TIME) |
| 7879 | uint64_t start; |
| 7880 | #endif |
| 7881 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7882 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7883 | size_t cert_len; |
| 7884 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7885 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7886 | |
| 7887 | const unsigned char *p = buf; |
| 7888 | const unsigned char * const end = buf + len; |
| 7889 | |
| 7890 | /* |
| 7891 | * Time |
| 7892 | */ |
| 7893 | #if defined(MBEDTLS_HAVE_TIME) |
| 7894 | if( 8 > (size_t)( end - p ) ) |
| 7895 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7896 | |
| 7897 | start = ( (uint64_t) p[0] << 56 ) | |
| 7898 | ( (uint64_t) p[1] << 48 ) | |
| 7899 | ( (uint64_t) p[2] << 40 ) | |
| 7900 | ( (uint64_t) p[3] << 32 ) | |
| 7901 | ( (uint64_t) p[4] << 24 ) | |
| 7902 | ( (uint64_t) p[5] << 16 ) | |
| 7903 | ( (uint64_t) p[6] << 8 ) | |
| 7904 | ( (uint64_t) p[7] ); |
| 7905 | p += 8; |
| 7906 | |
| 7907 | session->start = (time_t) start; |
| 7908 | #endif /* MBEDTLS_HAVE_TIME */ |
| 7909 | |
| 7910 | /* |
| 7911 | * Basic mandatory fields |
| 7912 | */ |
| 7913 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 7914 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7915 | |
| 7916 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 7917 | p += 2; |
| 7918 | |
| 7919 | session->compression = *p++; |
| 7920 | |
| 7921 | session->id_len = *p++; |
| 7922 | memcpy( session->id, p, 32 ); |
| 7923 | p += 32; |
| 7924 | |
| 7925 | memcpy( session->master, p, 48 ); |
| 7926 | p += 48; |
| 7927 | |
| 7928 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 7929 | ( (uint32_t) p[1] << 16 ) | |
| 7930 | ( (uint32_t) p[2] << 8 ) | |
| 7931 | ( (uint32_t) p[3] ); |
| 7932 | p += 4; |
| 7933 | |
| 7934 | /* Immediately clear invalid pointer values that have been read, in case |
| 7935 | * we exit early before we replaced them with valid ones. */ |
| 7936 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7937 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7938 | session->peer_cert = NULL; |
| 7939 | #else |
| 7940 | session->peer_cert_digest = NULL; |
| 7941 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7942 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7943 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7944 | session->ticket = NULL; |
| 7945 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7946 | |
| 7947 | /* |
| 7948 | * Peer certificate |
| 7949 | */ |
| 7950 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7951 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7952 | /* Deserialize CRT from the end of the ticket. */ |
| 7953 | if( 3 > (size_t)( end - p ) ) |
| 7954 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7955 | |
| 7956 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 7957 | p += 3; |
| 7958 | |
| 7959 | if( cert_len != 0 ) |
| 7960 | { |
| 7961 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7962 | |
| 7963 | if( cert_len > (size_t)( end - p ) ) |
| 7964 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7965 | |
| 7966 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7967 | |
| 7968 | if( session->peer_cert == NULL ) |
| 7969 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7970 | |
| 7971 | mbedtls_x509_crt_init( session->peer_cert ); |
| 7972 | |
| 7973 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 7974 | p, cert_len ) ) != 0 ) |
| 7975 | { |
| 7976 | mbedtls_x509_crt_free( session->peer_cert ); |
| 7977 | mbedtls_free( session->peer_cert ); |
| 7978 | session->peer_cert = NULL; |
| 7979 | return( ret ); |
| 7980 | } |
| 7981 | |
| 7982 | p += cert_len; |
| 7983 | } |
| 7984 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7985 | /* Deserialize CRT digest from the end of the ticket. */ |
| 7986 | if( 2 > (size_t)( end - p ) ) |
| 7987 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7988 | |
| 7989 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 7990 | session->peer_cert_digest_len = (size_t) *p++; |
| 7991 | |
| 7992 | if( session->peer_cert_digest_len != 0 ) |
| 7993 | { |
| 7994 | const mbedtls_md_info_t *md_info = |
| 7995 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 7996 | if( md_info == NULL ) |
| 7997 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7998 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 7999 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8000 | |
| 8001 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 8002 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8003 | |
| 8004 | session->peer_cert_digest = |
| 8005 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 8006 | if( session->peer_cert_digest == NULL ) |
| 8007 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8008 | |
| 8009 | memcpy( session->peer_cert_digest, p, |
| 8010 | session->peer_cert_digest_len ); |
| 8011 | p += session->peer_cert_digest_len; |
| 8012 | } |
| 8013 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 8014 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 8015 | |
| 8016 | /* |
| 8017 | * Session ticket and associated data |
| 8018 | */ |
| 8019 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 8020 | if( 3 > (size_t)( end - p ) ) |
| 8021 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8022 | |
| 8023 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 8024 | p += 3; |
| 8025 | |
| 8026 | if( session->ticket_len != 0 ) |
| 8027 | { |
| 8028 | if( session->ticket_len > (size_t)( end - p ) ) |
| 8029 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8030 | |
| 8031 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 8032 | if( session->ticket == NULL ) |
| 8033 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 8034 | |
| 8035 | memcpy( session->ticket, p, session->ticket_len ); |
| 8036 | p += session->ticket_len; |
| 8037 | } |
| 8038 | |
| 8039 | if( 4 > (size_t)( end - p ) ) |
| 8040 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8041 | |
| 8042 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 8043 | ( (uint32_t) p[1] << 16 ) | |
| 8044 | ( (uint32_t) p[2] << 8 ) | |
| 8045 | ( (uint32_t) p[3] ); |
| 8046 | p += 4; |
| 8047 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 8048 | |
| 8049 | /* |
| 8050 | * Misc extension-related info |
| 8051 | */ |
| 8052 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 8053 | if( 1 > (size_t)( end - p ) ) |
| 8054 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8055 | |
| 8056 | session->mfl_code = *p++; |
| 8057 | #endif |
| 8058 | |
| 8059 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 8060 | if( 1 > (size_t)( end - p ) ) |
| 8061 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8062 | |
| 8063 | session->encrypt_then_mac = *p++; |
| 8064 | #endif |
| 8065 | |
| 8066 | /* Done, should have consumed entire buffer */ |
| 8067 | if( p != end ) |
| 8068 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 8069 | |
| 8070 | return( 0 ); |
| 8071 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 8072 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8073 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8074 | int mbedtls_ssl_validate_ciphersuite( |
| 8075 | const mbedtls_ssl_context *ssl, |
| 8076 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 8077 | mbedtls_ssl_protocol_version min_tls_version, |
| 8078 | mbedtls_ssl_protocol_version max_tls_version ) |
| 8079 | { |
| 8080 | (void) ssl; |
| 8081 | |
| 8082 | if( suite_info == NULL ) |
| 8083 | return( -1 ); |
| 8084 | |
| 8085 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 8086 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 8087 | { |
| 8088 | return( -1 ); |
| 8089 | } |
| 8090 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 8091 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 8092 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 8093 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 8094 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 8095 | { |
| 8096 | return( -1 ); |
| 8097 | } |
| 8098 | #endif |
| 8099 | |
| 8100 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8101 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8102 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8103 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8104 | { |
| 8105 | return( -1 ); |
| 8106 | } |
| 8107 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8108 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8109 | |
| 8110 | return( 0 ); |
| 8111 | } |
| 8112 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8113 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8114 | /* |
| 8115 | * Function for writing a signature algorithm extension. |
| 8116 | * |
| 8117 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8118 | * value (TLS 1.3 RFC8446): |
| 8119 | * enum { |
| 8120 | * .... |
| 8121 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8122 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8123 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8124 | * .... |
| 8125 | * } SignatureScheme; |
| 8126 | * |
| 8127 | * struct { |
| 8128 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8129 | * } SignatureSchemeList; |
| 8130 | * |
| 8131 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8132 | * value (TLS 1.2 RFC5246): |
| 8133 | * enum { |
| 8134 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8135 | * sha512(6), (255) |
| 8136 | * } HashAlgorithm; |
| 8137 | * |
| 8138 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8139 | * SignatureAlgorithm; |
| 8140 | * |
| 8141 | * struct { |
| 8142 | * HashAlgorithm hash; |
| 8143 | * SignatureAlgorithm signature; |
| 8144 | * } SignatureAndHashAlgorithm; |
| 8145 | * |
| 8146 | * SignatureAndHashAlgorithm |
| 8147 | * supported_signature_algorithms<2..2^16-2>; |
| 8148 | * |
| 8149 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8150 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8151 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8152 | * `SignatureScheme` field of TLS 1.3 |
| 8153 | * |
| 8154 | */ |
| 8155 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8156 | const unsigned char *end, size_t *out_len ) |
| 8157 | { |
| 8158 | unsigned char *p = buf; |
| 8159 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8160 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8161 | |
| 8162 | *out_len = 0; |
| 8163 | |
| 8164 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8165 | |
| 8166 | /* Check if we have space for header and length field: |
| 8167 | * - extension_type (2 bytes) |
| 8168 | * - extension_data_length (2 bytes) |
| 8169 | * - supported_signature_algorithms_length (2 bytes) |
| 8170 | */ |
| 8171 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8172 | p += 6; |
| 8173 | |
| 8174 | /* |
| 8175 | * Write supported_signature_algorithms |
| 8176 | */ |
| 8177 | supported_sig_alg = p; |
| 8178 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8179 | if( sig_alg == NULL ) |
| 8180 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8181 | |
| 8182 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8183 | { |
| 8184 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8185 | continue; |
| 8186 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8187 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8188 | p += 2; |
| 8189 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); |
| 8190 | } |
| 8191 | |
| 8192 | /* Length of supported_signature_algorithms */ |
| 8193 | supported_sig_alg_len = p - supported_sig_alg; |
| 8194 | if( supported_sig_alg_len == 0 ) |
| 8195 | { |
| 8196 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8197 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8198 | } |
| 8199 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8200 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8201 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8202 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8203 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8204 | *out_len = p - buf; |
| 8205 | |
| 8206 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 8207 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 8208 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8209 | return( 0 ); |
| 8210 | } |
| 8211 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8212 | |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8213 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8214 | /* |
| 8215 | * mbedtls_ssl_parse_server_name_ext |
| 8216 | * |
| 8217 | * Structure of server_name extension: |
| 8218 | * |
| 8219 | * enum { |
| 8220 | * host_name(0), (255) |
| 8221 | * } NameType; |
| 8222 | * opaque HostName<1..2^16-1>; |
| 8223 | * |
| 8224 | * struct { |
| 8225 | * NameType name_type; |
| 8226 | * select (name_type) { |
| 8227 | * case host_name: HostName; |
| 8228 | * } name; |
| 8229 | * } ServerName; |
| 8230 | * struct { |
| 8231 | * ServerName server_name_list<1..2^16-1> |
| 8232 | * } ServerNameList; |
| 8233 | */ |
| 8234 | int mbedtls_ssl_parse_server_name_ext( mbedtls_ssl_context *ssl, |
| 8235 | const unsigned char *buf, |
| 8236 | const unsigned char *end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8237 | { |
| 8238 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 8239 | const unsigned char *p = buf; |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8240 | size_t server_name_list_len, hostname_len; |
| 8241 | const unsigned char *server_name_list_end; |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8242 | |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8243 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8244 | |
| 8245 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8246 | server_name_list_len = MBEDTLS_GET_UINT16_BE( p, 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8247 | p += 2; |
| 8248 | |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8249 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, server_name_list_len ); |
| 8250 | server_name_list_end = p + server_name_list_len; |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8251 | while( p < server_name_list_end ) |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8252 | { |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8253 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8254 | hostname_len = MBEDTLS_GET_UINT16_BE( p, 1 ); |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8255 | MBEDTLS_SSL_CHK_BUF_READ_PTR( p, server_name_list_end, |
| 8256 | hostname_len + 3 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8257 | |
| 8258 | if( p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) |
| 8259 | { |
XiaokangQian | 75fe8c7 | 2022-06-15 09:42:45 +0000 | [diff] [blame] | 8260 | /* sni_name is intended to be used only during the parsing of the |
| 8261 | * ClientHello message (it is reset to NULL before the end of |
| 8262 | * the message parsing). Thus it is ok to just point to the |
| 8263 | * reception buffer and not make a copy of it. |
| 8264 | */ |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8265 | ssl->handshake->sni_name = p + 3; |
| 8266 | ssl->handshake->sni_name_len = hostname_len; |
| 8267 | if( ssl->conf->f_sni == NULL ) |
| 8268 | return( 0 ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8269 | ret = ssl->conf->f_sni( ssl->conf->p_sni, |
XiaokangQian | 9b2b771 | 2022-05-17 02:57:00 +0000 | [diff] [blame] | 8270 | ssl, p + 3, hostname_len ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8271 | if( ret != 0 ) |
| 8272 | { |
XiaokangQian | f2a9420 | 2022-05-20 06:44:24 +0000 | [diff] [blame] | 8273 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret ); |
XiaokangQian | 129aeb9 | 2022-06-02 09:29:18 +0000 | [diff] [blame] | 8274 | MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME, |
| 8275 | MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
XiaokangQian | 40a3523 | 2022-05-07 09:02:40 +0000 | [diff] [blame] | 8276 | return( MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME ); |
| 8277 | } |
| 8278 | return( 0 ); |
| 8279 | } |
| 8280 | |
| 8281 | p += hostname_len + 3; |
| 8282 | } |
| 8283 | |
| 8284 | return( 0 ); |
| 8285 | } |
| 8286 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 8287 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8288 | #endif /* MBEDTLS_SSL_TLS_C */ |