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 | |
| 619 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 620 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 621 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 622 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 624 | #if defined(MBEDTLS_DHM_C) |
| 625 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 626 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 627 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 629 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 630 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 631 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 632 | #if defined(MBEDTLS_SSL_CLI_C) |
| 633 | handshake->ecjpake_cache = NULL; |
| 634 | handshake->ecjpake_cache_len = 0; |
| 635 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 636 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 637 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 638 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 639 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 640 | #endif |
| 641 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 642 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 643 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 644 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 645 | |
| 646 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 647 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 648 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 649 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 652 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 653 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 655 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 656 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 657 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 658 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 659 | #else |
| 660 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 661 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 662 | #endif |
| 663 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 664 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 665 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 666 | transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 667 | transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 668 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 669 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 670 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 671 | #endif |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 672 | #endif |
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 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 676 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 681 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 682 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 683 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 685 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 687 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 688 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 689 | |
| 690 | /* |
| 691 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 692 | * Now allocate missing structures. |
| 693 | */ |
| 694 | if( ssl->transform_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->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
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 | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 699 | if( ssl->session_negotiate == 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->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 702 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 703 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 704 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 705 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 706 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 707 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 708 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 709 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 710 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 711 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 712 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 713 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 714 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 715 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 716 | if( ssl->handshake == NULL || |
| 717 | ssl->transform_negotiate == NULL || |
| 718 | ssl->session_negotiate == NULL ) |
| 719 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 720 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 721 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 722 | mbedtls_free( ssl->handshake ); |
| 723 | mbedtls_free( ssl->transform_negotiate ); |
| 724 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 725 | |
| 726 | ssl->handshake = NULL; |
| 727 | ssl->transform_negotiate = NULL; |
| 728 | ssl->session_negotiate = NULL; |
| 729 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 730 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 733 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 734 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 735 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 736 | ssl_handshake_params_init( ssl->handshake ); |
| 737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 739 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 740 | { |
| 741 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 742 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 743 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 744 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 745 | else |
| 746 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 747 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 748 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 749 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 750 | #endif |
| 751 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 752 | /* |
| 753 | * curve_list is translated to IANA TLS group identifiers here because |
| 754 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 755 | * any error codes. |
| 756 | */ |
| 757 | #if defined(MBEDTLS_ECP_C) |
| 758 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 759 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 760 | if ( ssl->conf->curve_list != NULL ) |
| 761 | { |
| 762 | size_t length; |
| 763 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 764 | |
| 765 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 766 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 767 | |
| 768 | /* Leave room for zero termination */ |
| 769 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 770 | if ( group_list == NULL ) |
| 771 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 772 | |
| 773 | for( size_t i = 0; i < length; i++ ) |
| 774 | { |
| 775 | const mbedtls_ecp_curve_info *info = |
| 776 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 777 | if ( info == NULL ) |
| 778 | { |
| 779 | mbedtls_free( group_list ); |
| 780 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 781 | } |
| 782 | group_list[i] = info->tls_id; |
| 783 | } |
| 784 | |
| 785 | group_list[length] = 0; |
| 786 | |
| 787 | ssl->handshake->group_list = group_list; |
| 788 | ssl->handshake->group_list_heap_allocated = 1; |
| 789 | } |
| 790 | else |
| 791 | { |
| 792 | ssl->handshake->group_list = ssl->conf->group_list; |
| 793 | ssl->handshake->group_list_heap_allocated = 0; |
| 794 | } |
| 795 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 796 | #endif /* MBEDTLS_ECP_C */ |
| 797 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 798 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 799 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 800 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 801 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 802 | signature algorithms IANA identifiers. */ |
| 803 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 804 | ssl->conf->sig_hashes != NULL ) |
| 805 | { |
| 806 | const int *md; |
| 807 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 808 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 809 | uint16_t *p; |
| 810 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 811 | #if defined(static_assert) |
| 812 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 813 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 814 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 815 | #endif |
| 816 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 817 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 818 | { |
| 819 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 820 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 821 | #if defined(MBEDTLS_ECDSA_C) |
| 822 | sig_algs_len += sizeof( uint16_t ); |
| 823 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 824 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 825 | #if defined(MBEDTLS_RSA_C) |
| 826 | sig_algs_len += sizeof( uint16_t ); |
| 827 | #endif |
| 828 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 829 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 830 | } |
| 831 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 832 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 833 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 834 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 835 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 836 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 837 | if( ssl->handshake->sig_algs == NULL ) |
| 838 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 839 | |
| 840 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 841 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 842 | { |
| 843 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 844 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 845 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 846 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 847 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 848 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 849 | #endif |
| 850 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 851 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 852 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 853 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 854 | } |
| 855 | *p = MBEDTLS_TLS1_3_SIG_NONE; |
| 856 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 857 | } |
| 858 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 859 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 860 | { |
| 861 | ssl->handshake->sig_algs = ssl->conf->sig_algs; |
| 862 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 863 | } |
| 864 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 865 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 866 | return( 0 ); |
| 867 | } |
| 868 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 869 | #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] | 870 | /* Dummy cookie callbacks for defaults */ |
| 871 | static int ssl_cookie_write_dummy( void *ctx, |
| 872 | unsigned char **p, unsigned char *end, |
| 873 | const unsigned char *cli_id, size_t cli_id_len ) |
| 874 | { |
| 875 | ((void) ctx); |
| 876 | ((void) p); |
| 877 | ((void) end); |
| 878 | ((void) cli_id); |
| 879 | ((void) cli_id_len); |
| 880 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 881 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | static int ssl_cookie_check_dummy( void *ctx, |
| 885 | const unsigned char *cookie, size_t cookie_len, |
| 886 | const unsigned char *cli_id, size_t cli_id_len ) |
| 887 | { |
| 888 | ((void) ctx); |
| 889 | ((void) cookie); |
| 890 | ((void) cookie_len); |
| 891 | ((void) cli_id); |
| 892 | ((void) cli_id_len); |
| 893 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 894 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 895 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 896 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 897 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 898 | /* |
| 899 | * Initialize an SSL context |
| 900 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 901 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 902 | { |
| 903 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 904 | } |
| 905 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 906 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 907 | { |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 908 | const mbedtls_ssl_config *conf = ssl->conf; |
| 909 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 910 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 911 | if( mbedtls_ssl_conf_is_tls13_only( conf ) ) |
| 912 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 913 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 914 | { |
| 915 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported." ) ); |
| 916 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 917 | } |
| 918 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 919 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 920 | return( 0 ); |
| 921 | } |
| 922 | #endif |
| 923 | |
| 924 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 086ee0b | 2022-03-15 15:18:51 +0100 | [diff] [blame] | 925 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 926 | { |
| 927 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 928 | return( 0 ); |
| 929 | } |
| 930 | #endif |
| 931 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 932 | #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] | 933 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( conf ) ) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 934 | { |
XiaokangQian | ed582dd | 2022-04-13 08:21:05 +0000 | [diff] [blame] | 935 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 936 | { |
| 937 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2" ) ); |
| 938 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 939 | } |
| 940 | |
XiaokangQian | 8f9dfe4 | 2022-04-15 02:52:39 +0000 | [diff] [blame] | 941 | if( conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 942 | { |
| 943 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS 1.3 server is not supported yet." ) ); |
| 944 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 945 | } |
| 946 | |
| 947 | |
Ronald Cron | e1d3f06 | 2022-02-10 14:50:54 +0100 | [diff] [blame] | 948 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is TLS 1.3 or TLS 1.2." ) ); |
| 949 | return( 0 ); |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 950 | } |
| 951 | #endif |
| 952 | |
| 953 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 954 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 955 | } |
| 956 | |
| 957 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 958 | { |
| 959 | int ret; |
| 960 | ret = ssl_conf_version_check( ssl ); |
| 961 | if( ret != 0 ) |
| 962 | return( ret ); |
| 963 | |
| 964 | /* Space for further checks */ |
| 965 | |
| 966 | return( 0 ); |
| 967 | } |
| 968 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 969 | /* |
| 970 | * Setup an SSL context |
| 971 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 972 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 973 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 974 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 975 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 976 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 977 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 978 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 979 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 980 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 981 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 982 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 983 | return( ret ); |
| 984 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 985 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 986 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 987 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 988 | |
| 989 | /* Set to NULL in case of an error condition */ |
| 990 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 991 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 992 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 993 | ssl->in_buf_len = in_buf_len; |
| 994 | #endif |
| 995 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 996 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 997 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 998 | 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] | 999 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1000 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1001 | } |
| 1002 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1003 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1004 | ssl->out_buf_len = out_buf_len; |
| 1005 | #endif |
| 1006 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1007 | if( ssl->out_buf == NULL ) |
| 1008 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1009 | 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] | 1010 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1011 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1014 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 1015 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1016 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 1017 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1018 | #endif |
| 1019 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1020 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1021 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1022 | |
| 1023 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1024 | |
| 1025 | error: |
| 1026 | mbedtls_free( ssl->in_buf ); |
| 1027 | mbedtls_free( ssl->out_buf ); |
| 1028 | |
| 1029 | ssl->conf = NULL; |
| 1030 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1031 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1032 | ssl->in_buf_len = 0; |
| 1033 | ssl->out_buf_len = 0; |
| 1034 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 1035 | ssl->in_buf = NULL; |
| 1036 | ssl->out_buf = NULL; |
| 1037 | |
| 1038 | ssl->in_hdr = NULL; |
| 1039 | ssl->in_ctr = NULL; |
| 1040 | ssl->in_len = NULL; |
| 1041 | ssl->in_iv = NULL; |
| 1042 | ssl->in_msg = NULL; |
| 1043 | |
| 1044 | ssl->out_hdr = NULL; |
| 1045 | ssl->out_ctr = NULL; |
| 1046 | ssl->out_len = NULL; |
| 1047 | ssl->out_iv = NULL; |
| 1048 | ssl->out_msg = NULL; |
| 1049 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 1050 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1054 | * Reset an initialized and used SSL context for re-use while retaining |
| 1055 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1056 | * |
| 1057 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 1058 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1059 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1060 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 1061 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1062 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1063 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1064 | size_t in_buf_len = ssl->in_buf_len; |
| 1065 | size_t out_buf_len = ssl->out_buf_len; |
| 1066 | #else |
| 1067 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1068 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1069 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1070 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1071 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 1072 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1073 | #endif |
| 1074 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1075 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1076 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1077 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1078 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1079 | |
| 1080 | /* Reset incoming message parsing */ |
| 1081 | ssl->in_offt = NULL; |
| 1082 | ssl->nb_zero = 0; |
| 1083 | ssl->in_msgtype = 0; |
| 1084 | ssl->in_msglen = 0; |
| 1085 | ssl->in_hslen = 0; |
| 1086 | ssl->keep_current_message = 0; |
| 1087 | ssl->transform_in = NULL; |
| 1088 | |
| 1089 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1090 | ssl->next_record_offset = 0; |
| 1091 | ssl->in_epoch = 0; |
| 1092 | #endif |
| 1093 | |
| 1094 | /* Keep current datagram if partial == 1 */ |
| 1095 | if( partial == 0 ) |
| 1096 | { |
| 1097 | ssl->in_left = 0; |
| 1098 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1099 | } |
| 1100 | |
| 1101 | /* Reset outgoing message writing */ |
| 1102 | ssl->out_msgtype = 0; |
| 1103 | ssl->out_msglen = 0; |
| 1104 | ssl->out_left = 0; |
| 1105 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1106 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1107 | ssl->transform_out = NULL; |
| 1108 | |
| 1109 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1110 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1111 | #endif |
| 1112 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1113 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1114 | if( ssl->transform ) |
| 1115 | { |
| 1116 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1117 | mbedtls_free( ssl->transform ); |
| 1118 | ssl->transform = NULL; |
| 1119 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1120 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1121 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1122 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1123 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1124 | mbedtls_free( ssl->transform_application ); |
| 1125 | ssl->transform_application = NULL; |
| 1126 | |
| 1127 | if( ssl->handshake != NULL ) |
| 1128 | { |
| 1129 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1130 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1131 | ssl->handshake->transform_earlydata = NULL; |
| 1132 | |
| 1133 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1134 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1135 | ssl->handshake->transform_handshake = NULL; |
| 1136 | } |
| 1137 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1138 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1142 | { |
| 1143 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1144 | |
| 1145 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1146 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1147 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1148 | |
| 1149 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1150 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1151 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1152 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1153 | |
| 1154 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1155 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1156 | 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] | 1157 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1158 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1159 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1160 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1161 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1162 | if( ssl->session ) |
| 1163 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1164 | mbedtls_ssl_session_free( ssl->session ); |
| 1165 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1166 | ssl->session = NULL; |
| 1167 | } |
| 1168 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1169 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1170 | ssl->alpn_chosen = NULL; |
| 1171 | #endif |
| 1172 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1173 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1174 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1175 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1176 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1177 | { |
| 1178 | mbedtls_free( ssl->cli_id ); |
| 1179 | ssl->cli_id = NULL; |
| 1180 | ssl->cli_id_len = 0; |
| 1181 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1182 | #endif |
| 1183 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1184 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1185 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1186 | |
| 1187 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1190 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1191 | * Reset an initialized and used SSL context for re-use while retaining |
| 1192 | * all application-set variables, function pointers and data. |
| 1193 | */ |
| 1194 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1195 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1196 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1200 | * SSL set accessors |
| 1201 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1202 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1203 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1204 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1207 | 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] | 1208 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1209 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1210 | } |
| 1211 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1213 | 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] | 1214 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1215 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1216 | } |
| 1217 | #endif |
| 1218 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1219 | 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] | 1220 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1221 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1222 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1224 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1225 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1226 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1227 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1228 | { |
| 1229 | ssl->disable_datagram_packing = !allow_packing; |
| 1230 | } |
| 1231 | |
| 1232 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1233 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1234 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1235 | conf->hs_timeout_min = min; |
| 1236 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1237 | } |
| 1238 | #endif |
| 1239 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1240 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1241 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1242 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1246 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1247 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1248 | void *p_vrfy ) |
| 1249 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1250 | conf->f_vrfy = f_vrfy; |
| 1251 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1252 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1253 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1254 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1255 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1256 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1257 | void *p_rng ) |
| 1258 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1259 | conf->f_rng = f_rng; |
| 1260 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1263 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1264 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1265 | void *p_dbg ) |
| 1266 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1267 | conf->f_dbg = f_dbg; |
| 1268 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1271 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1272 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1273 | mbedtls_ssl_send_t *f_send, |
| 1274 | mbedtls_ssl_recv_t *f_recv, |
| 1275 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1276 | { |
| 1277 | ssl->p_bio = p_bio; |
| 1278 | ssl->f_send = f_send; |
| 1279 | ssl->f_recv = f_recv; |
| 1280 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1283 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1284 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1285 | { |
| 1286 | ssl->mtu = mtu; |
| 1287 | } |
| 1288 | #endif |
| 1289 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1290 | 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] | 1291 | { |
| 1292 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1295 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1296 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1297 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1298 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1299 | { |
| 1300 | ssl->p_timer = p_timer; |
| 1301 | ssl->f_set_timer = f_set_timer; |
| 1302 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1303 | |
| 1304 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1305 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1308 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1309 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1310 | void *p_cache, |
| 1311 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1312 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1313 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1314 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1315 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1316 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1317 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1318 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1320 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1321 | 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] | 1322 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1323 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1324 | |
| 1325 | if( ssl == NULL || |
| 1326 | session == NULL || |
| 1327 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1328 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1329 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1330 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1331 | } |
| 1332 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1333 | if( ssl->handshake->resume == 1 ) |
| 1334 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1335 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1336 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1337 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1338 | return( ret ); |
| 1339 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1340 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1341 | |
| 1342 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1343 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1344 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1345 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1346 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1347 | const int *ciphersuites ) |
| 1348 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1349 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1352 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1353 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1354 | const int kex_modes ) |
| 1355 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1356 | 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] | 1357 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1358 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1361 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1362 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1363 | { |
| 1364 | conf->cert_profile = profile; |
| 1365 | } |
| 1366 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1367 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
| 1368 | { |
| 1369 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
| 1370 | |
| 1371 | while( cur != NULL ) |
| 1372 | { |
| 1373 | next = cur->next; |
| 1374 | mbedtls_free( cur ); |
| 1375 | cur = next; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1379 | /* Append a new keycert entry to a (possibly empty) list */ |
| 1380 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1381 | mbedtls_x509_crt *cert, |
| 1382 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1383 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1384 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1385 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1386 | if( cert == NULL ) |
| 1387 | { |
| 1388 | /* Free list if cert is null */ |
| 1389 | ssl_key_cert_free( *head ); |
| 1390 | *head = NULL; |
| 1391 | return( 0 ); |
| 1392 | } |
| 1393 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1394 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1395 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1396 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1397 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1398 | new_cert->cert = cert; |
| 1399 | new_cert->key = key; |
| 1400 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1401 | |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 1402 | /* 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] | 1403 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1404 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1405 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1406 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1407 | else |
| 1408 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1409 | mbedtls_ssl_key_cert *cur = *head; |
| 1410 | while( cur->next != NULL ) |
| 1411 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1412 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1415 | return( 0 ); |
| 1416 | } |
| 1417 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1418 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1419 | mbedtls_x509_crt *own_cert, |
| 1420 | mbedtls_pk_context *pk_key ) |
| 1421 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1422 | 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] | 1423 | } |
| 1424 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1425 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1426 | mbedtls_x509_crt *ca_chain, |
| 1427 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1428 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1429 | conf->ca_chain = ca_chain; |
| 1430 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1431 | |
| 1432 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1433 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1434 | * cannot be used together. */ |
| 1435 | conf->f_ca_cb = NULL; |
| 1436 | conf->p_ca_cb = NULL; |
| 1437 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1438 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1439 | |
| 1440 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1441 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1442 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1443 | void *p_ca_cb ) |
| 1444 | { |
| 1445 | conf->f_ca_cb = f_ca_cb; |
| 1446 | conf->p_ca_cb = p_ca_cb; |
| 1447 | |
| 1448 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1449 | * cannot be used together. */ |
| 1450 | conf->ca_chain = NULL; |
| 1451 | conf->ca_crl = NULL; |
| 1452 | } |
| 1453 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1454 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1455 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1456 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Glenn Strauss | 6989407 | 2022-01-24 12:58:00 -0500 | [diff] [blame] | 1457 | const unsigned char *mbedtls_ssl_get_hs_sni( mbedtls_ssl_context *ssl, |
| 1458 | size_t *name_len ) |
| 1459 | { |
| 1460 | *name_len = ssl->handshake->sni_name_len; |
| 1461 | return( ssl->handshake->sni_name ); |
| 1462 | } |
| 1463 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1464 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1465 | mbedtls_x509_crt *own_cert, |
| 1466 | mbedtls_pk_context *pk_key ) |
| 1467 | { |
| 1468 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1469 | own_cert, pk_key ) ); |
| 1470 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1471 | |
| 1472 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1473 | mbedtls_x509_crt *ca_chain, |
| 1474 | mbedtls_x509_crl *ca_crl ) |
| 1475 | { |
| 1476 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1477 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1478 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1479 | |
| 1480 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1481 | int authmode ) |
| 1482 | { |
| 1483 | ssl->handshake->sni_authmode = authmode; |
| 1484 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1485 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1486 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1487 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1488 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1489 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1490 | void *p_vrfy ) |
| 1491 | { |
| 1492 | ssl->f_vrfy = f_vrfy; |
| 1493 | ssl->p_vrfy = p_vrfy; |
| 1494 | } |
| 1495 | #endif |
| 1496 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1497 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1498 | /* |
| 1499 | * Set EC J-PAKE password for current handshake |
| 1500 | */ |
| 1501 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1502 | const unsigned char *pw, |
| 1503 | size_t pw_len ) |
| 1504 | { |
| 1505 | mbedtls_ecjpake_role role; |
| 1506 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1507 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1508 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1509 | |
| 1510 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1511 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1512 | else |
| 1513 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1514 | |
| 1515 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1516 | role, |
| 1517 | MBEDTLS_MD_SHA256, |
| 1518 | MBEDTLS_ECP_DP_SECP256R1, |
| 1519 | pw, pw_len ) ); |
| 1520 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1521 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1522 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1523 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1524 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1525 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 1526 | { |
| 1527 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1528 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 1529 | return( 1 ); |
| 1530 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1531 | if( conf->psk != NULL ) |
| 1532 | return( 1 ); |
| 1533 | |
| 1534 | return( 0 ); |
| 1535 | } |
| 1536 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1537 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1538 | { |
| 1539 | /* Remove reference to existing PSK, if any. */ |
| 1540 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1541 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1542 | { |
| 1543 | /* The maintenance of the PSK key slot is the |
| 1544 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1545 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1546 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1547 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1548 | if( conf->psk != NULL ) |
| 1549 | { |
| 1550 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1551 | |
| 1552 | mbedtls_free( conf->psk ); |
| 1553 | conf->psk = NULL; |
| 1554 | conf->psk_len = 0; |
| 1555 | } |
| 1556 | |
| 1557 | /* Remove reference to PSK identity, if any. */ |
| 1558 | if( conf->psk_identity != NULL ) |
| 1559 | { |
| 1560 | mbedtls_free( conf->psk_identity ); |
| 1561 | conf->psk_identity = NULL; |
| 1562 | conf->psk_identity_len = 0; |
| 1563 | } |
| 1564 | } |
| 1565 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1566 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1567 | * It checks that the provided identity is well-formed and attempts |
| 1568 | * to make a copy of it in the SSL config. |
| 1569 | * On failure, the PSK identity in the config remains unset. */ |
| 1570 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1571 | unsigned char const *psk_identity, |
| 1572 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1573 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1574 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1575 | if( psk_identity == NULL || |
| 1576 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1577 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1578 | { |
| 1579 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1580 | } |
| 1581 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1582 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1583 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1584 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1585 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1586 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1587 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1588 | |
| 1589 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1592 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1593 | const unsigned char *psk, size_t psk_len, |
| 1594 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1595 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1596 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1597 | |
| 1598 | /* We currently only support one PSK, raw or opaque. */ |
| 1599 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1600 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1601 | |
| 1602 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1603 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1604 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1605 | if( psk_len == 0 ) |
| 1606 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1607 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1608 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1609 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1610 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1611 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1612 | conf->psk_len = psk_len; |
| 1613 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1614 | |
| 1615 | /* Check and set PSK Identity */ |
| 1616 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1617 | if( ret != 0 ) |
| 1618 | ssl_conf_remove_psk( conf ); |
| 1619 | |
| 1620 | return( ret ); |
| 1621 | } |
| 1622 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1623 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1624 | { |
| 1625 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1626 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1627 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1628 | /* The maintenance of the external PSK key slot is the |
| 1629 | * user's responsibility. */ |
| 1630 | if( ssl->handshake->psk_opaque_is_internal ) |
| 1631 | { |
| 1632 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 1633 | ssl->handshake->psk_opaque_is_internal = 0; |
| 1634 | } |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1635 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1636 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1637 | #else |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1638 | if( ssl->handshake->psk != NULL ) |
| 1639 | { |
| 1640 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1641 | ssl->handshake->psk_len ); |
| 1642 | mbedtls_free( ssl->handshake->psk ); |
| 1643 | ssl->handshake->psk_len = 0; |
| 1644 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 1645 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1648 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1649 | const unsigned char *psk, size_t psk_len ) |
| 1650 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1651 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1652 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 1653 | psa_status_t status; |
| 1654 | psa_algorithm_t alg; |
| 1655 | mbedtls_svc_key_id_t key; |
| 1656 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1657 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1658 | if( psk == NULL || ssl->handshake == NULL ) |
| 1659 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1660 | |
| 1661 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1662 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1663 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1664 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1665 | |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1666 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1667 | if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) |
| 1668 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 1669 | else |
| 1670 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1671 | |
| 1672 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 1673 | psa_set_key_algorithm( &key_attributes, alg ); |
| 1674 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 1675 | |
| 1676 | status = psa_import_key( &key_attributes, psk, psk_len, &key ); |
| 1677 | if( status != PSA_SUCCESS ) |
| 1678 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1679 | |
| 1680 | /* Allow calling psa_destroy_key() on psk remove */ |
| 1681 | ssl->handshake->psk_opaque_is_internal = 1; |
| 1682 | return mbedtls_ssl_set_hs_psk_opaque( ssl, key ); |
| 1683 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1684 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1685 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1686 | |
| 1687 | ssl->handshake->psk_len = psk_len; |
| 1688 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1689 | |
| 1690 | return( 0 ); |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 1691 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1692 | } |
| 1693 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1694 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1695 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1696 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1697 | const unsigned char *psk_identity, |
| 1698 | size_t psk_identity_len ) |
| 1699 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1700 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1701 | |
| 1702 | /* We currently only support one PSK, raw or opaque. */ |
| 1703 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1704 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1705 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1706 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1707 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1708 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1709 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1710 | |
| 1711 | /* Check and set PSK Identity */ |
| 1712 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1713 | psk_identity_len ); |
| 1714 | if( ret != 0 ) |
| 1715 | ssl_conf_remove_psk( conf ); |
| 1716 | |
| 1717 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1718 | } |
| 1719 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1720 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1721 | mbedtls_svc_key_id_t psk ) |
| 1722 | { |
| 1723 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1724 | ( ssl->handshake == NULL ) ) |
| 1725 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1726 | |
| 1727 | ssl_remove_psk( ssl ); |
| 1728 | ssl->handshake->psk_opaque = psk; |
| 1729 | return( 0 ); |
| 1730 | } |
| 1731 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1732 | |
| 1733 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1734 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1735 | size_t), |
| 1736 | void *p_psk ) |
| 1737 | { |
| 1738 | conf->f_psk = f_psk; |
| 1739 | conf->p_psk = p_psk; |
| 1740 | } |
| 1741 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 1742 | |
| 1743 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1744 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1745 | psa_algorithm_t alg ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1746 | { |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1747 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1748 | if( alg == PSA_ALG_CBC_NO_PADDING ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1749 | return( MBEDTLS_SSL_MODE_CBC ); |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1750 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1751 | if( PSA_ALG_IS_AEAD( alg ) ) |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1752 | return( MBEDTLS_SSL_MODE_AEAD ); |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1753 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1754 | } |
| 1755 | |
| 1756 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1757 | |
| 1758 | static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode( |
| 1759 | mbedtls_cipher_mode_t mode ) |
| 1760 | { |
| 1761 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1762 | if( mode == MBEDTLS_MODE_CBC ) |
| 1763 | return( MBEDTLS_SSL_MODE_CBC ); |
| 1764 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1765 | |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1766 | #if defined(MBEDTLS_GCM_C) || \ |
| 1767 | defined(MBEDTLS_CCM_C) || \ |
| 1768 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1769 | if( mode == MBEDTLS_MODE_GCM || |
| 1770 | mode == MBEDTLS_MODE_CCM || |
| 1771 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
| 1772 | return( MBEDTLS_SSL_MODE_AEAD ); |
| 1773 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1774 | |
| 1775 | return( MBEDTLS_SSL_MODE_STREAM ); |
| 1776 | } |
Gilles Peskine | 301711e | 2022-04-26 16:57:05 +0200 | [diff] [blame] | 1777 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Neil Armstrong | 8a0f3e8 | 2022-03-30 10:57:37 +0200 | [diff] [blame] | 1778 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1779 | static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode( |
| 1780 | mbedtls_ssl_mode_t base_mode, |
| 1781 | int encrypt_then_mac ) |
| 1782 | { |
| 1783 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1784 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED && |
| 1785 | base_mode == MBEDTLS_SSL_MODE_CBC ) |
| 1786 | { |
| 1787 | return( MBEDTLS_SSL_MODE_CBC_ETM ); |
| 1788 | } |
| 1789 | #else |
| 1790 | (void) encrypt_then_mac; |
| 1791 | #endif |
| 1792 | return( base_mode ); |
| 1793 | } |
| 1794 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1795 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1796 | const mbedtls_ssl_transform *transform ) |
| 1797 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1798 | mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode( |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1799 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1800 | transform->psa_alg |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1801 | #else |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1802 | mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) |
| 1803 | #endif |
| 1804 | ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1805 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1806 | int encrypt_then_mac = 0; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1807 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1808 | encrypt_then_mac = transform->encrypt_then_mac; |
| 1809 | #endif |
| 1810 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1811 | } |
| 1812 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 1813 | mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1814 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1815 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 1816 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1817 | const mbedtls_ssl_ciphersuite_t *suite ) |
| 1818 | { |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1819 | mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM; |
| 1820 | |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1821 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1822 | psa_status_t status; |
| 1823 | psa_algorithm_t alg; |
| 1824 | psa_key_type_t type; |
| 1825 | size_t size; |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1826 | status = mbedtls_ssl_cipher_to_psa( suite->cipher, 0, &alg, &type, &size ); |
| 1827 | if( status == PSA_SUCCESS ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1828 | base_mode = mbedtls_ssl_get_base_mode( alg ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1829 | #else |
| 1830 | const mbedtls_cipher_info_t *cipher = |
| 1831 | mbedtls_cipher_info_from_type( suite->cipher ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1832 | if( cipher != NULL ) |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1833 | { |
| 1834 | base_mode = |
| 1835 | mbedtls_ssl_get_base_mode( |
| 1836 | mbedtls_cipher_info_get_mode( cipher ) ); |
| 1837 | } |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1838 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1839 | |
Gilles Peskine | e108d98 | 2022-04-26 16:50:40 +0200 | [diff] [blame] | 1840 | #if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
| 1841 | int encrypt_then_mac = 0; |
| 1842 | #endif |
| 1843 | return( mbedtls_ssl_get_actual_mode( base_mode, encrypt_then_mac ) ); |
Neil Armstrong | 4bf4c86 | 2022-04-01 10:35:48 +0200 | [diff] [blame] | 1844 | } |
| 1845 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1846 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1847 | 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] | 1848 | size_t taglen, |
| 1849 | psa_algorithm_t *alg, |
| 1850 | psa_key_type_t *key_type, |
| 1851 | size_t *key_size ) |
| 1852 | { |
| 1853 | switch ( mbedtls_cipher_type ) |
| 1854 | { |
| 1855 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 1856 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1857 | *key_type = PSA_KEY_TYPE_AES; |
| 1858 | *key_size = 128; |
| 1859 | break; |
| 1860 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 1861 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1862 | *key_type = PSA_KEY_TYPE_AES; |
| 1863 | *key_size = 128; |
| 1864 | break; |
| 1865 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 1866 | *alg = PSA_ALG_GCM; |
| 1867 | *key_type = PSA_KEY_TYPE_AES; |
| 1868 | *key_size = 128; |
| 1869 | break; |
| 1870 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 1871 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1872 | *key_type = PSA_KEY_TYPE_AES; |
| 1873 | *key_size = 192; |
| 1874 | break; |
| 1875 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 1876 | *alg = PSA_ALG_GCM; |
| 1877 | *key_type = PSA_KEY_TYPE_AES; |
| 1878 | *key_size = 192; |
| 1879 | break; |
| 1880 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 1881 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1882 | *key_type = PSA_KEY_TYPE_AES; |
| 1883 | *key_size = 256; |
| 1884 | break; |
| 1885 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 1886 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1887 | *key_type = PSA_KEY_TYPE_AES; |
| 1888 | *key_size = 256; |
| 1889 | break; |
| 1890 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 1891 | *alg = PSA_ALG_GCM; |
| 1892 | *key_type = PSA_KEY_TYPE_AES; |
| 1893 | *key_size = 256; |
| 1894 | break; |
| 1895 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 1896 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1897 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1898 | *key_size = 128; |
| 1899 | break; |
| 1900 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 1901 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1902 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1903 | *key_size = 128; |
| 1904 | break; |
| 1905 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 1906 | *alg = PSA_ALG_GCM; |
| 1907 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1908 | *key_size = 128; |
| 1909 | break; |
| 1910 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 1911 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1912 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1913 | *key_size = 192; |
| 1914 | break; |
| 1915 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 1916 | *alg = PSA_ALG_GCM; |
| 1917 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1918 | *key_size = 192; |
| 1919 | break; |
| 1920 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 1921 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1922 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1923 | *key_size = 256; |
| 1924 | break; |
| 1925 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 1926 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1927 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1928 | *key_size = 256; |
| 1929 | break; |
| 1930 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 1931 | *alg = PSA_ALG_GCM; |
| 1932 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1933 | *key_size = 256; |
| 1934 | break; |
| 1935 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 1936 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1937 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1938 | *key_size = 128; |
| 1939 | break; |
| 1940 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 1941 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1942 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1943 | *key_size = 128; |
| 1944 | break; |
| 1945 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 1946 | *alg = PSA_ALG_GCM; |
| 1947 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1948 | *key_size = 128; |
| 1949 | break; |
| 1950 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 1951 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1952 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1953 | *key_size = 192; |
| 1954 | break; |
| 1955 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 1956 | *alg = PSA_ALG_GCM; |
| 1957 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1958 | *key_size = 192; |
| 1959 | break; |
| 1960 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 1961 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1962 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1963 | *key_size = 256; |
| 1964 | break; |
| 1965 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 1966 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1967 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1968 | *key_size = 256; |
| 1969 | break; |
| 1970 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 1971 | *alg = PSA_ALG_GCM; |
| 1972 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1973 | *key_size = 256; |
| 1974 | break; |
| 1975 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 1976 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 1977 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 1978 | *key_size = 256; |
| 1979 | break; |
| 1980 | case MBEDTLS_CIPHER_NULL: |
| 1981 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 1982 | *key_type = 0; |
| 1983 | *key_size = 0; |
| 1984 | break; |
| 1985 | default: |
| 1986 | return PSA_ERROR_NOT_SUPPORTED; |
| 1987 | } |
| 1988 | |
| 1989 | return PSA_SUCCESS; |
| 1990 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1991 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1992 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1993 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1994 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 1995 | const unsigned char *dhm_P, size_t P_len, |
| 1996 | const unsigned char *dhm_G, size_t G_len ) |
| 1997 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1998 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1999 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2000 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2001 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2002 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 2003 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 2004 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 2005 | { |
| 2006 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2007 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2008 | return( ret ); |
| 2009 | } |
| 2010 | |
| 2011 | return( 0 ); |
| 2012 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2013 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2014 | 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] | 2015 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2016 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2017 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 2018 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2019 | mbedtls_mpi_free( &conf->dhm_G ); |
| 2020 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 2021 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 2022 | &conf->dhm_P ) ) != 0 || |
| 2023 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 2024 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2025 | { |
| 2026 | mbedtls_mpi_free( &conf->dhm_P ); |
| 2027 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2028 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 2029 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2030 | |
| 2031 | return( 0 ); |
| 2032 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 2033 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 2034 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 2035 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 2036 | /* |
| 2037 | * Set the minimum length for Diffie-Hellman parameters |
| 2038 | */ |
| 2039 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 2040 | unsigned int bitlen ) |
| 2041 | { |
| 2042 | conf->dhm_min_bitlen = bitlen; |
| 2043 | } |
| 2044 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 2045 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2046 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2047 | #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] | 2048 | /* |
| 2049 | * Set allowed/preferred hashes for handshake signatures |
| 2050 | */ |
| 2051 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 2052 | const int *hashes ) |
| 2053 | { |
| 2054 | conf->sig_hashes = hashes; |
| 2055 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 2056 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2057 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2058 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2059 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 2060 | const uint16_t* sig_algs ) |
| 2061 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2062 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2063 | conf->sig_hashes = NULL; |
| 2064 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 2065 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 2066 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2067 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 2068 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 2069 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2070 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2071 | /* |
| 2072 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2073 | * |
| 2074 | * mbedtls_ssl_setup() takes the provided list |
| 2075 | * and translates it to a list of IANA TLS group identifiers, |
| 2076 | * stored in ssl->handshake->group_list. |
| 2077 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2078 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2079 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2080 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2081 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2082 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2083 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2084 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2085 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2086 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 2087 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2088 | /* |
| 2089 | * Set the allowed groups |
| 2090 | */ |
| 2091 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 2092 | const uint16_t *group_list ) |
| 2093 | { |
| 2094 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2095 | conf->curve_list = NULL; |
| 2096 | #endif |
| 2097 | conf->group_list = group_list; |
| 2098 | } |
| 2099 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2100 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2101 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2102 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2103 | /* Initialize to suppress unnecessary compiler warning */ |
| 2104 | size_t hostname_len = 0; |
| 2105 | |
| 2106 | /* Check if new hostname is valid before |
| 2107 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2108 | if( hostname != NULL ) |
| 2109 | { |
| 2110 | hostname_len = strlen( hostname ); |
| 2111 | |
| 2112 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 2113 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2114 | } |
| 2115 | |
| 2116 | /* Now it's clear that we will overwrite the old hostname, |
| 2117 | * so we can free it safely */ |
| 2118 | |
| 2119 | if( ssl->hostname != NULL ) |
| 2120 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2121 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2122 | mbedtls_free( ssl->hostname ); |
| 2123 | } |
| 2124 | |
| 2125 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 2126 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2127 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2128 | { |
| 2129 | ssl->hostname = NULL; |
| 2130 | } |
| 2131 | else |
| 2132 | { |
| 2133 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2134 | if( ssl->hostname == NULL ) |
| 2135 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2136 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2137 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 2138 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 2139 | ssl->hostname[hostname_len] = '\0'; |
| 2140 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2141 | |
| 2142 | return( 0 ); |
| 2143 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 2144 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2145 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 2146 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2147 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2148 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2149 | const unsigned char *, size_t), |
| 2150 | void *p_sni ) |
| 2151 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2152 | conf->f_sni = f_sni; |
| 2153 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2154 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2155 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 2156 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2157 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2158 | 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] | 2159 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2160 | size_t cur_len, tot_len; |
| 2161 | const char **p; |
| 2162 | |
| 2163 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 2164 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 2165 | * MUST NOT be truncated." |
| 2166 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2167 | */ |
| 2168 | tot_len = 0; |
| 2169 | for( p = protos; *p != NULL; p++ ) |
| 2170 | { |
| 2171 | cur_len = strlen( *p ); |
| 2172 | tot_len += cur_len; |
| 2173 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 2174 | if( ( cur_len == 0 ) || |
| 2175 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 2176 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2177 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2178 | } |
| 2179 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2180 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 2181 | |
| 2182 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2183 | } |
| 2184 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2185 | 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] | 2186 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2187 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2188 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2189 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 2190 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2191 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2192 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 2193 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2194 | { |
| 2195 | conf->dtls_srtp_mki_support = support_mki_value; |
| 2196 | } |
| 2197 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2198 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 2199 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 2200 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2201 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2202 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
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_BAD_INPUT_DATA ); |
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 | 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] | 2208 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2209 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2210 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2211 | |
| 2212 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 2213 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 2214 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 2215 | } |
| 2216 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 2217 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2218 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2219 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2220 | const mbedtls_ssl_srtp_profile *p; |
| 2221 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2222 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2223 | /* check the profiles list: all entry must be valid, |
| 2224 | * 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] | 2225 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 2226 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 2227 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2228 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2229 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2230 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2231 | list_size++; |
| 2232 | } |
| 2233 | else |
| 2234 | { |
| 2235 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2236 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2237 | } |
| 2238 | } |
| 2239 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2240 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2241 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2242 | conf->dtls_srtp_profile_list = NULL; |
| 2243 | conf->dtls_srtp_profile_list_len = 0; |
| 2244 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2245 | } |
| 2246 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2247 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2248 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2249 | |
| 2250 | return( 0 ); |
| 2251 | } |
| 2252 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2253 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2254 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2255 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2256 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2257 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2258 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2259 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2260 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2261 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2262 | else |
| 2263 | { |
| 2264 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2265 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2266 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2267 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2268 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2269 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2270 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2271 | 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] | 2272 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2273 | conf->max_tls_version = (major << 8) | minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2276 | 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] | 2277 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 2278 | conf->min_tls_version = (major << 8) | minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2281 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2282 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2283 | char cert_req_ca_list ) |
| 2284 | { |
| 2285 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2286 | } |
| 2287 | #endif |
| 2288 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2289 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2290 | 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] | 2291 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2292 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2293 | } |
| 2294 | #endif |
| 2295 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2296 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2297 | 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] | 2298 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2299 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2300 | } |
| 2301 | #endif |
| 2302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2303 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2304 | 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] | 2305 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2306 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2307 | 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] | 2308 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2309 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2312 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2313 | |
| 2314 | return( 0 ); |
| 2315 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2316 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2317 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2318 | 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] | 2319 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2320 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2323 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2324 | 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] | 2325 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2326 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2327 | } |
| 2328 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2329 | 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] | 2330 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2331 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2332 | } |
| 2333 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2334 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2335 | const unsigned char period[8] ) |
| 2336 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2337 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2338 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2339 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2341 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2342 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2343 | 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] | 2344 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2345 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2346 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2347 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2348 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2349 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2350 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2351 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2352 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2353 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2354 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2355 | conf->f_ticket_write = f_ticket_write; |
| 2356 | conf->f_ticket_parse = f_ticket_parse; |
| 2357 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2358 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2359 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2360 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2361 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2362 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2363 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2364 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2365 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2366 | ssl->f_export_keys = f_export_keys; |
| 2367 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2368 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2369 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2370 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2371 | void mbedtls_ssl_conf_async_private_cb( |
| 2372 | mbedtls_ssl_config *conf, |
| 2373 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2374 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2375 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2376 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2377 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2378 | { |
| 2379 | conf->f_async_sign_start = f_async_sign; |
| 2380 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2381 | conf->f_async_resume = f_async_resume; |
| 2382 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2383 | conf->p_async_config_data = async_config_data; |
| 2384 | } |
| 2385 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2386 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2387 | { |
| 2388 | return( conf->p_async_config_data ); |
| 2389 | } |
| 2390 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2391 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2392 | { |
| 2393 | if( ssl->handshake == NULL ) |
| 2394 | return( NULL ); |
| 2395 | else |
| 2396 | return( ssl->handshake->user_async_ctx ); |
| 2397 | } |
| 2398 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2399 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2400 | void *ctx ) |
| 2401 | { |
| 2402 | if( ssl->handshake != NULL ) |
| 2403 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2404 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2405 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2406 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2407 | /* |
| 2408 | * SSL get accessors |
| 2409 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2410 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2411 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2412 | if( ssl->session != NULL ) |
| 2413 | return( ssl->session->verify_result ); |
| 2414 | |
| 2415 | if( ssl->session_negotiate != NULL ) |
| 2416 | return( ssl->session_negotiate->verify_result ); |
| 2417 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2418 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2421 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2422 | { |
| 2423 | if( ssl == NULL || ssl->session == NULL ) |
| 2424 | return( 0 ); |
| 2425 | |
| 2426 | return( ssl->session->ciphersuite ); |
| 2427 | } |
| 2428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2429 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2430 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2431 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2432 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2433 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2434 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2437 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2438 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2439 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2440 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2441 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2442 | switch( ssl->tls_version ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2443 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2444 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2445 | return( "DTLSv1.2" ); |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2446 | default: |
| 2447 | return( "unknown (DTLS)" ); |
| 2448 | } |
| 2449 | } |
| 2450 | #endif |
| 2451 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2452 | switch( ssl->tls_version ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2453 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2454 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2455 | return( "TLSv1.2" ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2456 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2457 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2458 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2459 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2460 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2463 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2464 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2465 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2466 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2467 | size_t read_mfl; |
| 2468 | |
| 2469 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2470 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2471 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2472 | { |
| 2473 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2474 | } |
| 2475 | |
| 2476 | /* Check if a smaller max length was negotiated */ |
| 2477 | if( ssl->session_out != NULL ) |
| 2478 | { |
| 2479 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2480 | if( read_mfl < max_len ) |
| 2481 | { |
| 2482 | max_len = read_mfl; |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | // During a handshake, use the value being negotiated |
| 2487 | if( ssl->session_negotiate != NULL ) |
| 2488 | { |
| 2489 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2490 | if( read_mfl < max_len ) |
| 2491 | { |
| 2492 | max_len = read_mfl; |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | return( max_len ); |
| 2497 | } |
| 2498 | |
| 2499 | 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] | 2500 | { |
| 2501 | size_t max_len; |
| 2502 | |
| 2503 | /* |
| 2504 | * Assume mfl_code is correct since it was checked when set |
| 2505 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2506 | 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] | 2507 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2508 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2509 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2510 | 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] | 2511 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2512 | 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] | 2513 | } |
| 2514 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2515 | /* During a handshake, use the value being negotiated */ |
| 2516 | if( ssl->session_negotiate != NULL && |
| 2517 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2518 | { |
| 2519 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2520 | } |
| 2521 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2522 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2523 | } |
| 2524 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2525 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2526 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2527 | 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] | 2528 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2529 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2530 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2531 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 2532 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 2533 | return ( 0 ); |
| 2534 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2535 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 2536 | return( ssl->mtu ); |
| 2537 | |
| 2538 | if( ssl->mtu == 0 ) |
| 2539 | return( ssl->handshake->mtu ); |
| 2540 | |
| 2541 | return( ssl->mtu < ssl->handshake->mtu ? |
| 2542 | ssl->mtu : ssl->handshake->mtu ); |
| 2543 | } |
| 2544 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2545 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2546 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 2547 | { |
| 2548 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2549 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2550 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2551 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2552 | (void) ssl; |
| 2553 | #endif |
| 2554 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2555 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2556 | 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] | 2557 | |
| 2558 | if( max_len > mfl ) |
| 2559 | max_len = mfl; |
| 2560 | #endif |
| 2561 | |
| 2562 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2563 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2564 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2565 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2566 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 2567 | const size_t overhead = (size_t) ret; |
| 2568 | |
| 2569 | if( ret < 0 ) |
| 2570 | return( ret ); |
| 2571 | |
| 2572 | if( mtu <= overhead ) |
| 2573 | { |
| 2574 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 2575 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2576 | } |
| 2577 | |
| 2578 | if( max_len > mtu - overhead ) |
| 2579 | max_len = mtu - overhead; |
| 2580 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2581 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2582 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2583 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2584 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2585 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2586 | #endif |
| 2587 | |
| 2588 | return( (int) max_len ); |
| 2589 | } |
| 2590 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2591 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 2592 | { |
| 2593 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 2594 | |
| 2595 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2596 | (void) ssl; |
| 2597 | #endif |
| 2598 | |
| 2599 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2600 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 2601 | |
| 2602 | if( max_len > mfl ) |
| 2603 | max_len = mfl; |
| 2604 | #endif |
| 2605 | |
| 2606 | return( (int) max_len ); |
| 2607 | } |
| 2608 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2609 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2610 | 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] | 2611 | { |
| 2612 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2613 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2614 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2615 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2616 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2617 | #else |
| 2618 | return( NULL ); |
| 2619 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2620 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2621 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2623 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2624 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 2625 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2626 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2627 | int ret; |
| 2628 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2629 | if( ssl == NULL || |
| 2630 | dst == NULL || |
| 2631 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2632 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2633 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2634 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2635 | } |
| 2636 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2637 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 2638 | * idempotent: Each session can only be exported once. |
| 2639 | * |
| 2640 | * (This is in preparation for TLS 1.3 support where we will |
| 2641 | * need the ability to export multiple sessions (aka tickets), |
| 2642 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 2643 | * multiple times until it fails.) |
| 2644 | * |
| 2645 | * Check whether we have already exported the current session, |
| 2646 | * and fail if so. |
| 2647 | */ |
| 2648 | if( ssl->session->exported == 1 ) |
| 2649 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2650 | |
| 2651 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 2652 | if( ret != 0 ) |
| 2653 | return( ret ); |
| 2654 | |
| 2655 | /* Remember that we've exported the session. */ |
| 2656 | ssl->session->exported = 1; |
| 2657 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2658 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2659 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2660 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2661 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2662 | * Define ticket header determining Mbed TLS version |
| 2663 | * and structure of the ticket. |
| 2664 | */ |
| 2665 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2666 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2667 | * Define bitflag determining compile-time settings influencing |
| 2668 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2669 | */ |
| 2670 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2671 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2672 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2673 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2674 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2675 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2676 | |
| 2677 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2678 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2679 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2680 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2681 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2682 | |
| 2683 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2684 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2685 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2686 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2687 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 2688 | |
| 2689 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2690 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2691 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2692 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2693 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2694 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2695 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2696 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2697 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2698 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2699 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 2700 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2701 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2702 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 2703 | #else |
| 2704 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 2705 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 2706 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2707 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 2708 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 2709 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 2710 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 2711 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 2712 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2713 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2714 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2715 | ( (uint16_t) ( \ |
| 2716 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 2717 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 2718 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 2719 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2720 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 2721 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2722 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2723 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2724 | MBEDTLS_VERSION_MAJOR, |
| 2725 | MBEDTLS_VERSION_MINOR, |
| 2726 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 2727 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 2728 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2729 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2730 | |
| 2731 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2732 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 2733 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2734 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2735 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2736 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 2737 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 2738 | * opaque session_format[2]; // library-version specific 16-bit field |
| 2739 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2740 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2741 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2742 | * Note: When updating the format, remember to keep |
| 2743 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2744 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2745 | * // In this version, `session_format` determines |
| 2746 | * // the setting of those compile-time |
| 2747 | * // configuration options which influence |
| 2748 | * // the structure of mbedtls_ssl_session. |
| 2749 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2750 | * uint8_t minor_ver; // Protocol minor version. Possible values: |
| 2751 | * // - TLS 1.2 (3) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2752 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2753 | * select (serialized_session.tls_version) { |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2754 | * |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2755 | * case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2756 | * serialized_session_tls12 data; |
| 2757 | * |
| 2758 | * }; |
| 2759 | * |
| 2760 | * } serialized_session; |
| 2761 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2762 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2763 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2764 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 2765 | unsigned char omit_header, |
| 2766 | unsigned char *buf, |
| 2767 | size_t buf_len, |
| 2768 | size_t *olen ) |
| 2769 | { |
| 2770 | unsigned char *p = buf; |
| 2771 | size_t used = 0; |
| 2772 | |
| 2773 | if( !omit_header ) |
| 2774 | { |
| 2775 | /* |
| 2776 | * Add Mbed TLS version identifier |
| 2777 | */ |
| 2778 | |
| 2779 | used += sizeof( ssl_serialized_session_header ); |
| 2780 | |
| 2781 | if( used <= buf_len ) |
| 2782 | { |
| 2783 | memcpy( p, ssl_serialized_session_header, |
| 2784 | sizeof( ssl_serialized_session_header ) ); |
| 2785 | p += sizeof( ssl_serialized_session_header ); |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | /* |
| 2790 | * TLS version identifier |
| 2791 | */ |
| 2792 | used += 1; |
| 2793 | if( used <= buf_len ) |
| 2794 | { |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2795 | *p++ = MBEDTLS_BYTE_0( session->tls_version ); |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | /* Forward to version-specific serialization routine. */ |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2799 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2800 | { |
| 2801 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2802 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2803 | { |
| 2804 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 2805 | used += ssl_session_save_tls12( session, p, remaining_len ); |
| 2806 | break; |
| 2807 | } |
| 2808 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2809 | |
| 2810 | default: |
| 2811 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2812 | } |
| 2813 | |
| 2814 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 2815 | if( used > buf_len ) |
| 2816 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2817 | |
| 2818 | return( 0 ); |
| 2819 | } |
| 2820 | |
| 2821 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2822 | * Public wrapper for ssl_session_save() |
| 2823 | */ |
| 2824 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 2825 | unsigned char *buf, |
| 2826 | size_t buf_len, |
| 2827 | size_t *olen ) |
| 2828 | { |
| 2829 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 2830 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2831 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 2832 | /* |
| 2833 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 2834 | * |
| 2835 | * This internal version is wrapped by a public function that cleans up in |
| 2836 | * case of error, and has an extra option omit_header. |
| 2837 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2838 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 2839 | unsigned char omit_header, |
| 2840 | const unsigned char *buf, |
| 2841 | size_t len ) |
| 2842 | { |
| 2843 | const unsigned char *p = buf; |
| 2844 | const unsigned char * const end = buf + len; |
| 2845 | |
| 2846 | if( !omit_header ) |
| 2847 | { |
| 2848 | /* |
| 2849 | * Check Mbed TLS version identifier |
| 2850 | */ |
| 2851 | |
| 2852 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 2853 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2854 | |
| 2855 | if( memcmp( p, ssl_serialized_session_header, |
| 2856 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 2857 | { |
| 2858 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 2859 | } |
| 2860 | p += sizeof( ssl_serialized_session_header ); |
| 2861 | } |
| 2862 | |
| 2863 | /* |
| 2864 | * TLS version identifier |
| 2865 | */ |
| 2866 | if( 1 > (size_t)( end - p ) ) |
| 2867 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2868 | session->tls_version = 0x0300 | *p++; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2869 | |
| 2870 | /* Dispatch according to TLS version. */ |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2871 | switch( session->tls_version ) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2872 | { |
| 2873 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 2874 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2875 | { |
| 2876 | size_t remaining_len = ( end - p ); |
| 2877 | return( ssl_session_load_tls12( session, p, remaining_len ) ); |
| 2878 | } |
| 2879 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2880 | |
| 2881 | default: |
| 2882 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2883 | } |
| 2884 | } |
| 2885 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2886 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 2887 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2888 | */ |
| 2889 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 2890 | const unsigned char *buf, |
| 2891 | size_t len ) |
| 2892 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2893 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2894 | |
| 2895 | if( ret != 0 ) |
| 2896 | mbedtls_ssl_session_free( session ); |
| 2897 | |
| 2898 | return( ret ); |
| 2899 | } |
| 2900 | |
| 2901 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2902 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2903 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2904 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 2905 | { |
| 2906 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2907 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2908 | /* |
| 2909 | * 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] | 2910 | * that were written into the output buffer by the previous handshake step, |
| 2911 | * if the write to the network callback returned with the |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame] | 2912 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 2913 | * We proceed to the next handshake step only when all data from the |
| 2914 | * previous one have been sent to the peer, thus we make sure that this is |
| 2915 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 2916 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 2917 | * we have to wait before to go ahead. |
| 2918 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 2919 | * peer. Data are only sent here and through |
| 2920 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
| 2921 | * alert occured. |
| 2922 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2923 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 2924 | return( ret ); |
| 2925 | |
| 2926 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2927 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2928 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 2929 | { |
| 2930 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2931 | return( ret ); |
| 2932 | } |
| 2933 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2934 | |
| 2935 | return( ret ); |
| 2936 | } |
| 2937 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2938 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2939 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2940 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2941 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2942 | if( ssl == NULL || |
| 2943 | ssl->conf == NULL || |
| 2944 | ssl->handshake == NULL || |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2945 | mbedtls_ssl_is_handshake_over( ssl ) == 1 ) |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2946 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2947 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | ret = ssl_prepare_handshake_step( ssl ); |
| 2951 | if( ret != 0 ) |
| 2952 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2953 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2954 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 2955 | if( ret != 0 ) |
| 2956 | goto cleanup; |
| 2957 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2958 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2959 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2960 | { |
Ronald Cron | 27c85e7 | 2022-03-08 11:37:55 +0100 | [diff] [blame] | 2961 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %s", |
| 2962 | mbedtls_ssl_states_str( ssl->state ) ) ); |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2963 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2964 | switch( ssl->state ) |
| 2965 | { |
| 2966 | case MBEDTLS_SSL_HELLO_REQUEST: |
| 2967 | ssl->state = MBEDTLS_SSL_CLIENT_HELLO; |
| 2968 | break; |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2969 | |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2970 | case MBEDTLS_SSL_CLIENT_HELLO: |
| 2971 | ret = mbedtls_ssl_write_client_hello( ssl ); |
| 2972 | break; |
| 2973 | |
| 2974 | default: |
| 2975 | #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] | 2976 | if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Ronald Cron | 9f0fba3 | 2022-02-10 16:45:15 +0100 | [diff] [blame] | 2977 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 2978 | else |
| 2979 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 2980 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2981 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 2982 | #else |
| 2983 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
| 2984 | #endif |
| 2985 | } |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2986 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2987 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2988 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2989 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2990 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2991 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2992 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 2993 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2994 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2995 | |
| 2996 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2997 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 2998 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 2999 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3000 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3001 | #endif |
| 3002 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3003 | if( ret != 0 ) |
| 3004 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 3005 | /* handshake_step return error. And it is same |
| 3006 | * with alert_reason. |
| 3007 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3008 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3009 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 3010 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 3011 | goto cleanup; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3016 | return( ret ); |
| 3017 | } |
| 3018 | |
| 3019 | /* |
| 3020 | * Perform the SSL handshake |
| 3021 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3022 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3023 | { |
| 3024 | int ret = 0; |
| 3025 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3026 | /* Sanity checks */ |
| 3027 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3028 | if( ssl == NULL || ssl->conf == NULL ) |
| 3029 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3030 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3031 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3032 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3033 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 3034 | { |
| 3035 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 3036 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 3037 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3038 | } |
| 3039 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3040 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3041 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3042 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 3043 | /* Main handshake loop */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3044 | while( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3045 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3046 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 3047 | |
| 3048 | if( ret != 0 ) |
| 3049 | break; |
| 3050 | } |
| 3051 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3052 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3053 | |
| 3054 | return( ret ); |
| 3055 | } |
| 3056 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3057 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3058 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3059 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3060 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3061 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3062 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3063 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3064 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3065 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3066 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3067 | |
| 3068 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3069 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3070 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3071 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3072 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3073 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 3074 | 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] | 3075 | return( ret ); |
| 3076 | } |
| 3077 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3078 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3079 | |
| 3080 | return( 0 ); |
| 3081 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3082 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3083 | |
| 3084 | /* |
| 3085 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3086 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 3087 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 3088 | * - 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] | 3089 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3090 | * 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] | 3091 | * 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] | 3092 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3093 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3094 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3095 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3096 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3097 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3098 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3099 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3100 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3101 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3102 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 3103 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3104 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3105 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3106 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3107 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3108 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3109 | ssl->handshake->out_msg_seq = 1; |
| 3110 | else |
| 3111 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 3112 | } |
| 3113 | #endif |
| 3114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3115 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3116 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3118 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3119 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3120 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3121 | return( ret ); |
| 3122 | } |
| 3123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3124 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3125 | |
| 3126 | return( 0 ); |
| 3127 | } |
| 3128 | |
| 3129 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3130 | * Renegotiate current connection on client, |
| 3131 | * or request renegotiation on server |
| 3132 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3133 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3134 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3135 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3136 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 3137 | if( ssl == NULL || ssl->conf == NULL ) |
| 3138 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3140 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3141 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3142 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3143 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3144 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3145 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3147 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3148 | |
| 3149 | /* Did we already try/start sending HelloRequest? */ |
| 3150 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3151 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 3152 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3153 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3154 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3155 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3156 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3157 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3158 | /* |
| 3159 | * On client, either start the renegotiation process or, |
| 3160 | * if already in progress, continue the handshake |
| 3161 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3162 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3163 | { |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3164 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3165 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3166 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3167 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3168 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 3169 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3170 | return( ret ); |
| 3171 | } |
| 3172 | } |
| 3173 | else |
| 3174 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3175 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3176 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3177 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3178 | return( ret ); |
| 3179 | } |
| 3180 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3181 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 3182 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 3183 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3184 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3185 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 3186 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3187 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3188 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3189 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 3190 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3191 | if( handshake == NULL ) |
| 3192 | return; |
| 3193 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3194 | #if defined(MBEDTLS_ECP_C) |
| 3195 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3196 | if ( ssl->handshake->group_list_heap_allocated ) |
| 3197 | mbedtls_free( (void*) handshake->group_list ); |
| 3198 | handshake->group_list = NULL; |
| 3199 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3200 | #endif /* MBEDTLS_ECP_C */ |
| 3201 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3202 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 3203 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3204 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 3205 | mbedtls_free( (void*) handshake->sig_algs ); |
| 3206 | handshake->sig_algs = NULL; |
| 3207 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 3208 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3209 | if( ssl->handshake->certificate_request_context ) |
| 3210 | { |
| 3211 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 3212 | } |
| 3213 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3214 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 3215 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3216 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3217 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3218 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3219 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3220 | handshake->async_in_progress = 0; |
| 3221 | } |
| 3222 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3223 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3224 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3225 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3226 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3227 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3228 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3229 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3230 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 3231 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3232 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3233 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3234 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3235 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 3236 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3237 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3239 | #if defined(MBEDTLS_DHM_C) |
| 3240 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3241 | #endif |
Neil Armstrong | f3f4641 | 2022-04-12 14:43:39 +0200 | [diff] [blame] | 3242 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3243 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3244 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3245 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3246 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3247 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3248 | mbedtls_free( handshake->ecjpake_cache ); |
| 3249 | handshake->ecjpake_cache = NULL; |
| 3250 | handshake->ecjpake_cache_len = 0; |
| 3251 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3252 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3253 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3254 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3255 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3256 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3257 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3258 | #endif |
| 3259 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3260 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 3261 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3262 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 3263 | { |
| 3264 | /* The maintenance of the external PSK key slot is the |
| 3265 | * user's responsibility. */ |
| 3266 | if( ssl->handshake->psk_opaque_is_internal ) |
| 3267 | { |
| 3268 | psa_destroy_key( ssl->handshake->psk_opaque ); |
| 3269 | ssl->handshake->psk_opaque_is_internal = 0; |
| 3270 | } |
| 3271 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 3272 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3273 | #else |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3274 | if( handshake->psk != NULL ) |
| 3275 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3276 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3277 | mbedtls_free( handshake->psk ); |
| 3278 | } |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 3279 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3280 | #endif |
| 3281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3282 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3283 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3284 | /* |
| 3285 | * Free only the linked list wrapper, not the keys themselves |
| 3286 | * since the belong to the SNI callback |
| 3287 | */ |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 3288 | ssl_key_cert_free( handshake->sni_key_cert ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3289 | #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] | 3290 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3291 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3292 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3293 | if( handshake->ecrs_peer_cert != NULL ) |
| 3294 | { |
| 3295 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3296 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3297 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3298 | #endif |
| 3299 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3300 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3301 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3302 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3303 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3304 | |
XiaokangQian | 9b93c0d | 2022-02-09 06:02:25 +0000 | [diff] [blame] | 3305 | #if defined(MBEDTLS_SSL_CLI_C) && \ |
| 3306 | ( defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
| 3307 | mbedtls_free( handshake->cookie ); |
| 3308 | #endif /* MBEDTLS_SSL_CLI_C && |
| 3309 | ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */ |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3310 | |
| 3311 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3312 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3313 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3314 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3315 | |
Ronald Cron | f12b81d | 2022-03-15 10:42:41 +0100 | [diff] [blame] | 3316 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3317 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) |
Neil Armstrong | f716a70 | 2022-04-04 11:23:46 +0200 | [diff] [blame] | 3318 | if( handshake->ecdh_psa_privkey_is_external == 0 ) |
Neil Armstrong | 8113d25 | 2022-03-23 10:57:04 +0100 | [diff] [blame] | 3319 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3320 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3321 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3322 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3323 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3324 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3325 | mbedtls_free( handshake->transform_earlydata ); |
| 3326 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3327 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3328 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3329 | |
| 3330 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3331 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3332 | * processes datagrams and the fact that a datagram is allowed to have |
| 3333 | * several records in it, it is possible that the I/O buffers are not |
| 3334 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3335 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3336 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3337 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3338 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3339 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3340 | mbedtls_platform_zeroize( handshake, |
| 3341 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3344 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3345 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3346 | if( session == NULL ) |
| 3347 | return; |
| 3348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3349 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3350 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3351 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3352 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3353 | #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] | 3354 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3355 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3356 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3357 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3360 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3361 | |
| 3362 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3363 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3364 | #else |
| 3365 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3366 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3367 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3368 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3369 | |
| 3370 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3371 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3372 | #else |
| 3373 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3374 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3375 | |
| 3376 | #if defined(MBEDTLS_SSL_ALPN) |
| 3377 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3378 | #else |
| 3379 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3380 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3381 | |
| 3382 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3383 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3384 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3385 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3386 | |
| 3387 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3388 | ( (uint32_t) ( \ |
| 3389 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3390 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3391 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3392 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3393 | 0u ) ) |
| 3394 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3395 | static unsigned char ssl_serialized_context_header[] = { |
| 3396 | MBEDTLS_VERSION_MAJOR, |
| 3397 | MBEDTLS_VERSION_MINOR, |
| 3398 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3399 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3400 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3401 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3402 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3403 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3404 | }; |
| 3405 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3406 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3407 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3408 | * |
| 3409 | * The format of the serialized data is: |
| 3410 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3411 | * |
| 3412 | * // header |
| 3413 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3414 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3415 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3416 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3417 | * Note: When updating the format, remember to keep these |
| 3418 | * 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] | 3419 | * |
| 3420 | * // session sub-structure |
| 3421 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3422 | * // transform sub-structure |
| 3423 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3424 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3425 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3426 | * // fields from ssl_context |
| 3427 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3428 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3429 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3430 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3431 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3432 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3433 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3434 | * |
| 3435 | * Note that many fields of the ssl_context or sub-structures are not |
| 3436 | * serialized, as they fall in one of the following categories: |
| 3437 | * |
| 3438 | * 1. forced value (eg in_left must be 0) |
| 3439 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3440 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3441 | * 4. value was temporary (eg content of input buffer) |
| 3442 | * 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] | 3443 | */ |
| 3444 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3445 | unsigned char *buf, |
| 3446 | size_t buf_len, |
| 3447 | size_t *olen ) |
| 3448 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3449 | unsigned char *p = buf; |
| 3450 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3451 | size_t session_len; |
| 3452 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3453 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3454 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3455 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3456 | * this function's documentation. |
| 3457 | * |
| 3458 | * These are due to assumptions/limitations in the implementation. Some of |
| 3459 | * them are likely to stay (no handshake in progress) some might go away |
| 3460 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3461 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3462 | /* The initial handshake must be over */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 3463 | if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3464 | { |
| 3465 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +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 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3469 | { |
| 3470 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3471 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3472 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3473 | /* Double-check that sub-structures are indeed ready */ |
| 3474 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3475 | { |
| 3476 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3477 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3478 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3479 | /* There must be no pending incoming or outgoing data */ |
| 3480 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3481 | { |
| 3482 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming 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 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3486 | { |
| 3487 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3488 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3489 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3490 | /* Protocol must be DLTS, not TLS */ |
| 3491 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3492 | { |
| 3493 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3494 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3495 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3496 | /* Version must be 1.2 */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3497 | if( ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3498 | { |
| 3499 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3500 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3501 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3502 | /* We must be using an AEAD ciphersuite */ |
| 3503 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3504 | { |
| 3505 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3506 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3507 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3508 | /* Renegotiation must not be enabled */ |
| 3509 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3510 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3511 | { |
| 3512 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3513 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3514 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3515 | #endif |
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 | /* |
| 3518 | * Version and format identifier |
| 3519 | */ |
| 3520 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3521 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3522 | if( used <= buf_len ) |
| 3523 | { |
| 3524 | memcpy( p, ssl_serialized_context_header, |
| 3525 | sizeof( ssl_serialized_context_header ) ); |
| 3526 | p += sizeof( ssl_serialized_context_header ); |
| 3527 | } |
| 3528 | |
| 3529 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3530 | * Session (length + data) |
| 3531 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3532 | 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] | 3533 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 3534 | return( ret ); |
| 3535 | |
| 3536 | used += 4 + session_len; |
| 3537 | if( used <= buf_len ) |
| 3538 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3539 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 3540 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3541 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3542 | ret = ssl_session_save( ssl->session, 1, |
| 3543 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3544 | if( ret != 0 ) |
| 3545 | return( ret ); |
| 3546 | |
| 3547 | p += session_len; |
| 3548 | } |
| 3549 | |
| 3550 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3551 | * Transform |
| 3552 | */ |
| 3553 | used += sizeof( ssl->transform->randbytes ); |
| 3554 | if( used <= buf_len ) |
| 3555 | { |
| 3556 | memcpy( p, ssl->transform->randbytes, |
| 3557 | sizeof( ssl->transform->randbytes ) ); |
| 3558 | p += sizeof( ssl->transform->randbytes ); |
| 3559 | } |
| 3560 | |
| 3561 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3562 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 3563 | if( used <= buf_len ) |
| 3564 | { |
| 3565 | *p++ = ssl->transform->in_cid_len; |
| 3566 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 3567 | p += ssl->transform->in_cid_len; |
| 3568 | |
| 3569 | *p++ = ssl->transform->out_cid_len; |
| 3570 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 3571 | p += ssl->transform->out_cid_len; |
| 3572 | } |
| 3573 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3574 | |
| 3575 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3576 | * Saved fields from top-level ssl_context structure |
| 3577 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3578 | used += 4; |
| 3579 | if( used <= buf_len ) |
| 3580 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3581 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 3582 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3583 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3584 | |
| 3585 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3586 | used += 16; |
| 3587 | if( used <= buf_len ) |
| 3588 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3589 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 3590 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3591 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3592 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 3593 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3594 | } |
| 3595 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3596 | |
| 3597 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3598 | used += 1; |
| 3599 | if( used <= buf_len ) |
| 3600 | { |
| 3601 | *p++ = ssl->disable_datagram_packing; |
| 3602 | } |
| 3603 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3604 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3605 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3606 | if( used <= buf_len ) |
| 3607 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3608 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 3609 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3613 | used += 2; |
| 3614 | if( used <= buf_len ) |
| 3615 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3616 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 3617 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3618 | } |
| 3619 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3620 | |
| 3621 | #if defined(MBEDTLS_SSL_ALPN) |
| 3622 | { |
| 3623 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 3624 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3625 | : 0; |
| 3626 | |
| 3627 | used += 1 + alpn_len; |
| 3628 | if( used <= buf_len ) |
| 3629 | { |
| 3630 | *p++ = alpn_len; |
| 3631 | |
| 3632 | if( ssl->alpn_chosen != NULL ) |
| 3633 | { |
| 3634 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 3635 | p += alpn_len; |
| 3636 | } |
| 3637 | } |
| 3638 | } |
| 3639 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3640 | |
| 3641 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3642 | * Done |
| 3643 | */ |
| 3644 | *olen = used; |
| 3645 | |
| 3646 | if( used > buf_len ) |
| 3647 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3648 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3649 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 3650 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3651 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3655 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3656 | * |
| 3657 | * This internal version is wrapped by a public function that cleans up in |
| 3658 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3659 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3660 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 3661 | const unsigned char *buf, |
| 3662 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3663 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3664 | const unsigned char *p = buf; |
| 3665 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3666 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3667 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3668 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3669 | /* |
| 3670 | * The context should have been freshly setup or reset. |
| 3671 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 3672 | * (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] | 3673 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 3674 | */ |
| 3675 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 3676 | ssl->session != NULL ) |
| 3677 | { |
| 3678 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3679 | } |
| 3680 | |
| 3681 | /* |
| 3682 | * We can't check that the config matches the initial one, but we can at |
| 3683 | * least check it matches the requirements for serializing. |
| 3684 | */ |
| 3685 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 3686 | ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 || |
| 3687 | ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3688 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3689 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3690 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3691 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3692 | { |
| 3693 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3694 | } |
| 3695 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3696 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 3697 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3698 | /* |
| 3699 | * Check version identifier |
| 3700 | */ |
| 3701 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 3702 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3703 | |
| 3704 | if( memcmp( p, ssl_serialized_context_header, |
| 3705 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 3706 | { |
| 3707 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3708 | } |
| 3709 | p += sizeof( ssl_serialized_context_header ); |
| 3710 | |
| 3711 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3712 | * Session |
| 3713 | */ |
| 3714 | if( (size_t)( end - p ) < 4 ) |
| 3715 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3716 | |
| 3717 | session_len = ( (size_t) p[0] << 24 ) | |
| 3718 | ( (size_t) p[1] << 16 ) | |
| 3719 | ( (size_t) p[2] << 8 ) | |
| 3720 | ( (size_t) p[3] ); |
| 3721 | p += 4; |
| 3722 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3723 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3724 | * 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] | 3725 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3726 | ssl->session_in = ssl->session; |
| 3727 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3728 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3729 | |
| 3730 | if( (size_t)( end - p ) < session_len ) |
| 3731 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3732 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3733 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3734 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3735 | { |
| 3736 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3737 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3738 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3739 | |
| 3740 | p += session_len; |
| 3741 | |
| 3742 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3743 | * Transform |
| 3744 | */ |
| 3745 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3746 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3747 | * 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] | 3748 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3749 | ssl->transform_in = ssl->transform; |
| 3750 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3751 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3752 | |
| 3753 | /* Read random bytes and populate structure */ |
| 3754 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 3755 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3756 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3757 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3758 | ssl->session->ciphersuite, |
| 3759 | ssl->session->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 3760 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3761 | ssl->session->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 3762 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3763 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 3764 | p, /* currently pointing to randbytes */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 3765 | 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] | 3766 | ssl->conf->endpoint, |
| 3767 | ssl ); |
| 3768 | if( ret != 0 ) |
| 3769 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3770 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3771 | p += sizeof( ssl->transform->randbytes ); |
| 3772 | |
| 3773 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3774 | /* Read connection IDs and store them */ |
| 3775 | if( (size_t)( end - p ) < 1 ) |
| 3776 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3777 | |
| 3778 | ssl->transform->in_cid_len = *p++; |
| 3779 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 3780 | 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] | 3781 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3782 | |
| 3783 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 3784 | p += ssl->transform->in_cid_len; |
| 3785 | |
| 3786 | ssl->transform->out_cid_len = *p++; |
| 3787 | |
| 3788 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 3789 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3790 | |
| 3791 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 3792 | p += ssl->transform->out_cid_len; |
| 3793 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3794 | |
| 3795 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3796 | * Saved fields from top-level ssl_context structure |
| 3797 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3798 | if( (size_t)( end - p ) < 4 ) |
| 3799 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3800 | |
| 3801 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 3802 | ( (uint32_t) p[1] << 16 ) | |
| 3803 | ( (uint32_t) p[2] << 8 ) | |
| 3804 | ( (uint32_t) p[3] ); |
| 3805 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3806 | |
| 3807 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3808 | if( (size_t)( end - p ) < 16 ) |
| 3809 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3810 | |
| 3811 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 3812 | ( (uint64_t) p[1] << 48 ) | |
| 3813 | ( (uint64_t) p[2] << 40 ) | |
| 3814 | ( (uint64_t) p[3] << 32 ) | |
| 3815 | ( (uint64_t) p[4] << 24 ) | |
| 3816 | ( (uint64_t) p[5] << 16 ) | |
| 3817 | ( (uint64_t) p[6] << 8 ) | |
| 3818 | ( (uint64_t) p[7] ); |
| 3819 | p += 8; |
| 3820 | |
| 3821 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 3822 | ( (uint64_t) p[1] << 48 ) | |
| 3823 | ( (uint64_t) p[2] << 40 ) | |
| 3824 | ( (uint64_t) p[3] << 32 ) | |
| 3825 | ( (uint64_t) p[4] << 24 ) | |
| 3826 | ( (uint64_t) p[5] << 16 ) | |
| 3827 | ( (uint64_t) p[6] << 8 ) | |
| 3828 | ( (uint64_t) p[7] ); |
| 3829 | p += 8; |
| 3830 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3831 | |
| 3832 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3833 | if( (size_t)( end - p ) < 1 ) |
| 3834 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3835 | |
| 3836 | ssl->disable_datagram_packing = *p++; |
| 3837 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3838 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3839 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3840 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3841 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 3842 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3843 | |
| 3844 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3845 | if( (size_t)( end - p ) < 2 ) |
| 3846 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3847 | |
| 3848 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 3849 | p += 2; |
| 3850 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3851 | |
| 3852 | #if defined(MBEDTLS_SSL_ALPN) |
| 3853 | { |
| 3854 | uint8_t alpn_len; |
| 3855 | const char **cur; |
| 3856 | |
| 3857 | if( (size_t)( end - p ) < 1 ) |
| 3858 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3859 | |
| 3860 | alpn_len = *p++; |
| 3861 | |
| 3862 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 3863 | { |
| 3864 | /* alpn_chosen should point to an item in the configured list */ |
| 3865 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 3866 | { |
| 3867 | if( strlen( *cur ) == alpn_len && |
| 3868 | memcmp( p, cur, alpn_len ) == 0 ) |
| 3869 | { |
| 3870 | ssl->alpn_chosen = *cur; |
| 3871 | break; |
| 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | |
| 3876 | /* can only happen on conf mismatch */ |
| 3877 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 3878 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3879 | |
| 3880 | p += alpn_len; |
| 3881 | } |
| 3882 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3883 | |
| 3884 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3885 | * Forced fields from top-level ssl_context structure |
| 3886 | * |
| 3887 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 3888 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 3889 | */ |
| 3890 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 3891 | ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3892 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3893 | /* Adjust pointers for header fields of outgoing records to |
| 3894 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3895 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3896 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3897 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3898 | ssl->in_epoch = 1; |
| 3899 | #endif |
| 3900 | |
| 3901 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 3902 | * 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] | 3903 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 3904 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3905 | if( ssl->handshake != NULL ) |
| 3906 | { |
| 3907 | mbedtls_ssl_handshake_free( ssl ); |
| 3908 | mbedtls_free( ssl->handshake ); |
| 3909 | ssl->handshake = NULL; |
| 3910 | } |
| 3911 | |
| 3912 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3913 | * Done - should have consumed entire buffer |
| 3914 | */ |
| 3915 | if( p != end ) |
| 3916 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3917 | |
| 3918 | return( 0 ); |
| 3919 | } |
| 3920 | |
| 3921 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3922 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3923 | */ |
| 3924 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 3925 | const unsigned char *buf, |
| 3926 | size_t len ) |
| 3927 | { |
| 3928 | int ret = ssl_context_load( context, buf, len ); |
| 3929 | |
| 3930 | if( ret != 0 ) |
| 3931 | mbedtls_ssl_free( context ); |
| 3932 | |
| 3933 | return( ret ); |
| 3934 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3935 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3936 | |
| 3937 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3938 | * Free an SSL context |
| 3939 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3940 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3941 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3942 | if( ssl == NULL ) |
| 3943 | return; |
| 3944 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3945 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3946 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3947 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3948 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3949 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3950 | size_t out_buf_len = ssl->out_buf_len; |
| 3951 | #else |
| 3952 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3953 | #endif |
| 3954 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3955 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3956 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3957 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3960 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3961 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3962 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3963 | size_t in_buf_len = ssl->in_buf_len; |
| 3964 | #else |
| 3965 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3966 | #endif |
| 3967 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3968 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3969 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3970 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3973 | if( ssl->transform ) |
| 3974 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3975 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3976 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
| 3979 | if( ssl->handshake ) |
| 3980 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3981 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3982 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 3983 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3984 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3985 | mbedtls_free( ssl->handshake ); |
| 3986 | mbedtls_free( ssl->transform_negotiate ); |
| 3987 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3990 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3991 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 3992 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3993 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3994 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3995 | if( ssl->session ) |
| 3996 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3997 | mbedtls_ssl_session_free( ssl->session ); |
| 3998 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3999 | } |
| 4000 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 4001 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4002 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4003 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4004 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4005 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4006 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 4007 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4008 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4009 | #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] | 4010 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 4011 | #endif |
| 4012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4013 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 4014 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 4015 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4016 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4019 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 4020 | * Initialize mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4021 | */ |
| 4022 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 4023 | { |
| 4024 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 4025 | } |
| 4026 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4027 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4028 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4029 | /* 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] | 4030 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 4031 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4032 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 4033 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4034 | static int ssl_preset_default_hashes[] = { |
| 4035 | #if defined(MBEDTLS_SHA512_C) |
| 4036 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4037 | #endif |
| 4038 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4039 | MBEDTLS_MD_SHA384, |
| 4040 | #endif |
| 4041 | #if defined(MBEDTLS_SHA256_C) |
| 4042 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4043 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4044 | MBEDTLS_MD_NONE |
| 4045 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4046 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 4047 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4048 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4049 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 4050 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4051 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4052 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4053 | * about this list. |
| 4054 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4055 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4056 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4057 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4058 | #endif |
| 4059 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4060 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4061 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4062 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4063 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4064 | #endif |
| 4065 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4066 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4067 | #endif |
| 4068 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4069 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4070 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4071 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4072 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4073 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4074 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4075 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4076 | #endif |
| 4077 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4078 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 4079 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4080 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4081 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 4082 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4083 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 4084 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 4085 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 4086 | 0 |
| 4087 | }; |
| 4088 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4089 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4090 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4091 | static int ssl_preset_suiteb_hashes[] = { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4092 | #if defined(MBEDTLS_SHA256_C) |
| 4093 | MBEDTLS_MD_SHA256, |
| 4094 | #endif |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4095 | #if defined(MBEDTLS_SHA384_C) |
| 4096 | MBEDTLS_MD_SHA384, |
| 4097 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4098 | MBEDTLS_MD_NONE |
| 4099 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4100 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4101 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4102 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 4103 | * 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] | 4104 | * rules SHOULD be upheld. |
| 4105 | * - No duplicate entries. |
| 4106 | * - But if there is a good reason, do not change the order of the algorithms. |
| 4107 | * - ssl_tls12_present* is for TLS 1.2 use only. |
| 4108 | * - 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] | 4109 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4110 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4111 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4112 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 4113 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4114 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 4115 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 4116 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4117 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4118 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 4119 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4120 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 4121 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 4122 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 4123 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 4124 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ |
| 4125 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 4126 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
| 4127 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 4128 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4129 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4130 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 4131 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4132 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
| 4133 | |
Ronald Cron | 8540cf6 | 2022-03-16 08:01:09 +0100 | [diff] [blame] | 4134 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA512_C) |
| 4135 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512, |
| 4136 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */ |
| 4137 | |
| 4138 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA384_C) |
| 4139 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384, |
| 4140 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */ |
| 4141 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4142 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 4143 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4144 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4145 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4146 | MBEDTLS_TLS1_3_SIG_NONE |
| 4147 | }; |
| 4148 | |
| 4149 | /* NOTICE: see above */ |
| 4150 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4151 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4152 | #if defined(MBEDTLS_SHA512_C) |
| 4153 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) |
| 4154 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 4155 | #if defined(MBEDTLS_SHA384_C) |
| 4156 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 4157 | #endif |
| 4158 | #if defined(MBEDTLS_SHA256_C) |
| 4159 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 4160 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4161 | MBEDTLS_TLS1_3_SIG_NONE |
| 4162 | }; |
| 4163 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4164 | /* NOTICE: see above */ |
| 4165 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 4166 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4167 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 4168 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 4169 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 4170 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 4171 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 4172 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4173 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 4174 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 4175 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 4176 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 4177 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4178 | |
| 4179 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 4180 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 4181 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4182 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 4183 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 4184 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 4185 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 4186 | |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4187 | MBEDTLS_TLS1_3_SIG_NONE |
| 4188 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4189 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4190 | /* NOTICE: see above */ |
| 4191 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4192 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 4193 | #if defined(MBEDTLS_SHA256_C) |
| 4194 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 4195 | #endif |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 4196 | #if defined(MBEDTLS_SHA384_C) |
| 4197 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 4198 | #endif |
Xiaofei Bai | 9539501 | 2021-11-25 08:51:30 +0000 | [diff] [blame] | 4199 | MBEDTLS_TLS1_3_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 4200 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4201 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4202 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4203 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4204 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4205 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4206 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4207 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4208 | #endif |
| 4209 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4210 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 4211 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4212 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4213 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4214 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4215 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4216 | /* 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] | 4217 | * to make sure there are no duplicated signature algorithm entries. */ |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4218 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4219 | { |
| 4220 | size_t i, j; |
| 4221 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4222 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4223 | for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4224 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4225 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4226 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4227 | if( sig_algs[i] != sig_algs[j] ) |
| 4228 | continue; |
| 4229 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4230 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4231 | sig_algs[i], j, i ); |
| 4232 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4233 | } |
| 4234 | } |
| 4235 | return( ret ); |
| 4236 | } |
| 4237 | |
| 4238 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4239 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4240 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4241 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4242 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4243 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4244 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4245 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4246 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4247 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4248 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4249 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4250 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4251 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4252 | { |
| 4253 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4254 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4255 | } |
| 4256 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4257 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4258 | { |
| 4259 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4260 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4261 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4262 | |
| 4263 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4264 | 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] | 4265 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4266 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4267 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4268 | } |
| 4269 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4270 | 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] | 4271 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4272 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4273 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4274 | } |
| 4275 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4276 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4277 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4278 | /* Use the functions here so that they are covered in tests, |
| 4279 | * but otherwise access member directly for efficiency */ |
| 4280 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4281 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4282 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4283 | /* |
| 4284 | * Things that are common to all presets |
| 4285 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4286 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4287 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4288 | { |
| 4289 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4290 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4291 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4292 | #endif |
| 4293 | } |
| 4294 | #endif |
| 4295 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4296 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4297 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4298 | #endif |
| 4299 | |
| 4300 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4301 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4302 | #endif |
| 4303 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4304 | #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] | 4305 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4306 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4307 | #endif |
| 4308 | |
| 4309 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4310 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4311 | #endif |
| 4312 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4313 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4314 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4315 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4316 | #endif |
| 4317 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4318 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4319 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4320 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4321 | #endif |
| 4322 | |
| 4323 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4324 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4325 | memset( conf->renego_period, 0x00, 2 ); |
| 4326 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4327 | #endif |
| 4328 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4329 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4330 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4331 | { |
| 4332 | const unsigned char dhm_p[] = |
| 4333 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4334 | const unsigned char dhm_g[] = |
| 4335 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4336 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4337 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4338 | dhm_p, sizeof( dhm_p ), |
| 4339 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4340 | { |
| 4341 | return( ret ); |
| 4342 | } |
| 4343 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4344 | #endif |
| 4345 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4346 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4347 | /* |
| 4348 | * Allow all TLS 1.3 key exchange modes by default. |
| 4349 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4350 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4351 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4352 | |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4353 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4354 | { |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4355 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4356 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4357 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4358 | #else |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4359 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 4360 | #endif |
XiaokangQian | 4d3a604 | 2022-04-21 13:46:17 +0000 | [diff] [blame] | 4361 | } |
| 4362 | else |
| 4363 | { |
| 4364 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4365 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4366 | { |
| 4367 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4368 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4369 | } |
| 4370 | else |
| 4371 | /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */ |
| 4372 | { |
| 4373 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4374 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4375 | } |
| 4376 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4377 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4378 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 4379 | #elif defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4380 | conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4381 | conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 4382 | #else |
| 4383 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4384 | #endif |
| 4385 | } |
Glenn Strauss | 2dfcea2 | 2022-03-14 17:26:42 -0400 | [diff] [blame] | 4386 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4387 | /* |
| 4388 | * Preset-specific defaults |
| 4389 | */ |
| 4390 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4391 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4392 | /* |
| 4393 | * NSA Suite B |
| 4394 | */ |
| 4395 | case MBEDTLS_SSL_PRESET_SUITEB: |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4396 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4397 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4398 | |
| 4399 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4400 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4401 | #endif |
| 4402 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4403 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4404 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4405 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4406 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4407 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4408 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4409 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4410 | else |
| 4411 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4412 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4413 | #endif |
| 4414 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4415 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4416 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4417 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4418 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4419 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4420 | |
| 4421 | /* |
| 4422 | * Default |
| 4423 | */ |
| 4424 | default: |
Ronald Cron | f660655 | 2022-03-15 11:23:25 +0100 | [diff] [blame] | 4425 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4426 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4427 | |
| 4428 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4429 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4430 | #endif |
| 4431 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4432 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4433 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4434 | conf->sig_hashes = ssl_preset_default_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4435 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4436 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4437 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4438 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4439 | else |
| 4440 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4441 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4442 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4443 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4444 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4445 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4446 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4447 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4448 | |
| 4449 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4450 | conf->dhm_min_bitlen = 1024; |
| 4451 | #endif |
| 4452 | } |
| 4453 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4454 | return( 0 ); |
| 4455 | } |
| 4456 | |
| 4457 | /* |
| 4458 | * Free mbedtls_ssl_config |
| 4459 | */ |
| 4460 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4461 | { |
| 4462 | #if defined(MBEDTLS_DHM_C) |
| 4463 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4464 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4465 | #endif |
| 4466 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4467 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4468 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4469 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 4470 | { |
Neil Armstrong | 501c932 | 2022-05-03 09:35:09 +0200 | [diff] [blame] | 4471 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
| 4472 | } |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4473 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4474 | if( conf->psk != NULL ) |
| 4475 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4476 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4477 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4478 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4479 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | if( conf->psk_identity != NULL ) |
| 4483 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4484 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4485 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4486 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4487 | conf->psk_identity_len = 0; |
| 4488 | } |
| 4489 | #endif |
| 4490 | |
| 4491 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4492 | ssl_key_cert_free( conf->key_cert ); |
| 4493 | #endif |
| 4494 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4495 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4496 | } |
| 4497 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4498 | #if defined(MBEDTLS_PK_C) && \ |
| 4499 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4500 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4501 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4502 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4503 | 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] | 4504 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4505 | #if defined(MBEDTLS_RSA_C) |
| 4506 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 4507 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4508 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4509 | #if defined(MBEDTLS_ECDSA_C) |
| 4510 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 4511 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4512 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4513 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4514 | } |
| 4515 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4516 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 4517 | { |
| 4518 | switch( type ) { |
| 4519 | case MBEDTLS_PK_RSA: |
| 4520 | return( MBEDTLS_SSL_SIG_RSA ); |
| 4521 | case MBEDTLS_PK_ECDSA: |
| 4522 | case MBEDTLS_PK_ECKEY: |
| 4523 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 4524 | default: |
| 4525 | return( MBEDTLS_SSL_SIG_ANON ); |
| 4526 | } |
| 4527 | } |
| 4528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4529 | 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] | 4530 | { |
| 4531 | switch( sig ) |
| 4532 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4533 | #if defined(MBEDTLS_RSA_C) |
| 4534 | case MBEDTLS_SSL_SIG_RSA: |
| 4535 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4536 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4537 | #if defined(MBEDTLS_ECDSA_C) |
| 4538 | case MBEDTLS_SSL_SIG_ECDSA: |
| 4539 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4540 | #endif |
| 4541 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4542 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4543 | } |
| 4544 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4545 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4546 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4547 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4548 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4549 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4550 | 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] | 4551 | { |
| 4552 | switch( hash ) |
| 4553 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4554 | #if defined(MBEDTLS_MD5_C) |
| 4555 | case MBEDTLS_SSL_HASH_MD5: |
| 4556 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4557 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4558 | #if defined(MBEDTLS_SHA1_C) |
| 4559 | case MBEDTLS_SSL_HASH_SHA1: |
| 4560 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4561 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4562 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4563 | case MBEDTLS_SSL_HASH_SHA224: |
| 4564 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4565 | #endif |
| 4566 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4567 | case MBEDTLS_SSL_HASH_SHA256: |
| 4568 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4569 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4570 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4571 | case MBEDTLS_SSL_HASH_SHA384: |
| 4572 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4573 | #endif |
| 4574 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4575 | case MBEDTLS_SSL_HASH_SHA512: |
| 4576 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4577 | #endif |
| 4578 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4579 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4580 | } |
| 4581 | } |
| 4582 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4583 | /* |
| 4584 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 4585 | */ |
| 4586 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 4587 | { |
| 4588 | switch( md ) |
| 4589 | { |
| 4590 | #if defined(MBEDTLS_MD5_C) |
| 4591 | case MBEDTLS_MD_MD5: |
| 4592 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 4593 | #endif |
| 4594 | #if defined(MBEDTLS_SHA1_C) |
| 4595 | case MBEDTLS_MD_SHA1: |
| 4596 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 4597 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4598 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4599 | case MBEDTLS_MD_SHA224: |
| 4600 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4601 | #endif |
| 4602 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4603 | case MBEDTLS_MD_SHA256: |
| 4604 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 4605 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4606 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4607 | case MBEDTLS_MD_SHA384: |
| 4608 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4609 | #endif |
| 4610 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4611 | case MBEDTLS_MD_SHA512: |
| 4612 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 4613 | #endif |
| 4614 | default: |
| 4615 | return( MBEDTLS_SSL_HASH_NONE ); |
| 4616 | } |
| 4617 | } |
| 4618 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4619 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4620 | * 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] | 4621 | * 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] | 4622 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4623 | 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] | 4624 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4625 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4626 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4627 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4628 | return( -1 ); |
| 4629 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4630 | for( ; *group_list != 0; group_list++ ) |
| 4631 | { |
| 4632 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4633 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4634 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4635 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4636 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4637 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4638 | |
| 4639 | #if defined(MBEDTLS_ECP_C) |
| 4640 | /* |
| 4641 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 4642 | */ |
| 4643 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 4644 | { |
Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 4645 | 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] | 4646 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 4647 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4648 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4650 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4651 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 4652 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4653 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4654 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4655 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4656 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4657 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4658 | const char *ext_oid; |
| 4659 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4661 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4662 | { |
| 4663 | /* Server part of the key exchange */ |
| 4664 | switch( ciphersuite->key_exchange ) |
| 4665 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4666 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 4667 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4668 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4669 | break; |
| 4670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4671 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 4672 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 4673 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 4674 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4675 | break; |
| 4676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4677 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 4678 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4679 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4680 | break; |
| 4681 | |
| 4682 | /* 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] | 4683 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 4684 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 4685 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 4686 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 4687 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4688 | usage = 0; |
| 4689 | } |
| 4690 | } |
| 4691 | else |
| 4692 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4693 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 4694 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4695 | } |
| 4696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4697 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4698 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4699 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4700 | ret = -1; |
| 4701 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4703 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4704 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4705 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 4706 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4707 | } |
| 4708 | else |
| 4709 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4710 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 4711 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4712 | } |
| 4713 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4714 | 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] | 4715 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4716 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4717 | ret = -1; |
| 4718 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4719 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4720 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4721 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4722 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 4723 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4724 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4725 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 4726 | const mbedtls_md_type_t md, |
| 4727 | unsigned char *dst, |
| 4728 | size_t dst_len, |
| 4729 | size_t *olen ) |
| 4730 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4731 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4732 | psa_hash_operation_t *hash_operation_to_clone; |
| 4733 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 4734 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4735 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4736 | |
| 4737 | switch( md ) |
| 4738 | { |
| 4739 | #if defined(MBEDTLS_SHA384_C) |
| 4740 | case MBEDTLS_MD_SHA384: |
| 4741 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 4742 | break; |
| 4743 | #endif |
| 4744 | |
| 4745 | #if defined(MBEDTLS_SHA256_C) |
| 4746 | case MBEDTLS_MD_SHA256: |
| 4747 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 4748 | break; |
| 4749 | #endif |
| 4750 | |
| 4751 | default: |
| 4752 | goto exit; |
| 4753 | } |
| 4754 | |
| 4755 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 4756 | if( status != PSA_SUCCESS ) |
| 4757 | goto exit; |
| 4758 | |
| 4759 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 4760 | if( status != PSA_SUCCESS ) |
| 4761 | goto exit; |
| 4762 | |
| 4763 | exit: |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 4764 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4765 | } |
| 4766 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4767 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4768 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4769 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 4770 | unsigned char *dst, |
| 4771 | size_t dst_len, |
| 4772 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4773 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4774 | int ret; |
| 4775 | mbedtls_sha512_context sha512; |
| 4776 | |
| 4777 | if( dst_len < 48 ) |
| 4778 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4779 | |
| 4780 | mbedtls_sha512_init( &sha512 ); |
| 4781 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 4782 | |
| 4783 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 4784 | { |
| 4785 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 4786 | goto exit; |
| 4787 | } |
| 4788 | |
| 4789 | *olen = 48; |
| 4790 | |
| 4791 | exit: |
| 4792 | |
| 4793 | mbedtls_sha512_free( &sha512 ); |
| 4794 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4795 | } |
| 4796 | #endif /* MBEDTLS_SHA384_C */ |
| 4797 | |
| 4798 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4799 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 4800 | unsigned char *dst, |
| 4801 | size_t dst_len, |
| 4802 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4803 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4804 | int ret; |
| 4805 | mbedtls_sha256_context sha256; |
| 4806 | |
| 4807 | if( dst_len < 32 ) |
| 4808 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4809 | |
| 4810 | mbedtls_sha256_init( &sha256 ); |
| 4811 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4812 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4813 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 4814 | { |
| 4815 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 4816 | goto exit; |
| 4817 | } |
| 4818 | |
| 4819 | *olen = 32; |
| 4820 | |
| 4821 | exit: |
| 4822 | |
| 4823 | mbedtls_sha256_free( &sha256 ); |
| 4824 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4825 | } |
| 4826 | #endif /* MBEDTLS_SHA256_C */ |
| 4827 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4828 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 4829 | const mbedtls_md_type_t md, |
| 4830 | unsigned char *dst, |
| 4831 | size_t dst_len, |
| 4832 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4833 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4834 | switch( md ) |
| 4835 | { |
| 4836 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4837 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4838 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4839 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4840 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4841 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4842 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4843 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4844 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4845 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4846 | |
| 4847 | default: |
| 4848 | break; |
| 4849 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4850 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4851 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 4852 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4853 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4854 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4855 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4856 | |
| 4857 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4858 | |
| 4859 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 4860 | mbedtls_svc_key_id_t key, |
| 4861 | psa_algorithm_t alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4862 | const unsigned char* raw_psk, size_t raw_psk_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4863 | const unsigned char* seed, size_t seed_length, |
| 4864 | const unsigned char* label, size_t label_length, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 4865 | const unsigned char* other_secret, |
| 4866 | size_t other_secret_length, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4867 | size_t capacity ) |
| 4868 | { |
| 4869 | psa_status_t status; |
| 4870 | |
| 4871 | status = psa_key_derivation_setup( derivation, alg ); |
| 4872 | if( status != PSA_SUCCESS ) |
| 4873 | return( status ); |
| 4874 | |
| 4875 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 4876 | { |
| 4877 | status = psa_key_derivation_input_bytes( derivation, |
| 4878 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 4879 | seed, seed_length ); |
| 4880 | if( status != PSA_SUCCESS ) |
| 4881 | return( status ); |
| 4882 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 4883 | if ( other_secret != NULL ) |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 4884 | { |
| 4885 | status = psa_key_derivation_input_bytes( derivation, |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 4886 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 4887 | other_secret, other_secret_length ); |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 4888 | if( status != PSA_SUCCESS ) |
| 4889 | return( status ); |
| 4890 | } |
| 4891 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4892 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 4893 | { |
| 4894 | status = psa_key_derivation_input_bytes( |
| 4895 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4896 | raw_psk, raw_psk_length ); |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4897 | } |
| 4898 | else |
| 4899 | { |
| 4900 | status = psa_key_derivation_input_key( |
| 4901 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 4902 | } |
| 4903 | if( status != PSA_SUCCESS ) |
| 4904 | return( status ); |
| 4905 | |
| 4906 | status = psa_key_derivation_input_bytes( derivation, |
| 4907 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 4908 | label, label_length ); |
| 4909 | if( status != PSA_SUCCESS ) |
| 4910 | return( status ); |
| 4911 | } |
| 4912 | else |
| 4913 | { |
| 4914 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 4915 | } |
| 4916 | |
| 4917 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 4918 | if( status != PSA_SUCCESS ) |
| 4919 | return( status ); |
| 4920 | |
| 4921 | return( PSA_SUCCESS ); |
| 4922 | } |
| 4923 | |
| 4924 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 4925 | const unsigned char *secret, size_t slen, |
| 4926 | const char *label, |
| 4927 | const unsigned char *random, size_t rlen, |
| 4928 | unsigned char *dstbuf, size_t dlen ) |
| 4929 | { |
| 4930 | psa_status_t status; |
| 4931 | psa_algorithm_t alg; |
| 4932 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4933 | psa_key_derivation_operation_t derivation = |
| 4934 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 4935 | |
| 4936 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 4937 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 4938 | else |
| 4939 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 4940 | |
| 4941 | /* Normally a "secret" should be long enough to be impossible to |
| 4942 | * find by brute force, and in particular should not be empty. But |
| 4943 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 4944 | * and for this use case it makes sense to have a 0-length "secret". |
| 4945 | * Since the key API doesn't allow importing a key of length 0, |
| 4946 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 4947 | * to mean a 0-length "secret" input. */ |
| 4948 | if( slen != 0 ) |
| 4949 | { |
| 4950 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 4951 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 4952 | psa_set_key_algorithm( &key_attributes, alg ); |
| 4953 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 4954 | |
| 4955 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 4956 | if( status != PSA_SUCCESS ) |
| 4957 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4958 | } |
| 4959 | |
| 4960 | status = setup_psa_key_derivation( &derivation, |
| 4961 | master_key, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 4962 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4963 | random, rlen, |
| 4964 | (unsigned char const *) label, |
| 4965 | (size_t) strlen( label ), |
Przemek Stekiel | 1f02703 | 2022-04-05 17:12:11 +0200 | [diff] [blame] | 4966 | NULL, 0, |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4967 | dlen ); |
| 4968 | if( status != PSA_SUCCESS ) |
| 4969 | { |
| 4970 | psa_key_derivation_abort( &derivation ); |
| 4971 | psa_destroy_key( master_key ); |
| 4972 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4973 | } |
| 4974 | |
| 4975 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 4976 | if( status != PSA_SUCCESS ) |
| 4977 | { |
| 4978 | psa_key_derivation_abort( &derivation ); |
| 4979 | psa_destroy_key( master_key ); |
| 4980 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4981 | } |
| 4982 | |
| 4983 | status = psa_key_derivation_abort( &derivation ); |
| 4984 | if( status != PSA_SUCCESS ) |
| 4985 | { |
| 4986 | psa_destroy_key( master_key ); |
| 4987 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4988 | } |
| 4989 | |
| 4990 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 4991 | status = psa_destroy_key( master_key ); |
| 4992 | if( status != PSA_SUCCESS ) |
| 4993 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 4994 | |
| 4995 | return( 0 ); |
| 4996 | } |
| 4997 | |
| 4998 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4999 | |
| 5000 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5001 | const unsigned char *secret, size_t slen, |
| 5002 | const char *label, |
| 5003 | const unsigned char *random, size_t rlen, |
| 5004 | unsigned char *dstbuf, size_t dlen ) |
| 5005 | { |
| 5006 | size_t nb; |
| 5007 | size_t i, j, k, md_len; |
| 5008 | unsigned char *tmp; |
| 5009 | size_t tmp_len = 0; |
| 5010 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5011 | const mbedtls_md_info_t *md_info; |
| 5012 | mbedtls_md_context_t md_ctx; |
| 5013 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5014 | |
| 5015 | mbedtls_md_init( &md_ctx ); |
| 5016 | |
| 5017 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5018 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5019 | |
| 5020 | md_len = mbedtls_md_get_size( md_info ); |
| 5021 | |
| 5022 | tmp_len = md_len + strlen( label ) + rlen; |
| 5023 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5024 | if( tmp == NULL ) |
| 5025 | { |
| 5026 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5027 | goto exit; |
| 5028 | } |
| 5029 | |
| 5030 | nb = strlen( label ); |
| 5031 | memcpy( tmp + md_len, label, nb ); |
| 5032 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5033 | nb += rlen; |
| 5034 | |
| 5035 | /* |
| 5036 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5037 | */ |
| 5038 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5039 | goto exit; |
| 5040 | |
| 5041 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5042 | if( ret != 0 ) |
| 5043 | goto exit; |
| 5044 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5045 | if( ret != 0 ) |
| 5046 | goto exit; |
| 5047 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5048 | if( ret != 0 ) |
| 5049 | goto exit; |
| 5050 | |
| 5051 | for( i = 0; i < dlen; i += md_len ) |
| 5052 | { |
| 5053 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5054 | if( ret != 0 ) |
| 5055 | goto exit; |
| 5056 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5057 | if( ret != 0 ) |
| 5058 | goto exit; |
| 5059 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5060 | if( ret != 0 ) |
| 5061 | goto exit; |
| 5062 | |
| 5063 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5064 | if( ret != 0 ) |
| 5065 | goto exit; |
| 5066 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5067 | if( ret != 0 ) |
| 5068 | goto exit; |
| 5069 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5070 | if( ret != 0 ) |
| 5071 | goto exit; |
| 5072 | |
| 5073 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5074 | |
| 5075 | for( j = 0; j < k; j++ ) |
| 5076 | dstbuf[i + j] = h_i[j]; |
| 5077 | } |
| 5078 | |
| 5079 | exit: |
| 5080 | mbedtls_md_free( &md_ctx ); |
| 5081 | |
| 5082 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5083 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5084 | |
| 5085 | mbedtls_free( tmp ); |
| 5086 | |
| 5087 | return( ret ); |
| 5088 | } |
| 5089 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5090 | |
| 5091 | #if defined(MBEDTLS_SHA256_C) |
| 5092 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5093 | const char *label, |
| 5094 | const unsigned char *random, size_t rlen, |
| 5095 | unsigned char *dstbuf, size_t dlen ) |
| 5096 | { |
| 5097 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5098 | label, random, rlen, dstbuf, dlen ) ); |
| 5099 | } |
| 5100 | #endif /* MBEDTLS_SHA256_C */ |
| 5101 | |
| 5102 | #if defined(MBEDTLS_SHA384_C) |
| 5103 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5104 | const char *label, |
| 5105 | const unsigned char *random, size_t rlen, |
| 5106 | unsigned char *dstbuf, size_t dlen ) |
| 5107 | { |
| 5108 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5109 | label, random, rlen, dstbuf, dlen ) ); |
| 5110 | } |
| 5111 | #endif /* MBEDTLS_SHA384_C */ |
| 5112 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5113 | /* |
| 5114 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5115 | * |
| 5116 | * Inputs: |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5117 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5118 | * |
| 5119 | * Outputs: |
| 5120 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5121 | */ |
| 5122 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5123 | mbedtls_md_type_t hash ) |
| 5124 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5125 | #if defined(MBEDTLS_SHA384_C) |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5126 | if( hash == MBEDTLS_MD_SHA384 ) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5127 | { |
| 5128 | handshake->tls_prf = tls_prf_sha384; |
| 5129 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5130 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5131 | } |
| 5132 | else |
| 5133 | #endif |
| 5134 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5135 | { |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5136 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5137 | handshake->tls_prf = tls_prf_sha256; |
| 5138 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5139 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5140 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5141 | #else |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5142 | { |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5143 | (void) handshake; |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5144 | (void) hash; |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5145 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5146 | } |
Ronald Cron | 81591aa | 2022-03-07 09:05:51 +0100 | [diff] [blame] | 5147 | #endif |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5148 | |
| 5149 | return( 0 ); |
| 5150 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5151 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5152 | /* |
| 5153 | * Compute master secret if needed |
| 5154 | * |
| 5155 | * Parameters: |
| 5156 | * [in/out] handshake |
| 5157 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5158 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5159 | * [out] premaster (cleared) |
| 5160 | * [out] master |
| 5161 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5162 | * debug: conf->f_dbg, conf->p_dbg |
| 5163 | * EMS: passed to calc_verify (debug + session_negotiate) |
Ronald Cron | a25cf58 | 2022-03-07 11:10:36 +0100 | [diff] [blame] | 5164 | * PSA-PSA: conf |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5165 | */ |
| 5166 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5167 | unsigned char *master, |
| 5168 | const mbedtls_ssl_context *ssl ) |
| 5169 | { |
| 5170 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5171 | |
| 5172 | /* cf. RFC 5246, Section 8.1: |
| 5173 | * "The master secret is always exactly 48 bytes in length." */ |
| 5174 | size_t const master_secret_len = 48; |
| 5175 | |
| 5176 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5177 | unsigned char session_hash[48]; |
| 5178 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5179 | |
| 5180 | /* The label for the KDF used for key expansion. |
| 5181 | * This is either "master secret" or "extended master secret" |
| 5182 | * depending on whether the Extended Master Secret extension |
| 5183 | * is used. */ |
| 5184 | char const *lbl = "master secret"; |
| 5185 | |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5186 | /* The seed for the KDF used for key expansion. |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5187 | * - If the Extended Master Secret extension is not used, |
| 5188 | * this is ClientHello.Random + ServerHello.Random |
| 5189 | * (see Sect. 8.1 in RFC 5246). |
| 5190 | * - If the Extended Master Secret extension is used, |
| 5191 | * this is the transcript of the handshake so far. |
| 5192 | * (see Sect. 4 in RFC 7627). */ |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5193 | unsigned char const *seed = handshake->randbytes; |
| 5194 | size_t seed_len = 64; |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5195 | |
| 5196 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5197 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5198 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5199 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5200 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5201 | (void) ssl; |
| 5202 | #endif |
| 5203 | |
| 5204 | if( handshake->resume != 0 ) |
| 5205 | { |
| 5206 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5207 | return( 0 ); |
| 5208 | } |
| 5209 | |
| 5210 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5211 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5212 | { |
| 5213 | lbl = "extended master secret"; |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5214 | seed = session_hash; |
| 5215 | handshake->calc_verify( ssl, session_hash, &seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5216 | |
| 5217 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5218 | session_hash, seed_len ); |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5219 | } |
Przemek Stekiel | 169bf0b | 2022-04-29 07:53:29 +0200 | [diff] [blame] | 5220 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5221 | |
Przemek Stekiel | 99114f3 | 2022-04-22 11:20:09 +0200 | [diff] [blame] | 5222 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Przemek Stekiel | 8a4b7fd | 2022-04-28 09:22:22 +0200 | [diff] [blame] | 5223 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Neil Armstrong | e952a30 | 2022-05-03 10:22:14 +0200 | [diff] [blame] | 5224 | if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 ) |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5225 | { |
| 5226 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5227 | psa_status_t status; |
| 5228 | psa_algorithm_t alg; |
| 5229 | mbedtls_svc_key_id_t psk; |
| 5230 | psa_key_derivation_operation_t derivation = |
| 5231 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5232 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5233 | |
| 5234 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5235 | |
| 5236 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5237 | |
| 5238 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5239 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5240 | else |
| 5241 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5242 | |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5243 | size_t other_secret_len = 0; |
| 5244 | unsigned char* other_secret = NULL; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5245 | |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5246 | switch( handshake->ciphersuite_info->key_exchange ) |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5247 | { |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5248 | /* Provide other secret. |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5249 | * Other secret is stored in premaster, where first 2 bytes hold the |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5250 | * length of the other key. |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5251 | */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5252 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5253 | /* For RSA-PSK other key length is always 48 bytes. */ |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5254 | other_secret_len = 48; |
| 5255 | other_secret = handshake->premaster + 2; |
| 5256 | break; |
| 5257 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5258 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
Przemek Stekiel | 19b80f8 | 2022-04-14 08:29:31 +0200 | [diff] [blame] | 5259 | other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0); |
| 5260 | other_secret = handshake->premaster + 2; |
| 5261 | break; |
| 5262 | default: |
| 5263 | break; |
Przemek Stekiel | c203340 | 2022-04-05 17:19:41 +0200 | [diff] [blame] | 5264 | } |
| 5265 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5266 | status = setup_psa_key_derivation( &derivation, psk, alg, |
Neil Armstrong | 8ecd668 | 2022-05-05 11:40:35 +0200 | [diff] [blame] | 5267 | ssl->conf->psk, ssl->conf->psk_len, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5268 | seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5269 | (unsigned char const *) lbl, |
| 5270 | (size_t) strlen( lbl ), |
Przemek Stekiel | 51a1f36 | 2022-04-13 08:57:06 +0200 | [diff] [blame] | 5271 | other_secret, other_secret_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5272 | master_secret_len ); |
| 5273 | if( status != PSA_SUCCESS ) |
| 5274 | { |
| 5275 | psa_key_derivation_abort( &derivation ); |
| 5276 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5277 | } |
| 5278 | |
| 5279 | status = psa_key_derivation_output_bytes( &derivation, |
| 5280 | master, |
| 5281 | master_secret_len ); |
| 5282 | if( status != PSA_SUCCESS ) |
| 5283 | { |
| 5284 | psa_key_derivation_abort( &derivation ); |
| 5285 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5286 | } |
| 5287 | |
| 5288 | status = psa_key_derivation_abort( &derivation ); |
| 5289 | if( status != PSA_SUCCESS ) |
| 5290 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5291 | } |
| 5292 | else |
| 5293 | #endif |
| 5294 | { |
| 5295 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
Przemek Stekiel | ae4ed30 | 2022-04-05 17:15:55 +0200 | [diff] [blame] | 5296 | lbl, seed, seed_len, |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5297 | master, |
| 5298 | master_secret_len ); |
| 5299 | if( ret != 0 ) |
| 5300 | { |
| 5301 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5302 | return( ret ); |
| 5303 | } |
| 5304 | |
| 5305 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5306 | handshake->premaster, |
| 5307 | handshake->pmslen ); |
| 5308 | |
| 5309 | mbedtls_platform_zeroize( handshake->premaster, |
| 5310 | sizeof(handshake->premaster) ); |
| 5311 | } |
| 5312 | |
| 5313 | return( 0 ); |
| 5314 | } |
| 5315 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5316 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5317 | { |
| 5318 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5319 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5320 | ssl->handshake->ciphersuite_info; |
| 5321 | |
| 5322 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5323 | |
| 5324 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5325 | ret = ssl_set_handshake_prfs( ssl->handshake, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5326 | ciphersuite_info->mac ); |
| 5327 | if( ret != 0 ) |
| 5328 | { |
| 5329 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5330 | return( ret ); |
| 5331 | } |
| 5332 | |
| 5333 | /* Compute master secret if needed */ |
| 5334 | ret = ssl_compute_master( ssl->handshake, |
| 5335 | ssl->session_negotiate->master, |
| 5336 | ssl ); |
| 5337 | if( ret != 0 ) |
| 5338 | { |
| 5339 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5340 | return( ret ); |
| 5341 | } |
| 5342 | |
| 5343 | /* Swap the client and server random values: |
| 5344 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 5345 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 5346 | { |
| 5347 | unsigned char tmp[64]; |
| 5348 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 5349 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 5350 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 5351 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 5352 | } |
| 5353 | |
| 5354 | /* Populate transform structure */ |
| 5355 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 5356 | ssl->session_negotiate->ciphersuite, |
| 5357 | ssl->session_negotiate->master, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5358 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5359 | ssl->session_negotiate->encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 5360 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5361 | ssl->handshake->tls_prf, |
| 5362 | ssl->handshake->randbytes, |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 5363 | ssl->tls_version, |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5364 | ssl->conf->endpoint, |
| 5365 | ssl ); |
| 5366 | if( ret != 0 ) |
| 5367 | { |
| 5368 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 5369 | return( ret ); |
| 5370 | } |
| 5371 | |
| 5372 | /* We no longer need Server/ClientHello.random values */ |
| 5373 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 5374 | sizeof( ssl->handshake->randbytes ) ); |
| 5375 | |
| 5376 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 5377 | |
| 5378 | return( 0 ); |
| 5379 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5380 | |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5381 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 5382 | { |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5383 | switch( md ) |
| 5384 | { |
| 5385 | #if defined(MBEDTLS_SHA384_C) |
| 5386 | case MBEDTLS_SSL_HASH_SHA384: |
| 5387 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5388 | break; |
| 5389 | #endif |
| 5390 | #if defined(MBEDTLS_SHA256_C) |
| 5391 | case MBEDTLS_SSL_HASH_SHA256: |
| 5392 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5393 | break; |
| 5394 | #endif |
| 5395 | default: |
| 5396 | return( -1 ); |
| 5397 | } |
| 5398 | |
Ronald Cron | c2f13a0 | 2022-03-07 10:25:24 +0100 | [diff] [blame] | 5399 | return( 0 ); |
Ronald Cron | 4dcbca9 | 2022-03-07 10:21:40 +0100 | [diff] [blame] | 5400 | } |
| 5401 | |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5402 | #if defined(MBEDTLS_SHA256_C) |
| 5403 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 5404 | unsigned char *hash, |
| 5405 | size_t *hlen ) |
| 5406 | { |
| 5407 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5408 | size_t hash_size; |
| 5409 | psa_status_t status; |
| 5410 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 5411 | |
| 5412 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 5413 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 5414 | if( status != PSA_SUCCESS ) |
| 5415 | { |
| 5416 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5417 | return; |
| 5418 | } |
| 5419 | |
| 5420 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 5421 | if( status != PSA_SUCCESS ) |
| 5422 | { |
| 5423 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5424 | return; |
| 5425 | } |
| 5426 | |
| 5427 | *hlen = 32; |
| 5428 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5429 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5430 | #else |
| 5431 | mbedtls_sha256_context sha256; |
| 5432 | |
| 5433 | mbedtls_sha256_init( &sha256 ); |
| 5434 | |
| 5435 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 5436 | |
| 5437 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 5438 | mbedtls_sha256_finish( &sha256, hash ); |
| 5439 | |
| 5440 | *hlen = 32; |
| 5441 | |
| 5442 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5443 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5444 | |
| 5445 | mbedtls_sha256_free( &sha256 ); |
| 5446 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5447 | return; |
| 5448 | } |
| 5449 | #endif /* MBEDTLS_SHA256_C */ |
| 5450 | |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5451 | #if defined(MBEDTLS_SHA384_C) |
| 5452 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 5453 | unsigned char *hash, |
| 5454 | size_t *hlen ) |
| 5455 | { |
| 5456 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5457 | size_t hash_size; |
| 5458 | psa_status_t status; |
| 5459 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 5460 | |
| 5461 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 5462 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 5463 | if( status != PSA_SUCCESS ) |
| 5464 | { |
| 5465 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5466 | return; |
| 5467 | } |
| 5468 | |
| 5469 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 5470 | if( status != PSA_SUCCESS ) |
| 5471 | { |
| 5472 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5473 | return; |
| 5474 | } |
| 5475 | |
| 5476 | *hlen = 48; |
| 5477 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5478 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5479 | #else |
| 5480 | mbedtls_sha512_context sha512; |
| 5481 | |
| 5482 | mbedtls_sha512_init( &sha512 ); |
| 5483 | |
| 5484 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 5485 | |
| 5486 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 5487 | mbedtls_sha512_finish( &sha512, hash ); |
| 5488 | |
| 5489 | *hlen = 48; |
| 5490 | |
| 5491 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5492 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5493 | |
| 5494 | mbedtls_sha512_free( &sha512 ); |
| 5495 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5496 | return; |
| 5497 | } |
| 5498 | #endif /* MBEDTLS_SHA384_C */ |
| 5499 | |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5500 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5501 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5502 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 5503 | { |
| 5504 | unsigned char *p = ssl->handshake->premaster; |
| 5505 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 5506 | const unsigned char *psk = NULL; |
| 5507 | size_t psk_len = 0; |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5508 | int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len ); |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5509 | |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5510 | if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5511 | { |
| 5512 | /* |
| 5513 | * This should never happen because the existence of a PSK is always |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5514 | * checked before calling this function. |
| 5515 | * |
| 5516 | * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with |
Przemek Stekiel | 8abcee9 | 2022-04-28 09:16:28 +0200 | [diff] [blame] | 5517 | * the shared secret without PSK. |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5518 | */ |
Przemek Stekiel | b293aaa | 2022-04-19 12:22:38 +0200 | [diff] [blame] | 5519 | if ( key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 5520 | { |
| 5521 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5522 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5523 | } |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | /* |
| 5527 | * PMS = struct { |
| 5528 | * opaque other_secret<0..2^16-1>; |
| 5529 | * opaque psk<0..2^16-1>; |
| 5530 | * }; |
| 5531 | * with "other_secret" depending on the particular key exchange |
| 5532 | */ |
| 5533 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 5534 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 5535 | { |
| 5536 | if( end - p < 2 ) |
| 5537 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5538 | |
| 5539 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5540 | p += 2; |
| 5541 | |
| 5542 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5543 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5544 | |
| 5545 | memset( p, 0, psk_len ); |
| 5546 | p += psk_len; |
| 5547 | } |
| 5548 | else |
| 5549 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 5550 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 5551 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 5552 | { |
| 5553 | /* |
| 5554 | * other_secret already set by the ClientKeyExchange message, |
| 5555 | * and is 48 bytes long |
| 5556 | */ |
| 5557 | if( end - p < 2 ) |
| 5558 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5559 | |
| 5560 | *p++ = 0; |
| 5561 | *p++ = 48; |
| 5562 | p += 48; |
| 5563 | } |
| 5564 | else |
| 5565 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 5566 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 5567 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 5568 | { |
| 5569 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5570 | size_t len; |
| 5571 | |
| 5572 | /* Write length only when we know the actual value */ |
| 5573 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 5574 | p + 2, end - ( p + 2 ), &len, |
| 5575 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5576 | { |
| 5577 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 5578 | return( ret ); |
| 5579 | } |
| 5580 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 5581 | p += 2 + len; |
| 5582 | |
| 5583 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 5584 | } |
| 5585 | else |
| 5586 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5587 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5588 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 5589 | { |
| 5590 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5591 | size_t zlen; |
| 5592 | |
| 5593 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 5594 | p + 2, end - ( p + 2 ), |
| 5595 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5596 | { |
| 5597 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 5598 | return( ret ); |
| 5599 | } |
| 5600 | |
| 5601 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 5602 | p += 2 + zlen; |
| 5603 | |
| 5604 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 5605 | MBEDTLS_DEBUG_ECDH_Z ); |
| 5606 | } |
| 5607 | else |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5608 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5609 | { |
| 5610 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5611 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5612 | } |
| 5613 | |
| 5614 | /* opaque psk<0..2^16-1>; */ |
| 5615 | if( end - p < 2 ) |
| 5616 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5617 | |
| 5618 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5619 | p += 2; |
| 5620 | |
| 5621 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5622 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5623 | |
| 5624 | memcpy( p, psk, psk_len ); |
| 5625 | p += psk_len; |
| 5626 | |
| 5627 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 5628 | |
| 5629 | return( 0 ); |
| 5630 | } |
Neil Armstrong | 80f6f32 | 2022-05-03 17:56:38 +0200 | [diff] [blame] | 5631 | #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 5632 | |
| 5633 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5634 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 5635 | |
| 5636 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5637 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 5638 | { |
| 5639 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 5640 | * timeout if we were using the usual handshake doubling scheme */ |
| 5641 | if( ssl->conf->renego_max_records < 0 ) |
| 5642 | { |
| 5643 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 5644 | unsigned char doublings = 1; |
| 5645 | |
| 5646 | while( ratio != 0 ) |
| 5647 | { |
| 5648 | ++doublings; |
| 5649 | ratio >>= 1; |
| 5650 | } |
| 5651 | |
| 5652 | if( ++ssl->renego_records_seen > doublings ) |
| 5653 | { |
| 5654 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 5655 | return( 0 ); |
| 5656 | } |
| 5657 | } |
| 5658 | |
| 5659 | return( ssl_write_hello_request( ssl ) ); |
| 5660 | } |
| 5661 | #endif |
| 5662 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5663 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5664 | /* |
| 5665 | * Handshake functions |
| 5666 | */ |
| 5667 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5668 | /* No certificate support -> dummy functions */ |
| 5669 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 5670 | { |
| 5671 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5672 | ssl->handshake->ciphersuite_info; |
| 5673 | |
| 5674 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 5675 | |
| 5676 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5677 | { |
| 5678 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5679 | ssl->state++; |
| 5680 | return( 0 ); |
| 5681 | } |
| 5682 | |
| 5683 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5684 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5685 | } |
| 5686 | |
| 5687 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 5688 | { |
| 5689 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5690 | ssl->handshake->ciphersuite_info; |
| 5691 | |
| 5692 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 5693 | |
| 5694 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5695 | { |
| 5696 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 5697 | ssl->state++; |
| 5698 | return( 0 ); |
| 5699 | } |
| 5700 | |
| 5701 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5702 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5703 | } |
| 5704 | |
| 5705 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5706 | /* Some certificate support -> implement write and parse */ |
| 5707 | |
| 5708 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 5709 | { |
| 5710 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 5711 | size_t i, n; |
| 5712 | const mbedtls_x509_crt *crt; |
| 5713 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5714 | ssl->handshake->ciphersuite_info; |
| 5715 | |
| 5716 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 5717 | |
| 5718 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5719 | { |
| 5720 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5721 | ssl->state++; |
| 5722 | return( 0 ); |
| 5723 | } |
| 5724 | |
| 5725 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5726 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5727 | { |
| 5728 | if( ssl->handshake->client_auth == 0 ) |
| 5729 | { |
| 5730 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5731 | ssl->state++; |
| 5732 | return( 0 ); |
| 5733 | } |
| 5734 | } |
| 5735 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 5736 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5737 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 5738 | { |
| 5739 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 5740 | { |
| 5741 | /* Should never happen because we shouldn't have picked the |
| 5742 | * ciphersuite if we don't have a certificate. */ |
| 5743 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5744 | } |
| 5745 | } |
| 5746 | #endif |
| 5747 | |
| 5748 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 5749 | |
| 5750 | /* |
| 5751 | * 0 . 0 handshake type |
| 5752 | * 1 . 3 handshake length |
| 5753 | * 4 . 6 length of all certs |
| 5754 | * 7 . 9 length of cert. 1 |
| 5755 | * 10 . n-1 peer certificate |
| 5756 | * n . n+2 length of cert. 2 |
| 5757 | * n+3 . ... upper level cert, etc. |
| 5758 | */ |
| 5759 | i = 7; |
| 5760 | crt = mbedtls_ssl_own_cert( ssl ); |
| 5761 | |
| 5762 | while( crt != NULL ) |
| 5763 | { |
| 5764 | n = crt->raw.len; |
| 5765 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 5766 | { |
| 5767 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 5768 | " > %" MBEDTLS_PRINTF_SIZET, |
| 5769 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 5770 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 5771 | } |
| 5772 | |
| 5773 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 5774 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 5775 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 5776 | |
| 5777 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 5778 | i += n; crt = crt->next; |
| 5779 | } |
| 5780 | |
| 5781 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 5782 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 5783 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 5784 | |
| 5785 | ssl->out_msglen = i; |
| 5786 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5787 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 5788 | |
| 5789 | ssl->state++; |
| 5790 | |
| 5791 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 5792 | { |
| 5793 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 5794 | return( ret ); |
| 5795 | } |
| 5796 | |
| 5797 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 5798 | |
| 5799 | return( ret ); |
| 5800 | } |
| 5801 | |
| 5802 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5803 | |
| 5804 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5805 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 5806 | unsigned char *crt_buf, |
| 5807 | size_t crt_buf_len ) |
| 5808 | { |
| 5809 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 5810 | |
| 5811 | if( peer_crt == NULL ) |
| 5812 | return( -1 ); |
| 5813 | |
| 5814 | if( peer_crt->raw.len != crt_buf_len ) |
| 5815 | return( -1 ); |
| 5816 | |
| 5817 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 5818 | } |
| 5819 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5820 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 5821 | unsigned char *crt_buf, |
| 5822 | size_t crt_buf_len ) |
| 5823 | { |
| 5824 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5825 | unsigned char const * const peer_cert_digest = |
| 5826 | ssl->session->peer_cert_digest; |
| 5827 | mbedtls_md_type_t const peer_cert_digest_type = |
| 5828 | ssl->session->peer_cert_digest_type; |
| 5829 | mbedtls_md_info_t const * const digest_info = |
| 5830 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 5831 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 5832 | size_t digest_len; |
| 5833 | |
| 5834 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 5835 | return( -1 ); |
| 5836 | |
| 5837 | digest_len = mbedtls_md_get_size( digest_info ); |
| 5838 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 5839 | return( -1 ); |
| 5840 | |
| 5841 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 5842 | if( ret != 0 ) |
| 5843 | return( -1 ); |
| 5844 | |
| 5845 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 5846 | } |
| 5847 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5848 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 5849 | |
| 5850 | /* |
| 5851 | * Once the certificate message is read, parse it into a cert chain and |
| 5852 | * perform basic checks, but leave actual verification to the caller |
| 5853 | */ |
| 5854 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 5855 | mbedtls_x509_crt *chain ) |
| 5856 | { |
| 5857 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5858 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5859 | int crt_cnt=0; |
| 5860 | #endif |
| 5861 | size_t i, n; |
| 5862 | uint8_t alert; |
| 5863 | |
| 5864 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 5865 | { |
| 5866 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5867 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5868 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 5869 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5870 | } |
| 5871 | |
| 5872 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 5873 | { |
| 5874 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5875 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 5876 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5877 | } |
| 5878 | |
| 5879 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 5880 | { |
| 5881 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5882 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5883 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5884 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5885 | } |
| 5886 | |
| 5887 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 5888 | |
| 5889 | /* |
| 5890 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 5891 | */ |
| 5892 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 5893 | |
| 5894 | if( ssl->in_msg[i] != 0 || |
| 5895 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 5896 | { |
| 5897 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5898 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5899 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5900 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5901 | } |
| 5902 | |
| 5903 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 5904 | i += 3; |
| 5905 | |
| 5906 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 5907 | while( i < ssl->in_hslen ) |
| 5908 | { |
| 5909 | /* Check that there's room for the next CRT's length fields. */ |
| 5910 | if ( i + 3 > ssl->in_hslen ) { |
| 5911 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5912 | mbedtls_ssl_send_alert_message( ssl, |
| 5913 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5914 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5915 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5916 | } |
| 5917 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 5918 | * anything beyond 2**16 ~ 64K. */ |
| 5919 | if( ssl->in_msg[i] != 0 ) |
| 5920 | { |
| 5921 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5922 | mbedtls_ssl_send_alert_message( ssl, |
| 5923 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5924 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 5925 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 5926 | } |
| 5927 | |
| 5928 | /* Read length of the next CRT in the chain. */ |
| 5929 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 5930 | | (unsigned int) ssl->in_msg[i + 2]; |
| 5931 | i += 3; |
| 5932 | |
| 5933 | if( n < 128 || i + n > ssl->in_hslen ) |
| 5934 | { |
| 5935 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5936 | mbedtls_ssl_send_alert_message( ssl, |
| 5937 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5938 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5939 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5940 | } |
| 5941 | |
| 5942 | /* Check if we're handling the first CRT in the chain. */ |
| 5943 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5944 | if( crt_cnt++ == 0 && |
| 5945 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 5946 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 5947 | { |
| 5948 | /* During client-side renegotiation, check that the server's |
| 5949 | * end-CRTs hasn't changed compared to the initial handshake, |
| 5950 | * mitigating the triple handshake attack. On success, reuse |
| 5951 | * the original end-CRT instead of parsing it again. */ |
| 5952 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 5953 | if( ssl_check_peer_crt_unchanged( ssl, |
| 5954 | &ssl->in_msg[i], |
| 5955 | n ) != 0 ) |
| 5956 | { |
| 5957 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 5958 | mbedtls_ssl_send_alert_message( ssl, |
| 5959 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5960 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 5961 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 5962 | } |
| 5963 | |
| 5964 | /* Now we can safely free the original chain. */ |
| 5965 | ssl_clear_peer_cert( ssl->session ); |
| 5966 | } |
| 5967 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 5968 | |
| 5969 | /* Parse the next certificate in the chain. */ |
| 5970 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5971 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 5972 | #else |
| 5973 | /* If we don't need to store the CRT chain permanently, parse |
| 5974 | * it in-place from the input buffer instead of making a copy. */ |
| 5975 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 5976 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5977 | switch( ret ) |
| 5978 | { |
| 5979 | case 0: /*ok*/ |
| 5980 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 5981 | /* Ignore certificate with an unknown algorithm: maybe a |
| 5982 | prior certificate was already trusted. */ |
| 5983 | break; |
| 5984 | |
| 5985 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 5986 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 5987 | goto crt_parse_der_failed; |
| 5988 | |
| 5989 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 5990 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 5991 | goto crt_parse_der_failed; |
| 5992 | |
| 5993 | default: |
| 5994 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 5995 | crt_parse_der_failed: |
| 5996 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 5997 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 5998 | return( ret ); |
| 5999 | } |
| 6000 | |
| 6001 | i += n; |
| 6002 | } |
| 6003 | |
| 6004 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6005 | return( 0 ); |
| 6006 | } |
| 6007 | |
| 6008 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6009 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6010 | { |
| 6011 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6012 | return( -1 ); |
| 6013 | |
| 6014 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6015 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6016 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6017 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6018 | { |
| 6019 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 6020 | return( 0 ); |
| 6021 | } |
| 6022 | return( -1 ); |
| 6023 | } |
| 6024 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6025 | |
| 6026 | /* Check if a certificate message is expected. |
| 6027 | * Return either |
| 6028 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6029 | * - SSL_CERTIFICATE_SKIP |
| 6030 | * indicating whether a Certificate message is expected or not. |
| 6031 | */ |
| 6032 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6033 | #define SSL_CERTIFICATE_SKIP 1 |
| 6034 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6035 | int authmode ) |
| 6036 | { |
| 6037 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6038 | ssl->handshake->ciphersuite_info; |
| 6039 | |
| 6040 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6041 | return( SSL_CERTIFICATE_SKIP ); |
| 6042 | |
| 6043 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6044 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6045 | { |
| 6046 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6047 | return( SSL_CERTIFICATE_SKIP ); |
| 6048 | |
| 6049 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6050 | { |
| 6051 | ssl->session_negotiate->verify_result = |
| 6052 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6053 | return( SSL_CERTIFICATE_SKIP ); |
| 6054 | } |
| 6055 | } |
| 6056 | #else |
| 6057 | ((void) authmode); |
| 6058 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6059 | |
| 6060 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6061 | } |
| 6062 | |
| 6063 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6064 | int authmode, |
| 6065 | mbedtls_x509_crt *chain, |
| 6066 | void *rs_ctx ) |
| 6067 | { |
| 6068 | int ret = 0; |
| 6069 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6070 | ssl->handshake->ciphersuite_info; |
| 6071 | int have_ca_chain = 0; |
| 6072 | |
| 6073 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6074 | void *p_vrfy; |
| 6075 | |
| 6076 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6077 | return( 0 ); |
| 6078 | |
| 6079 | if( ssl->f_vrfy != NULL ) |
| 6080 | { |
| 6081 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6082 | f_vrfy = ssl->f_vrfy; |
| 6083 | p_vrfy = ssl->p_vrfy; |
| 6084 | } |
| 6085 | else |
| 6086 | { |
| 6087 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6088 | f_vrfy = ssl->conf->f_vrfy; |
| 6089 | p_vrfy = ssl->conf->p_vrfy; |
| 6090 | } |
| 6091 | |
| 6092 | /* |
| 6093 | * Main check: verify certificate |
| 6094 | */ |
| 6095 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6096 | if( ssl->conf->f_ca_cb != NULL ) |
| 6097 | { |
| 6098 | ((void) rs_ctx); |
| 6099 | have_ca_chain = 1; |
| 6100 | |
| 6101 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6102 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6103 | chain, |
| 6104 | ssl->conf->f_ca_cb, |
| 6105 | ssl->conf->p_ca_cb, |
| 6106 | ssl->conf->cert_profile, |
| 6107 | ssl->hostname, |
| 6108 | &ssl->session_negotiate->verify_result, |
| 6109 | f_vrfy, p_vrfy ); |
| 6110 | } |
| 6111 | else |
| 6112 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6113 | { |
| 6114 | mbedtls_x509_crt *ca_chain; |
| 6115 | mbedtls_x509_crl *ca_crl; |
| 6116 | |
| 6117 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6118 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6119 | { |
| 6120 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6121 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6122 | } |
| 6123 | else |
| 6124 | #endif |
| 6125 | { |
| 6126 | ca_chain = ssl->conf->ca_chain; |
| 6127 | ca_crl = ssl->conf->ca_crl; |
| 6128 | } |
| 6129 | |
| 6130 | if( ca_chain != NULL ) |
| 6131 | have_ca_chain = 1; |
| 6132 | |
| 6133 | ret = mbedtls_x509_crt_verify_restartable( |
| 6134 | chain, |
| 6135 | ca_chain, ca_crl, |
| 6136 | ssl->conf->cert_profile, |
| 6137 | ssl->hostname, |
| 6138 | &ssl->session_negotiate->verify_result, |
| 6139 | f_vrfy, p_vrfy, rs_ctx ); |
| 6140 | } |
| 6141 | |
| 6142 | if( ret != 0 ) |
| 6143 | { |
| 6144 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6145 | } |
| 6146 | |
| 6147 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6148 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6149 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6150 | #endif |
| 6151 | |
| 6152 | /* |
| 6153 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6154 | */ |
| 6155 | |
| 6156 | #if defined(MBEDTLS_ECP_C) |
| 6157 | { |
| 6158 | const mbedtls_pk_context *pk = &chain->pk; |
| 6159 | |
| 6160 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 6161 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 6162 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 6163 | { |
| 6164 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6165 | |
| 6166 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6167 | if( ret == 0 ) |
| 6168 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6169 | } |
| 6170 | } |
| 6171 | #endif /* MBEDTLS_ECP_C */ |
| 6172 | |
| 6173 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6174 | ciphersuite_info, |
| 6175 | ! ssl->conf->endpoint, |
| 6176 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6177 | { |
| 6178 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6179 | if( ret == 0 ) |
| 6180 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6181 | } |
| 6182 | |
| 6183 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6184 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6185 | * with details encoded in the verification flags. All other kinds |
| 6186 | * of error codes, including those from the user provided f_vrfy |
| 6187 | * functions, are treated as fatal and lead to a failure of |
| 6188 | * ssl_parse_certificate even if verification was optional. */ |
| 6189 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6190 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6191 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6192 | { |
| 6193 | ret = 0; |
| 6194 | } |
| 6195 | |
| 6196 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6197 | { |
| 6198 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6199 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6200 | } |
| 6201 | |
| 6202 | if( ret != 0 ) |
| 6203 | { |
| 6204 | uint8_t alert; |
| 6205 | |
| 6206 | /* The certificate may have been rejected for several reasons. |
| 6207 | Pick one and send the corresponding alert. Which alert to send |
| 6208 | may be a subject of debate in some cases. */ |
| 6209 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6210 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6211 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6212 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6213 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6214 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6215 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6216 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6217 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6218 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6219 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6220 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6221 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6222 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6223 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6224 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6225 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6226 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6227 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6228 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6229 | else |
| 6230 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6231 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6232 | alert ); |
| 6233 | } |
| 6234 | |
| 6235 | #if defined(MBEDTLS_DEBUG_C) |
| 6236 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6237 | { |
| 6238 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6239 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6240 | } |
| 6241 | else |
| 6242 | { |
| 6243 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6244 | } |
| 6245 | #endif /* MBEDTLS_DEBUG_C */ |
| 6246 | |
| 6247 | return( ret ); |
| 6248 | } |
| 6249 | |
| 6250 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6251 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6252 | unsigned char *start, size_t len ) |
| 6253 | { |
| 6254 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6255 | /* Remember digest of the peer's end-CRT. */ |
| 6256 | ssl->session_negotiate->peer_cert_digest = |
| 6257 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6258 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6259 | { |
| 6260 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6261 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6262 | mbedtls_ssl_send_alert_message( ssl, |
| 6263 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6264 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6265 | |
| 6266 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6267 | } |
| 6268 | |
| 6269 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6270 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6271 | start, len, |
| 6272 | ssl->session_negotiate->peer_cert_digest ); |
| 6273 | |
| 6274 | ssl->session_negotiate->peer_cert_digest_type = |
| 6275 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6276 | ssl->session_negotiate->peer_cert_digest_len = |
| 6277 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6278 | |
| 6279 | return( ret ); |
| 6280 | } |
| 6281 | |
| 6282 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6283 | unsigned char *start, size_t len ) |
| 6284 | { |
| 6285 | unsigned char *end = start + len; |
| 6286 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6287 | |
| 6288 | /* Make a copy of the peer's raw public key. */ |
| 6289 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6290 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6291 | &ssl->handshake->peer_pubkey ); |
| 6292 | if( ret != 0 ) |
| 6293 | { |
| 6294 | /* We should have parsed the public key before. */ |
| 6295 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6296 | } |
| 6297 | |
| 6298 | return( 0 ); |
| 6299 | } |
| 6300 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6301 | |
| 6302 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6303 | { |
| 6304 | int ret = 0; |
| 6305 | int crt_expected; |
| 6306 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6307 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6308 | ? ssl->handshake->sni_authmode |
| 6309 | : ssl->conf->authmode; |
| 6310 | #else |
| 6311 | const int authmode = ssl->conf->authmode; |
| 6312 | #endif |
| 6313 | void *rs_ctx = NULL; |
| 6314 | mbedtls_x509_crt *chain = NULL; |
| 6315 | |
| 6316 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6317 | |
| 6318 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6319 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 6320 | { |
| 6321 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6322 | goto exit; |
| 6323 | } |
| 6324 | |
| 6325 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6326 | if( ssl->handshake->ecrs_enabled && |
| 6327 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 6328 | { |
| 6329 | chain = ssl->handshake->ecrs_peer_cert; |
| 6330 | ssl->handshake->ecrs_peer_cert = NULL; |
| 6331 | goto crt_verify; |
| 6332 | } |
| 6333 | #endif |
| 6334 | |
| 6335 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6336 | { |
| 6337 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6338 | let it decide whether to alert. */ |
| 6339 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6340 | goto exit; |
| 6341 | } |
| 6342 | |
| 6343 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6344 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6345 | { |
| 6346 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 6347 | |
| 6348 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6349 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 6350 | |
| 6351 | goto exit; |
| 6352 | } |
| 6353 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6354 | |
| 6355 | /* Clear existing peer CRT structure in case we tried to |
| 6356 | * reuse a session but it failed, and allocate a new one. */ |
| 6357 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 6358 | |
| 6359 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6360 | if( chain == NULL ) |
| 6361 | { |
| 6362 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 6363 | sizeof( mbedtls_x509_crt ) ) ); |
| 6364 | mbedtls_ssl_send_alert_message( ssl, |
| 6365 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6366 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6367 | |
| 6368 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6369 | goto exit; |
| 6370 | } |
| 6371 | mbedtls_x509_crt_init( chain ); |
| 6372 | |
| 6373 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 6374 | if( ret != 0 ) |
| 6375 | goto exit; |
| 6376 | |
| 6377 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6378 | if( ssl->handshake->ecrs_enabled) |
| 6379 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 6380 | |
| 6381 | crt_verify: |
| 6382 | if( ssl->handshake->ecrs_enabled) |
| 6383 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6384 | #endif |
| 6385 | |
| 6386 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 6387 | chain, rs_ctx ); |
| 6388 | if( ret != 0 ) |
| 6389 | goto exit; |
| 6390 | |
| 6391 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6392 | { |
| 6393 | unsigned char *crt_start, *pk_start; |
| 6394 | size_t crt_len, pk_len; |
| 6395 | |
| 6396 | /* We parse the CRT chain without copying, so |
| 6397 | * these pointers point into the input buffer, |
| 6398 | * and are hence still valid after freeing the |
| 6399 | * CRT chain. */ |
| 6400 | |
| 6401 | crt_start = chain->raw.p; |
| 6402 | crt_len = chain->raw.len; |
| 6403 | |
| 6404 | pk_start = chain->pk_raw.p; |
| 6405 | pk_len = chain->pk_raw.len; |
| 6406 | |
| 6407 | /* Free the CRT structures before computing |
| 6408 | * digest and copying the peer's public key. */ |
| 6409 | mbedtls_x509_crt_free( chain ); |
| 6410 | mbedtls_free( chain ); |
| 6411 | chain = NULL; |
| 6412 | |
| 6413 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 6414 | if( ret != 0 ) |
| 6415 | goto exit; |
| 6416 | |
| 6417 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 6418 | if( ret != 0 ) |
| 6419 | goto exit; |
| 6420 | } |
| 6421 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6422 | /* Pass ownership to session structure. */ |
| 6423 | ssl->session_negotiate->peer_cert = chain; |
| 6424 | chain = NULL; |
| 6425 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6426 | |
| 6427 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 6428 | |
| 6429 | exit: |
| 6430 | |
| 6431 | if( ret == 0 ) |
| 6432 | ssl->state++; |
| 6433 | |
| 6434 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6435 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 6436 | { |
| 6437 | ssl->handshake->ecrs_peer_cert = chain; |
| 6438 | chain = NULL; |
| 6439 | } |
| 6440 | #endif |
| 6441 | |
| 6442 | if( chain != NULL ) |
| 6443 | { |
| 6444 | mbedtls_x509_crt_free( chain ); |
| 6445 | mbedtls_free( chain ); |
| 6446 | } |
| 6447 | |
| 6448 | return( ret ); |
| 6449 | } |
| 6450 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6451 | |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6452 | #if defined(MBEDTLS_SHA256_C) |
| 6453 | static void ssl_calc_finished_tls_sha256( |
| 6454 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6455 | { |
| 6456 | int len = 12; |
| 6457 | const char *sender; |
| 6458 | unsigned char padbuf[32]; |
| 6459 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6460 | size_t hash_size; |
| 6461 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 6462 | psa_status_t status; |
| 6463 | #else |
| 6464 | mbedtls_sha256_context sha256; |
| 6465 | #endif |
| 6466 | |
| 6467 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6468 | if( !session ) |
| 6469 | session = ssl->session; |
| 6470 | |
| 6471 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6472 | ? "client finished" |
| 6473 | : "server finished"; |
| 6474 | |
| 6475 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6476 | sha256_psa = psa_hash_operation_init(); |
| 6477 | |
| 6478 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 6479 | |
| 6480 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6481 | if( status != PSA_SUCCESS ) |
| 6482 | { |
| 6483 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6484 | return; |
| 6485 | } |
| 6486 | |
| 6487 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6488 | if( status != PSA_SUCCESS ) |
| 6489 | { |
| 6490 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6491 | return; |
| 6492 | } |
| 6493 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 6494 | #else |
| 6495 | |
| 6496 | mbedtls_sha256_init( &sha256 ); |
| 6497 | |
| 6498 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 6499 | |
| 6500 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6501 | |
| 6502 | /* |
| 6503 | * TLSv1.2: |
| 6504 | * hash = PRF( master, finished_label, |
| 6505 | * Hash( handshake ) )[0.11] |
| 6506 | */ |
| 6507 | |
| 6508 | #if !defined(MBEDTLS_SHA256_ALT) |
| 6509 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 6510 | sha256.state, sizeof( sha256.state ) ); |
| 6511 | #endif |
| 6512 | |
| 6513 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 6514 | mbedtls_sha256_free( &sha256 ); |
| 6515 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6516 | |
| 6517 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6518 | padbuf, 32, buf, len ); |
| 6519 | |
| 6520 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6521 | |
| 6522 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6523 | |
| 6524 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6525 | } |
| 6526 | #endif /* MBEDTLS_SHA256_C */ |
| 6527 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6528 | |
| 6529 | #if defined(MBEDTLS_SHA384_C) |
| 6530 | |
| 6531 | static void ssl_calc_finished_tls_sha384( |
| 6532 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6533 | { |
| 6534 | int len = 12; |
| 6535 | const char *sender; |
| 6536 | unsigned char padbuf[48]; |
| 6537 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6538 | size_t hash_size; |
| 6539 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 6540 | psa_status_t status; |
| 6541 | #else |
| 6542 | mbedtls_sha512_context sha512; |
| 6543 | #endif |
| 6544 | |
| 6545 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6546 | if( !session ) |
| 6547 | session = ssl->session; |
| 6548 | |
| 6549 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6550 | ? "client finished" |
| 6551 | : "server finished"; |
| 6552 | |
| 6553 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6554 | sha384_psa = psa_hash_operation_init(); |
| 6555 | |
| 6556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 6557 | |
| 6558 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6559 | if( status != PSA_SUCCESS ) |
| 6560 | { |
| 6561 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6562 | return; |
| 6563 | } |
| 6564 | |
| 6565 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6566 | if( status != PSA_SUCCESS ) |
| 6567 | { |
| 6568 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6569 | return; |
| 6570 | } |
| 6571 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 6572 | #else |
| 6573 | mbedtls_sha512_init( &sha512 ); |
| 6574 | |
| 6575 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 6576 | |
| 6577 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 6578 | |
| 6579 | /* |
| 6580 | * TLSv1.2: |
| 6581 | * hash = PRF( master, finished_label, |
| 6582 | * Hash( handshake ) )[0.11] |
| 6583 | */ |
| 6584 | |
| 6585 | #if !defined(MBEDTLS_SHA512_ALT) |
| 6586 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 6587 | sha512.state, sizeof( sha512.state ) ); |
| 6588 | #endif |
| 6589 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 6590 | |
| 6591 | mbedtls_sha512_free( &sha512 ); |
| 6592 | #endif |
| 6593 | |
| 6594 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6595 | padbuf, 48, buf, len ); |
| 6596 | |
| 6597 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6598 | |
| 6599 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6600 | |
| 6601 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6602 | } |
| 6603 | #endif /* MBEDTLS_SHA384_C */ |
| 6604 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 6605 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 6606 | { |
| 6607 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 6608 | |
| 6609 | /* |
| 6610 | * Free our handshake params |
| 6611 | */ |
| 6612 | mbedtls_ssl_handshake_free( ssl ); |
| 6613 | mbedtls_free( ssl->handshake ); |
| 6614 | ssl->handshake = NULL; |
| 6615 | |
| 6616 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6617 | * Free the previous transform and switch in the current one |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 6618 | */ |
| 6619 | if( ssl->transform ) |
| 6620 | { |
| 6621 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6622 | mbedtls_free( ssl->transform ); |
| 6623 | } |
| 6624 | ssl->transform = ssl->transform_negotiate; |
| 6625 | ssl->transform_negotiate = NULL; |
| 6626 | |
| 6627 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 6628 | } |
| 6629 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 6630 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 6631 | { |
| 6632 | int resume = ssl->handshake->resume; |
| 6633 | |
| 6634 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 6635 | |
| 6636 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6637 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6638 | { |
| 6639 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 6640 | ssl->renego_records_seen = 0; |
| 6641 | } |
| 6642 | #endif |
| 6643 | |
| 6644 | /* |
| 6645 | * Free the previous session and switch in the current one |
| 6646 | */ |
| 6647 | if( ssl->session ) |
| 6648 | { |
| 6649 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6650 | /* RFC 7366 3.1: keep the EtM state */ |
| 6651 | ssl->session_negotiate->encrypt_then_mac = |
| 6652 | ssl->session->encrypt_then_mac; |
| 6653 | #endif |
| 6654 | |
| 6655 | mbedtls_ssl_session_free( ssl->session ); |
| 6656 | mbedtls_free( ssl->session ); |
| 6657 | } |
| 6658 | ssl->session = ssl->session_negotiate; |
| 6659 | ssl->session_negotiate = NULL; |
| 6660 | |
| 6661 | /* |
| 6662 | * Add cache entry |
| 6663 | */ |
| 6664 | if( ssl->conf->f_set_cache != NULL && |
| 6665 | ssl->session->id_len != 0 && |
| 6666 | resume == 0 ) |
| 6667 | { |
| 6668 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 6669 | ssl->session->id, |
| 6670 | ssl->session->id_len, |
| 6671 | ssl->session ) != 0 ) |
| 6672 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 6673 | } |
| 6674 | |
| 6675 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6676 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6677 | ssl->handshake->flight != NULL ) |
| 6678 | { |
| 6679 | /* Cancel handshake timer */ |
| 6680 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 6681 | |
| 6682 | /* Keep last flight around in case we need to resend it: |
| 6683 | * we need the handshake and transform structures for that */ |
| 6684 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 6685 | } |
| 6686 | else |
| 6687 | #endif |
| 6688 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6689 | |
| 6690 | ssl->state++; |
| 6691 | |
| 6692 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 6693 | } |
| 6694 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 6695 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 6696 | { |
| 6697 | int ret, hash_len; |
| 6698 | |
| 6699 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 6700 | |
| 6701 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 6702 | |
| 6703 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 6704 | |
| 6705 | /* |
| 6706 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 6707 | * may define some other value. Currently (early 2016), no defined |
| 6708 | * ciphersuite does this (and this is unlikely to change as activity has |
| 6709 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 6710 | */ |
| 6711 | hash_len = 12; |
| 6712 | |
| 6713 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6714 | ssl->verify_data_len = hash_len; |
| 6715 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 6716 | #endif |
| 6717 | |
| 6718 | ssl->out_msglen = 4 + hash_len; |
| 6719 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6720 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 6721 | |
| 6722 | /* |
| 6723 | * In case of session resuming, invert the client and server |
| 6724 | * ChangeCipherSpec messages order. |
| 6725 | */ |
| 6726 | if( ssl->handshake->resume != 0 ) |
| 6727 | { |
| 6728 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6729 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6730 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 6731 | #endif |
| 6732 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6733 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6734 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 6735 | #endif |
| 6736 | } |
| 6737 | else |
| 6738 | ssl->state++; |
| 6739 | |
| 6740 | /* |
| 6741 | * Switch to our negotiated transform and session parameters for outbound |
| 6742 | * data. |
| 6743 | */ |
| 6744 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 6745 | |
| 6746 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6747 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6748 | { |
| 6749 | unsigned char i; |
| 6750 | |
| 6751 | /* Remember current epoch settings for resending */ |
| 6752 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 6753 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 6754 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 6755 | |
| 6756 | /* Set sequence_number to zero */ |
| 6757 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 6758 | |
| 6759 | |
| 6760 | /* Increment epoch */ |
| 6761 | for( i = 2; i > 0; i-- ) |
| 6762 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 6763 | break; |
| 6764 | |
| 6765 | /* The loop goes to its end iff the counter is wrapping */ |
| 6766 | if( i == 0 ) |
| 6767 | { |
| 6768 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 6769 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 6770 | } |
| 6771 | } |
| 6772 | else |
| 6773 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6774 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 6775 | |
| 6776 | ssl->transform_out = ssl->transform_negotiate; |
| 6777 | ssl->session_out = ssl->session_negotiate; |
| 6778 | |
| 6779 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6780 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6781 | mbedtls_ssl_send_flight_completed( ssl ); |
| 6782 | #endif |
| 6783 | |
| 6784 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6785 | { |
| 6786 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6787 | return( ret ); |
| 6788 | } |
| 6789 | |
| 6790 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6791 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6792 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 6793 | { |
| 6794 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 6795 | return( ret ); |
| 6796 | } |
| 6797 | #endif |
| 6798 | |
| 6799 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 6800 | |
| 6801 | return( 0 ); |
| 6802 | } |
| 6803 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 6804 | #define SSL_MAX_HASH_LEN 12 |
| 6805 | |
| 6806 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 6807 | { |
| 6808 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6809 | unsigned int hash_len = 12; |
| 6810 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 6811 | |
| 6812 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 6813 | |
| 6814 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 6815 | |
| 6816 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6817 | { |
| 6818 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6819 | goto exit; |
| 6820 | } |
| 6821 | |
| 6822 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6823 | { |
| 6824 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6825 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6826 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6827 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 6828 | goto exit; |
| 6829 | } |
| 6830 | |
| 6831 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 6832 | { |
| 6833 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6834 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6835 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 6836 | goto exit; |
| 6837 | } |
| 6838 | |
| 6839 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 6840 | { |
| 6841 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6842 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6843 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6844 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 6845 | goto exit; |
| 6846 | } |
| 6847 | |
| 6848 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 6849 | buf, hash_len ) != 0 ) |
| 6850 | { |
| 6851 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6852 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6853 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 6854 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 6855 | goto exit; |
| 6856 | } |
| 6857 | |
| 6858 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6859 | ssl->verify_data_len = hash_len; |
| 6860 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 6861 | #endif |
| 6862 | |
| 6863 | if( ssl->handshake->resume != 0 ) |
| 6864 | { |
| 6865 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6866 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6867 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 6868 | #endif |
| 6869 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6870 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6871 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 6872 | #endif |
| 6873 | } |
| 6874 | else |
| 6875 | ssl->state++; |
| 6876 | |
| 6877 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6878 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6879 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 6880 | #endif |
| 6881 | |
| 6882 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 6883 | |
| 6884 | exit: |
| 6885 | mbedtls_platform_zeroize( buf, hash_len ); |
| 6886 | return( ret ); |
| 6887 | } |
| 6888 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 6889 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 6890 | /* |
| 6891 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 6892 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 6893 | */ |
| 6894 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 6895 | { |
| 6896 | #if defined(MBEDTLS_SHA384_C) |
| 6897 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6898 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 6899 | |
| 6900 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 6901 | return( tls_prf_sha384 ); |
| 6902 | #else |
| 6903 | (void) ciphersuite_id; |
| 6904 | #endif |
| 6905 | return( tls_prf_sha256 ); |
| 6906 | } |
| 6907 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 6908 | |
| 6909 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 6910 | { |
| 6911 | ((void) tls_prf); |
| 6912 | #if defined(MBEDTLS_SHA384_C) |
| 6913 | if( tls_prf == tls_prf_sha384 ) |
| 6914 | { |
| 6915 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 6916 | } |
| 6917 | else |
| 6918 | #endif |
| 6919 | #if defined(MBEDTLS_SHA256_C) |
| 6920 | if( tls_prf == tls_prf_sha256 ) |
| 6921 | { |
| 6922 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 6923 | } |
| 6924 | else |
| 6925 | #endif |
| 6926 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 6927 | } |
| 6928 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6929 | /* |
| 6930 | * Populate a transform structure with session keys and all the other |
| 6931 | * necessary information. |
| 6932 | * |
| 6933 | * Parameters: |
| 6934 | * - [in/out]: transform: structure to populate |
| 6935 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 6936 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 6937 | * - [in] ciphersuite |
| 6938 | * - [in] master |
| 6939 | * - [in] encrypt_then_mac |
| 6940 | * - [in] compression |
| 6941 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 6942 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 6943 | * - [in] tls_version: TLS version |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6944 | * - [in] endpoint: client or server |
| 6945 | * - [in] ssl: used for: |
| 6946 | * - ssl->conf->{f,p}_export_keys |
| 6947 | * [in] optionally used for: |
| 6948 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 6949 | */ |
| 6950 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 6951 | int ciphersuite, |
| 6952 | const unsigned char master[48], |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6953 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6954 | int encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6955 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6956 | ssl_tls_prf_t tls_prf, |
| 6957 | const unsigned char randbytes[64], |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 6958 | mbedtls_ssl_protocol_version tls_version, |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6959 | unsigned endpoint, |
| 6960 | const mbedtls_ssl_context *ssl ) |
| 6961 | { |
| 6962 | int ret = 0; |
| 6963 | unsigned char keyblk[256]; |
| 6964 | unsigned char *key1; |
| 6965 | unsigned char *key2; |
| 6966 | unsigned char *mac_enc; |
| 6967 | unsigned char *mac_dec; |
| 6968 | size_t mac_key_len = 0; |
| 6969 | size_t iv_copy_len; |
| 6970 | size_t keylen; |
| 6971 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 6972 | mbedtls_ssl_mode_t ssl_mode; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6973 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 6974 | const mbedtls_cipher_info_t *cipher_info; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6975 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6976 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6977 | |
| 6978 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6979 | psa_key_type_t key_type; |
| 6980 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6981 | psa_algorithm_t alg; |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 6982 | psa_algorithm_t mac_alg = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6983 | size_t key_bits; |
| 6984 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 6985 | #endif |
| 6986 | |
| 6987 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 6988 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6989 | if( ssl->f_export_keys == NULL ) |
| 6990 | { |
| 6991 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 6992 | (void) ssl; |
| 6993 | } |
| 6994 | #endif |
| 6995 | |
| 6996 | /* |
| 6997 | * Some data just needs copying into the structure |
| 6998 | */ |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 6999 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7000 | transform->encrypt_then_mac = encrypt_then_mac; |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7001 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7002 | transform->tls_version = tls_version; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7003 | |
| 7004 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7005 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7006 | #endif |
| 7007 | |
| 7008 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7009 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7010 | { |
| 7011 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7012 | * generation separate. This should never happen. */ |
| 7013 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7014 | } |
| 7015 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7016 | |
| 7017 | /* |
| 7018 | * Get various info structures |
| 7019 | */ |
| 7020 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7021 | if( ciphersuite_info == NULL ) |
| 7022 | { |
| 7023 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7024 | ciphersuite ) ); |
| 7025 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7026 | } |
| 7027 | |
Neil Armstrong | ab555e0 | 2022-04-04 11:07:59 +0200 | [diff] [blame] | 7028 | ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite( |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7029 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7030 | encrypt_then_mac, |
Neil Armstrong | f2c82f0 | 2022-04-05 11:16:53 +0200 | [diff] [blame] | 7031 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7032 | ciphersuite_info ); |
| 7033 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7034 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
| 7035 | transform->taglen = |
| 7036 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7037 | |
| 7038 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7039 | if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, |
| 7040 | transform->taglen, |
| 7041 | &alg, |
| 7042 | &key_type, |
| 7043 | &key_bits ) ) != PSA_SUCCESS ) |
| 7044 | { |
| 7045 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7046 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7047 | goto end; |
| 7048 | } |
| 7049 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7050 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7051 | if( cipher_info == NULL ) |
| 7052 | { |
| 7053 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7054 | ciphersuite_info->cipher ) ); |
| 7055 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7056 | } |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7057 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7058 | |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7059 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7060 | mac_alg = mbedtls_psa_translate_md( ciphersuite_info->mac ); |
| 7061 | if( mac_alg == 0 ) |
| 7062 | { |
| 7063 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_psa_translate_md for %u not found", |
| 7064 | (unsigned) ciphersuite_info->mac ) ); |
| 7065 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7066 | } |
| 7067 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7068 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7069 | if( md_info == NULL ) |
| 7070 | { |
| 7071 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7072 | (unsigned) ciphersuite_info->mac ) ); |
| 7073 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7074 | } |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7075 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7076 | |
| 7077 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7078 | /* Copy own and peer's CID if the use of the CID |
| 7079 | * extension has been negotiated. */ |
| 7080 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7081 | { |
| 7082 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7083 | |
| 7084 | transform->in_cid_len = ssl->own_cid_len; |
| 7085 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7086 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7087 | transform->in_cid_len ); |
| 7088 | |
| 7089 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7090 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7091 | ssl->handshake->peer_cid_len ); |
| 7092 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7093 | transform->out_cid_len ); |
| 7094 | } |
| 7095 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7096 | |
| 7097 | /* |
| 7098 | * Compute key block using the PRF |
| 7099 | */ |
| 7100 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7101 | if( ret != 0 ) |
| 7102 | { |
| 7103 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7104 | return( ret ); |
| 7105 | } |
| 7106 | |
| 7107 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7108 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7109 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7110 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7111 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7112 | |
| 7113 | /* |
| 7114 | * Determine the appropriate key, IV and MAC length. |
| 7115 | */ |
| 7116 | |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7117 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7118 | keylen = PSA_BITS_TO_BYTES(key_bits); |
| 7119 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7120 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7121 | #endif |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7122 | |
| 7123 | #if defined(MBEDTLS_GCM_C) || \ |
| 7124 | defined(MBEDTLS_CCM_C) || \ |
| 7125 | defined(MBEDTLS_CHACHAPOLY_C) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7126 | if( ssl_mode == MBEDTLS_SSL_MODE_AEAD ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7127 | { |
| 7128 | size_t explicit_ivlen; |
| 7129 | |
| 7130 | transform->maclen = 0; |
| 7131 | mac_key_len = 0; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7132 | |
| 7133 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7134 | * with mode and version: |
| 7135 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7136 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7137 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7138 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7139 | * sequence number). |
| 7140 | */ |
| 7141 | transform->ivlen = 12; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7142 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7143 | if( key_type == PSA_KEY_TYPE_CHACHA20 ) |
| 7144 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7145 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7146 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7147 | transform->fixed_ivlen = 12; |
| 7148 | else |
| 7149 | transform->fixed_ivlen = 4; |
| 7150 | |
| 7151 | /* Minimum length of encrypted record */ |
| 7152 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7153 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7154 | } |
| 7155 | else |
| 7156 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7157 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7158 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 7159 | ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 7160 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7161 | { |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7162 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | d1be767 | 2022-04-04 11:21:41 +0200 | [diff] [blame] | 7163 | size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7164 | #else |
| 7165 | size_t block_size = cipher_info->block_size; |
| 7166 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7167 | |
| 7168 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7169 | /* Get MAC length */ |
| 7170 | mac_key_len = PSA_HASH_LENGTH(mac_alg); |
| 7171 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7172 | /* Initialize HMAC contexts */ |
| 7173 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7174 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7175 | { |
| 7176 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7177 | goto end; |
| 7178 | } |
| 7179 | |
| 7180 | /* Get MAC length */ |
| 7181 | mac_key_len = mbedtls_md_get_size( md_info ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7182 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7183 | transform->maclen = mac_key_len; |
| 7184 | |
| 7185 | /* IV length */ |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7186 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 2230e6c | 2022-04-27 10:36:14 +0200 | [diff] [blame] | 7187 | transform->ivlen = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7188 | #else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7189 | transform->ivlen = cipher_info->iv_size; |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7190 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7191 | |
| 7192 | /* Minimum length */ |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7193 | if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7194 | transform->minlen = transform->maclen; |
| 7195 | else |
| 7196 | { |
| 7197 | /* |
| 7198 | * GenericBlockCipher: |
| 7199 | * 1. if EtM is in use: one block plus MAC |
| 7200 | * otherwise: * first multiple of blocklen greater than maclen |
| 7201 | * 2. IV |
| 7202 | */ |
| 7203 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Neil Armstrong | 7fea33e | 2022-04-01 15:40:25 +0200 | [diff] [blame] | 7204 | if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7205 | { |
| 7206 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7207 | + block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7208 | } |
| 7209 | else |
| 7210 | #endif |
| 7211 | { |
| 7212 | transform->minlen = transform->maclen |
Neil Armstrong | a0eeb7f | 2022-04-01 17:36:10 +0200 | [diff] [blame] | 7213 | + block_size |
| 7214 | - transform->maclen % block_size; |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7215 | } |
| 7216 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 7217 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_2 ) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7218 | { |
| 7219 | transform->minlen += transform->ivlen; |
| 7220 | } |
| 7221 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7222 | { |
| 7223 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7224 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7225 | goto end; |
| 7226 | } |
| 7227 | } |
| 7228 | } |
| 7229 | else |
| 7230 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7231 | { |
| 7232 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7233 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7234 | } |
| 7235 | |
| 7236 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7237 | (unsigned) keylen, |
| 7238 | (unsigned) transform->minlen, |
| 7239 | (unsigned) transform->ivlen, |
| 7240 | (unsigned) transform->maclen ) ); |
| 7241 | |
| 7242 | /* |
| 7243 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7244 | */ |
| 7245 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7246 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7247 | { |
| 7248 | key1 = keyblk + mac_key_len * 2; |
| 7249 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7250 | |
| 7251 | mac_enc = keyblk; |
| 7252 | mac_dec = keyblk + mac_key_len; |
| 7253 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7254 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7255 | transform->fixed_ivlen : transform->ivlen; |
| 7256 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7257 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7258 | iv_copy_len ); |
| 7259 | } |
| 7260 | else |
| 7261 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7262 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7263 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7264 | { |
| 7265 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7266 | key2 = keyblk + mac_key_len * 2; |
| 7267 | |
| 7268 | mac_enc = keyblk + mac_key_len; |
| 7269 | mac_dec = keyblk; |
| 7270 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7271 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7272 | transform->fixed_ivlen : transform->ivlen; |
| 7273 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7274 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7275 | iv_copy_len ); |
| 7276 | } |
| 7277 | else |
| 7278 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7279 | { |
| 7280 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7281 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7282 | goto end; |
| 7283 | } |
| 7284 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7285 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7286 | { |
| 7287 | ssl->f_export_keys( ssl->p_export_keys, |
| 7288 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7289 | master, 48, |
| 7290 | randbytes + 32, |
| 7291 | randbytes, |
| 7292 | tls_prf_get_type( tls_prf ) ); |
| 7293 | } |
| 7294 | |
| 7295 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7296 | transform->psa_alg = alg; |
| 7297 | |
| 7298 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7299 | { |
| 7300 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7301 | psa_set_key_algorithm( &attributes, alg ); |
| 7302 | psa_set_key_type( &attributes, key_type ); |
| 7303 | |
| 7304 | if( ( status = psa_import_key( &attributes, |
| 7305 | key1, |
| 7306 | PSA_BITS_TO_BYTES( key_bits ), |
| 7307 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 7308 | { |
| 7309 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 7310 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7311 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7312 | goto end; |
| 7313 | } |
| 7314 | |
| 7315 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7316 | |
| 7317 | if( ( status = psa_import_key( &attributes, |
| 7318 | key2, |
| 7319 | PSA_BITS_TO_BYTES( key_bits ), |
| 7320 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 7321 | { |
| 7322 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7323 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7324 | goto end; |
| 7325 | } |
| 7326 | } |
| 7327 | #else |
| 7328 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 7329 | cipher_info ) ) != 0 ) |
| 7330 | { |
| 7331 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7332 | goto end; |
| 7333 | } |
| 7334 | |
| 7335 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 7336 | cipher_info ) ) != 0 ) |
| 7337 | { |
| 7338 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7339 | goto end; |
| 7340 | } |
| 7341 | |
| 7342 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 7343 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7344 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 7345 | { |
| 7346 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7347 | goto end; |
| 7348 | } |
| 7349 | |
| 7350 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 7351 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7352 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 7353 | { |
| 7354 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7355 | goto end; |
| 7356 | } |
| 7357 | |
| 7358 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 7359 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7360 | { |
| 7361 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 7362 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7363 | { |
| 7364 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7365 | goto end; |
| 7366 | } |
| 7367 | |
| 7368 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 7369 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7370 | { |
| 7371 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7372 | goto end; |
| 7373 | } |
| 7374 | } |
| 7375 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 7376 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7377 | |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7378 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7379 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 7380 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 7381 | if( mac_key_len != 0 ) |
| 7382 | { |
| 7383 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7384 | transform->psa_mac_alg = PSA_ALG_HMAC( mac_alg ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7385 | |
| 7386 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
Neil Armstrong | e451295 | 2022-03-08 09:08:22 +0100 | [diff] [blame] | 7387 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( mac_alg ) ); |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7388 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 7389 | |
| 7390 | if( ( status = psa_import_key( &attributes, |
| 7391 | mac_enc, mac_key_len, |
| 7392 | &transform->psa_mac_enc ) ) != PSA_SUCCESS ) |
| 7393 | { |
| 7394 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7395 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7396 | goto end; |
| 7397 | } |
| 7398 | |
Ronald Cron | fb39f15 | 2022-03-25 14:36:28 +0100 | [diff] [blame] | 7399 | if( ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ) || |
| 7400 | ( ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING ) && |
| 7401 | ( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ) ) ) |
Neil Armstrong | 29c0c04 | 2022-03-17 17:47:28 +0100 | [diff] [blame] | 7402 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 7403 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 7404 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 7405 | else |
| 7406 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 7407 | |
| 7408 | if( ( status = psa_import_key( &attributes, |
| 7409 | mac_dec, mac_key_len, |
| 7410 | &transform->psa_mac_dec ) ) != PSA_SUCCESS ) |
| 7411 | { |
| 7412 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7413 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_mac_key", ret ); |
| 7414 | goto end; |
| 7415 | } |
| 7416 | #else |
| 7417 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 7418 | if( ret != 0 ) |
| 7419 | goto end; |
| 7420 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 7421 | if( ret != 0 ) |
| 7422 | goto end; |
| 7423 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7424 | } |
| 7425 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7426 | |
| 7427 | ((void) mac_dec); |
| 7428 | ((void) mac_enc); |
| 7429 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7430 | end: |
| 7431 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 7432 | return( ret ); |
| 7433 | } |
| 7434 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7435 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7436 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7437 | unsigned char *hash, size_t *hashlen, |
| 7438 | unsigned char *data, size_t data_len, |
| 7439 | mbedtls_md_type_t md_alg ) |
| 7440 | { |
| 7441 | psa_status_t status; |
| 7442 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
| 7443 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7444 | |
| 7445 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 7446 | |
| 7447 | if( ( status = psa_hash_setup( &hash_operation, |
| 7448 | hash_alg ) ) != PSA_SUCCESS ) |
| 7449 | { |
| 7450 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 7451 | goto exit; |
| 7452 | } |
| 7453 | |
| 7454 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7455 | 64 ) ) != PSA_SUCCESS ) |
| 7456 | { |
| 7457 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7458 | goto exit; |
| 7459 | } |
| 7460 | |
| 7461 | if( ( status = psa_hash_update( &hash_operation, |
| 7462 | data, data_len ) ) != PSA_SUCCESS ) |
| 7463 | { |
| 7464 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7465 | goto exit; |
| 7466 | } |
| 7467 | |
| 7468 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 7469 | hashlen ) ) != PSA_SUCCESS ) |
| 7470 | { |
| 7471 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 7472 | goto exit; |
| 7473 | } |
| 7474 | |
| 7475 | exit: |
| 7476 | if( status != PSA_SUCCESS ) |
| 7477 | { |
| 7478 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7479 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7480 | switch( status ) |
| 7481 | { |
| 7482 | case PSA_ERROR_NOT_SUPPORTED: |
| 7483 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 7484 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 7485 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7486 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7487 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7488 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7489 | default: |
| 7490 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 7491 | } |
| 7492 | } |
| 7493 | return( 0 ); |
| 7494 | } |
| 7495 | |
| 7496 | #else |
| 7497 | |
| 7498 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7499 | unsigned char *hash, size_t *hashlen, |
| 7500 | unsigned char *data, size_t data_len, |
| 7501 | mbedtls_md_type_t md_alg ) |
| 7502 | { |
| 7503 | int ret = 0; |
| 7504 | mbedtls_md_context_t ctx; |
| 7505 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 7506 | *hashlen = mbedtls_md_get_size( md_info ); |
| 7507 | |
| 7508 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 7509 | |
| 7510 | mbedtls_md_init( &ctx ); |
| 7511 | |
| 7512 | /* |
| 7513 | * digitally-signed struct { |
| 7514 | * opaque client_random[32]; |
| 7515 | * opaque server_random[32]; |
| 7516 | * ServerDHParams params; |
| 7517 | * }; |
| 7518 | */ |
| 7519 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7520 | { |
| 7521 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7522 | goto exit; |
| 7523 | } |
| 7524 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7525 | { |
| 7526 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7527 | goto exit; |
| 7528 | } |
| 7529 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7530 | { |
| 7531 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7532 | goto exit; |
| 7533 | } |
| 7534 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7535 | { |
| 7536 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7537 | goto exit; |
| 7538 | } |
| 7539 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 7540 | { |
| 7541 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7542 | goto exit; |
| 7543 | } |
| 7544 | |
| 7545 | exit: |
| 7546 | mbedtls_md_free( &ctx ); |
| 7547 | |
| 7548 | if( ret != 0 ) |
| 7549 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7550 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7551 | |
| 7552 | return( ret ); |
| 7553 | } |
| 7554 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7555 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7556 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7557 | |
| 7558 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 7559 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 7560 | mbedtls_pk_type_t sig_alg ) |
| 7561 | { |
| 7562 | switch( sig_alg ) |
| 7563 | { |
| 7564 | case MBEDTLS_PK_RSA: |
| 7565 | return( set->rsa ); |
| 7566 | case MBEDTLS_PK_ECDSA: |
| 7567 | return( set->ecdsa ); |
| 7568 | default: |
| 7569 | return( MBEDTLS_MD_NONE ); |
| 7570 | } |
| 7571 | } |
| 7572 | |
| 7573 | /* Add a signature-hash-pair to a signature-hash set */ |
| 7574 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 7575 | mbedtls_pk_type_t sig_alg, |
| 7576 | mbedtls_md_type_t md_alg ) |
| 7577 | { |
| 7578 | switch( sig_alg ) |
| 7579 | { |
| 7580 | case MBEDTLS_PK_RSA: |
| 7581 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 7582 | set->rsa = md_alg; |
| 7583 | break; |
| 7584 | |
| 7585 | case MBEDTLS_PK_ECDSA: |
| 7586 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 7587 | set->ecdsa = md_alg; |
| 7588 | break; |
| 7589 | |
| 7590 | default: |
| 7591 | break; |
| 7592 | } |
| 7593 | } |
| 7594 | |
| 7595 | /* Allow exactly one hash algorithm for each signature. */ |
| 7596 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 7597 | mbedtls_md_type_t md_alg ) |
| 7598 | { |
| 7599 | set->rsa = md_alg; |
| 7600 | set->ecdsa = md_alg; |
| 7601 | } |
| 7602 | |
| 7603 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7604 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7605 | /* Serialization of TLS 1.2 sessions: |
| 7606 | * |
| 7607 | * struct { |
| 7608 | * uint64 start_time; |
| 7609 | * uint8 ciphersuite[2]; // defined by the standard |
| 7610 | * uint8 compression; // 0 or 1 |
| 7611 | * uint8 session_id_len; // at most 32 |
| 7612 | * opaque session_id[32]; |
| 7613 | * opaque master[48]; // fixed length in the standard |
| 7614 | * uint32 verify_result; |
| 7615 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 7616 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 7617 | * uint32 ticket_lifetime; |
| 7618 | * uint8 mfl_code; // up to 255 according to standard |
| 7619 | * uint8 encrypt_then_mac; // 0 or 1 |
| 7620 | * } serialized_session_tls12; |
| 7621 | * |
| 7622 | */ |
| 7623 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 7624 | unsigned char *buf, |
| 7625 | size_t buf_len ) |
| 7626 | { |
| 7627 | unsigned char *p = buf; |
| 7628 | size_t used = 0; |
| 7629 | |
| 7630 | #if defined(MBEDTLS_HAVE_TIME) |
| 7631 | uint64_t start; |
| 7632 | #endif |
| 7633 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7634 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7635 | size_t cert_len; |
| 7636 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7637 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7638 | |
| 7639 | /* |
| 7640 | * Time |
| 7641 | */ |
| 7642 | #if defined(MBEDTLS_HAVE_TIME) |
| 7643 | used += 8; |
| 7644 | |
| 7645 | if( used <= buf_len ) |
| 7646 | { |
| 7647 | start = (uint64_t) session->start; |
| 7648 | |
| 7649 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 7650 | p += 8; |
| 7651 | } |
| 7652 | #endif /* MBEDTLS_HAVE_TIME */ |
| 7653 | |
| 7654 | /* |
| 7655 | * Basic mandatory fields |
| 7656 | */ |
| 7657 | used += 2 /* ciphersuite */ |
| 7658 | + 1 /* compression */ |
| 7659 | + 1 /* id_len */ |
| 7660 | + sizeof( session->id ) |
| 7661 | + sizeof( session->master ) |
| 7662 | + 4; /* verify_result */ |
| 7663 | |
| 7664 | if( used <= buf_len ) |
| 7665 | { |
| 7666 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 7667 | p += 2; |
| 7668 | |
| 7669 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
| 7670 | |
| 7671 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 7672 | memcpy( p, session->id, 32 ); |
| 7673 | p += 32; |
| 7674 | |
| 7675 | memcpy( p, session->master, 48 ); |
| 7676 | p += 48; |
| 7677 | |
| 7678 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 7679 | p += 4; |
| 7680 | } |
| 7681 | |
| 7682 | /* |
| 7683 | * Peer's end-entity certificate |
| 7684 | */ |
| 7685 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7686 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7687 | if( session->peer_cert == NULL ) |
| 7688 | cert_len = 0; |
| 7689 | else |
| 7690 | cert_len = session->peer_cert->raw.len; |
| 7691 | |
| 7692 | used += 3 + cert_len; |
| 7693 | |
| 7694 | if( used <= buf_len ) |
| 7695 | { |
| 7696 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 7697 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 7698 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 7699 | |
| 7700 | if( session->peer_cert != NULL ) |
| 7701 | { |
| 7702 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 7703 | p += cert_len; |
| 7704 | } |
| 7705 | } |
| 7706 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7707 | if( session->peer_cert_digest != NULL ) |
| 7708 | { |
| 7709 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 7710 | if( used <= buf_len ) |
| 7711 | { |
| 7712 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 7713 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 7714 | memcpy( p, session->peer_cert_digest, |
| 7715 | session->peer_cert_digest_len ); |
| 7716 | p += session->peer_cert_digest_len; |
| 7717 | } |
| 7718 | } |
| 7719 | else |
| 7720 | { |
| 7721 | used += 2; |
| 7722 | if( used <= buf_len ) |
| 7723 | { |
| 7724 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 7725 | *p++ = 0; |
| 7726 | } |
| 7727 | } |
| 7728 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7729 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7730 | |
| 7731 | /* |
| 7732 | * Session ticket if any, plus associated data |
| 7733 | */ |
| 7734 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7735 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 7736 | |
| 7737 | if( used <= buf_len ) |
| 7738 | { |
| 7739 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 7740 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 7741 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 7742 | |
| 7743 | if( session->ticket != NULL ) |
| 7744 | { |
| 7745 | memcpy( p, session->ticket, session->ticket_len ); |
| 7746 | p += session->ticket_len; |
| 7747 | } |
| 7748 | |
| 7749 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 7750 | p += 4; |
| 7751 | } |
| 7752 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7753 | |
| 7754 | /* |
| 7755 | * Misc extension-related info |
| 7756 | */ |
| 7757 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 7758 | used += 1; |
| 7759 | |
| 7760 | if( used <= buf_len ) |
| 7761 | *p++ = session->mfl_code; |
| 7762 | #endif |
| 7763 | |
| 7764 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7765 | used += 1; |
| 7766 | |
| 7767 | if( used <= buf_len ) |
| 7768 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 7769 | #endif |
| 7770 | |
| 7771 | return( used ); |
| 7772 | } |
| 7773 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7774 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 7775 | const unsigned char *buf, |
| 7776 | size_t len ) |
| 7777 | { |
| 7778 | #if defined(MBEDTLS_HAVE_TIME) |
| 7779 | uint64_t start; |
| 7780 | #endif |
| 7781 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7782 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7783 | size_t cert_len; |
| 7784 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7785 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7786 | |
| 7787 | const unsigned char *p = buf; |
| 7788 | const unsigned char * const end = buf + len; |
| 7789 | |
| 7790 | /* |
| 7791 | * Time |
| 7792 | */ |
| 7793 | #if defined(MBEDTLS_HAVE_TIME) |
| 7794 | if( 8 > (size_t)( end - p ) ) |
| 7795 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7796 | |
| 7797 | start = ( (uint64_t) p[0] << 56 ) | |
| 7798 | ( (uint64_t) p[1] << 48 ) | |
| 7799 | ( (uint64_t) p[2] << 40 ) | |
| 7800 | ( (uint64_t) p[3] << 32 ) | |
| 7801 | ( (uint64_t) p[4] << 24 ) | |
| 7802 | ( (uint64_t) p[5] << 16 ) | |
| 7803 | ( (uint64_t) p[6] << 8 ) | |
| 7804 | ( (uint64_t) p[7] ); |
| 7805 | p += 8; |
| 7806 | |
| 7807 | session->start = (time_t) start; |
| 7808 | #endif /* MBEDTLS_HAVE_TIME */ |
| 7809 | |
| 7810 | /* |
| 7811 | * Basic mandatory fields |
| 7812 | */ |
| 7813 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 7814 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7815 | |
| 7816 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 7817 | p += 2; |
| 7818 | |
| 7819 | session->compression = *p++; |
| 7820 | |
| 7821 | session->id_len = *p++; |
| 7822 | memcpy( session->id, p, 32 ); |
| 7823 | p += 32; |
| 7824 | |
| 7825 | memcpy( session->master, p, 48 ); |
| 7826 | p += 48; |
| 7827 | |
| 7828 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 7829 | ( (uint32_t) p[1] << 16 ) | |
| 7830 | ( (uint32_t) p[2] << 8 ) | |
| 7831 | ( (uint32_t) p[3] ); |
| 7832 | p += 4; |
| 7833 | |
| 7834 | /* Immediately clear invalid pointer values that have been read, in case |
| 7835 | * we exit early before we replaced them with valid ones. */ |
| 7836 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7837 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7838 | session->peer_cert = NULL; |
| 7839 | #else |
| 7840 | session->peer_cert_digest = NULL; |
| 7841 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7842 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7843 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7844 | session->ticket = NULL; |
| 7845 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7846 | |
| 7847 | /* |
| 7848 | * Peer certificate |
| 7849 | */ |
| 7850 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7851 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7852 | /* Deserialize CRT from the end of the ticket. */ |
| 7853 | if( 3 > (size_t)( end - p ) ) |
| 7854 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7855 | |
| 7856 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 7857 | p += 3; |
| 7858 | |
| 7859 | if( cert_len != 0 ) |
| 7860 | { |
| 7861 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7862 | |
| 7863 | if( cert_len > (size_t)( end - p ) ) |
| 7864 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7865 | |
| 7866 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7867 | |
| 7868 | if( session->peer_cert == NULL ) |
| 7869 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7870 | |
| 7871 | mbedtls_x509_crt_init( session->peer_cert ); |
| 7872 | |
| 7873 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 7874 | p, cert_len ) ) != 0 ) |
| 7875 | { |
| 7876 | mbedtls_x509_crt_free( session->peer_cert ); |
| 7877 | mbedtls_free( session->peer_cert ); |
| 7878 | session->peer_cert = NULL; |
| 7879 | return( ret ); |
| 7880 | } |
| 7881 | |
| 7882 | p += cert_len; |
| 7883 | } |
| 7884 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7885 | /* Deserialize CRT digest from the end of the ticket. */ |
| 7886 | if( 2 > (size_t)( end - p ) ) |
| 7887 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7888 | |
| 7889 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 7890 | session->peer_cert_digest_len = (size_t) *p++; |
| 7891 | |
| 7892 | if( session->peer_cert_digest_len != 0 ) |
| 7893 | { |
| 7894 | const mbedtls_md_info_t *md_info = |
| 7895 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 7896 | if( md_info == NULL ) |
| 7897 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7898 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 7899 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7900 | |
| 7901 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 7902 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7903 | |
| 7904 | session->peer_cert_digest = |
| 7905 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 7906 | if( session->peer_cert_digest == NULL ) |
| 7907 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7908 | |
| 7909 | memcpy( session->peer_cert_digest, p, |
| 7910 | session->peer_cert_digest_len ); |
| 7911 | p += session->peer_cert_digest_len; |
| 7912 | } |
| 7913 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7914 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7915 | |
| 7916 | /* |
| 7917 | * Session ticket and associated data |
| 7918 | */ |
| 7919 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7920 | if( 3 > (size_t)( end - p ) ) |
| 7921 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7922 | |
| 7923 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 7924 | p += 3; |
| 7925 | |
| 7926 | if( session->ticket_len != 0 ) |
| 7927 | { |
| 7928 | if( session->ticket_len > (size_t)( end - p ) ) |
| 7929 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7930 | |
| 7931 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 7932 | if( session->ticket == NULL ) |
| 7933 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7934 | |
| 7935 | memcpy( session->ticket, p, session->ticket_len ); |
| 7936 | p += session->ticket_len; |
| 7937 | } |
| 7938 | |
| 7939 | if( 4 > (size_t)( end - p ) ) |
| 7940 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7941 | |
| 7942 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 7943 | ( (uint32_t) p[1] << 16 ) | |
| 7944 | ( (uint32_t) p[2] << 8 ) | |
| 7945 | ( (uint32_t) p[3] ); |
| 7946 | p += 4; |
| 7947 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7948 | |
| 7949 | /* |
| 7950 | * Misc extension-related info |
| 7951 | */ |
| 7952 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 7953 | if( 1 > (size_t)( end - p ) ) |
| 7954 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7955 | |
| 7956 | session->mfl_code = *p++; |
| 7957 | #endif |
| 7958 | |
| 7959 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7960 | if( 1 > (size_t)( end - p ) ) |
| 7961 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7962 | |
| 7963 | session->encrypt_then_mac = *p++; |
| 7964 | #endif |
| 7965 | |
| 7966 | /* Done, should have consumed entire buffer */ |
| 7967 | if( p != end ) |
| 7968 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7969 | |
| 7970 | return( 0 ); |
| 7971 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 7972 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7973 | |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 7974 | int mbedtls_ssl_validate_ciphersuite( |
| 7975 | const mbedtls_ssl_context *ssl, |
| 7976 | const mbedtls_ssl_ciphersuite_t *suite_info, |
| 7977 | mbedtls_ssl_protocol_version min_tls_version, |
| 7978 | mbedtls_ssl_protocol_version max_tls_version ) |
| 7979 | { |
| 7980 | (void) ssl; |
| 7981 | |
| 7982 | if( suite_info == NULL ) |
| 7983 | return( -1 ); |
| 7984 | |
| 7985 | if( ( suite_info->min_tls_version > max_tls_version ) || |
| 7986 | ( suite_info->max_tls_version < min_tls_version ) ) |
| 7987 | { |
| 7988 | return( -1 ); |
| 7989 | } |
| 7990 | |
XiaokangQian | 060d867 | 2022-04-21 09:24:56 +0000 | [diff] [blame] | 7991 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C) |
XiaokangQian | 75d40ef | 2022-04-20 11:05:24 +0000 | [diff] [blame] | 7992 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
| 7993 | if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE && |
| 7994 | mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 ) |
| 7995 | { |
| 7996 | return( -1 ); |
| 7997 | } |
| 7998 | #endif |
| 7999 | |
| 8000 | /* Don't suggest PSK-based ciphersuite if no PSK is available. */ |
| 8001 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 8002 | if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) && |
| 8003 | mbedtls_ssl_conf_has_static_psk( ssl->conf ) == 0 ) |
| 8004 | { |
| 8005 | return( -1 ); |
| 8006 | } |
| 8007 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 8008 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 8009 | |
| 8010 | return( 0 ); |
| 8011 | } |
| 8012 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8013 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 8014 | /* |
| 8015 | * Function for writing a signature algorithm extension. |
| 8016 | * |
| 8017 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
| 8018 | * value (TLS 1.3 RFC8446): |
| 8019 | * enum { |
| 8020 | * .... |
| 8021 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 8022 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 8023 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 8024 | * .... |
| 8025 | * } SignatureScheme; |
| 8026 | * |
| 8027 | * struct { |
| 8028 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 8029 | * } SignatureSchemeList; |
| 8030 | * |
| 8031 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 8032 | * value (TLS 1.2 RFC5246): |
| 8033 | * enum { |
| 8034 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 8035 | * sha512(6), (255) |
| 8036 | * } HashAlgorithm; |
| 8037 | * |
| 8038 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 8039 | * SignatureAlgorithm; |
| 8040 | * |
| 8041 | * struct { |
| 8042 | * HashAlgorithm hash; |
| 8043 | * SignatureAlgorithm signature; |
| 8044 | * } SignatureAndHashAlgorithm; |
| 8045 | * |
| 8046 | * SignatureAndHashAlgorithm |
| 8047 | * supported_signature_algorithms<2..2^16-2>; |
| 8048 | * |
| 8049 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 8050 | * generalization of the TLS 1.2 signature algorithm extension. |
| 8051 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 8052 | * `SignatureScheme` field of TLS 1.3 |
| 8053 | * |
| 8054 | */ |
| 8055 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 8056 | const unsigned char *end, size_t *out_len ) |
| 8057 | { |
| 8058 | unsigned char *p = buf; |
| 8059 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 8060 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 8061 | |
| 8062 | *out_len = 0; |
| 8063 | |
| 8064 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 8065 | |
| 8066 | /* Check if we have space for header and length field: |
| 8067 | * - extension_type (2 bytes) |
| 8068 | * - extension_data_length (2 bytes) |
| 8069 | * - supported_signature_algorithms_length (2 bytes) |
| 8070 | */ |
| 8071 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 8072 | p += 6; |
| 8073 | |
| 8074 | /* |
| 8075 | * Write supported_signature_algorithms |
| 8076 | */ |
| 8077 | supported_sig_alg = p; |
| 8078 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 8079 | if( sig_alg == NULL ) |
| 8080 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 8081 | |
| 8082 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
| 8083 | { |
| 8084 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 8085 | continue; |
| 8086 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 8087 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 8088 | p += 2; |
| 8089 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); |
| 8090 | } |
| 8091 | |
| 8092 | /* Length of supported_signature_algorithms */ |
| 8093 | supported_sig_alg_len = p - supported_sig_alg; |
| 8094 | if( supported_sig_alg_len == 0 ) |
| 8095 | { |
| 8096 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 8097 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 8098 | } |
| 8099 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8100 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8101 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8102 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 8103 | |
XiaokangQian | eaf3651 | 2022-04-24 09:07:44 +0000 | [diff] [blame] | 8104 | *out_len = p - buf; |
| 8105 | |
| 8106 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 8107 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 8108 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 8109 | return( 0 ); |
| 8110 | } |
| 8111 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 8112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8113 | #endif /* MBEDTLS_SSL_TLS_C */ |