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" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 41 | #include "ssl_misc.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 42 | #include "mbedtls/debug.h" |
| 43 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 44 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 45 | #include "mbedtls/version.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 46 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 47 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 48 | #include <string.h> |
| 49 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 50 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 51 | #include "mbedtls/psa_util.h" |
| 52 | #include "psa/crypto.h" |
| 53 | #endif |
| 54 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 55 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 60 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 61 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 62 | /* Top-level Connection ID API */ |
| 63 | |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 64 | int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf, |
| 65 | size_t len, |
| 66 | int ignore_other_cid ) |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 67 | { |
| 68 | if( len > MBEDTLS_SSL_CID_IN_LEN_MAX ) |
| 69 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 70 | |
Hanno Becker | 611ac77 | 2019-05-14 11:45:26 +0100 | [diff] [blame] | 71 | if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL && |
| 72 | ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 73 | { |
| 74 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 75 | } |
| 76 | |
| 77 | conf->ignore_unexpected_cid = ignore_other_cid; |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 78 | conf->cid_len = len; |
| 79 | return( 0 ); |
| 80 | } |
| 81 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 82 | int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl, |
| 83 | int enable, |
| 84 | unsigned char const *own_cid, |
| 85 | size_t own_cid_len ) |
| 86 | { |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 87 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 88 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 89 | |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 90 | ssl->negotiate_cid = enable; |
| 91 | if( enable == MBEDTLS_SSL_CID_DISABLED ) |
| 92 | { |
| 93 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) ); |
| 94 | return( 0 ); |
| 95 | } |
| 96 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) ); |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 97 | MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 98 | |
Hanno Becker | ad4a137 | 2019-05-03 13:06:44 +0100 | [diff] [blame] | 99 | if( own_cid_len != ssl->conf->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 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config", |
| 102 | (unsigned) own_cid_len, |
| 103 | (unsigned) ssl->conf->cid_len ) ); |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 104 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 105 | } |
| 106 | |
| 107 | memcpy( ssl->own_cid, own_cid, own_cid_len ); |
Hanno Becker | b7ee0cf | 2019-04-30 14:07:31 +0100 | [diff] [blame] | 108 | /* Truncation is not an issue here because |
| 109 | * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */ |
| 110 | ssl->own_cid_len = (uint8_t) own_cid_len; |
Hanno Becker | ca09224 | 2019-04-25 16:01:49 +0100 | [diff] [blame] | 111 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 112 | return( 0 ); |
| 113 | } |
| 114 | |
| 115 | int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl, |
| 116 | int *enabled, |
| 117 | unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ], |
| 118 | size_t *peer_cid_len ) |
| 119 | { |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 120 | *enabled = MBEDTLS_SSL_CID_DISABLED; |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 121 | |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 122 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 123 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 124 | { |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 125 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 76a79ab | 2019-05-03 14:38:32 +0100 | [diff] [blame] | 126 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 127 | |
Hanno Becker | c5f2422 | 2019-05-03 12:54:52 +0100 | [diff] [blame] | 128 | /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions |
| 129 | * were used, but client and server requested the empty CID. |
| 130 | * This is indistinguishable from not using the CID extension |
| 131 | * in the first place. */ |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 132 | if( ssl->transform_in->in_cid_len == 0 && |
| 133 | ssl->transform_in->out_cid_len == 0 ) |
| 134 | { |
| 135 | return( 0 ); |
| 136 | } |
| 137 | |
Hanno Becker | 615ef17 | 2019-05-22 16:50:35 +0100 | [diff] [blame] | 138 | if( peer_cid_len != NULL ) |
| 139 | { |
| 140 | *peer_cid_len = ssl->transform_in->out_cid_len; |
| 141 | if( peer_cid != NULL ) |
| 142 | { |
| 143 | memcpy( peer_cid, ssl->transform_in->out_cid, |
| 144 | ssl->transform_in->out_cid_len ); |
| 145 | } |
| 146 | } |
Hanno Becker | b1f89cd | 2019-04-26 17:08:02 +0100 | [diff] [blame] | 147 | |
| 148 | *enabled = MBEDTLS_SSL_CID_ENABLED; |
| 149 | |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 150 | return( 0 ); |
| 151 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 152 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f8542cf | 2019-04-09 15:22:03 +0100 | [diff] [blame] | 153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 157 | /* |
| 158 | * Convert max_fragment_length codes to length. |
| 159 | * RFC 6066 says: |
| 160 | * enum{ |
| 161 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 162 | * } MaxFragmentLength; |
| 163 | * and we add 0 -> extension unused |
| 164 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 165 | static unsigned int ssl_mfl_code_to_length( int mfl ) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 166 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 167 | switch( mfl ) |
| 168 | { |
| 169 | case MBEDTLS_SSL_MAX_FRAG_LEN_NONE: |
| 170 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 171 | case MBEDTLS_SSL_MAX_FRAG_LEN_512: |
| 172 | return 512; |
| 173 | case MBEDTLS_SSL_MAX_FRAG_LEN_1024: |
| 174 | return 1024; |
| 175 | case MBEDTLS_SSL_MAX_FRAG_LEN_2048: |
| 176 | return 2048; |
| 177 | case MBEDTLS_SSL_MAX_FRAG_LEN_4096: |
| 178 | return 4096; |
| 179 | default: |
| 180 | return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ); |
| 181 | } |
| 182 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 184 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 185 | int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst, |
| 186 | const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_ssl_session_free( dst ); |
| 189 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 190 | |
吴敬辉 | 0b71611 | 2021-11-29 10:46:35 +0800 | [diff] [blame] | 191 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 192 | dst->ticket = NULL; |
| 193 | #endif |
| 194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 196 | |
| 197 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 198 | if( src->peer_cert != NULL ) |
| 199 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 200 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 202 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 203 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 204 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | 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] | 209 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 212 | dst->peer_cert = NULL; |
| 213 | return( ret ); |
| 214 | } |
| 215 | } |
Hanno Becker | 6d1986e | 2019-02-07 12:27:42 +0000 | [diff] [blame] | 216 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 217 | if( src->peer_cert_digest != NULL ) |
| 218 | { |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 219 | dst->peer_cert_digest = |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 220 | mbedtls_calloc( 1, src->peer_cert_digest_len ); |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 221 | if( dst->peer_cert_digest == NULL ) |
| 222 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 223 | |
| 224 | memcpy( dst->peer_cert_digest, src->peer_cert_digest, |
| 225 | src->peer_cert_digest_len ); |
| 226 | dst->peer_cert_digest_type = src->peer_cert_digest_type; |
Hanno Becker | accc599 | 2019-02-25 10:06:59 +0000 | [diff] [blame] | 227 | dst->peer_cert_digest_len = src->peer_cert_digest_len; |
Hanno Becker | 9198ad1 | 2019-02-05 17:00:50 +0000 | [diff] [blame] | 228 | } |
| 229 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 233 | #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] | 234 | if( src->ticket != NULL ) |
| 235 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 236 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 237 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 238 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 239 | |
| 240 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 241 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 242 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 243 | |
| 244 | return( 0 ); |
| 245 | } |
| 246 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 247 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 248 | static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old ) |
| 249 | { |
| 250 | unsigned char* resized_buffer = mbedtls_calloc( 1, len_new ); |
| 251 | if( resized_buffer == NULL ) |
| 252 | return -1; |
| 253 | |
| 254 | /* We want to copy len_new bytes when downsizing the buffer, and |
| 255 | * len_old bytes when upsizing, so we choose the smaller of two sizes, |
| 256 | * to fit one buffer into another. Size checks, ensuring that no data is |
| 257 | * lost, are done outside of this function. */ |
| 258 | memcpy( resized_buffer, *buffer, |
| 259 | ( len_new < *len_old ) ? len_new : *len_old ); |
| 260 | mbedtls_platform_zeroize( *buffer, *len_old ); |
| 261 | mbedtls_free( *buffer ); |
| 262 | |
| 263 | *buffer = resized_buffer; |
| 264 | *len_old = len_new; |
| 265 | |
| 266 | return 0; |
| 267 | } |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 268 | |
| 269 | static void handle_buffer_resizing( mbedtls_ssl_context *ssl, int downsizing, |
Andrzej Kurek | 069fa96 | 2021-01-07 08:02:15 -0500 | [diff] [blame] | 270 | size_t in_buf_new_len, |
| 271 | size_t out_buf_new_len ) |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 272 | { |
| 273 | int modified = 0; |
| 274 | size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0; |
| 275 | size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0; |
| 276 | if( ssl->in_buf != NULL ) |
| 277 | { |
| 278 | written_in = ssl->in_msg - ssl->in_buf; |
| 279 | iv_offset_in = ssl->in_iv - ssl->in_buf; |
| 280 | len_offset_in = ssl->in_len - ssl->in_buf; |
| 281 | if( downsizing ? |
| 282 | ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len : |
| 283 | ssl->in_buf_len < in_buf_new_len ) |
| 284 | { |
| 285 | if( resize_buffer( &ssl->in_buf, in_buf_new_len, &ssl->in_buf_len ) != 0 ) |
| 286 | { |
| 287 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) ); |
| 288 | } |
| 289 | else |
| 290 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 291 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET, |
| 292 | in_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 293 | modified = 1; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if( ssl->out_buf != NULL ) |
| 299 | { |
| 300 | written_out = ssl->out_msg - ssl->out_buf; |
| 301 | iv_offset_out = ssl->out_iv - ssl->out_buf; |
| 302 | len_offset_out = ssl->out_len - ssl->out_buf; |
| 303 | if( downsizing ? |
| 304 | ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len : |
| 305 | ssl->out_buf_len < out_buf_new_len ) |
| 306 | { |
| 307 | if( resize_buffer( &ssl->out_buf, out_buf_new_len, &ssl->out_buf_len ) != 0 ) |
| 308 | { |
| 309 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) ); |
| 310 | } |
| 311 | else |
| 312 | { |
Paul Elliott | b744990 | 2021-03-10 18:14:58 +0000 | [diff] [blame] | 313 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET, |
| 314 | out_buf_new_len ) ); |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 315 | modified = 1; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | if( modified ) |
| 320 | { |
| 321 | /* Update pointers here to avoid doing it twice. */ |
| 322 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 323 | /* Fields below might not be properly updated with record |
| 324 | * splitting or with CID, so they are manually updated here. */ |
| 325 | ssl->out_msg = ssl->out_buf + written_out; |
| 326 | ssl->out_len = ssl->out_buf + len_offset_out; |
| 327 | ssl->out_iv = ssl->out_buf + iv_offset_out; |
| 328 | |
| 329 | ssl->in_msg = ssl->in_buf + written_in; |
| 330 | ssl->in_len = ssl->in_buf + len_offset_in; |
| 331 | ssl->in_iv = ssl->in_buf + iv_offset_in; |
| 332 | } |
| 333 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 334 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 335 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 336 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 337 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 338 | |
| 339 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 340 | mbedtls_svc_key_id_t key, |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 341 | psa_algorithm_t alg, |
| 342 | const unsigned char* seed, size_t seed_length, |
| 343 | const unsigned char* label, size_t label_length, |
| 344 | size_t capacity ) |
| 345 | { |
| 346 | psa_status_t status; |
| 347 | |
| 348 | status = psa_key_derivation_setup( derivation, alg ); |
| 349 | if( status != PSA_SUCCESS ) |
| 350 | return( status ); |
| 351 | |
| 352 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 353 | { |
| 354 | status = psa_key_derivation_input_bytes( derivation, |
| 355 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 356 | seed, seed_length ); |
| 357 | if( status != PSA_SUCCESS ) |
| 358 | return( status ); |
| 359 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 360 | if( mbedtls_svc_key_id_is_null( key ) ) |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 361 | { |
| 362 | status = psa_key_derivation_input_bytes( |
| 363 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 364 | NULL, 0 ); |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | status = psa_key_derivation_input_key( |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 369 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 370 | } |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 371 | if( status != PSA_SUCCESS ) |
| 372 | return( status ); |
| 373 | |
| 374 | status = psa_key_derivation_input_bytes( derivation, |
| 375 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 376 | label, label_length ); |
| 377 | if( status != PSA_SUCCESS ) |
| 378 | return( status ); |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 383 | } |
| 384 | |
| 385 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 386 | if( status != PSA_SUCCESS ) |
| 387 | return( status ); |
| 388 | |
| 389 | return( PSA_SUCCESS ); |
| 390 | } |
| 391 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 392 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 393 | const unsigned char *secret, size_t slen, |
| 394 | const char *label, |
| 395 | const unsigned char *random, size_t rlen, |
| 396 | unsigned char *dstbuf, size_t dlen ) |
| 397 | { |
| 398 | psa_status_t status; |
| 399 | psa_algorithm_t alg; |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 400 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 401 | psa_key_derivation_operation_t derivation = |
Janos Follath | 8dee877 | 2019-07-30 12:53:32 +0100 | [diff] [blame] | 402 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 403 | |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 404 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 405 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 406 | else |
| 407 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 408 | |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 409 | /* Normally a "secret" should be long enough to be impossible to |
| 410 | * find by brute force, and in particular should not be empty. But |
| 411 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 412 | * and for this use case it makes sense to have a 0-length "secret". |
| 413 | * Since the key API doesn't allow importing a key of length 0, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 414 | * keep master_key=0, which setup_psa_key_derivation() understands |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 415 | * to mean a 0-length "secret" input. */ |
| 416 | if( slen != 0 ) |
| 417 | { |
| 418 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 419 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 420 | psa_set_key_algorithm( &key_attributes, alg ); |
| 421 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 422 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 423 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
Gilles Peskine | 311f54d | 2019-09-23 18:19:22 +0200 | [diff] [blame] | 424 | if( status != PSA_SUCCESS ) |
| 425 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 426 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 427 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 428 | status = setup_psa_key_derivation( &derivation, |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 429 | master_key, alg, |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 430 | random, rlen, |
| 431 | (unsigned char const *) label, |
| 432 | (size_t) strlen( label ), |
| 433 | dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 434 | if( status != PSA_SUCCESS ) |
| 435 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 436 | psa_key_derivation_abort( &derivation ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 437 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 438 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 439 | } |
| 440 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 441 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 442 | if( status != PSA_SUCCESS ) |
| 443 | { |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 444 | psa_key_derivation_abort( &derivation ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 445 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 446 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 447 | } |
| 448 | |
Janos Follath | da6ac01 | 2019-08-16 13:47:29 +0100 | [diff] [blame] | 449 | status = psa_key_derivation_abort( &derivation ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 450 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 451 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 452 | psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 453 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Andrzej Kurek | 70737ca | 2019-01-14 05:37:13 -0500 | [diff] [blame] | 454 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 455 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 456 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 457 | status = psa_destroy_key( master_key ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 458 | if( status != PSA_SUCCESS ) |
| 459 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 460 | |
Andrzej Kurek | 3317126 | 2019-01-15 03:25:18 -0500 | [diff] [blame] | 461 | return( 0 ); |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 466 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 467 | const unsigned char *secret, size_t slen, |
| 468 | const char *label, |
| 469 | const unsigned char *random, size_t rlen, |
| 470 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 471 | { |
| 472 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 473 | size_t i, j, k, md_len; |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 474 | unsigned char *tmp; |
| 475 | size_t tmp_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 477 | const mbedtls_md_info_t *md_info; |
| 478 | mbedtls_md_context_t md_ctx; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 479 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 484 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 487 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 488 | tmp_len = md_len + strlen( label ) + rlen; |
| 489 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 490 | if( tmp == NULL ) |
| 491 | { |
| 492 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 493 | goto exit; |
| 494 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 495 | |
| 496 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 497 | memcpy( tmp + md_len, label, nb ); |
| 498 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 499 | nb += rlen; |
| 500 | |
| 501 | /* |
| 502 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 503 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 505 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 506 | |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 507 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 508 | if( ret != 0 ) |
| 509 | goto exit; |
| 510 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 511 | if( ret != 0 ) |
| 512 | goto exit; |
| 513 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 514 | if( ret != 0 ) |
| 515 | goto exit; |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 516 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 517 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 518 | { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 519 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 520 | if( ret != 0 ) |
| 521 | goto exit; |
| 522 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 523 | if( ret != 0 ) |
| 524 | goto exit; |
| 525 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 526 | if( ret != 0 ) |
| 527 | goto exit; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 528 | |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 529 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 530 | if( ret != 0 ) |
| 531 | goto exit; |
| 532 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 533 | if( ret != 0 ) |
| 534 | goto exit; |
| 535 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 536 | if( ret != 0 ) |
| 537 | goto exit; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 538 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 539 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 540 | |
| 541 | for( j = 0; j < k; j++ ) |
| 542 | dstbuf[i + j] = h_i[j]; |
| 543 | } |
| 544 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 545 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 547 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 548 | mbedtls_platform_zeroize( tmp, tmp_len ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 549 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 550 | |
Ron Eldor | 3b35085 | 2019-05-07 18:31:49 +0300 | [diff] [blame] | 551 | mbedtls_free( tmp ); |
| 552 | |
| 553 | return( ret ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 554 | } |
Andrzej Kurek | c929a82 | 2019-01-14 03:51:11 -0500 | [diff] [blame] | 555 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 557 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 558 | const char *label, |
| 559 | const unsigned char *random, size_t rlen, |
| 560 | unsigned char *dstbuf, size_t dlen ) |
| 561 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 563 | label, random, rlen, dstbuf, dlen ) ); |
| 564 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 566 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 567 | #if defined(MBEDTLS_SHA384_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 568 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 569 | const char *label, |
| 570 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 571 | unsigned char *dstbuf, size_t dlen ) |
| 572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 574 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 575 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 576 | #endif /* MBEDTLS_SHA384_C */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 577 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 578 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 579 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 580 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 581 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | #if defined(MBEDTLS_SHA256_C) |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 583 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 585 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 586 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 587 | #if defined(MBEDTLS_SHA384_C) |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 588 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 590 | #endif |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 591 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 592 | |
| 593 | #if defined(MBEDTLS_SHA256_C) |
| 594 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 595 | #endif |
| 596 | |
| 597 | #if defined(MBEDTLS_SHA384_C) |
| 598 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 599 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 600 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 601 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 602 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 603 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 604 | { |
| 605 | if( ssl->conf->f_psk != NULL ) |
| 606 | { |
| 607 | /* If we've used a callback to select the PSK, |
| 608 | * the static configuration is irrelevant. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 609 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 610 | return( 1 ); |
| 611 | |
| 612 | return( 0 ); |
| 613 | } |
| 614 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 615 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 616 | return( 1 ); |
| 617 | |
| 618 | return( 0 ); |
| 619 | } |
| 620 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 621 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 622 | |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 623 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 624 | { |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 625 | ((void) tls_prf); |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 626 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 627 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 628 | if( tls_prf == tls_prf_sha384 ) |
| 629 | { |
| 630 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 631 | } |
| 632 | else |
| 633 | #endif |
| 634 | #if defined(MBEDTLS_SHA256_C) |
| 635 | if( tls_prf == tls_prf_sha256 ) |
| 636 | { |
| 637 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 638 | } |
| 639 | else |
| 640 | #endif |
| 641 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 642 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 643 | } |
Ron Eldor | cf28009 | 2019-05-14 20:19:13 +0300 | [diff] [blame] | 644 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 645 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 646 | const unsigned char *secret, size_t slen, |
| 647 | const char *label, |
| 648 | const unsigned char *random, size_t rlen, |
| 649 | unsigned char *dstbuf, size_t dlen ) |
| 650 | { |
| 651 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 652 | |
| 653 | switch( prf ) |
| 654 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 655 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 656 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 657 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 658 | tls_prf = tls_prf_sha384; |
| 659 | break; |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 660 | #endif /* MBEDTLS_SHA384_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 661 | #if defined(MBEDTLS_SHA256_C) |
| 662 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 663 | tls_prf = tls_prf_sha256; |
| 664 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 665 | #endif /* MBEDTLS_SHA256_C */ |
| 666 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 667 | default: |
| 668 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 669 | } |
| 670 | |
| 671 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 672 | } |
| 673 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 674 | /* Type for the TLS PRF */ |
| 675 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 676 | const unsigned char *, size_t, |
| 677 | unsigned char *, size_t); |
| 678 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 679 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 680 | * Populate a transform structure with session keys and all the other |
| 681 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 682 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 683 | * Parameters: |
| 684 | * - [in/out]: transform: structure to populate |
| 685 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 686 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 687 | * - [in] ciphersuite |
| 688 | * - [in] master |
| 689 | * - [in] encrypt_then_mac |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 690 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 691 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 692 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 693 | * - [in] minor_ver: SSL/TLS minor version |
| 694 | * - [in] endpoint: client or server |
Andrzej Kurek | 5902cd6 | 2021-09-28 10:00:32 -0400 | [diff] [blame] | 695 | * - [in] ssl: used for: |
| 696 | * - ssl->conf->{f,p}_export_keys |
| 697 | * [in] optionally used for: |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 698 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 699 | */ |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 700 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 701 | int ciphersuite, |
| 702 | const unsigned char master[48], |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 703 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 704 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 705 | int encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 706 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 707 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 708 | ssl_tls_prf_t tls_prf, |
| 709 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 710 | int minor_ver, |
| 711 | unsigned endpoint, |
Mateusz Starzyk | e204dbf | 2021-03-15 17:57:20 +0100 | [diff] [blame] | 712 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 713 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 714 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 715 | unsigned char keyblk[256]; |
| 716 | unsigned char *key1; |
| 717 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 718 | unsigned char *mac_enc; |
| 719 | unsigned char *mac_dec; |
sander-visser | 3888b03 | 2020-05-06 21:49:46 +0200 | [diff] [blame] | 720 | size_t mac_key_len = 0; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 721 | size_t iv_copy_len; |
Gilles Peskine | 88d681c | 2021-09-01 11:19:33 +0200 | [diff] [blame] | 722 | size_t keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 723 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | const mbedtls_cipher_info_t *cipher_info; |
| 725 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 726 | |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 727 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 728 | psa_key_type_t key_type; |
| 729 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 730 | psa_algorithm_t alg; |
| 731 | size_t key_bits; |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 732 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 733 | #endif |
| 734 | |
Andrzej Kurek | 324f72e | 2021-09-29 04:21:21 -0400 | [diff] [blame] | 735 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 736 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Andrzej Kurek | a72fe64 | 2021-09-29 15:57:30 -0400 | [diff] [blame] | 737 | if( ssl->f_export_keys == NULL ) |
| 738 | { |
| 739 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 740 | (void) ssl; |
| 741 | } |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 742 | #endif |
| 743 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 744 | /* |
| 745 | * Some data just needs copying into the structure |
| 746 | */ |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 747 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 748 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 749 | transform->encrypt_then_mac = encrypt_then_mac; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 750 | #endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 751 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 752 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 753 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 754 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 755 | #endif |
| 756 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 757 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 758 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 759 | { |
| 760 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 761 | * generation separate. This should never happen. */ |
| 762 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 763 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 764 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 765 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 766 | /* |
| 767 | * Get various info structures |
| 768 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 769 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 770 | if( ciphersuite_info == NULL ) |
| 771 | { |
| 772 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 773 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 774 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 775 | } |
| 776 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 777 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 778 | if( cipher_info == NULL ) |
| 779 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 780 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 781 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 785 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 786 | if( md_info == NULL ) |
| 787 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 788 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 789 | (unsigned) ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 793 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 794 | /* Copy own and peer's CID if the use of the CID |
| 795 | * extension has been negotiated. */ |
| 796 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 797 | { |
| 798 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 799 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 800 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 801 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 802 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 803 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 804 | |
| 805 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 806 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 807 | ssl->handshake->peer_cid_len ); |
| 808 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 809 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 810 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 811 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 812 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 813 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 814 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 815 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 816 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 817 | if( ret != 0 ) |
| 818 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 819 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 820 | return( ret ); |
| 821 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 822 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 823 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 824 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 825 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 826 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 827 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 828 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 829 | /* |
| 830 | * Determine the appropriate key, IV and MAC length. |
| 831 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 832 | |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 833 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 834 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 835 | #if defined(MBEDTLS_GCM_C) || \ |
| 836 | defined(MBEDTLS_CCM_C) || \ |
| 837 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 838 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM || |
| 839 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM || |
| 840 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 841 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 842 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 843 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 844 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 845 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 846 | transform->taglen = |
| 847 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 848 | |
Hanno Becker | 447558d | 2020-05-28 07:36:33 +0100 | [diff] [blame] | 849 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 850 | * with mode and version: |
| 851 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 852 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 853 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 854 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 855 | * sequence number). |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 856 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 857 | transform->ivlen = 12; |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 858 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 859 | transform->fixed_ivlen = 12; |
| 860 | else |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 861 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 862 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 863 | /* Minimum length of encrypted record */ |
| 864 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 865 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 866 | } |
| 867 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 868 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 869 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 870 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM || |
| 871 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 872 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 873 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 875 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 876 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 877 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 878 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 879 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 880 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 881 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 882 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 883 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 884 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 885 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 886 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 887 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 888 | /* Minimum length */ |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 889 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 890 | transform->minlen = transform->maclen; |
| 891 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 892 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 893 | /* |
| 894 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 895 | * 1. if EtM is in use: one block plus MAC |
| 896 | * otherwise: * first multiple of blocklen greater than maclen |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 897 | * 2. IV |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 898 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 899 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 900 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 901 | { |
| 902 | transform->minlen = transform->maclen |
| 903 | + cipher_info->block_size; |
| 904 | } |
| 905 | else |
| 906 | #endif |
| 907 | { |
| 908 | transform->minlen = transform->maclen |
| 909 | + cipher_info->block_size |
| 910 | - transform->maclen % cipher_info->block_size; |
| 911 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 912 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 913 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 914 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 915 | { |
| 916 | transform->minlen += transform->ivlen; |
| 917 | } |
| 918 | else |
| 919 | #endif |
| 920 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 921 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 922 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 923 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 924 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 925 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 926 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 927 | else |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 928 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 929 | { |
| 930 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 931 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 932 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 933 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 934 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 935 | (unsigned) keylen, |
| 936 | (unsigned) transform->minlen, |
| 937 | (unsigned) transform->ivlen, |
| 938 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 939 | |
| 940 | /* |
| 941 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 942 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 944 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 945 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 946 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 947 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 948 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 949 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 950 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 951 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 952 | /* |
| 953 | * This is not used in TLS v1.1. |
| 954 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 955 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 956 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 957 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 958 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 959 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 960 | } |
| 961 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 962 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 963 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 964 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 965 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 966 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 967 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 968 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 969 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 970 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 971 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 972 | /* |
| 973 | * This is not used in TLS v1.1. |
| 974 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 975 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 976 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 977 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 978 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 979 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 980 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 981 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 982 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 983 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 984 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 985 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 986 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 987 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 988 | |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 989 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 990 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 991 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 992 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 993 | if( mac_key_len != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 994 | { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 995 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 996 | if( ret != 0 ) |
| 997 | goto end; |
| 998 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 999 | if( ret != 0 ) |
| 1000 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1001 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1002 | #endif |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1003 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1004 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 1005 | ((void) mac_dec); |
| 1006 | ((void) mac_enc); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 1007 | |
Andrzej Kurek | a72fe64 | 2021-09-29 15:57:30 -0400 | [diff] [blame] | 1008 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1009 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 1010 | ssl->f_export_keys( ssl->p_export_keys, |
| 1011 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 1012 | master, 48, |
| 1013 | randbytes + 32, |
| 1014 | randbytes, |
| 1015 | tls_prf_get_type( tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 1016 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 1017 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 1018 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1019 | if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type, |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1020 | transform->taglen, |
| 1021 | &alg, |
| 1022 | &key_type, |
| 1023 | &key_bits ) ) != PSA_SUCCESS ) |
| 1024 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 1025 | ret = psa_ssl_status_to_mbedtls( status ); |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1026 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1027 | goto end; |
| 1028 | } |
| 1029 | |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1030 | transform->psa_alg = alg; |
| 1031 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1032 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1033 | { |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1034 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1035 | psa_set_key_algorithm( &attributes, alg ); |
| 1036 | psa_set_key_type( &attributes, key_type ); |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 1037 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1038 | if( ( status = psa_import_key( &attributes, |
| 1039 | key1, |
| 1040 | PSA_BITS_TO_BYTES( key_bits ), |
| 1041 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 1042 | { |
| 1043 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 1044 | ret = psa_ssl_status_to_mbedtls( status ); |
| 1045 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 1046 | goto end; |
| 1047 | } |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 1048 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1049 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 1050 | |
| 1051 | if( ( status = psa_import_key( &attributes, |
| 1052 | key2, |
| 1053 | PSA_BITS_TO_BYTES( key_bits ), |
| 1054 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 1055 | { |
| 1056 | ret = psa_ssl_status_to_mbedtls( status ); |
| 1057 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 1058 | goto end; |
| 1059 | } |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1060 | } |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1061 | #else |
| 1062 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 1063 | cipher_info ) ) != 0 ) |
| 1064 | { |
| 1065 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 1066 | goto end; |
| 1067 | } |
| 1068 | |
| 1069 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 1070 | cipher_info ) ) != 0 ) |
| 1071 | { |
| 1072 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 1073 | goto end; |
| 1074 | } |
| 1075 | |
| 1076 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 1077 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 1078 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 1079 | { |
| 1080 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 1081 | goto end; |
| 1082 | } |
| 1083 | |
| 1084 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 1085 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 1086 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 1087 | { |
| 1088 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 1089 | goto end; |
| 1090 | } |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 1091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 1093 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1094 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1095 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 1096 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1097 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1099 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 1100 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1102 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 1103 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1104 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1106 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 1107 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1108 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1109 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1110 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1111 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1112 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 1113 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 1114 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame^] | 1117 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1118 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 1119 | { |
| 1120 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1121 | if( session->peer_cert != NULL ) |
| 1122 | { |
| 1123 | mbedtls_x509_crt_free( session->peer_cert ); |
| 1124 | mbedtls_free( session->peer_cert ); |
| 1125 | session->peer_cert = NULL; |
| 1126 | } |
| 1127 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1128 | if( session->peer_cert_digest != NULL ) |
| 1129 | { |
| 1130 | /* Zeroization is not necessary. */ |
| 1131 | mbedtls_free( session->peer_cert_digest ); |
| 1132 | session->peer_cert_digest = NULL; |
| 1133 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 1134 | session->peer_cert_digest_len = 0; |
| 1135 | } |
| 1136 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1137 | } |
| 1138 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 1139 | |
| 1140 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 1141 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 1142 | { |
| 1143 | ((void) ciphersuite_info); |
| 1144 | |
| 1145 | #if defined(MBEDTLS_SHA384_C) |
| 1146 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 1147 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 1148 | else |
| 1149 | #endif |
| 1150 | #if defined(MBEDTLS_SHA256_C) |
| 1151 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 1152 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 1153 | else |
| 1154 | #endif |
| 1155 | { |
| 1156 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1157 | return; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 1162 | { |
| 1163 | ((void) ssl); |
| 1164 | #if defined(MBEDTLS_SHA256_C) |
| 1165 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1166 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 1167 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 1168 | #else |
| 1169 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 1170 | #endif |
| 1171 | #endif |
| 1172 | #if defined(MBEDTLS_SHA384_C) |
| 1173 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1174 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 1175 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 1176 | #else |
| 1177 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); |
| 1178 | #endif |
| 1179 | #endif |
| 1180 | } |
| 1181 | |
| 1182 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 1183 | const unsigned char *buf, size_t len ) |
| 1184 | { |
| 1185 | #if defined(MBEDTLS_SHA256_C) |
| 1186 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1187 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 1188 | #else |
| 1189 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 1190 | #endif |
| 1191 | #endif |
| 1192 | #if defined(MBEDTLS_SHA384_C) |
| 1193 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1194 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 1195 | #else |
| 1196 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 1197 | #endif |
| 1198 | #endif |
| 1199 | } |
| 1200 | |
| 1201 | #if defined(MBEDTLS_SHA256_C) |
| 1202 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 1203 | const unsigned char *buf, size_t len ) |
| 1204 | { |
| 1205 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1206 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 1207 | #else |
| 1208 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 1209 | #endif |
| 1210 | } |
| 1211 | #endif |
| 1212 | |
| 1213 | #if defined(MBEDTLS_SHA384_C) |
| 1214 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 1215 | const unsigned char *buf, size_t len ) |
| 1216 | { |
| 1217 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1218 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 1219 | #else |
| 1220 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 1221 | #endif |
| 1222 | } |
| 1223 | #endif |
| 1224 | |
Jerry Yu | bef175d | 2022-01-28 10:52:05 +0800 | [diff] [blame] | 1225 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1226 | /* |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1227 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1228 | * |
| 1229 | * Inputs: |
| 1230 | * - SSL/TLS minor version |
| 1231 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1232 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1233 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1234 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1235 | */ |
| 1236 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1237 | int minor_ver, |
| 1238 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1239 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1240 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1241 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1242 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1243 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1244 | { |
| 1245 | handshake->tls_prf = tls_prf_sha384; |
| 1246 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1247 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1248 | } |
| 1249 | else |
| 1250 | #endif |
| 1251 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1252 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1253 | { |
| 1254 | handshake->tls_prf = tls_prf_sha256; |
| 1255 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1256 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1257 | } |
| 1258 | else |
| 1259 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1260 | { |
Jerry Yu | bef175d | 2022-01-28 10:52:05 +0800 | [diff] [blame] | 1261 | (void) hash; |
| 1262 | (void) minor_ver; |
| 1263 | (void) handshake; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1264 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1265 | } |
Jerry Yu | bef175d | 2022-01-28 10:52:05 +0800 | [diff] [blame] | 1266 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1267 | |
| 1268 | return( 0 ); |
| 1269 | } |
| 1270 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1271 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1272 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1273 | * |
| 1274 | * Parameters: |
| 1275 | * [in/out] handshake |
| 1276 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1277 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1278 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1279 | * [out] master |
| 1280 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1281 | * debug: conf->f_dbg, conf->p_dbg |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1282 | * EMS: passed to calc_verify (debug + session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1283 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1284 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1285 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1286 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1287 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1288 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1289 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1290 | |
| 1291 | /* cf. RFC 5246, Section 8.1: |
| 1292 | * "The master secret is always exactly 48 bytes in length." */ |
| 1293 | size_t const master_secret_len = 48; |
| 1294 | |
| 1295 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1296 | unsigned char session_hash[48]; |
| 1297 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1298 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1299 | /* The label for the KDF used for key expansion. |
| 1300 | * This is either "master secret" or "extended master secret" |
| 1301 | * depending on whether the Extended Master Secret extension |
| 1302 | * is used. */ |
| 1303 | char const *lbl = "master secret"; |
| 1304 | |
| 1305 | /* The salt for the KDF used for key expansion. |
| 1306 | * - If the Extended Master Secret extension is not used, |
| 1307 | * this is ClientHello.Random + ServerHello.Random |
| 1308 | * (see Sect. 8.1 in RFC 5246). |
| 1309 | * - If the Extended Master Secret extension is used, |
| 1310 | * this is the transcript of the handshake so far. |
| 1311 | * (see Sect. 4 in RFC 7627). */ |
| 1312 | unsigned char const *salt = handshake->randbytes; |
| 1313 | size_t salt_len = 64; |
| 1314 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1315 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1316 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1317 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1318 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1319 | ssl = NULL; /* make sure we don't use it except for those cases */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1320 | (void) ssl; |
| 1321 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1322 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1323 | if( handshake->resume != 0 ) |
| 1324 | { |
| 1325 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1326 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1327 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1328 | |
| 1329 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1330 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1331 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1332 | lbl = "extended master secret"; |
| 1333 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1334 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1335 | |
Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1336 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1337 | session_hash, salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1338 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1339 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1340 | |
| 1341 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1342 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1343 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1344 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1345 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1346 | { |
| 1347 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1348 | psa_status_t status; |
| 1349 | psa_algorithm_t alg; |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1350 | mbedtls_svc_key_id_t psk; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1351 | psa_key_derivation_operation_t derivation = |
| 1352 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1353 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1354 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1355 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1356 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1357 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1358 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1359 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1360 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1361 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1362 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1363 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 1364 | status = setup_psa_key_derivation( &derivation, psk, alg, |
| 1365 | salt, salt_len, |
| 1366 | (unsigned char const *) lbl, |
| 1367 | (size_t) strlen( lbl ), |
| 1368 | master_secret_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1369 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1370 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1371 | psa_key_derivation_abort( &derivation ); |
| 1372 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1373 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1374 | |
| 1375 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1376 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1377 | master_secret_len ); |
| 1378 | if( status != PSA_SUCCESS ) |
| 1379 | { |
| 1380 | psa_key_derivation_abort( &derivation ); |
| 1381 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1382 | } |
| 1383 | |
| 1384 | status = psa_key_derivation_abort( &derivation ); |
| 1385 | if( status != PSA_SUCCESS ) |
| 1386 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1387 | } |
| 1388 | else |
| 1389 | #endif |
| 1390 | { |
| 1391 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1392 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1393 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1394 | master_secret_len ); |
| 1395 | if( ret != 0 ) |
| 1396 | { |
| 1397 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1398 | return( ret ); |
| 1399 | } |
| 1400 | |
| 1401 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1402 | handshake->premaster, |
| 1403 | handshake->pmslen ); |
| 1404 | |
| 1405 | mbedtls_platform_zeroize( handshake->premaster, |
| 1406 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | return( 0 ); |
| 1410 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1411 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1412 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1413 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1414 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1415 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1416 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1417 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1418 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1419 | |
| 1420 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1421 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1422 | ssl->minor_ver, |
| 1423 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1424 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1425 | { |
| 1426 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1427 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1428 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1429 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1430 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1431 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1432 | ssl->session_negotiate->master, |
| 1433 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1434 | if( ret != 0 ) |
| 1435 | { |
| 1436 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1437 | return( ret ); |
| 1438 | } |
| 1439 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1440 | /* Swap the client and server random values: |
| 1441 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1442 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1443 | { |
| 1444 | unsigned char tmp[64]; |
| 1445 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1446 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1447 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1448 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1449 | } |
| 1450 | |
| 1451 | /* Populate transform structure */ |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 1452 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 1453 | ssl->session_negotiate->ciphersuite, |
| 1454 | ssl->session_negotiate->master, |
| 1455 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 1456 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1457 | ssl->session_negotiate->encrypt_then_mac, |
| 1458 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 1459 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1460 | ssl->handshake->tls_prf, |
| 1461 | ssl->handshake->randbytes, |
| 1462 | ssl->minor_ver, |
| 1463 | ssl->conf->endpoint, |
| 1464 | ssl ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1465 | if( ret != 0 ) |
| 1466 | { |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 1467 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1468 | return( ret ); |
| 1469 | } |
| 1470 | |
| 1471 | /* We no longer need Server/ClientHello.random values */ |
| 1472 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1473 | sizeof( ssl->handshake->randbytes ) ); |
| 1474 | |
| 1475 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1476 | |
| 1477 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1481 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1482 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1483 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1484 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1485 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1486 | size_t hash_size; |
| 1487 | psa_status_t status; |
| 1488 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1489 | |
| 1490 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1491 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1492 | if( status != PSA_SUCCESS ) |
| 1493 | { |
| 1494 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1495 | return; |
| 1496 | } |
| 1497 | |
| 1498 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1499 | if( status != PSA_SUCCESS ) |
| 1500 | { |
| 1501 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1502 | return; |
| 1503 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1504 | |
| 1505 | *hlen = 32; |
| 1506 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1507 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1508 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1509 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1510 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1511 | mbedtls_sha256_init( &sha256 ); |
| 1512 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1513 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1514 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1515 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 1516 | mbedtls_sha256_finish( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1517 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1518 | *hlen = 32; |
| 1519 | |
| 1520 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1521 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1522 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1523 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1524 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1525 | return; |
| 1526 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1527 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1528 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1529 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1530 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1531 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1532 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1533 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1534 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1535 | size_t hash_size; |
| 1536 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1537 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1538 | |
| 1539 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1540 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1541 | if( status != PSA_SUCCESS ) |
| 1542 | { |
| 1543 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1544 | return; |
| 1545 | } |
| 1546 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1547 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1548 | if( status != PSA_SUCCESS ) |
| 1549 | { |
| 1550 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1551 | return; |
| 1552 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1553 | |
| 1554 | *hlen = 48; |
| 1555 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1557 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1558 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1559 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1560 | mbedtls_sha512_init( &sha512 ); |
| 1561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1562 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1563 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1564 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 1565 | mbedtls_sha512_finish( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1566 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1567 | *hlen = 48; |
| 1568 | |
| 1569 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1570 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1571 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1572 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1573 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1574 | return; |
| 1575 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1576 | #endif /* MBEDTLS_SHA384_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1577 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1578 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1579 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1580 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1581 | unsigned char *p = ssl->handshake->premaster; |
| 1582 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Guilhem Bryant | b5f04e4 | 2020-04-01 11:23:58 +0100 | [diff] [blame] | 1583 | const unsigned char *psk = NULL; |
Guilhem Bryant | 61b0fe6 | 2020-03-27 11:12:12 +0000 | [diff] [blame] | 1584 | size_t psk_len = 0; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1585 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1586 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) |
| 1587 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1588 | { |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1589 | /* |
| 1590 | * This should never happen because the existence of a PSK is always |
| 1591 | * checked before calling this function |
| 1592 | */ |
Guilhem Bryant | 82194c8 | 2020-03-26 17:04:31 +0000 | [diff] [blame] | 1593 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Guilhem Bryant | 0c9b195 | 2020-04-08 11:02:38 +0100 | [diff] [blame] | 1594 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1595 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1596 | |
| 1597 | /* |
| 1598 | * PMS = struct { |
| 1599 | * opaque other_secret<0..2^16-1>; |
| 1600 | * opaque psk<0..2^16-1>; |
| 1601 | * }; |
| 1602 | * with "other_secret" depending on the particular key exchange |
| 1603 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1604 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1605 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1606 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1607 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1608 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1609 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1610 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1611 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1612 | |
| 1613 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1614 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1615 | |
| 1616 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1617 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1618 | } |
| 1619 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1620 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1621 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1622 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1623 | { |
| 1624 | /* |
| 1625 | * other_secret already set by the ClientKeyExchange message, |
| 1626 | * and is 48 bytes long |
| 1627 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1628 | if( end - p < 2 ) |
| 1629 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1630 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1631 | *p++ = 0; |
| 1632 | *p++ = 48; |
| 1633 | p += 48; |
| 1634 | } |
| 1635 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1637 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1638 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1639 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1640 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1641 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1642 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1643 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1644 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1645 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1646 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1647 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1648 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1649 | return( ret ); |
| 1650 | } |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1651 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 1652 | p += 2 + len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1654 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1655 | } |
| 1656 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1658 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1659 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1660 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1661 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1662 | size_t zlen; |
| 1663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1665 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1666 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1667 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1668 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1669 | return( ret ); |
| 1670 | } |
| 1671 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1672 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 1673 | p += 2 + zlen; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1674 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1675 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1676 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1677 | } |
| 1678 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1679 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1680 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1681 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1682 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1686 | if( end - p < 2 ) |
| 1687 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1688 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1689 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1690 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1691 | |
| 1692 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1693 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1694 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1695 | memcpy( p, psk, psk_len ); |
| 1696 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1697 | |
| 1698 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1699 | |
| 1700 | return( 0 ); |
| 1701 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1702 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1703 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1704 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1705 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1707 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 1708 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1709 | { |
| 1710 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 1711 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1712 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1713 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1714 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1715 | unsigned char doublings = 1; |
| 1716 | |
| 1717 | while( ratio != 0 ) |
| 1718 | { |
| 1719 | ++doublings; |
| 1720 | ratio >>= 1; |
| 1721 | } |
| 1722 | |
| 1723 | if( ++ssl->renego_records_seen > doublings ) |
| 1724 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1725 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1726 | return( 0 ); |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | return( ssl_write_hello_request( ssl ) ); |
| 1731 | } |
| 1732 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1733 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1734 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1735 | /* |
| 1736 | * Handshake functions |
| 1737 | */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1738 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1739 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1740 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1741 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1742 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1743 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1746 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1747 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1748 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1749 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1750 | ssl->state++; |
| 1751 | return( 0 ); |
| 1752 | } |
| 1753 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1754 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1755 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1756 | } |
| 1757 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1758 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1759 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1760 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1761 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1762 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1763 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1764 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1765 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1766 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1767 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1768 | ssl->state++; |
| 1769 | return( 0 ); |
| 1770 | } |
| 1771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1772 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1773 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1774 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1775 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1776 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1777 | /* Some certificate support -> implement write and parse */ |
| 1778 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1779 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1780 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1781 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1782 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1783 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1784 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1785 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1786 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1787 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1788 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1789 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1790 | { |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 1791 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 1792 | ssl->state++; |
| 1793 | return( 0 ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1794 | } |
| 1795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1796 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1797 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1798 | { |
Jerry Yu | fb28b88 | 2022-01-28 11:05:58 +0800 | [diff] [blame] | 1799 | if( ssl->handshake->client_auth == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1800 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1801 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1802 | ssl->state++; |
| 1803 | return( 0 ); |
| 1804 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1805 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1806 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1807 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1808 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1809 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1810 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1811 | { |
Hanno Becker | 9cfe6e9 | 2021-04-30 05:38:24 +0100 | [diff] [blame] | 1812 | /* Should never happen because we shouldn't have picked the |
| 1813 | * ciphersuite if we don't have a certificate. */ |
| 1814 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1815 | } |
| 1816 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1817 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1818 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1819 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1820 | |
| 1821 | /* |
| 1822 | * 0 . 0 handshake type |
| 1823 | * 1 . 3 handshake length |
| 1824 | * 4 . 6 length of all certs |
| 1825 | * 7 . 9 length of cert. 1 |
| 1826 | * 10 . n-1 peer certificate |
| 1827 | * n . n+2 length of cert. 2 |
| 1828 | * n+3 . ... upper level cert, etc. |
| 1829 | */ |
| 1830 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1831 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1832 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 1833 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1834 | { |
| 1835 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1836 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1837 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1838 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 1839 | " > %" MBEDTLS_PRINTF_SIZET, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 1840 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Hanno Becker | 91e1cc3 | 2021-04-30 05:28:49 +0100 | [diff] [blame] | 1841 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 1844 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 1845 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 1846 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1847 | |
| 1848 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 1849 | i += n; crt = crt->next; |
| 1850 | } |
| 1851 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 1852 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 1853 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 1854 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1855 | |
| 1856 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1857 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 1858 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1859 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1860 | ssl->state++; |
| 1861 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1862 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1863 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1864 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1865 | return( ret ); |
| 1866 | } |
| 1867 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1868 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1869 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1870 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 1873 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1874 | |
| 1875 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1876 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 1877 | unsigned char *crt_buf, |
| 1878 | size_t crt_buf_len ) |
| 1879 | { |
| 1880 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 1881 | |
| 1882 | if( peer_crt == NULL ) |
| 1883 | return( -1 ); |
| 1884 | |
| 1885 | if( peer_crt->raw.len != crt_buf_len ) |
| 1886 | return( -1 ); |
| 1887 | |
k-stachowiak | 95b68ef | 2019-09-16 12:21:00 +0200 | [diff] [blame] | 1888 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1889 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1890 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1891 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 1892 | unsigned char *crt_buf, |
| 1893 | size_t crt_buf_len ) |
| 1894 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1895 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1896 | unsigned char const * const peer_cert_digest = |
| 1897 | ssl->session->peer_cert_digest; |
| 1898 | mbedtls_md_type_t const peer_cert_digest_type = |
| 1899 | ssl->session->peer_cert_digest_type; |
| 1900 | mbedtls_md_info_t const * const digest_info = |
| 1901 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 1902 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 1903 | size_t digest_len; |
| 1904 | |
| 1905 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 1906 | return( -1 ); |
| 1907 | |
| 1908 | digest_len = mbedtls_md_get_size( digest_info ); |
| 1909 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 1910 | return( -1 ); |
| 1911 | |
| 1912 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 1913 | if( ret != 0 ) |
| 1914 | return( -1 ); |
| 1915 | |
| 1916 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 1917 | } |
| 1918 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 1919 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1920 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1921 | /* |
| 1922 | * Once the certificate message is read, parse it into a cert chain and |
| 1923 | * perform basic checks, but leave actual verification to the caller |
| 1924 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1925 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 1926 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1927 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1928 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1929 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1930 | int crt_cnt=0; |
| 1931 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1932 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1933 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1934 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1935 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1936 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1937 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1938 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1939 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1940 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1943 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 1944 | { |
| 1945 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1946 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 1947 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 1948 | } |
| 1949 | |
| 1950 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1951 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1952 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1953 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1954 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1955 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1958 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1959 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1960 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1961 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1962 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1963 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1964 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1965 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1966 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1967 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1968 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1969 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1970 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1971 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1974 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 1975 | i += 3; |
| 1976 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1977 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1978 | while( i < ssl->in_hslen ) |
| 1979 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1980 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1981 | if ( i + 3 > ssl->in_hslen ) { |
| 1982 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1983 | mbedtls_ssl_send_alert_message( ssl, |
| 1984 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1985 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1986 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1987 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1988 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 1989 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1990 | if( ssl->in_msg[i] != 0 ) |
| 1991 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1992 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1993 | mbedtls_ssl_send_alert_message( ssl, |
| 1994 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1995 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 1996 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1999 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2000 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 2001 | | (unsigned int) ssl->in_msg[i + 2]; |
| 2002 | i += 3; |
| 2003 | |
| 2004 | if( n < 128 || i + n > ssl->in_hslen ) |
| 2005 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2006 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 2007 | mbedtls_ssl_send_alert_message( ssl, |
| 2008 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2009 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2010 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2013 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2014 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 2015 | if( crt_cnt++ == 0 && |
| 2016 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2017 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2018 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 2019 | /* During client-side renegotiation, check that the server's |
| 2020 | * end-CRTs hasn't changed compared to the initial handshake, |
| 2021 | * mitigating the triple handshake attack. On success, reuse |
| 2022 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2023 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 2024 | if( ssl_check_peer_crt_unchanged( ssl, |
| 2025 | &ssl->in_msg[i], |
| 2026 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2027 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2028 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 2029 | mbedtls_ssl_send_alert_message( ssl, |
| 2030 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 2031 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 2032 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2033 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2034 | |
| 2035 | /* Now we can safely free the original chain. */ |
| 2036 | ssl_clear_peer_cert( ssl->session ); |
| 2037 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2038 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 2039 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2040 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2041 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2042 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2043 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 2044 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 2045 | * it in-place from the input buffer instead of making a copy. */ |
| 2046 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 2047 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2048 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2049 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2050 | case 0: /*ok*/ |
| 2051 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 2052 | /* Ignore certificate with an unknown algorithm: maybe a |
| 2053 | prior certificate was already trusted. */ |
| 2054 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2055 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2056 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 2057 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 2058 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2059 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2060 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 2061 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2062 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2063 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 2064 | default: |
| 2065 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2066 | crt_parse_der_failed: |
| 2067 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 2068 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 2069 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | i += n; |
| 2073 | } |
| 2074 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2075 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2076 | return( 0 ); |
| 2077 | } |
| 2078 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2079 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2080 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 2081 | { |
| 2082 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2083 | return( -1 ); |
| 2084 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2085 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 2086 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2087 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 2088 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 2089 | { |
| 2090 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 2091 | return( 0 ); |
| 2092 | } |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 2093 | return( -1 ); |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2094 | } |
| 2095 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2096 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2097 | /* Check if a certificate message is expected. |
| 2098 | * Return either |
| 2099 | * - SSL_CERTIFICATE_EXPECTED, or |
| 2100 | * - SSL_CERTIFICATE_SKIP |
| 2101 | * indicating whether a Certificate message is expected or not. |
| 2102 | */ |
| 2103 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 2104 | #define SSL_CERTIFICATE_SKIP 1 |
| 2105 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 2106 | int authmode ) |
| 2107 | { |
| 2108 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2109 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2110 | |
| 2111 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 2112 | return( SSL_CERTIFICATE_SKIP ); |
| 2113 | |
| 2114 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2115 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 2116 | { |
| 2117 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 2118 | return( SSL_CERTIFICATE_SKIP ); |
| 2119 | |
| 2120 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2121 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2122 | ssl->session_negotiate->verify_result = |
| 2123 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 2124 | return( SSL_CERTIFICATE_SKIP ); |
| 2125 | } |
| 2126 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 2127 | #else |
| 2128 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2129 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2130 | |
| 2131 | return( SSL_CERTIFICATE_EXPECTED ); |
| 2132 | } |
| 2133 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2134 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 2135 | int authmode, |
| 2136 | mbedtls_x509_crt *chain, |
| 2137 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2138 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2139 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2140 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 2141 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2142 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2143 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2144 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 2145 | void *p_vrfy; |
| 2146 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2147 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 2148 | return( 0 ); |
| 2149 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2150 | if( ssl->f_vrfy != NULL ) |
| 2151 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2152 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2153 | f_vrfy = ssl->f_vrfy; |
| 2154 | p_vrfy = ssl->p_vrfy; |
| 2155 | } |
| 2156 | else |
| 2157 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 2158 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 2159 | f_vrfy = ssl->conf->f_vrfy; |
| 2160 | p_vrfy = ssl->conf->p_vrfy; |
| 2161 | } |
| 2162 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2163 | /* |
| 2164 | * Main check: verify certificate |
| 2165 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2166 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 2167 | if( ssl->conf->f_ca_cb != NULL ) |
| 2168 | { |
| 2169 | ((void) rs_ctx); |
| 2170 | have_ca_chain = 1; |
| 2171 | |
| 2172 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
Jarno Lamsa | 9822c0d | 2019-04-01 16:59:48 +0300 | [diff] [blame] | 2173 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2174 | chain, |
| 2175 | ssl->conf->f_ca_cb, |
| 2176 | ssl->conf->p_ca_cb, |
| 2177 | ssl->conf->cert_profile, |
| 2178 | ssl->hostname, |
| 2179 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2180 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2181 | } |
| 2182 | else |
| 2183 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 2184 | { |
| 2185 | mbedtls_x509_crt *ca_chain; |
| 2186 | mbedtls_x509_crl *ca_crl; |
| 2187 | |
| 2188 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2189 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 2190 | { |
| 2191 | ca_chain = ssl->handshake->sni_ca_chain; |
| 2192 | ca_crl = ssl->handshake->sni_ca_crl; |
| 2193 | } |
| 2194 | else |
| 2195 | #endif |
| 2196 | { |
| 2197 | ca_chain = ssl->conf->ca_chain; |
| 2198 | ca_crl = ssl->conf->ca_crl; |
| 2199 | } |
| 2200 | |
| 2201 | if( ca_chain != NULL ) |
| 2202 | have_ca_chain = 1; |
| 2203 | |
| 2204 | ret = mbedtls_x509_crt_verify_restartable( |
| 2205 | chain, |
| 2206 | ca_chain, ca_crl, |
| 2207 | ssl->conf->cert_profile, |
| 2208 | ssl->hostname, |
| 2209 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2210 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2211 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2212 | |
| 2213 | if( ret != 0 ) |
| 2214 | { |
| 2215 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 2216 | } |
| 2217 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2218 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2219 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2220 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 2221 | #endif |
| 2222 | |
| 2223 | /* |
| 2224 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 2225 | */ |
| 2226 | |
| 2227 | #if defined(MBEDTLS_ECP_C) |
| 2228 | { |
| 2229 | const mbedtls_pk_context *pk = &chain->pk; |
| 2230 | |
| 2231 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 2232 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 2233 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 2234 | { |
| 2235 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 2236 | |
| 2237 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 2238 | if( ret == 0 ) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2239 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | #endif /* MBEDTLS_ECP_C */ |
| 2243 | |
| 2244 | if( mbedtls_ssl_check_cert_usage( chain, |
| 2245 | ciphersuite_info, |
| 2246 | ! ssl->conf->endpoint, |
| 2247 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 2248 | { |
| 2249 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 2250 | if( ret == 0 ) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2251 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 2255 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 2256 | * with details encoded in the verification flags. All other kinds |
| 2257 | * of error codes, including those from the user provided f_vrfy |
| 2258 | * functions, are treated as fatal and lead to a failure of |
| 2259 | * ssl_parse_certificate even if verification was optional. */ |
| 2260 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 2261 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2262 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2263 | { |
| 2264 | ret = 0; |
| 2265 | } |
| 2266 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2267 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2268 | { |
| 2269 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 2270 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 2271 | } |
| 2272 | |
| 2273 | if( ret != 0 ) |
| 2274 | { |
| 2275 | uint8_t alert; |
| 2276 | |
| 2277 | /* The certificate may have been rejected for several reasons. |
| 2278 | Pick one and send the corresponding alert. Which alert to send |
| 2279 | may be a subject of debate in some cases. */ |
| 2280 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 2281 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 2282 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 2283 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2284 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 2285 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2286 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 2287 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2288 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 2289 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2290 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 2291 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2292 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 2293 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2294 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 2295 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 2296 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 2297 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 2298 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 2299 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 2300 | else |
| 2301 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 2302 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2303 | alert ); |
| 2304 | } |
| 2305 | |
| 2306 | #if defined(MBEDTLS_DEBUG_C) |
| 2307 | if( ssl->session_negotiate->verify_result != 0 ) |
| 2308 | { |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2309 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2310 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2311 | } |
| 2312 | else |
| 2313 | { |
| 2314 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 2315 | } |
| 2316 | #endif /* MBEDTLS_DEBUG_C */ |
| 2317 | |
| 2318 | return( ret ); |
| 2319 | } |
| 2320 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2321 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2322 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 2323 | unsigned char *start, size_t len ) |
| 2324 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2325 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2326 | /* Remember digest of the peer's end-CRT. */ |
| 2327 | ssl->session_negotiate->peer_cert_digest = |
| 2328 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 2329 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 2330 | { |
| 2331 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
irwir | 40883e9 | 2019-09-21 17:55:33 +0300 | [diff] [blame] | 2332 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2333 | mbedtls_ssl_send_alert_message( ssl, |
| 2334 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2335 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 2336 | |
| 2337 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2338 | } |
| 2339 | |
| 2340 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 2341 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 2342 | start, len, |
| 2343 | ssl->session_negotiate->peer_cert_digest ); |
| 2344 | |
| 2345 | ssl->session_negotiate->peer_cert_digest_type = |
| 2346 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 2347 | ssl->session_negotiate->peer_cert_digest_len = |
| 2348 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 2349 | |
| 2350 | return( ret ); |
| 2351 | } |
| 2352 | |
| 2353 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 2354 | unsigned char *start, size_t len ) |
| 2355 | { |
| 2356 | unsigned char *end = start + len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2357 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2358 | |
| 2359 | /* Make a copy of the peer's raw public key. */ |
| 2360 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 2361 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 2362 | &ssl->handshake->peer_pubkey ); |
| 2363 | if( ret != 0 ) |
| 2364 | { |
| 2365 | /* We should have parsed the public key before. */ |
| 2366 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2367 | } |
| 2368 | |
| 2369 | return( 0 ); |
| 2370 | } |
| 2371 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2372 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2373 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 2374 | { |
| 2375 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2376 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2377 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2378 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 2379 | ? ssl->handshake->sni_authmode |
| 2380 | : ssl->conf->authmode; |
| 2381 | #else |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2382 | const int authmode = ssl->conf->authmode; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2383 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2384 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2385 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2386 | |
| 2387 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 2388 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2389 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 2390 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2391 | { |
| 2392 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2393 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2396 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2397 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2398 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2399 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2400 | chain = ssl->handshake->ecrs_peer_cert; |
| 2401 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2402 | goto crt_verify; |
| 2403 | } |
| 2404 | #endif |
| 2405 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 2406 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2407 | { |
| 2408 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 2409 | let it decide whether to alert. */ |
| 2410 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2411 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2412 | } |
| 2413 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2414 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2415 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 2416 | { |
| 2417 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2418 | |
irwir | d3085ab | 2020-04-21 22:26:05 +0300 | [diff] [blame] | 2419 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2420 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2421 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2422 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2423 | } |
| 2424 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2425 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2426 | /* Clear existing peer CRT structure in case we tried to |
| 2427 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2428 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2429 | |
| 2430 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 2431 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2432 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2433 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2434 | sizeof( mbedtls_x509_crt ) ) ); |
| 2435 | mbedtls_ssl_send_alert_message( ssl, |
| 2436 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2437 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2438 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2439 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2440 | goto exit; |
| 2441 | } |
| 2442 | mbedtls_x509_crt_init( chain ); |
| 2443 | |
| 2444 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2445 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2446 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2447 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2448 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2449 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2450 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2451 | |
| 2452 | crt_verify: |
| 2453 | if( ssl->handshake->ecrs_enabled) |
| 2454 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 2455 | #endif |
| 2456 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2457 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2458 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2459 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2460 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2461 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2462 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2463 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2464 | unsigned char *crt_start, *pk_start; |
| 2465 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2466 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2467 | /* We parse the CRT chain without copying, so |
| 2468 | * these pointers point into the input buffer, |
| 2469 | * and are hence still valid after freeing the |
| 2470 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2471 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2472 | crt_start = chain->raw.p; |
| 2473 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2474 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2475 | pk_start = chain->pk_raw.p; |
| 2476 | pk_len = chain->pk_raw.len; |
| 2477 | |
| 2478 | /* Free the CRT structures before computing |
| 2479 | * digest and copying the peer's public key. */ |
| 2480 | mbedtls_x509_crt_free( chain ); |
| 2481 | mbedtls_free( chain ); |
| 2482 | chain = NULL; |
| 2483 | |
| 2484 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2485 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2486 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2487 | |
| 2488 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 2489 | if( ret != 0 ) |
| 2490 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2491 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2492 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2493 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2494 | ssl->session_negotiate->peer_cert = chain; |
| 2495 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2496 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2498 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2499 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2500 | exit: |
| 2501 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2502 | if( ret == 0 ) |
| 2503 | ssl->state++; |
| 2504 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2505 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2506 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 2507 | { |
| 2508 | ssl->handshake->ecrs_peer_cert = chain; |
| 2509 | chain = NULL; |
| 2510 | } |
| 2511 | #endif |
| 2512 | |
| 2513 | if( chain != NULL ) |
| 2514 | { |
| 2515 | mbedtls_x509_crt_free( chain ); |
| 2516 | mbedtls_free( chain ); |
| 2517 | } |
| 2518 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2519 | return( ret ); |
| 2520 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2521 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2523 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2524 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2525 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2526 | { |
| 2527 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2528 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2529 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2530 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2531 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2532 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2533 | psa_status_t status; |
| 2534 | #else |
| 2535 | mbedtls_sha256_context sha256; |
| 2536 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2537 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2538 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2539 | if( !session ) |
| 2540 | session = ssl->session; |
| 2541 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2542 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 2543 | ? "client finished" |
| 2544 | : "server finished"; |
| 2545 | |
| 2546 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2547 | sha256_psa = psa_hash_operation_init(); |
| 2548 | |
| 2549 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 2550 | |
| 2551 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 2552 | if( status != PSA_SUCCESS ) |
| 2553 | { |
| 2554 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2555 | return; |
| 2556 | } |
| 2557 | |
| 2558 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 2559 | if( status != PSA_SUCCESS ) |
| 2560 | { |
| 2561 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2562 | return; |
| 2563 | } |
| 2564 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 2565 | #else |
| 2566 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2567 | mbedtls_sha256_init( &sha256 ); |
| 2568 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2569 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2570 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2571 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2572 | |
| 2573 | /* |
| 2574 | * TLSv1.2: |
| 2575 | * hash = PRF( master, finished_label, |
| 2576 | * Hash( handshake ) )[0.11] |
| 2577 | */ |
| 2578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2579 | #if !defined(MBEDTLS_SHA256_ALT) |
| 2580 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2581 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 2582 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2583 | |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2584 | mbedtls_sha256_finish( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2585 | mbedtls_sha256_free( &sha256 ); |
| 2586 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2587 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2588 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2589 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2590 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2591 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2592 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2593 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2595 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2596 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2597 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2598 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2599 | #if defined(MBEDTLS_SHA384_C) |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2600 | |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2601 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2602 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2603 | { |
| 2604 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2605 | const char *sender; |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2606 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2607 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2608 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2609 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2610 | psa_status_t status; |
| 2611 | #else |
| 2612 | mbedtls_sha512_context sha512; |
| 2613 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2614 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2615 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2616 | if( !session ) |
| 2617 | session = ssl->session; |
| 2618 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2619 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 2620 | ? "client finished" |
| 2621 | : "server finished"; |
| 2622 | |
| 2623 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2624 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2625 | |
| 2626 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 2627 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2628 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2629 | if( status != PSA_SUCCESS ) |
| 2630 | { |
| 2631 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2632 | return; |
| 2633 | } |
| 2634 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2635 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2636 | if( status != PSA_SUCCESS ) |
| 2637 | { |
| 2638 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2639 | return; |
| 2640 | } |
| 2641 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 2642 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2643 | mbedtls_sha512_init( &sha512 ); |
| 2644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2645 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2646 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2647 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2648 | |
| 2649 | /* |
| 2650 | * TLSv1.2: |
| 2651 | * hash = PRF( master, finished_label, |
| 2652 | * Hash( handshake ) )[0.11] |
| 2653 | */ |
| 2654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2655 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2656 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 2657 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 2658 | #endif |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2659 | mbedtls_sha512_finish( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2660 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2661 | mbedtls_sha512_free( &sha512 ); |
| 2662 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2663 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2664 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2665 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2667 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2668 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2669 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2671 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2672 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2673 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame^] | 2674 | |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 2675 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2676 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2677 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2678 | |
| 2679 | /* |
| 2680 | * Free our handshake params |
| 2681 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2682 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2684 | ssl->handshake = NULL; |
| 2685 | |
| 2686 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2687 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2688 | */ |
| 2689 | if( ssl->transform ) |
| 2690 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2691 | mbedtls_ssl_transform_free( ssl->transform ); |
| 2692 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2693 | } |
| 2694 | ssl->transform = ssl->transform_negotiate; |
| 2695 | ssl->transform_negotiate = NULL; |
| 2696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2697 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2698 | } |
| 2699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2700 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2701 | { |
| 2702 | int resume = ssl->handshake->resume; |
| 2703 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2704 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2706 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2707 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2709 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2710 | ssl->renego_records_seen = 0; |
| 2711 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2712 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2713 | |
| 2714 | /* |
| 2715 | * Free the previous session and switch in the current one |
| 2716 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2717 | if( ssl->session ) |
| 2718 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2719 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 2720 | /* RFC 7366 3.1: keep the EtM state */ |
| 2721 | ssl->session_negotiate->encrypt_then_mac = |
| 2722 | ssl->session->encrypt_then_mac; |
| 2723 | #endif |
| 2724 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2725 | mbedtls_ssl_session_free( ssl->session ); |
| 2726 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2727 | } |
| 2728 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2729 | ssl->session_negotiate = NULL; |
| 2730 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2731 | /* |
| 2732 | * Add cache entry |
| 2733 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2734 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2735 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2736 | resume == 0 ) |
| 2737 | { |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 2738 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 2739 | ssl->session->id, |
| 2740 | ssl->session->id_len, |
| 2741 | ssl->session ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2742 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2743 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2745 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2746 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2747 | ssl->handshake->flight != NULL ) |
| 2748 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2749 | /* Cancel handshake timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 2750 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2751 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2752 | /* Keep last flight around in case we need to resend it: |
| 2753 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2754 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2755 | } |
| 2756 | else |
| 2757 | #endif |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 2758 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2759 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2760 | ssl->state++; |
| 2761 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2762 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2765 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2766 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2767 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2769 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2770 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 2771 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 2772 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2773 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2774 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 2775 | /* |
| 2776 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 2777 | * may define some other value. Currently (early 2016), no defined |
| 2778 | * ciphersuite does this (and this is unlikely to change as activity has |
| 2779 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 2780 | */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2781 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2783 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2784 | ssl->verify_data_len = hash_len; |
| 2785 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2786 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2787 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2788 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2789 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2790 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2791 | |
| 2792 | /* |
| 2793 | * In case of session resuming, invert the client and server |
| 2794 | * ChangeCipherSpec messages order. |
| 2795 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2796 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2797 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2798 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2799 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2800 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2801 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2802 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2803 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2804 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2805 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2806 | } |
| 2807 | else |
| 2808 | ssl->state++; |
| 2809 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2810 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2811 | * Switch to our negotiated transform and session parameters for outbound |
| 2812 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2813 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2814 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2815 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2816 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2817 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2818 | { |
| 2819 | unsigned char i; |
| 2820 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2821 | /* Remember current epoch settings for resending */ |
| 2822 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 2823 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
Jerry Yu | d96a5c2 | 2021-09-29 17:46:51 +0800 | [diff] [blame] | 2824 | sizeof( ssl->handshake->alt_out_ctr ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2825 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2826 | /* Set sequence_number to zero */ |
Jerry Yu | fd320e9 | 2021-10-08 21:52:41 +0800 | [diff] [blame] | 2827 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 2828 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2829 | |
| 2830 | /* Increment epoch */ |
| 2831 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 2832 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2833 | break; |
| 2834 | |
| 2835 | /* The loop goes to its end iff the counter is wrapping */ |
| 2836 | if( i == 0 ) |
| 2837 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2838 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 2839 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2843 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Jerry Yu | fd320e9 | 2021-10-08 21:52:41 +0800 | [diff] [blame] | 2844 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2845 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2846 | ssl->transform_out = ssl->transform_negotiate; |
| 2847 | ssl->session_out = ssl->session_negotiate; |
| 2848 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2849 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2850 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2851 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2852 | #endif |
| 2853 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2854 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2855 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2856 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2857 | return( ret ); |
| 2858 | } |
| 2859 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2860 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2861 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2862 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2863 | { |
| 2864 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 2865 | return( ret ); |
| 2866 | } |
| 2867 | #endif |
| 2868 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2869 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2870 | |
| 2871 | return( 0 ); |
| 2872 | } |
| 2873 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2874 | #define SSL_MAX_HASH_LEN 12 |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2875 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2876 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2877 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2878 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a417431 | 2021-12-13 14:38:40 +0100 | [diff] [blame] | 2879 | unsigned int hash_len = 12; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2880 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2882 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2883 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2884 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2885 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2886 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2887 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2889 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2892 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2893 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2894 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2895 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2896 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2897 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 2898 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2901 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 2902 | { |
| 2903 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2904 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2905 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 2906 | goto exit; |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2911 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2912 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2913 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2914 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 2915 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 2918 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 2919 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2920 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2921 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2922 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 2923 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2924 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 2925 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2928 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2929 | ssl->verify_data_len = hash_len; |
| 2930 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2931 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2932 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2933 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2934 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2935 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2936 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2937 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2938 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2939 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2940 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2941 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2942 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2943 | } |
| 2944 | else |
| 2945 | ssl->state++; |
| 2946 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2947 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2948 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2949 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2950 | #endif |
| 2951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2952 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2953 | |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2954 | exit: |
| 2955 | mbedtls_platform_zeroize( buf, hash_len ); |
| 2956 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame^] | 2959 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2960 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2961 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2962 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2963 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2964 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2965 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2966 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2967 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 2968 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 2969 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2970 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2971 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2972 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2973 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2974 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2975 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2976 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 2977 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2978 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2979 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2980 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2981 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2982 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2983 | |
| 2984 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2985 | |
| 2986 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2987 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2988 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 2989 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2991 | #if defined(MBEDTLS_DHM_C) |
| 2992 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2993 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2994 | #if defined(MBEDTLS_ECDH_C) |
| 2995 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2996 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2997 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 2998 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 2999 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3000 | handshake->ecjpake_cache = NULL; |
| 3001 | handshake->ecjpake_cache_len = 0; |
| 3002 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3003 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3004 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3005 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3006 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3007 | #endif |
| 3008 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3009 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3010 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 3011 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3012 | |
| 3013 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3014 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3015 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 3016 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3017 | } |
| 3018 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3019 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3020 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3021 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 3022 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 3023 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3024 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 3025 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 3026 | #else |
| 3027 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 3028 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 3029 | #endif |
| 3030 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 3031 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3032 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 3033 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 3034 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3035 | } |
| 3036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3037 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3038 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3039 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3040 | } |
| 3041 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3042 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3043 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3044 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3045 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3046 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3047 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3048 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3049 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3050 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3051 | |
| 3052 | /* |
| 3053 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 3054 | * Now allocate missing structures. |
| 3055 | */ |
| 3056 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3057 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3058 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3059 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3060 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3061 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3062 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3063 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3064 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3065 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 3066 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3067 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3068 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 3069 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3070 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3071 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 3072 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3073 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 3074 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3075 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3076 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3077 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3078 | if( ssl->handshake == NULL || |
| 3079 | ssl->transform_negotiate == NULL || |
| 3080 | ssl->session_negotiate == NULL ) |
| 3081 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 3082 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3083 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3084 | mbedtls_free( ssl->handshake ); |
| 3085 | mbedtls_free( ssl->transform_negotiate ); |
| 3086 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3087 | |
| 3088 | ssl->handshake = NULL; |
| 3089 | ssl->transform_negotiate = NULL; |
| 3090 | ssl->session_negotiate = NULL; |
| 3091 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3092 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3095 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3096 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3097 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 3098 | ssl_handshake_params_init( ssl->handshake ); |
| 3099 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3100 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3101 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3102 | { |
| 3103 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3104 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3105 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 3106 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 3107 | else |
| 3108 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3109 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3110 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 3111 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 3112 | #endif |
| 3113 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3114 | /* |
| 3115 | * curve_list is translated to IANA TLS group identifiers here because |
| 3116 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 3117 | * any error codes. |
| 3118 | */ |
| 3119 | #if defined(MBEDTLS_ECP_C) |
| 3120 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 3121 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 3122 | if ( ssl->conf->curve_list != NULL ) |
| 3123 | { |
| 3124 | size_t length; |
| 3125 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 3126 | |
| 3127 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 3128 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 3129 | |
| 3130 | /* Leave room for zero termination */ |
| 3131 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 3132 | if ( group_list == NULL ) |
| 3133 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3134 | |
| 3135 | for( size_t i = 0; i < length; i++ ) |
| 3136 | { |
| 3137 | const mbedtls_ecp_curve_info *info = |
| 3138 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 3139 | if ( info == NULL ) |
| 3140 | { |
| 3141 | mbedtls_free( group_list ); |
| 3142 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3143 | } |
| 3144 | group_list[i] = info->tls_id; |
| 3145 | } |
| 3146 | |
| 3147 | group_list[length] = 0; |
| 3148 | |
| 3149 | ssl->handshake->group_list = group_list; |
| 3150 | ssl->handshake->group_list_heap_allocated = 1; |
| 3151 | } |
| 3152 | else |
| 3153 | { |
| 3154 | ssl->handshake->group_list = ssl->conf->group_list; |
| 3155 | ssl->handshake->group_list_heap_allocated = 0; |
| 3156 | } |
| 3157 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3158 | #endif /* MBEDTLS_ECP_C */ |
| 3159 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3160 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 3161 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 3162 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3163 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 3164 | signature algorithms IANA identifiers. */ |
| 3165 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3166 | ssl->conf->sig_hashes != NULL ) |
| 3167 | { |
| 3168 | const int *md; |
| 3169 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3170 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3171 | uint16_t *p; |
| 3172 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3173 | #if defined(static_assert) |
| 3174 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 3175 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 3176 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 3177 | #endif |
| 3178 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3179 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 3180 | { |
| 3181 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 3182 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3183 | #if defined(MBEDTLS_ECDSA_C) |
| 3184 | sig_algs_len += sizeof( uint16_t ); |
| 3185 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 3186 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3187 | #if defined(MBEDTLS_RSA_C) |
| 3188 | sig_algs_len += sizeof( uint16_t ); |
| 3189 | #endif |
| 3190 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 3191 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3192 | } |
| 3193 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3194 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3195 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3196 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3197 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 3198 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3199 | if( ssl->handshake->sig_algs == NULL ) |
| 3200 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3201 | |
| 3202 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 3203 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 3204 | { |
| 3205 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 3206 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 3207 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3208 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3209 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 3210 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3211 | #endif |
| 3212 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3213 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 3214 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3215 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3216 | } |
| 3217 | *p = MBEDTLS_TLS1_3_SIG_NONE; |
| 3218 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 3219 | } |
| 3220 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 3221 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3222 | { |
| 3223 | ssl->handshake->sig_algs = ssl->conf->sig_algs; |
| 3224 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 3225 | } |
| 3226 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3227 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3228 | return( 0 ); |
| 3229 | } |
| 3230 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3231 | #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] | 3232 | /* Dummy cookie callbacks for defaults */ |
| 3233 | static int ssl_cookie_write_dummy( void *ctx, |
| 3234 | unsigned char **p, unsigned char *end, |
| 3235 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3236 | { |
| 3237 | ((void) ctx); |
| 3238 | ((void) p); |
| 3239 | ((void) end); |
| 3240 | ((void) cli_id); |
| 3241 | ((void) cli_id_len); |
| 3242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3243 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | static int ssl_cookie_check_dummy( void *ctx, |
| 3247 | const unsigned char *cookie, size_t cookie_len, |
| 3248 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3249 | { |
| 3250 | ((void) ctx); |
| 3251 | ((void) cookie); |
| 3252 | ((void) cookie_len); |
| 3253 | ((void) cli_id); |
| 3254 | ((void) cli_id_len); |
| 3255 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3256 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3257 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3258 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3259 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3260 | /* |
| 3261 | * Initialize an SSL context |
| 3262 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3263 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 3264 | { |
| 3265 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 3266 | } |
| 3267 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3268 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 3269 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3270 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3271 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
| 3272 | { |
| 3273 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3274 | { |
| 3275 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) ); |
| 3276 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3277 | } |
| 3278 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 3279 | return( 0 ); |
| 3280 | } |
| 3281 | #endif |
| 3282 | |
| 3283 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3284 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3285 | { |
| 3286 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 3287 | return( 0 ); |
| 3288 | } |
| 3289 | #endif |
| 3290 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3291 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3292 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) ) |
| 3293 | { |
| 3294 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) ); |
| 3295 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3296 | } |
| 3297 | #endif |
| 3298 | |
| 3299 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 3300 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3301 | } |
| 3302 | |
| 3303 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 3304 | { |
| 3305 | int ret; |
| 3306 | ret = ssl_conf_version_check( ssl ); |
| 3307 | if( ret != 0 ) |
| 3308 | return( ret ); |
| 3309 | |
| 3310 | /* Space for further checks */ |
| 3311 | |
| 3312 | return( 0 ); |
| 3313 | } |
| 3314 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3315 | /* |
| 3316 | * Setup an SSL context |
| 3317 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 3318 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3319 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 3320 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3321 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3322 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3323 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3324 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3325 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3326 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3327 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3328 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 3329 | return( ret ); |
| 3330 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3331 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3332 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3333 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 3334 | |
| 3335 | /* Set to NULL in case of an error condition */ |
| 3336 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3337 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3338 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3339 | ssl->in_buf_len = in_buf_len; |
| 3340 | #endif |
| 3341 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3342 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3343 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3344 | 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] | 3345 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3346 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3347 | } |
| 3348 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3349 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3350 | ssl->out_buf_len = out_buf_len; |
| 3351 | #endif |
| 3352 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3353 | if( ssl->out_buf == NULL ) |
| 3354 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3355 | 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] | 3356 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3357 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3360 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3361 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3362 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 3363 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3364 | #endif |
| 3365 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3366 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3367 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3368 | |
| 3369 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3370 | |
| 3371 | error: |
| 3372 | mbedtls_free( ssl->in_buf ); |
| 3373 | mbedtls_free( ssl->out_buf ); |
| 3374 | |
| 3375 | ssl->conf = NULL; |
| 3376 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3377 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3378 | ssl->in_buf_len = 0; |
| 3379 | ssl->out_buf_len = 0; |
| 3380 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3381 | ssl->in_buf = NULL; |
| 3382 | ssl->out_buf = NULL; |
| 3383 | |
| 3384 | ssl->in_hdr = NULL; |
| 3385 | ssl->in_ctr = NULL; |
| 3386 | ssl->in_len = NULL; |
| 3387 | ssl->in_iv = NULL; |
| 3388 | ssl->in_msg = NULL; |
| 3389 | |
| 3390 | ssl->out_hdr = NULL; |
| 3391 | ssl->out_ctr = NULL; |
| 3392 | ssl->out_len = NULL; |
| 3393 | ssl->out_iv = NULL; |
| 3394 | ssl->out_msg = NULL; |
| 3395 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3396 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3400 | * Reset an initialized and used SSL context for re-use while retaining |
| 3401 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3402 | * |
| 3403 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 3404 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3405 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 3406 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 3407 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3408 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3409 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3410 | size_t in_buf_len = ssl->in_buf_len; |
| 3411 | size_t out_buf_len = ssl->out_buf_len; |
| 3412 | #else |
| 3413 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3414 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3415 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3416 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3417 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 3418 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 3419 | #endif |
| 3420 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3421 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3422 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3423 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3424 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 3425 | |
| 3426 | /* Reset incoming message parsing */ |
| 3427 | ssl->in_offt = NULL; |
| 3428 | ssl->nb_zero = 0; |
| 3429 | ssl->in_msgtype = 0; |
| 3430 | ssl->in_msglen = 0; |
| 3431 | ssl->in_hslen = 0; |
| 3432 | ssl->keep_current_message = 0; |
| 3433 | ssl->transform_in = NULL; |
| 3434 | |
| 3435 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3436 | ssl->next_record_offset = 0; |
| 3437 | ssl->in_epoch = 0; |
| 3438 | #endif |
| 3439 | |
| 3440 | /* Keep current datagram if partial == 1 */ |
| 3441 | if( partial == 0 ) |
| 3442 | { |
| 3443 | ssl->in_left = 0; |
| 3444 | memset( ssl->in_buf, 0, in_buf_len ); |
| 3445 | } |
| 3446 | |
| 3447 | /* Reset outgoing message writing */ |
| 3448 | ssl->out_msgtype = 0; |
| 3449 | ssl->out_msglen = 0; |
| 3450 | ssl->out_left = 0; |
| 3451 | memset( ssl->out_buf, 0, out_buf_len ); |
| 3452 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 3453 | ssl->transform_out = NULL; |
| 3454 | |
| 3455 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3456 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 3457 | #endif |
| 3458 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3459 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3460 | if( ssl->transform ) |
| 3461 | { |
| 3462 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3463 | mbedtls_free( ssl->transform ); |
| 3464 | ssl->transform = NULL; |
| 3465 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3466 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3467 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3468 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3469 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 3470 | mbedtls_free( ssl->transform_application ); |
| 3471 | ssl->transform_application = NULL; |
| 3472 | |
| 3473 | if( ssl->handshake != NULL ) |
| 3474 | { |
| 3475 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 3476 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 3477 | ssl->handshake->transform_earlydata = NULL; |
| 3478 | |
| 3479 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 3480 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 3481 | ssl->handshake->transform_handshake = NULL; |
| 3482 | } |
| 3483 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3484 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 3488 | { |
| 3489 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3490 | |
| 3491 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3492 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 3493 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3494 | |
| 3495 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3496 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3497 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3498 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3499 | |
| 3500 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3501 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 3502 | 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] | 3503 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3504 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3505 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3506 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 3507 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3508 | if( ssl->session ) |
| 3509 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3510 | mbedtls_ssl_session_free( ssl->session ); |
| 3511 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3512 | ssl->session = NULL; |
| 3513 | } |
| 3514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3515 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3516 | ssl->alpn_chosen = NULL; |
| 3517 | #endif |
| 3518 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3519 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3520 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3521 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3522 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3523 | { |
| 3524 | mbedtls_free( ssl->cli_id ); |
| 3525 | ssl->cli_id = NULL; |
| 3526 | ssl->cli_id_len = 0; |
| 3527 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3528 | #endif |
| 3529 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3530 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3531 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3532 | |
| 3533 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 3536 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3537 | * Reset an initialized and used SSL context for re-use while retaining |
| 3538 | * all application-set variables, function pointers and data. |
| 3539 | */ |
| 3540 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 3541 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3542 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3546 | * SSL set accessors |
| 3547 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3548 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3549 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3550 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3551 | } |
| 3552 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 3553 | 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] | 3554 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3555 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 3556 | } |
| 3557 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3558 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3559 | 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] | 3560 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3561 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3562 | } |
| 3563 | #endif |
| 3564 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3565 | 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] | 3566 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3567 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3568 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3569 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3570 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3571 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 3572 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 3573 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3574 | { |
| 3575 | ssl->disable_datagram_packing = !allow_packing; |
| 3576 | } |
| 3577 | |
| 3578 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 3579 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3580 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3581 | conf->hs_timeout_min = min; |
| 3582 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3583 | } |
| 3584 | #endif |
| 3585 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3586 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3587 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3588 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3591 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3592 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 3593 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3594 | void *p_vrfy ) |
| 3595 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3596 | conf->f_vrfy = f_vrfy; |
| 3597 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3598 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3599 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3600 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3601 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 3602 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3603 | void *p_rng ) |
| 3604 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3605 | conf->f_rng = f_rng; |
| 3606 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3609 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 3610 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3611 | void *p_dbg ) |
| 3612 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3613 | conf->f_dbg = f_dbg; |
| 3614 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3617 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3618 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 3619 | mbedtls_ssl_send_t *f_send, |
| 3620 | mbedtls_ssl_recv_t *f_recv, |
| 3621 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3622 | { |
| 3623 | ssl->p_bio = p_bio; |
| 3624 | ssl->f_send = f_send; |
| 3625 | ssl->f_recv = f_recv; |
| 3626 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 3627 | } |
| 3628 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 3629 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3630 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 3631 | { |
| 3632 | ssl->mtu = mtu; |
| 3633 | } |
| 3634 | #endif |
| 3635 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3636 | 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] | 3637 | { |
| 3638 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3639 | } |
| 3640 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3641 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 3642 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 3643 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 3644 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3645 | { |
| 3646 | ssl->p_timer = p_timer; |
| 3647 | ssl->f_set_timer = f_set_timer; |
| 3648 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3649 | |
| 3650 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3651 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3652 | } |
| 3653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3655 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 3656 | void *p_cache, |
| 3657 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 3658 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3659 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 3660 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3661 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3662 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3663 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3664 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3665 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3666 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3667 | 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] | 3668 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3669 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3670 | |
| 3671 | if( ssl == NULL || |
| 3672 | session == NULL || |
| 3673 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3674 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3675 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3676 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3677 | } |
| 3678 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3679 | if( ssl->handshake->resume == 1 ) |
| 3680 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3681 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 3682 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 3683 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3684 | return( ret ); |
| 3685 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3686 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3687 | |
| 3688 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3689 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3690 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3691 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 3692 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 3693 | const int *ciphersuites ) |
| 3694 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 3695 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3698 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 3699 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3700 | const int kex_modes ) |
| 3701 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3702 | 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] | 3703 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3704 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3706 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3707 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 3708 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3709 | { |
| 3710 | conf->cert_profile = profile; |
| 3711 | } |
| 3712 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3713 | /* Append a new keycert entry to a (possibly empty) list */ |
| 3714 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 3715 | mbedtls_x509_crt *cert, |
| 3716 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3717 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3718 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3719 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3720 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 3721 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3722 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3723 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3724 | new_cert->cert = cert; |
| 3725 | new_cert->key = key; |
| 3726 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3727 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3728 | /* Update head is the list was null, else add to the end */ |
| 3729 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 3730 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3731 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 3732 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3733 | else |
| 3734 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3735 | mbedtls_ssl_key_cert *cur = *head; |
| 3736 | while( cur->next != NULL ) |
| 3737 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3738 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3739 | } |
| 3740 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3741 | return( 0 ); |
| 3742 | } |
| 3743 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3744 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3745 | mbedtls_x509_crt *own_cert, |
| 3746 | mbedtls_pk_context *pk_key ) |
| 3747 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 3748 | 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] | 3749 | } |
| 3750 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3751 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3752 | mbedtls_x509_crt *ca_chain, |
| 3753 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3754 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3755 | conf->ca_chain = ca_chain; |
| 3756 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3757 | |
| 3758 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3759 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3760 | * cannot be used together. */ |
| 3761 | conf->f_ca_cb = NULL; |
| 3762 | conf->p_ca_cb = NULL; |
| 3763 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3764 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3765 | |
| 3766 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3767 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 3768 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3769 | void *p_ca_cb ) |
| 3770 | { |
| 3771 | conf->f_ca_cb = f_ca_cb; |
| 3772 | conf->p_ca_cb = p_ca_cb; |
| 3773 | |
| 3774 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3775 | * cannot be used together. */ |
| 3776 | conf->ca_chain = NULL; |
| 3777 | conf->ca_crl = NULL; |
| 3778 | } |
| 3779 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3780 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 3781 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3782 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3783 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 3784 | mbedtls_x509_crt *own_cert, |
| 3785 | mbedtls_pk_context *pk_key ) |
| 3786 | { |
| 3787 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 3788 | own_cert, pk_key ) ); |
| 3789 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 3790 | |
| 3791 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 3792 | mbedtls_x509_crt *ca_chain, |
| 3793 | mbedtls_x509_crl *ca_crl ) |
| 3794 | { |
| 3795 | ssl->handshake->sni_ca_chain = ca_chain; |
| 3796 | ssl->handshake->sni_ca_crl = ca_crl; |
| 3797 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3798 | |
| 3799 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 3800 | int authmode ) |
| 3801 | { |
| 3802 | ssl->handshake->sni_authmode = authmode; |
| 3803 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3804 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3805 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 3806 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3807 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 3808 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3809 | void *p_vrfy ) |
| 3810 | { |
| 3811 | ssl->f_vrfy = f_vrfy; |
| 3812 | ssl->p_vrfy = p_vrfy; |
| 3813 | } |
| 3814 | #endif |
| 3815 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3816 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3817 | /* |
| 3818 | * Set EC J-PAKE password for current handshake |
| 3819 | */ |
| 3820 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 3821 | const unsigned char *pw, |
| 3822 | size_t pw_len ) |
| 3823 | { |
| 3824 | mbedtls_ecjpake_role role; |
| 3825 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 3826 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3827 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3828 | |
| 3829 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 3830 | role = MBEDTLS_ECJPAKE_SERVER; |
| 3831 | else |
| 3832 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 3833 | |
| 3834 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 3835 | role, |
| 3836 | MBEDTLS_MD_SHA256, |
| 3837 | MBEDTLS_ECP_DP_SECP256R1, |
| 3838 | pw, pw_len ) ); |
| 3839 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3840 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3841 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3842 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3843 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3844 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 3845 | { |
| 3846 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3847 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 3848 | return( 1 ); |
| 3849 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3850 | |
| 3851 | if( conf->psk != NULL ) |
| 3852 | return( 1 ); |
| 3853 | |
| 3854 | return( 0 ); |
| 3855 | } |
| 3856 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3857 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 3858 | { |
| 3859 | /* Remove reference to existing PSK, if any. */ |
| 3860 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3861 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3862 | { |
| 3863 | /* The maintenance of the PSK key slot is the |
| 3864 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3865 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3866 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 3867 | /* This and the following branch should never |
| 3868 | * be taken simultaenously as we maintain the |
| 3869 | * invariant that raw and opaque PSKs are never |
| 3870 | * configured simultaneously. As a safeguard, |
| 3871 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3872 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3873 | if( conf->psk != NULL ) |
| 3874 | { |
| 3875 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 3876 | |
| 3877 | mbedtls_free( conf->psk ); |
| 3878 | conf->psk = NULL; |
| 3879 | conf->psk_len = 0; |
| 3880 | } |
| 3881 | |
| 3882 | /* Remove reference to PSK identity, if any. */ |
| 3883 | if( conf->psk_identity != NULL ) |
| 3884 | { |
| 3885 | mbedtls_free( conf->psk_identity ); |
| 3886 | conf->psk_identity = NULL; |
| 3887 | conf->psk_identity_len = 0; |
| 3888 | } |
| 3889 | } |
| 3890 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3891 | /* This function assumes that PSK identity in the SSL config is unset. |
| 3892 | * It checks that the provided identity is well-formed and attempts |
| 3893 | * to make a copy of it in the SSL config. |
| 3894 | * On failure, the PSK identity in the config remains unset. */ |
| 3895 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 3896 | unsigned char const *psk_identity, |
| 3897 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3898 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3899 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3900 | if( psk_identity == NULL || |
| 3901 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3902 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3903 | { |
| 3904 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3905 | } |
| 3906 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3907 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 3908 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3909 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3910 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3911 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3912 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 3913 | |
| 3914 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3915 | } |
| 3916 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3917 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 3918 | const unsigned char *psk, size_t psk_len, |
| 3919 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 3920 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3921 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3922 | |
| 3923 | /* We currently only support one PSK, raw or opaque. */ |
| 3924 | if( ssl_conf_psk_is_configured( conf ) ) |
| 3925 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3926 | |
| 3927 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3928 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3929 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3930 | if( psk_len == 0 ) |
| 3931 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3932 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 3933 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3934 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3935 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 3936 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3937 | conf->psk_len = psk_len; |
| 3938 | memcpy( conf->psk, psk, conf->psk_len ); |
| 3939 | |
| 3940 | /* Check and set PSK Identity */ |
| 3941 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 3942 | if( ret != 0 ) |
| 3943 | ssl_conf_remove_psk( conf ); |
| 3944 | |
| 3945 | return( ret ); |
| 3946 | } |
| 3947 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3948 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 3949 | { |
| 3950 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3951 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3952 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3953 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3954 | } |
| 3955 | else |
| 3956 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3957 | if( ssl->handshake->psk != NULL ) |
| 3958 | { |
| 3959 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 3960 | ssl->handshake->psk_len ); |
| 3961 | mbedtls_free( ssl->handshake->psk ); |
| 3962 | ssl->handshake->psk_len = 0; |
| 3963 | } |
| 3964 | } |
| 3965 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3966 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 3967 | const unsigned char *psk, size_t psk_len ) |
| 3968 | { |
| 3969 | if( psk == NULL || ssl->handshake == NULL ) |
| 3970 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3971 | |
| 3972 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 3973 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3974 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3975 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3976 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3977 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3978 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3979 | |
| 3980 | ssl->handshake->psk_len = psk_len; |
| 3981 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 3982 | |
| 3983 | return( 0 ); |
| 3984 | } |
| 3985 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3986 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3987 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 3988 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3989 | const unsigned char *psk_identity, |
| 3990 | size_t psk_identity_len ) |
| 3991 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3992 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3993 | |
| 3994 | /* We currently only support one PSK, raw or opaque. */ |
| 3995 | if( ssl_conf_psk_is_configured( conf ) ) |
| 3996 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3997 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3998 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3999 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4000 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 4001 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 4002 | |
| 4003 | /* Check and set PSK Identity */ |
| 4004 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 4005 | psk_identity_len ); |
| 4006 | if( ret != 0 ) |
| 4007 | ssl_conf_remove_psk( conf ); |
| 4008 | |
| 4009 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4010 | } |
| 4011 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 4012 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 4013 | mbedtls_svc_key_id_t psk ) |
| 4014 | { |
| 4015 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 4016 | ( ssl->handshake == NULL ) ) |
| 4017 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4018 | |
| 4019 | ssl_remove_psk( ssl ); |
| 4020 | ssl->handshake->psk_opaque = psk; |
| 4021 | return( 0 ); |
| 4022 | } |
| 4023 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4024 | |
| 4025 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 4026 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 4027 | size_t), |
| 4028 | void *p_psk ) |
| 4029 | { |
| 4030 | conf->f_psk = f_psk; |
| 4031 | conf->p_psk = p_psk; |
| 4032 | } |
| 4033 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 4034 | |
| 4035 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 4036 | 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] | 4037 | size_t taglen, |
| 4038 | psa_algorithm_t *alg, |
| 4039 | psa_key_type_t *key_type, |
| 4040 | size_t *key_size ) |
| 4041 | { |
| 4042 | switch ( mbedtls_cipher_type ) |
| 4043 | { |
| 4044 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 4045 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4046 | *key_type = PSA_KEY_TYPE_AES; |
| 4047 | *key_size = 128; |
| 4048 | break; |
| 4049 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 4050 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4051 | *key_type = PSA_KEY_TYPE_AES; |
| 4052 | *key_size = 128; |
| 4053 | break; |
| 4054 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 4055 | *alg = PSA_ALG_GCM; |
| 4056 | *key_type = PSA_KEY_TYPE_AES; |
| 4057 | *key_size = 128; |
| 4058 | break; |
| 4059 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 4060 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4061 | *key_type = PSA_KEY_TYPE_AES; |
| 4062 | *key_size = 192; |
| 4063 | break; |
| 4064 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 4065 | *alg = PSA_ALG_GCM; |
| 4066 | *key_type = PSA_KEY_TYPE_AES; |
| 4067 | *key_size = 192; |
| 4068 | break; |
| 4069 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 4070 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4071 | *key_type = PSA_KEY_TYPE_AES; |
| 4072 | *key_size = 256; |
| 4073 | break; |
| 4074 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 4075 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4076 | *key_type = PSA_KEY_TYPE_AES; |
| 4077 | *key_size = 256; |
| 4078 | break; |
| 4079 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 4080 | *alg = PSA_ALG_GCM; |
| 4081 | *key_type = PSA_KEY_TYPE_AES; |
| 4082 | *key_size = 256; |
| 4083 | break; |
| 4084 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 4085 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4086 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4087 | *key_size = 128; |
| 4088 | break; |
| 4089 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 4090 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4091 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4092 | *key_size = 128; |
| 4093 | break; |
| 4094 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 4095 | *alg = PSA_ALG_GCM; |
| 4096 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4097 | *key_size = 128; |
| 4098 | break; |
| 4099 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 4100 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4101 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4102 | *key_size = 192; |
| 4103 | break; |
| 4104 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 4105 | *alg = PSA_ALG_GCM; |
| 4106 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4107 | *key_size = 192; |
| 4108 | break; |
| 4109 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 4110 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4111 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4112 | *key_size = 256; |
| 4113 | break; |
| 4114 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 4115 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4116 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4117 | *key_size = 256; |
| 4118 | break; |
| 4119 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 4120 | *alg = PSA_ALG_GCM; |
| 4121 | *key_type = PSA_KEY_TYPE_ARIA; |
| 4122 | *key_size = 256; |
| 4123 | break; |
| 4124 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 4125 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4126 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4127 | *key_size = 128; |
| 4128 | break; |
| 4129 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 4130 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4131 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4132 | *key_size = 128; |
| 4133 | break; |
| 4134 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 4135 | *alg = PSA_ALG_GCM; |
| 4136 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4137 | *key_size = 128; |
| 4138 | break; |
| 4139 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 4140 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4141 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4142 | *key_size = 192; |
| 4143 | break; |
| 4144 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 4145 | *alg = PSA_ALG_GCM; |
| 4146 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4147 | *key_size = 192; |
| 4148 | break; |
| 4149 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 4150 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 4151 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4152 | *key_size = 256; |
| 4153 | break; |
| 4154 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 4155 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 4156 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4157 | *key_size = 256; |
| 4158 | break; |
| 4159 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 4160 | *alg = PSA_ALG_GCM; |
| 4161 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 4162 | *key_size = 256; |
| 4163 | break; |
| 4164 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 4165 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 4166 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 4167 | *key_size = 256; |
| 4168 | break; |
| 4169 | case MBEDTLS_CIPHER_NULL: |
| 4170 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 4171 | *key_type = 0; |
| 4172 | *key_size = 0; |
| 4173 | break; |
| 4174 | default: |
| 4175 | return PSA_ERROR_NOT_SUPPORTED; |
| 4176 | } |
| 4177 | |
| 4178 | return PSA_SUCCESS; |
| 4179 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 4180 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4181 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4182 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4183 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 4184 | const unsigned char *dhm_P, size_t P_len, |
| 4185 | const unsigned char *dhm_G, size_t G_len ) |
| 4186 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4187 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4188 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 4189 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4190 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4191 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 4192 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 4193 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 4194 | { |
| 4195 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4196 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4197 | return( ret ); |
| 4198 | } |
| 4199 | |
| 4200 | return( 0 ); |
| 4201 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4202 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4203 | 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] | 4204 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4205 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4206 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 4207 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4208 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4209 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 4210 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 4211 | &conf->dhm_P ) ) != 0 || |
| 4212 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 4213 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4214 | { |
| 4215 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4216 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4217 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4218 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4219 | |
| 4220 | return( 0 ); |
| 4221 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4222 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4223 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4224 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4225 | /* |
| 4226 | * Set the minimum length for Diffie-Hellman parameters |
| 4227 | */ |
| 4228 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 4229 | unsigned int bitlen ) |
| 4230 | { |
| 4231 | conf->dhm_min_bitlen = bitlen; |
| 4232 | } |
| 4233 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 4234 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4235 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4236 | #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] | 4237 | /* |
| 4238 | * Set allowed/preferred hashes for handshake signatures |
| 4239 | */ |
| 4240 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 4241 | const int *hashes ) |
| 4242 | { |
| 4243 | conf->sig_hashes = hashes; |
| 4244 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4245 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4246 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4247 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4248 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 4249 | const uint16_t* sig_algs ) |
| 4250 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4251 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4252 | conf->sig_hashes = NULL; |
| 4253 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 4254 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4255 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4256 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4257 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4258 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4259 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4260 | /* |
| 4261 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4262 | * |
| 4263 | * mbedtls_ssl_setup() takes the provided list |
| 4264 | * and translates it to a list of IANA TLS group identifiers, |
| 4265 | * stored in ssl->handshake->group_list. |
| 4266 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4267 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4268 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4269 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4270 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4271 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4272 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4273 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4274 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4275 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4276 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4277 | /* |
| 4278 | * Set the allowed groups |
| 4279 | */ |
| 4280 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 4281 | const uint16_t *group_list ) |
| 4282 | { |
| 4283 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4284 | conf->curve_list = NULL; |
| 4285 | #endif |
| 4286 | conf->group_list = group_list; |
| 4287 | } |
| 4288 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4289 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4290 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4291 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4292 | /* Initialize to suppress unnecessary compiler warning */ |
| 4293 | size_t hostname_len = 0; |
| 4294 | |
| 4295 | /* Check if new hostname is valid before |
| 4296 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4297 | if( hostname != NULL ) |
| 4298 | { |
| 4299 | hostname_len = strlen( hostname ); |
| 4300 | |
| 4301 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 4302 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4303 | } |
| 4304 | |
| 4305 | /* Now it's clear that we will overwrite the old hostname, |
| 4306 | * so we can free it safely */ |
| 4307 | |
| 4308 | if( ssl->hostname != NULL ) |
| 4309 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4310 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4311 | mbedtls_free( ssl->hostname ); |
| 4312 | } |
| 4313 | |
| 4314 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 4315 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4316 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4317 | { |
| 4318 | ssl->hostname = NULL; |
| 4319 | } |
| 4320 | else |
| 4321 | { |
| 4322 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4323 | if( ssl->hostname == NULL ) |
| 4324 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4325 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4326 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4327 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4328 | ssl->hostname[hostname_len] = '\0'; |
| 4329 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4330 | |
| 4331 | return( 0 ); |
| 4332 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 4333 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4334 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4335 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4336 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4337 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4338 | const unsigned char *, size_t), |
| 4339 | void *p_sni ) |
| 4340 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4341 | conf->f_sni = f_sni; |
| 4342 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4343 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4344 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4345 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4346 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4347 | 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] | 4348 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4349 | size_t cur_len, tot_len; |
| 4350 | const char **p; |
| 4351 | |
| 4352 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4353 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 4354 | * MUST NOT be truncated." |
| 4355 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4356 | */ |
| 4357 | tot_len = 0; |
| 4358 | for( p = protos; *p != NULL; p++ ) |
| 4359 | { |
| 4360 | cur_len = strlen( *p ); |
| 4361 | tot_len += cur_len; |
| 4362 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 4363 | if( ( cur_len == 0 ) || |
| 4364 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 4365 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4366 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4367 | } |
| 4368 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4369 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4370 | |
| 4371 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4372 | } |
| 4373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4374 | 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] | 4375 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4376 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4377 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4378 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4379 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4380 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4381 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 4382 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4383 | { |
| 4384 | conf->dtls_srtp_mki_support = support_mki_value; |
| 4385 | } |
| 4386 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4387 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 4388 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 4389 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4390 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4391 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4392 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4393 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4394 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4395 | |
| 4396 | 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] | 4397 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4398 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4399 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4400 | |
| 4401 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 4402 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4403 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4404 | } |
| 4405 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4406 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4407 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4408 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4409 | const mbedtls_ssl_srtp_profile *p; |
| 4410 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4411 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4412 | /* check the profiles list: all entry must be valid, |
| 4413 | * 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] | 4414 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 4415 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 4416 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4417 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4418 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4419 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 4420 | list_size++; |
| 4421 | } |
| 4422 | else |
| 4423 | { |
| 4424 | /* unsupported value, stop parsing and set the size to an error value */ |
| 4425 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4426 | } |
| 4427 | } |
| 4428 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4429 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 4430 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4431 | conf->dtls_srtp_profile_list = NULL; |
| 4432 | conf->dtls_srtp_profile_list_len = 0; |
| 4433 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4434 | } |
| 4435 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 4436 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4437 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4438 | |
| 4439 | return( 0 ); |
| 4440 | } |
| 4441 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4442 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 4443 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4444 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4445 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 4446 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4447 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4448 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4449 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4450 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4451 | else |
| 4452 | { |
| 4453 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4454 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 4455 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4456 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4457 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4458 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 4459 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4460 | 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] | 4461 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4462 | conf->max_major_ver = major; |
| 4463 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4466 | 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] | 4467 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4468 | conf->min_major_ver = major; |
| 4469 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4472 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4473 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 4474 | char cert_req_ca_list ) |
| 4475 | { |
| 4476 | conf->cert_req_ca_list = cert_req_ca_list; |
| 4477 | } |
| 4478 | #endif |
| 4479 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4480 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4481 | 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] | 4482 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4483 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 4484 | } |
| 4485 | #endif |
| 4486 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4487 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4488 | 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] | 4489 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4490 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 4491 | } |
| 4492 | #endif |
| 4493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4494 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4495 | 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] | 4496 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4497 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4498 | 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] | 4499 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4500 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4501 | } |
| 4502 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 4503 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4504 | |
| 4505 | return( 0 ); |
| 4506 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4507 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4508 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4509 | 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] | 4510 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4511 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4514 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4515 | 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] | 4516 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4517 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4518 | } |
| 4519 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4520 | 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] | 4521 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4522 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 4523 | } |
| 4524 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4525 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4526 | const unsigned char period[8] ) |
| 4527 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4528 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4529 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4530 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4532 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4533 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4534 | 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] | 4535 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 4536 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4537 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4538 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4539 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4540 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4541 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 4542 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 4543 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 4544 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4545 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4546 | conf->f_ticket_write = f_ticket_write; |
| 4547 | conf->f_ticket_parse = f_ticket_parse; |
| 4548 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4549 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4550 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4551 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4552 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 4553 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 4554 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 4555 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4556 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 4557 | ssl->f_export_keys = f_export_keys; |
| 4558 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 4559 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4560 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4561 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4562 | void mbedtls_ssl_conf_async_private_cb( |
| 4563 | mbedtls_ssl_config *conf, |
| 4564 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 4565 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 4566 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 4567 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4568 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4569 | { |
| 4570 | conf->f_async_sign_start = f_async_sign; |
| 4571 | conf->f_async_decrypt_start = f_async_decrypt; |
| 4572 | conf->f_async_resume = f_async_resume; |
| 4573 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4574 | conf->p_async_config_data = async_config_data; |
| 4575 | } |
| 4576 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4577 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 4578 | { |
| 4579 | return( conf->p_async_config_data ); |
| 4580 | } |
| 4581 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4582 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4583 | { |
| 4584 | if( ssl->handshake == NULL ) |
| 4585 | return( NULL ); |
| 4586 | else |
| 4587 | return( ssl->handshake->user_async_ctx ); |
| 4588 | } |
| 4589 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4590 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4591 | void *ctx ) |
| 4592 | { |
| 4593 | if( ssl->handshake != NULL ) |
| 4594 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4595 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4596 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4597 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4598 | /* |
| 4599 | * SSL get accessors |
| 4600 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4601 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4602 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4603 | if( ssl->session != NULL ) |
| 4604 | return( ssl->session->verify_result ); |
| 4605 | |
| 4606 | if( ssl->session_negotiate != NULL ) |
| 4607 | return( ssl->session_negotiate->verify_result ); |
| 4608 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 4609 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 4612 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 4613 | { |
| 4614 | if( ssl == NULL || ssl->session == NULL ) |
| 4615 | return( 0 ); |
| 4616 | |
| 4617 | return( ssl->session->ciphersuite ); |
| 4618 | } |
| 4619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4620 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4621 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4622 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4623 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4625 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4626 | } |
| 4627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4628 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4629 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4630 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4631 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4632 | { |
| 4633 | switch( ssl->minor_ver ) |
| 4634 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4635 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4636 | return( "DTLSv1.2" ); |
| 4637 | |
| 4638 | default: |
| 4639 | return( "unknown (DTLS)" ); |
| 4640 | } |
| 4641 | } |
| 4642 | #endif |
| 4643 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4644 | switch( ssl->minor_ver ) |
| 4645 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4646 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4647 | return( "TLSv1.2" ); |
| 4648 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4649 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4650 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4651 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4654 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4655 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 4656 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 4657 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4658 | size_t read_mfl; |
| 4659 | |
| 4660 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 4661 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4662 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 4663 | { |
| 4664 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 4665 | } |
| 4666 | |
| 4667 | /* Check if a smaller max length was negotiated */ |
| 4668 | if( ssl->session_out != NULL ) |
| 4669 | { |
| 4670 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 4671 | if( read_mfl < max_len ) |
| 4672 | { |
| 4673 | max_len = read_mfl; |
| 4674 | } |
| 4675 | } |
| 4676 | |
| 4677 | // During a handshake, use the value being negotiated |
| 4678 | if( ssl->session_negotiate != NULL ) |
| 4679 | { |
| 4680 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4681 | if( read_mfl < max_len ) |
| 4682 | { |
| 4683 | max_len = read_mfl; |
| 4684 | } |
| 4685 | } |
| 4686 | |
| 4687 | return( max_len ); |
| 4688 | } |
| 4689 | |
| 4690 | 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] | 4691 | { |
| 4692 | size_t max_len; |
| 4693 | |
| 4694 | /* |
| 4695 | * Assume mfl_code is correct since it was checked when set |
| 4696 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4697 | 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] | 4698 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4699 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4700 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4701 | 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] | 4702 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4703 | 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] | 4704 | } |
| 4705 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4706 | /* During a handshake, use the value being negotiated */ |
| 4707 | if( ssl->session_negotiate != NULL && |
| 4708 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 4709 | { |
| 4710 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4711 | } |
| 4712 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4713 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4714 | } |
| 4715 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4716 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4717 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4718 | 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] | 4719 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 4720 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 4721 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4722 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 4723 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 4724 | return ( 0 ); |
| 4725 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4726 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 4727 | return( ssl->mtu ); |
| 4728 | |
| 4729 | if( ssl->mtu == 0 ) |
| 4730 | return( ssl->handshake->mtu ); |
| 4731 | |
| 4732 | return( ssl->mtu < ssl->handshake->mtu ? |
| 4733 | ssl->mtu : ssl->handshake->mtu ); |
| 4734 | } |
| 4735 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4736 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4737 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 4738 | { |
| 4739 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 4740 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 4741 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4742 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4743 | (void) ssl; |
| 4744 | #endif |
| 4745 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4746 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4747 | 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] | 4748 | |
| 4749 | if( max_len > mfl ) |
| 4750 | max_len = mfl; |
| 4751 | #endif |
| 4752 | |
| 4753 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4754 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4755 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4756 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4757 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 4758 | const size_t overhead = (size_t) ret; |
| 4759 | |
| 4760 | if( ret < 0 ) |
| 4761 | return( ret ); |
| 4762 | |
| 4763 | if( mtu <= overhead ) |
| 4764 | { |
| 4765 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 4766 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4767 | } |
| 4768 | |
| 4769 | if( max_len > mtu - overhead ) |
| 4770 | max_len = mtu - overhead; |
| 4771 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4772 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4773 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 4774 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4775 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4776 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4777 | #endif |
| 4778 | |
| 4779 | return( (int) max_len ); |
| 4780 | } |
| 4781 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4782 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 4783 | { |
| 4784 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 4785 | |
| 4786 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4787 | (void) ssl; |
| 4788 | #endif |
| 4789 | |
| 4790 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4791 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 4792 | |
| 4793 | if( max_len > mfl ) |
| 4794 | max_len = mfl; |
| 4795 | #endif |
| 4796 | |
| 4797 | return( (int) max_len ); |
| 4798 | } |
| 4799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4800 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4801 | 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] | 4802 | { |
| 4803 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4804 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4805 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 4806 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4807 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 4808 | #else |
| 4809 | return( NULL ); |
| 4810 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4811 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4812 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4813 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4814 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 4815 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 4816 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4817 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4818 | int ret; |
| 4819 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4820 | if( ssl == NULL || |
| 4821 | dst == NULL || |
| 4822 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4823 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4824 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4825 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4826 | } |
| 4827 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4828 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 4829 | * idempotent: Each session can only be exported once. |
| 4830 | * |
| 4831 | * (This is in preparation for TLS 1.3 support where we will |
| 4832 | * need the ability to export multiple sessions (aka tickets), |
| 4833 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 4834 | * multiple times until it fails.) |
| 4835 | * |
| 4836 | * Check whether we have already exported the current session, |
| 4837 | * and fail if so. |
| 4838 | */ |
| 4839 | if( ssl->session->exported == 1 ) |
| 4840 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4841 | |
| 4842 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 4843 | if( ret != 0 ) |
| 4844 | return( ret ); |
| 4845 | |
| 4846 | /* Remember that we've exported the session. */ |
| 4847 | ssl->session->exported = 1; |
| 4848 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4849 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4850 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4851 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4852 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4853 | * Define ticket header determining Mbed TLS version |
| 4854 | * and structure of the ticket. |
| 4855 | */ |
| 4856 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4857 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4858 | * Define bitflag determining compile-time settings influencing |
| 4859 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4860 | */ |
| 4861 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4862 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4863 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4864 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4865 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4866 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4867 | |
| 4868 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4869 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4870 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4871 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4872 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4873 | |
| 4874 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4875 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4876 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4877 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4878 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 4879 | |
| 4880 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4881 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4882 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4883 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4884 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4885 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4886 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4887 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4888 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4889 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4890 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 4891 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4892 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4893 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4894 | #else |
| 4895 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4896 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4897 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4898 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4899 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4900 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4901 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 4902 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4903 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4904 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4905 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4906 | ( (uint16_t) ( \ |
| 4907 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 4908 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 4909 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 4910 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4911 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 4912 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4913 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4914 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4915 | MBEDTLS_VERSION_MAJOR, |
| 4916 | MBEDTLS_VERSION_MINOR, |
| 4917 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4918 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4919 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4920 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4921 | |
| 4922 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4923 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4924 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4925 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4926 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4927 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 4928 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4929 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4930 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4931 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4932 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4933 | * Note: When updating the format, remember to keep |
| 4934 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4935 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4936 | * // In this version, `session_format` determines |
| 4937 | * // the setting of those compile-time |
| 4938 | * // configuration options which influence |
| 4939 | * // the structure of mbedtls_ssl_session. |
| 4940 | * |
Hanno Becker | fa0d61e | 2021-08-02 08:56:14 +0100 | [diff] [blame] | 4941 | * uint8_t minor_ver; // Protocol-version. Possible values: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4942 | * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3) |
| 4943 | * |
| 4944 | * select (serialized_session.minor_ver) { |
| 4945 | * |
| 4946 | * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2 |
| 4947 | * serialized_session_tls12 data; |
| 4948 | * |
| 4949 | * }; |
| 4950 | * |
| 4951 | * } serialized_session; |
| 4952 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4953 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4954 | |
| 4955 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4956 | /* Serialization of TLS 1.2 sessions: |
| 4957 | * |
| 4958 | * struct { |
| 4959 | * uint64 start_time; |
| 4960 | * uint8 ciphersuite[2]; // defined by the standard |
| 4961 | * uint8 compression; // 0 or 1 |
| 4962 | * uint8 session_id_len; // at most 32 |
| 4963 | * opaque session_id[32]; |
| 4964 | * opaque master[48]; // fixed length in the standard |
| 4965 | * uint32 verify_result; |
| 4966 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4967 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4968 | * uint32 ticket_lifetime; |
| 4969 | * uint8 mfl_code; // up to 255 according to standard |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4970 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4971 | * } serialized_session_tls12; |
| 4972 | * |
| 4973 | */ |
| 4974 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 4975 | unsigned char *buf, |
| 4976 | size_t buf_len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4977 | { |
| 4978 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4979 | size_t used = 0; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4980 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4981 | #if defined(MBEDTLS_HAVE_TIME) |
| 4982 | uint64_t start; |
| 4983 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4984 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4985 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4986 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4987 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4988 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4989 | |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4990 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4991 | * Time |
| 4992 | */ |
| 4993 | #if defined(MBEDTLS_HAVE_TIME) |
| 4994 | used += 8; |
| 4995 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4996 | if( used <= buf_len ) |
| 4997 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4998 | start = (uint64_t) session->start; |
| 4999 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5000 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 5001 | p += 8; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5002 | } |
| 5003 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5004 | |
| 5005 | /* |
| 5006 | * Basic mandatory fields |
| 5007 | */ |
| 5008 | used += 2 /* ciphersuite */ |
| 5009 | + 1 /* compression */ |
| 5010 | + 1 /* id_len */ |
| 5011 | + sizeof( session->id ) |
| 5012 | + sizeof( session->master ) |
| 5013 | + 4; /* verify_result */ |
| 5014 | |
| 5015 | if( used <= buf_len ) |
| 5016 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5017 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 5018 | p += 2; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5019 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5020 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5021 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5022 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5023 | memcpy( p, session->id, 32 ); |
| 5024 | p += 32; |
| 5025 | |
| 5026 | memcpy( p, session->master, 48 ); |
| 5027 | p += 48; |
| 5028 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5029 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 5030 | p += 4; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5031 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5032 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5033 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5034 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5035 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5036 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5037 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5038 | if( session->peer_cert == NULL ) |
| 5039 | cert_len = 0; |
| 5040 | else |
| 5041 | cert_len = session->peer_cert->raw.len; |
| 5042 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5043 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5044 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5045 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5046 | { |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5047 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 5048 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 5049 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5050 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5051 | if( session->peer_cert != NULL ) |
| 5052 | { |
| 5053 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 5054 | p += cert_len; |
| 5055 | } |
| 5056 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5057 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5058 | if( session->peer_cert_digest != NULL ) |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5059 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5060 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 5061 | if( used <= buf_len ) |
| 5062 | { |
| 5063 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 5064 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 5065 | memcpy( p, session->peer_cert_digest, |
| 5066 | session->peer_cert_digest_len ); |
| 5067 | p += session->peer_cert_digest_len; |
| 5068 | } |
| 5069 | } |
| 5070 | else |
| 5071 | { |
| 5072 | used += 2; |
| 5073 | if( used <= buf_len ) |
| 5074 | { |
| 5075 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 5076 | *p++ = 0; |
| 5077 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5078 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5079 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5080 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5081 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5082 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5083 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5084 | */ |
| 5085 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5086 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5087 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5088 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5089 | { |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5090 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 5091 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 5092 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5093 | |
| 5094 | if( session->ticket != NULL ) |
| 5095 | { |
| 5096 | memcpy( p, session->ticket, session->ticket_len ); |
| 5097 | p += session->ticket_len; |
| 5098 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5099 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5100 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 5101 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5102 | } |
| 5103 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5104 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5105 | /* |
| 5106 | * Misc extension-related info |
| 5107 | */ |
| 5108 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5109 | used += 1; |
| 5110 | |
| 5111 | if( used <= buf_len ) |
| 5112 | *p++ = session->mfl_code; |
| 5113 | #endif |
| 5114 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5115 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5116 | used += 1; |
| 5117 | |
| 5118 | if( used <= buf_len ) |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5119 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5120 | #endif |
| 5121 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5122 | return( used ); |
| 5123 | } |
| 5124 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5125 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5126 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 5127 | unsigned char omit_header, |
| 5128 | unsigned char *buf, |
| 5129 | size_t buf_len, |
| 5130 | size_t *olen ) |
| 5131 | { |
| 5132 | unsigned char *p = buf; |
| 5133 | size_t used = 0; |
| 5134 | |
| 5135 | if( !omit_header ) |
| 5136 | { |
| 5137 | /* |
| 5138 | * Add Mbed TLS version identifier |
| 5139 | */ |
| 5140 | |
| 5141 | used += sizeof( ssl_serialized_session_header ); |
| 5142 | |
| 5143 | if( used <= buf_len ) |
| 5144 | { |
| 5145 | memcpy( p, ssl_serialized_session_header, |
| 5146 | sizeof( ssl_serialized_session_header ) ); |
| 5147 | p += sizeof( ssl_serialized_session_header ); |
| 5148 | } |
| 5149 | } |
| 5150 | |
| 5151 | /* |
| 5152 | * TLS version identifier |
| 5153 | */ |
| 5154 | used += 1; |
| 5155 | if( used <= buf_len ) |
| 5156 | { |
| 5157 | *p++ = session->minor_ver; |
| 5158 | } |
| 5159 | |
| 5160 | /* Forward to version-specific serialization routine. */ |
| 5161 | switch( session->minor_ver ) |
| 5162 | { |
| 5163 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5164 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 5165 | { |
| 5166 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 5167 | used += ssl_session_save_tls12( session, p, remaining_len ); |
| 5168 | break; |
| 5169 | } |
| 5170 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5171 | |
| 5172 | default: |
| 5173 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5174 | } |
| 5175 | |
| 5176 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 5177 | if( used > buf_len ) |
| 5178 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5179 | |
| 5180 | return( 0 ); |
| 5181 | } |
| 5182 | |
| 5183 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5184 | * Public wrapper for ssl_session_save() |
| 5185 | */ |
| 5186 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 5187 | unsigned char *buf, |
| 5188 | size_t buf_len, |
| 5189 | size_t *olen ) |
| 5190 | { |
| 5191 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 5192 | } |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 5193 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5194 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5195 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5196 | * |
| 5197 | * This internal version is wrapped by a public function that cleans up in |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5198 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5199 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5200 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 5201 | const unsigned char *buf, |
| 5202 | size_t len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5203 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5204 | #if defined(MBEDTLS_HAVE_TIME) |
| 5205 | uint64_t start; |
| 5206 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5207 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5208 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5209 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5210 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5211 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5212 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5213 | const unsigned char *p = buf; |
| 5214 | const unsigned char * const end = buf + len; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5215 | |
| 5216 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5217 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5218 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5219 | #if defined(MBEDTLS_HAVE_TIME) |
| 5220 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5221 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5222 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5223 | start = ( (uint64_t) p[0] << 56 ) | |
| 5224 | ( (uint64_t) p[1] << 48 ) | |
| 5225 | ( (uint64_t) p[2] << 40 ) | |
| 5226 | ( (uint64_t) p[3] << 32 ) | |
| 5227 | ( (uint64_t) p[4] << 24 ) | |
| 5228 | ( (uint64_t) p[5] << 16 ) | |
| 5229 | ( (uint64_t) p[6] << 8 ) | |
| 5230 | ( (uint64_t) p[7] ); |
| 5231 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5232 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5233 | session->start = (time_t) start; |
| 5234 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5235 | |
| 5236 | /* |
| 5237 | * Basic mandatory fields |
| 5238 | */ |
| 5239 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 5240 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5241 | |
| 5242 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 5243 | p += 2; |
| 5244 | |
| 5245 | session->compression = *p++; |
| 5246 | |
| 5247 | session->id_len = *p++; |
| 5248 | memcpy( session->id, p, 32 ); |
| 5249 | p += 32; |
| 5250 | |
| 5251 | memcpy( session->master, p, 48 ); |
| 5252 | p += 48; |
| 5253 | |
| 5254 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 5255 | ( (uint32_t) p[1] << 16 ) | |
| 5256 | ( (uint32_t) p[2] << 8 ) | |
| 5257 | ( (uint32_t) p[3] ); |
| 5258 | p += 4; |
| 5259 | |
| 5260 | /* Immediately clear invalid pointer values that have been read, in case |
| 5261 | * we exit early before we replaced them with valid ones. */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5262 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5263 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5264 | session->peer_cert = NULL; |
| 5265 | #else |
| 5266 | session->peer_cert_digest = NULL; |
| 5267 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5268 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5269 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5270 | session->ticket = NULL; |
| 5271 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5272 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5273 | /* |
| 5274 | * Peer certificate |
| 5275 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5276 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5277 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5278 | /* Deserialize CRT from the end of the ticket. */ |
| 5279 | if( 3 > (size_t)( end - p ) ) |
| 5280 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5281 | |
| 5282 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5283 | p += 3; |
| 5284 | |
| 5285 | if( cert_len != 0 ) |
| 5286 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5287 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5288 | |
| 5289 | if( cert_len > (size_t)( end - p ) ) |
| 5290 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5291 | |
| 5292 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 5293 | |
| 5294 | if( session->peer_cert == NULL ) |
| 5295 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5296 | |
| 5297 | mbedtls_x509_crt_init( session->peer_cert ); |
| 5298 | |
| 5299 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 5300 | p, cert_len ) ) != 0 ) |
| 5301 | { |
| 5302 | mbedtls_x509_crt_free( session->peer_cert ); |
| 5303 | mbedtls_free( session->peer_cert ); |
| 5304 | session->peer_cert = NULL; |
| 5305 | return( ret ); |
| 5306 | } |
| 5307 | |
| 5308 | p += cert_len; |
| 5309 | } |
| 5310 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5311 | /* Deserialize CRT digest from the end of the ticket. */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5312 | if( 2 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5313 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5314 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5315 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 5316 | session->peer_cert_digest_len = (size_t) *p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5317 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5318 | if( session->peer_cert_digest_len != 0 ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5319 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5320 | const mbedtls_md_info_t *md_info = |
| 5321 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 5322 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5323 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5324 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 5325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5326 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5327 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 5328 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5329 | |
| 5330 | session->peer_cert_digest = |
| 5331 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5332 | if( session->peer_cert_digest == NULL ) |
| 5333 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5334 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5335 | memcpy( session->peer_cert_digest, p, |
| 5336 | session->peer_cert_digest_len ); |
| 5337 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5338 | } |
| 5339 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5340 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5341 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5342 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5343 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5344 | */ |
| 5345 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5346 | if( 3 > (size_t)( end - p ) ) |
| 5347 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5348 | |
| 5349 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5350 | p += 3; |
| 5351 | |
| 5352 | if( session->ticket_len != 0 ) |
| 5353 | { |
| 5354 | if( session->ticket_len > (size_t)( end - p ) ) |
| 5355 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5356 | |
| 5357 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 5358 | if( session->ticket == NULL ) |
| 5359 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5360 | |
| 5361 | memcpy( session->ticket, p, session->ticket_len ); |
| 5362 | p += session->ticket_len; |
| 5363 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5364 | |
| 5365 | if( 4 > (size_t)( end - p ) ) |
| 5366 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5367 | |
| 5368 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 5369 | ( (uint32_t) p[1] << 16 ) | |
| 5370 | ( (uint32_t) p[2] << 8 ) | |
| 5371 | ( (uint32_t) p[3] ); |
| 5372 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5373 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5374 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5375 | /* |
| 5376 | * Misc extension-related info |
| 5377 | */ |
| 5378 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5379 | if( 1 > (size_t)( end - p ) ) |
| 5380 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5381 | |
| 5382 | session->mfl_code = *p++; |
| 5383 | #endif |
| 5384 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5385 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5386 | if( 1 > (size_t)( end - p ) ) |
| 5387 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5388 | |
| 5389 | session->encrypt_then_mac = *p++; |
| 5390 | #endif |
| 5391 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5392 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5393 | if( p != end ) |
| 5394 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5395 | |
| 5396 | return( 0 ); |
| 5397 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5398 | #endif /*MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5399 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 5400 | unsigned char omit_header, |
| 5401 | const unsigned char *buf, |
| 5402 | size_t len ) |
| 5403 | { |
| 5404 | const unsigned char *p = buf; |
| 5405 | const unsigned char * const end = buf + len; |
| 5406 | |
| 5407 | if( !omit_header ) |
| 5408 | { |
| 5409 | /* |
| 5410 | * Check Mbed TLS version identifier |
| 5411 | */ |
| 5412 | |
| 5413 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 5414 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5415 | |
| 5416 | if( memcmp( p, ssl_serialized_session_header, |
| 5417 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 5418 | { |
| 5419 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 5420 | } |
| 5421 | p += sizeof( ssl_serialized_session_header ); |
| 5422 | } |
| 5423 | |
| 5424 | /* |
| 5425 | * TLS version identifier |
| 5426 | */ |
| 5427 | if( 1 > (size_t)( end - p ) ) |
| 5428 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5429 | session->minor_ver = *p++; |
| 5430 | |
| 5431 | /* Dispatch according to TLS version. */ |
| 5432 | switch( session->minor_ver ) |
| 5433 | { |
| 5434 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5435 | case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */ |
| 5436 | { |
| 5437 | size_t remaining_len = ( end - p ); |
| 5438 | return( ssl_session_load_tls12( session, p, remaining_len ) ); |
| 5439 | } |
| 5440 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5441 | |
| 5442 | default: |
| 5443 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5444 | } |
| 5445 | } |
| 5446 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5447 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5448 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5449 | */ |
| 5450 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 5451 | const unsigned char *buf, |
| 5452 | size_t len ) |
| 5453 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5454 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5455 | |
| 5456 | if( ret != 0 ) |
| 5457 | mbedtls_ssl_session_free( session ); |
| 5458 | |
| 5459 | return( ret ); |
| 5460 | } |
| 5461 | |
| 5462 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5463 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5464 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5465 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 5466 | { |
| 5467 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5468 | |
| 5469 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 5470 | return( ret ); |
| 5471 | |
| 5472 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5473 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5474 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 5475 | { |
| 5476 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 5477 | return( ret ); |
| 5478 | } |
| 5479 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5480 | |
| 5481 | return( ret ); |
| 5482 | } |
| 5483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5484 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5485 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5486 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5487 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5488 | if( ssl == NULL || |
| 5489 | ssl->conf == NULL || |
| 5490 | ssl->handshake == NULL || |
| 5491 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5492 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5493 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | ret = ssl_prepare_handshake_step( ssl ); |
| 5497 | if( ret != 0 ) |
| 5498 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5499 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5500 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 5501 | if( ret != 0 ) |
| 5502 | goto cleanup; |
| 5503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5504 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5505 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5506 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5507 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5508 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 5509 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5510 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5511 | |
| 5512 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5513 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 5514 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 5515 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5516 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5517 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5518 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5519 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5520 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5521 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5522 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 5523 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5524 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5525 | |
| 5526 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5527 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 5528 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 5529 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5530 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5531 | #endif |
| 5532 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5533 | if( ret != 0 ) |
| 5534 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 5535 | /* handshake_step return error. And it is same |
| 5536 | * with alert_reason. |
| 5537 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 5538 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5539 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 5540 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5541 | goto cleanup; |
| 5542 | } |
| 5543 | } |
| 5544 | |
| 5545 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5546 | return( ret ); |
| 5547 | } |
| 5548 | |
| 5549 | /* |
| 5550 | * Perform the SSL handshake |
| 5551 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5552 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5553 | { |
| 5554 | int ret = 0; |
| 5555 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5556 | /* Sanity checks */ |
| 5557 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5558 | if( ssl == NULL || ssl->conf == NULL ) |
| 5559 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5560 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5561 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5562 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5563 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 5564 | { |
| 5565 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 5566 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 5567 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5568 | } |
| 5569 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5571 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5572 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5573 | /* Main handshake loop */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5574 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5575 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5576 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5577 | |
| 5578 | if( ret != 0 ) |
| 5579 | break; |
| 5580 | } |
| 5581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5582 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5583 | |
| 5584 | return( ret ); |
| 5585 | } |
| 5586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5587 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5588 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5589 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5590 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5591 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5592 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5593 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5594 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5595 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5596 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5597 | |
| 5598 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5599 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5600 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5601 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5602 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5603 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5604 | 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] | 5605 | return( ret ); |
| 5606 | } |
| 5607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5608 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5609 | |
| 5610 | return( 0 ); |
| 5611 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5612 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5613 | |
| 5614 | /* |
| 5615 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5616 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 5617 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 5618 | * - 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] | 5619 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5620 | * 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] | 5621 | * 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] | 5622 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5623 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5624 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5625 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5627 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5628 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5629 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5630 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5631 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5632 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 5633 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5634 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5635 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5636 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5637 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5638 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 5639 | ssl->handshake->out_msg_seq = 1; |
| 5640 | else |
| 5641 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5642 | } |
| 5643 | #endif |
| 5644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5645 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 5646 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5647 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5648 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5649 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5650 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5651 | return( ret ); |
| 5652 | } |
| 5653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5654 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5655 | |
| 5656 | return( 0 ); |
| 5657 | } |
| 5658 | |
| 5659 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5660 | * Renegotiate current connection on client, |
| 5661 | * or request renegotiation on server |
| 5662 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5663 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5664 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5665 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5666 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5667 | if( ssl == NULL || ssl->conf == NULL ) |
| 5668 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5669 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5670 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5671 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5672 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5673 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5674 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5675 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5677 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5678 | |
| 5679 | /* Did we already try/start sending HelloRequest? */ |
| 5680 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5681 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5682 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5683 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5684 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5685 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5687 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5688 | /* |
| 5689 | * On client, either start the renegotiation process or, |
| 5690 | * if already in progress, continue the handshake |
| 5691 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5692 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5694 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5695 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5696 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5697 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5698 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5699 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5700 | return( ret ); |
| 5701 | } |
| 5702 | } |
| 5703 | else |
| 5704 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5705 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5706 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5707 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5708 | return( ret ); |
| 5709 | } |
| 5710 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5711 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5712 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 5713 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5714 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5715 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5717 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5718 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5719 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5720 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5721 | |
| 5722 | while( cur != NULL ) |
| 5723 | { |
| 5724 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5725 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5726 | cur = next; |
| 5727 | } |
| 5728 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5729 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5730 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5731 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5732 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5733 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 5734 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5735 | if( handshake == NULL ) |
| 5736 | return; |
| 5737 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5738 | #if defined(MBEDTLS_ECP_C) |
| 5739 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5740 | if ( ssl->handshake->group_list_heap_allocated ) |
| 5741 | mbedtls_free( (void*) handshake->group_list ); |
| 5742 | handshake->group_list = NULL; |
| 5743 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 5744 | #endif /* MBEDTLS_ECP_C */ |
| 5745 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 5746 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5747 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5748 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 5749 | mbedtls_free( (void*) handshake->sig_algs ); |
| 5750 | handshake->sig_algs = NULL; |
| 5751 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 5752 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5753 | if( ssl->handshake->certificate_request_context ) |
| 5754 | { |
| 5755 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 5756 | } |
| 5757 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 5758 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5759 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5760 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 5761 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 5762 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 5763 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5764 | handshake->async_in_progress = 0; |
| 5765 | } |
| 5766 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 5767 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5768 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5769 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5770 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 5771 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5772 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 5773 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5774 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 5775 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5776 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 5777 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5778 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5779 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 5780 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5781 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5783 | #if defined(MBEDTLS_DHM_C) |
| 5784 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5785 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5786 | #if defined(MBEDTLS_ECDH_C) |
| 5787 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5788 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 5789 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5790 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 5791 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5792 | mbedtls_free( handshake->ecjpake_cache ); |
| 5793 | handshake->ecjpake_cache = NULL; |
| 5794 | handshake->ecjpake_cache_len = 0; |
| 5795 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5796 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5797 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 5798 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 5799 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 5800 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5801 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 5802 | #endif |
| 5803 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5804 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5805 | if( handshake->psk != NULL ) |
| 5806 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5807 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5808 | mbedtls_free( handshake->psk ); |
| 5809 | } |
| 5810 | #endif |
| 5811 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5812 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5813 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5814 | /* |
| 5815 | * Free only the linked list wrapper, not the keys themselves |
| 5816 | * since the belong to the SNI callback |
| 5817 | */ |
| 5818 | if( handshake->sni_key_cert != NULL ) |
| 5819 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5820 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5821 | |
| 5822 | while( cur != NULL ) |
| 5823 | { |
| 5824 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5825 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5826 | cur = next; |
| 5827 | } |
| 5828 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5829 | #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] | 5830 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5831 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 5832 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 5833 | if( handshake->ecrs_peer_cert != NULL ) |
| 5834 | { |
| 5835 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 5836 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 5837 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 5838 | #endif |
| 5839 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 5840 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5841 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5842 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 5843 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5844 | |
XiaokangQian | 3490974 | 2022-01-27 02:25:04 +0000 | [diff] [blame] | 5845 | #if defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5846 | mbedtls_free( handshake->verify_cookie ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 5847 | #endif /* MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 5848 | |
| 5849 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 5850 | mbedtls_ssl_flight_free( handshake->flight ); |
| 5851 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 5852 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 5853 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 5854 | #if defined(MBEDTLS_ECDH_C) && \ |
| 5855 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5856 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 5857 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 5858 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5859 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 5860 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 5861 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5862 | mbedtls_free( handshake->transform_earlydata ); |
| 5863 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5864 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5865 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5866 | |
| 5867 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5868 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 5869 | * processes datagrams and the fact that a datagram is allowed to have |
| 5870 | * several records in it, it is possible that the I/O buffers are not |
| 5871 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 5872 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 5873 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5874 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 5875 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5876 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 5877 | mbedtls_platform_zeroize( handshake, |
| 5878 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5879 | } |
| 5880 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5881 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5882 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5883 | if( session == NULL ) |
| 5884 | return; |
| 5885 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5886 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 5887 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 5888 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5889 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5890 | #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] | 5891 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 5892 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 5893 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5894 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5895 | } |
| 5896 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5897 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5898 | |
| 5899 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5900 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 5901 | #else |
| 5902 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 5903 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5904 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5905 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5906 | |
| 5907 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5908 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 5909 | #else |
| 5910 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 5911 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5912 | |
| 5913 | #if defined(MBEDTLS_SSL_ALPN) |
| 5914 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 5915 | #else |
| 5916 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 5917 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5918 | |
| 5919 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 5920 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 5921 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 5922 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 5923 | |
| 5924 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 5925 | ( (uint32_t) ( \ |
| 5926 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 5927 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 5928 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 5929 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 5930 | 0u ) ) |
| 5931 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5932 | static unsigned char ssl_serialized_context_header[] = { |
| 5933 | MBEDTLS_VERSION_MAJOR, |
| 5934 | MBEDTLS_VERSION_MINOR, |
| 5935 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5936 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5937 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5938 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 5939 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 5940 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5941 | }; |
| 5942 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5943 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5944 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5945 | * |
| 5946 | * The format of the serialized data is: |
| 5947 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 5948 | * |
| 5949 | * // header |
| 5950 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5951 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5952 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5953 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5954 | * Note: When updating the format, remember to keep these |
| 5955 | * 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] | 5956 | * |
| 5957 | * // session sub-structure |
| 5958 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5959 | * // transform sub-structure |
| 5960 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5961 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5962 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5963 | * // fields from ssl_context |
| 5964 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 5965 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5966 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5967 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5968 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5969 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5970 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5971 | * |
| 5972 | * Note that many fields of the ssl_context or sub-structures are not |
| 5973 | * serialized, as they fall in one of the following categories: |
| 5974 | * |
| 5975 | * 1. forced value (eg in_left must be 0) |
| 5976 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5977 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5978 | * 4. value was temporary (eg content of input buffer) |
| 5979 | * 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] | 5980 | */ |
| 5981 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 5982 | unsigned char *buf, |
| 5983 | size_t buf_len, |
| 5984 | size_t *olen ) |
| 5985 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5986 | unsigned char *p = buf; |
| 5987 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5988 | size_t session_len; |
| 5989 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5990 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5991 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5992 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5993 | * this function's documentation. |
| 5994 | * |
| 5995 | * These are due to assumptions/limitations in the implementation. Some of |
| 5996 | * them are likely to stay (no handshake in progress) some might go away |
| 5997 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5998 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5999 | /* The initial handshake must be over */ |
| 6000 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6001 | { |
| 6002 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 6003 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6004 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6005 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6006 | { |
| 6007 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6008 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6009 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6010 | /* Double-check that sub-structures are indeed ready */ |
| 6011 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6012 | { |
| 6013 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6014 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6015 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6016 | /* There must be no pending incoming or outgoing data */ |
| 6017 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6018 | { |
| 6019 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6020 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6021 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6022 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6023 | { |
| 6024 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6025 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6026 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6027 | /* Protocol must be DLTS, not TLS */ |
| 6028 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6029 | { |
| 6030 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6031 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6032 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6033 | /* Version must be 1.2 */ |
| 6034 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6035 | { |
| 6036 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6037 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6038 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6039 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6040 | { |
| 6041 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6042 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6043 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6044 | /* We must be using an AEAD ciphersuite */ |
| 6045 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6046 | { |
| 6047 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6048 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6049 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6050 | /* Renegotiation must not be enabled */ |
| 6051 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6052 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6053 | { |
| 6054 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6055 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 6056 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 6057 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6058 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6059 | /* |
| 6060 | * Version and format identifier |
| 6061 | */ |
| 6062 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6063 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6064 | if( used <= buf_len ) |
| 6065 | { |
| 6066 | memcpy( p, ssl_serialized_context_header, |
| 6067 | sizeof( ssl_serialized_context_header ) ); |
| 6068 | p += sizeof( ssl_serialized_context_header ); |
| 6069 | } |
| 6070 | |
| 6071 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6072 | * Session (length + data) |
| 6073 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6074 | 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] | 6075 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 6076 | return( ret ); |
| 6077 | |
| 6078 | used += 4 + session_len; |
| 6079 | if( used <= buf_len ) |
| 6080 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6081 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 6082 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6083 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6084 | ret = ssl_session_save( ssl->session, 1, |
| 6085 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6086 | if( ret != 0 ) |
| 6087 | return( ret ); |
| 6088 | |
| 6089 | p += session_len; |
| 6090 | } |
| 6091 | |
| 6092 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6093 | * Transform |
| 6094 | */ |
| 6095 | used += sizeof( ssl->transform->randbytes ); |
| 6096 | if( used <= buf_len ) |
| 6097 | { |
| 6098 | memcpy( p, ssl->transform->randbytes, |
| 6099 | sizeof( ssl->transform->randbytes ) ); |
| 6100 | p += sizeof( ssl->transform->randbytes ); |
| 6101 | } |
| 6102 | |
| 6103 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6104 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 6105 | if( used <= buf_len ) |
| 6106 | { |
| 6107 | *p++ = ssl->transform->in_cid_len; |
| 6108 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 6109 | p += ssl->transform->in_cid_len; |
| 6110 | |
| 6111 | *p++ = ssl->transform->out_cid_len; |
| 6112 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 6113 | p += ssl->transform->out_cid_len; |
| 6114 | } |
| 6115 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6116 | |
| 6117 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6118 | * Saved fields from top-level ssl_context structure |
| 6119 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6120 | used += 4; |
| 6121 | if( used <= buf_len ) |
| 6122 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6123 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 6124 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6125 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6126 | |
| 6127 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6128 | used += 16; |
| 6129 | if( used <= buf_len ) |
| 6130 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6131 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 6132 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6133 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6134 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 6135 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6136 | } |
| 6137 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6138 | |
| 6139 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6140 | used += 1; |
| 6141 | if( used <= buf_len ) |
| 6142 | { |
| 6143 | *p++ = ssl->disable_datagram_packing; |
| 6144 | } |
| 6145 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6146 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 6147 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6148 | if( used <= buf_len ) |
| 6149 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 6150 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 6151 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6155 | used += 2; |
| 6156 | if( used <= buf_len ) |
| 6157 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 6158 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 6159 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6160 | } |
| 6161 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6162 | |
| 6163 | #if defined(MBEDTLS_SSL_ALPN) |
| 6164 | { |
| 6165 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 6166 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6167 | : 0; |
| 6168 | |
| 6169 | used += 1 + alpn_len; |
| 6170 | if( used <= buf_len ) |
| 6171 | { |
| 6172 | *p++ = alpn_len; |
| 6173 | |
| 6174 | if( ssl->alpn_chosen != NULL ) |
| 6175 | { |
| 6176 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 6177 | p += alpn_len; |
| 6178 | } |
| 6179 | } |
| 6180 | } |
| 6181 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6182 | |
| 6183 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6184 | * Done |
| 6185 | */ |
| 6186 | *olen = used; |
| 6187 | |
| 6188 | if( used > buf_len ) |
| 6189 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6190 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6191 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 6192 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6193 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6194 | } |
| 6195 | |
| 6196 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6197 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 6198 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 6199 | */ |
| 6200 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 6201 | const char *label, |
| 6202 | const unsigned char *random, size_t rlen, |
| 6203 | unsigned char *dstbuf, size_t dlen ); |
| 6204 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 6205 | { |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 6206 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 6207 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6208 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6209 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 6210 | |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6211 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 6212 | return( tls_prf_sha384 ); |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 6213 | #else |
| 6214 | (void) ciphersuite_id; |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6215 | #endif |
| 6216 | return( tls_prf_sha256 ); |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 6217 | #else |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 6218 | (void) ciphersuite_id; |
| 6219 | return( NULL ); |
| 6220 | #endif |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 6221 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6225 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6226 | * |
| 6227 | * This internal version is wrapped by a public function that cleans up in |
| 6228 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6229 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6230 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 6231 | const unsigned char *buf, |
| 6232 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6233 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6234 | const unsigned char *p = buf; |
| 6235 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6236 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6237 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6238 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6239 | /* |
| 6240 | * The context should have been freshly setup or reset. |
| 6241 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 6242 | * (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] | 6243 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 6244 | */ |
| 6245 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 6246 | ssl->session != NULL ) |
| 6247 | { |
| 6248 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6249 | } |
| 6250 | |
| 6251 | /* |
| 6252 | * We can't check that the config matches the initial one, but we can at |
| 6253 | * least check it matches the requirements for serializing. |
| 6254 | */ |
| 6255 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 6256 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6257 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6258 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6259 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6260 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6261 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6262 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6263 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6264 | { |
| 6265 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6266 | } |
| 6267 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6268 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 6269 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6270 | /* |
| 6271 | * Check version identifier |
| 6272 | */ |
| 6273 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 6274 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6275 | |
| 6276 | if( memcmp( p, ssl_serialized_context_header, |
| 6277 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 6278 | { |
| 6279 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 6280 | } |
| 6281 | p += sizeof( ssl_serialized_context_header ); |
| 6282 | |
| 6283 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6284 | * Session |
| 6285 | */ |
| 6286 | if( (size_t)( end - p ) < 4 ) |
| 6287 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6288 | |
| 6289 | session_len = ( (size_t) p[0] << 24 ) | |
| 6290 | ( (size_t) p[1] << 16 ) | |
| 6291 | ( (size_t) p[2] << 8 ) | |
| 6292 | ( (size_t) p[3] ); |
| 6293 | p += 4; |
| 6294 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6295 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6296 | * 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] | 6297 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6298 | ssl->session_in = ssl->session; |
| 6299 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6300 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6301 | |
| 6302 | if( (size_t)( end - p ) < session_len ) |
| 6303 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6304 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6305 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6306 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6307 | { |
| 6308 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6309 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6310 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6311 | |
| 6312 | p += session_len; |
| 6313 | |
| 6314 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6315 | * Transform |
| 6316 | */ |
| 6317 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6318 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6319 | * 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] | 6320 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6321 | ssl->transform_in = ssl->transform; |
| 6322 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6323 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6324 | |
| 6325 | /* Read random bytes and populate structure */ |
| 6326 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 6327 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6328 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6329 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6330 | ssl->session->ciphersuite, |
| 6331 | ssl->session->master, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6332 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 6333 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6334 | ssl->session->encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6335 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 6336 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6337 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 6338 | p, /* currently pointing to randbytes */ |
| 6339 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 6340 | ssl->conf->endpoint, |
| 6341 | ssl ); |
| 6342 | if( ret != 0 ) |
| 6343 | return( ret ); |
| 6344 | |
| 6345 | p += sizeof( ssl->transform->randbytes ); |
| 6346 | |
| 6347 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6348 | /* Read connection IDs and store them */ |
| 6349 | if( (size_t)( end - p ) < 1 ) |
| 6350 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6351 | |
| 6352 | ssl->transform->in_cid_len = *p++; |
| 6353 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 6354 | 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] | 6355 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6356 | |
| 6357 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 6358 | p += ssl->transform->in_cid_len; |
| 6359 | |
| 6360 | ssl->transform->out_cid_len = *p++; |
| 6361 | |
| 6362 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 6363 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6364 | |
| 6365 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 6366 | p += ssl->transform->out_cid_len; |
| 6367 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6368 | |
| 6369 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6370 | * Saved fields from top-level ssl_context structure |
| 6371 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6372 | if( (size_t)( end - p ) < 4 ) |
| 6373 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6374 | |
| 6375 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 6376 | ( (uint32_t) p[1] << 16 ) | |
| 6377 | ( (uint32_t) p[2] << 8 ) | |
| 6378 | ( (uint32_t) p[3] ); |
| 6379 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6380 | |
| 6381 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6382 | if( (size_t)( end - p ) < 16 ) |
| 6383 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6384 | |
| 6385 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 6386 | ( (uint64_t) p[1] << 48 ) | |
| 6387 | ( (uint64_t) p[2] << 40 ) | |
| 6388 | ( (uint64_t) p[3] << 32 ) | |
| 6389 | ( (uint64_t) p[4] << 24 ) | |
| 6390 | ( (uint64_t) p[5] << 16 ) | |
| 6391 | ( (uint64_t) p[6] << 8 ) | |
| 6392 | ( (uint64_t) p[7] ); |
| 6393 | p += 8; |
| 6394 | |
| 6395 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 6396 | ( (uint64_t) p[1] << 48 ) | |
| 6397 | ( (uint64_t) p[2] << 40 ) | |
| 6398 | ( (uint64_t) p[3] << 32 ) | |
| 6399 | ( (uint64_t) p[4] << 24 ) | |
| 6400 | ( (uint64_t) p[5] << 16 ) | |
| 6401 | ( (uint64_t) p[6] << 8 ) | |
| 6402 | ( (uint64_t) p[7] ); |
| 6403 | p += 8; |
| 6404 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6405 | |
| 6406 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6407 | if( (size_t)( end - p ) < 1 ) |
| 6408 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6409 | |
| 6410 | ssl->disable_datagram_packing = *p++; |
| 6411 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6412 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 6413 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6414 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 6415 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 6416 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6417 | |
| 6418 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6419 | if( (size_t)( end - p ) < 2 ) |
| 6420 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6421 | |
| 6422 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 6423 | p += 2; |
| 6424 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6425 | |
| 6426 | #if defined(MBEDTLS_SSL_ALPN) |
| 6427 | { |
| 6428 | uint8_t alpn_len; |
| 6429 | const char **cur; |
| 6430 | |
| 6431 | if( (size_t)( end - p ) < 1 ) |
| 6432 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6433 | |
| 6434 | alpn_len = *p++; |
| 6435 | |
| 6436 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 6437 | { |
| 6438 | /* alpn_chosen should point to an item in the configured list */ |
| 6439 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 6440 | { |
| 6441 | if( strlen( *cur ) == alpn_len && |
| 6442 | memcmp( p, cur, alpn_len ) == 0 ) |
| 6443 | { |
| 6444 | ssl->alpn_chosen = *cur; |
| 6445 | break; |
| 6446 | } |
| 6447 | } |
| 6448 | } |
| 6449 | |
| 6450 | /* can only happen on conf mismatch */ |
| 6451 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 6452 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6453 | |
| 6454 | p += alpn_len; |
| 6455 | } |
| 6456 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6457 | |
| 6458 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6459 | * Forced fields from top-level ssl_context structure |
| 6460 | * |
| 6461 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 6462 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 6463 | */ |
| 6464 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 6465 | |
| 6466 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6467 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6468 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6469 | /* Adjust pointers for header fields of outgoing records to |
| 6470 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6471 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6472 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6473 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6474 | ssl->in_epoch = 1; |
| 6475 | #endif |
| 6476 | |
| 6477 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 6478 | * 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] | 6479 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 6480 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6481 | if( ssl->handshake != NULL ) |
| 6482 | { |
| 6483 | mbedtls_ssl_handshake_free( ssl ); |
| 6484 | mbedtls_free( ssl->handshake ); |
| 6485 | ssl->handshake = NULL; |
| 6486 | } |
| 6487 | |
| 6488 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6489 | * Done - should have consumed entire buffer |
| 6490 | */ |
| 6491 | if( p != end ) |
| 6492 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6493 | |
| 6494 | return( 0 ); |
| 6495 | } |
| 6496 | |
| 6497 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6498 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6499 | */ |
| 6500 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 6501 | const unsigned char *buf, |
| 6502 | size_t len ) |
| 6503 | { |
| 6504 | int ret = ssl_context_load( context, buf, len ); |
| 6505 | |
| 6506 | if( ret != 0 ) |
| 6507 | mbedtls_ssl_free( context ); |
| 6508 | |
| 6509 | return( ret ); |
| 6510 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6511 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6512 | |
| 6513 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6514 | * Free an SSL context |
| 6515 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6516 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6517 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6518 | if( ssl == NULL ) |
| 6519 | return; |
| 6520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6521 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6522 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6523 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6524 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6525 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6526 | size_t out_buf_len = ssl->out_buf_len; |
| 6527 | #else |
| 6528 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 6529 | #endif |
| 6530 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6531 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6532 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6533 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6536 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6537 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6538 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6539 | size_t in_buf_len = ssl->in_buf_len; |
| 6540 | #else |
| 6541 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 6542 | #endif |
| 6543 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6544 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6545 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6546 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6547 | } |
| 6548 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6549 | if( ssl->transform ) |
| 6550 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6551 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6552 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6553 | } |
| 6554 | |
| 6555 | if( ssl->handshake ) |
| 6556 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6557 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6558 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 6559 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6561 | mbedtls_free( ssl->handshake ); |
| 6562 | mbedtls_free( ssl->transform_negotiate ); |
| 6563 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6564 | } |
| 6565 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6566 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 6567 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 6568 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6569 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 6570 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6571 | if( ssl->session ) |
| 6572 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6573 | mbedtls_ssl_session_free( ssl->session ); |
| 6574 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6575 | } |
| 6576 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 6577 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 6578 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6579 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6580 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6581 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6582 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 6583 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6584 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6585 | #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] | 6586 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 6587 | #endif |
| 6588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6589 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 6590 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 6591 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6592 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6593 | } |
| 6594 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6595 | /* |
| 6596 | * Initialze mbedtls_ssl_config |
| 6597 | */ |
| 6598 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 6599 | { |
| 6600 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 6601 | } |
| 6602 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6603 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6604 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6605 | /* 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] | 6606 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 6607 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6608 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 6609 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6610 | static int ssl_preset_default_hashes[] = { |
| 6611 | #if defined(MBEDTLS_SHA512_C) |
| 6612 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6613 | #endif |
| 6614 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6615 | MBEDTLS_MD_SHA384, |
| 6616 | #endif |
| 6617 | #if defined(MBEDTLS_SHA256_C) |
| 6618 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6619 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6620 | MBEDTLS_MD_NONE |
| 6621 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6622 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 6623 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6624 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6625 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 6626 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6627 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6628 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6629 | * about this list. |
| 6630 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6631 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6632 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6633 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6634 | #endif |
| 6635 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6636 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6637 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6638 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6639 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6640 | #endif |
| 6641 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6642 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6643 | #endif |
| 6644 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6645 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6646 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6647 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6648 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6649 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6650 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6651 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6652 | #endif |
| 6653 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6654 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6655 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6656 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6657 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6658 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6659 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 6660 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 6661 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 6662 | 0 |
| 6663 | }; |
| 6664 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6665 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6666 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6667 | static int ssl_preset_suiteb_hashes[] = { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6668 | #if defined(MBEDTLS_SHA256_C) |
| 6669 | MBEDTLS_MD_SHA256, |
| 6670 | #endif |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6671 | #if defined(MBEDTLS_SHA384_C) |
| 6672 | MBEDTLS_MD_SHA384, |
| 6673 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6674 | MBEDTLS_MD_NONE |
| 6675 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6676 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6677 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6678 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 6679 | * 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] | 6680 | * rules SHOULD be upheld. |
| 6681 | * - No duplicate entries. |
| 6682 | * - But if there is a good reason, do not change the order of the algorithms. |
| 6683 | * - ssl_tls12_present* is for TLS 1.2 use only. |
| 6684 | * - 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] | 6685 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6686 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6687 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 6688 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 6689 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 6690 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 6691 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 6692 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6693 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6694 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 6695 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 6696 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 6697 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6698 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 6699 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 6700 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ |
| 6701 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 6702 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
| 6703 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6704 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6705 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6706 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 6707 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 6708 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
| 6709 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6710 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 6711 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 6712 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 6713 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6714 | MBEDTLS_TLS1_3_SIG_NONE |
| 6715 | }; |
| 6716 | |
| 6717 | /* NOTICE: see above */ |
| 6718 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6719 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6720 | #if defined(MBEDTLS_SHA512_C) |
| 6721 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) |
| 6722 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 6723 | #if defined(MBEDTLS_SHA384_C) |
| 6724 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 6725 | #endif |
| 6726 | #if defined(MBEDTLS_SHA256_C) |
| 6727 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 6728 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6729 | MBEDTLS_TLS1_3_SIG_NONE |
| 6730 | }; |
| 6731 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6732 | /* NOTICE: see above */ |
| 6733 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 6734 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6735 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 6736 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 6737 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 6738 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 6739 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 6740 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6741 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 6742 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 6743 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 6744 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6745 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6746 | |
| 6747 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 6748 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 6749 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6750 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6751 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 6752 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 6753 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 6754 | |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6755 | MBEDTLS_TLS1_3_SIG_NONE |
| 6756 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 6757 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6758 | /* NOTICE: see above */ |
| 6759 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6760 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6761 | #if defined(MBEDTLS_SHA256_C) |
| 6762 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 6763 | #endif |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 6764 | #if defined(MBEDTLS_SHA384_C) |
| 6765 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 6766 | #endif |
Xiaofei Bai | 9539501 | 2021-11-25 08:51:30 +0000 | [diff] [blame] | 6767 | MBEDTLS_TLS1_3_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6768 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6769 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6770 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6771 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6772 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6773 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6774 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6775 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6776 | #endif |
| 6777 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6778 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6779 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6780 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6781 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6782 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6783 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6784 | /* 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] | 6785 | * to make sure there are no duplicated signature algorithm entries. */ |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6786 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6787 | { |
| 6788 | size_t i, j; |
| 6789 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6790 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6791 | for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6792 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6793 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6794 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6795 | if( sig_algs[i] != sig_algs[j] ) |
| 6796 | continue; |
| 6797 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 6798 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 6799 | sig_algs[i], j, i ); |
| 6800 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6801 | } |
| 6802 | } |
| 6803 | return( ret ); |
| 6804 | } |
| 6805 | |
| 6806 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6807 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6808 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 6809 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6810 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6811 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6812 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6813 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6814 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6815 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6816 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6817 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6818 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6819 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6820 | { |
| 6821 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 6822 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6823 | } |
| 6824 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6825 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6826 | { |
| 6827 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 6828 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6829 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6830 | |
| 6831 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6832 | 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] | 6833 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6834 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6835 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6836 | } |
| 6837 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6838 | 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] | 6839 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6840 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6841 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6842 | } |
| 6843 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6844 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6845 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 6846 | /* Use the functions here so that they are covered in tests, |
| 6847 | * but otherwise access member directly for efficiency */ |
| 6848 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 6849 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6850 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6851 | /* |
| 6852 | * Things that are common to all presets |
| 6853 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6854 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6855 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6856 | { |
| 6857 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 6858 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 6859 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 6860 | #endif |
| 6861 | } |
| 6862 | #endif |
| 6863 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6864 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6865 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 6866 | #endif |
| 6867 | |
| 6868 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6869 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 6870 | #endif |
| 6871 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6872 | #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] | 6873 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 6874 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 6875 | #endif |
| 6876 | |
| 6877 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6878 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 6879 | #endif |
| 6880 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6881 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6882 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 6883 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6884 | #endif |
| 6885 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6886 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6887 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 6888 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 6889 | #endif |
| 6890 | |
| 6891 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6892 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 6893 | memset( conf->renego_period, 0x00, 2 ); |
| 6894 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6895 | #endif |
| 6896 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6897 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6898 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6899 | { |
| 6900 | const unsigned char dhm_p[] = |
| 6901 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 6902 | const unsigned char dhm_g[] = |
| 6903 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 6904 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6905 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 6906 | dhm_p, sizeof( dhm_p ), |
| 6907 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 6908 | { |
| 6909 | return( ret ); |
| 6910 | } |
| 6911 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6912 | #endif |
| 6913 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6914 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6915 | /* |
| 6916 | * Allow all TLS 1.3 key exchange modes by default. |
| 6917 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 6918 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6919 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6920 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6921 | /* |
| 6922 | * Preset-specific defaults |
| 6923 | */ |
| 6924 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6925 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6926 | /* |
| 6927 | * NSA Suite B |
| 6928 | */ |
| 6929 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6930 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6931 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 6932 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 6933 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 6934 | /* Hybrid TLS 1.2/1.3 is not supported yet */ |
| 6935 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6936 | #else |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6937 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 6938 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6939 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6940 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6941 | |
| 6942 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6943 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6944 | #endif |
| 6945 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6946 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6947 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6948 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6949 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6950 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6951 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 6952 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 6953 | else |
| 6954 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6955 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6956 | #endif |
| 6957 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6958 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6959 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6960 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6961 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6962 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6963 | |
| 6964 | /* |
| 6965 | * Default |
| 6966 | */ |
| 6967 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 6968 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 6969 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 6970 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 6971 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 6972 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 6973 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 6974 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 6975 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6976 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 6977 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 6978 | /* Hybrid TLS 1.2/1.3 is not supported yet */ |
| 6979 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6980 | #else |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6981 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 6982 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3 */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6983 | |
| 6984 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6985 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 6986 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6987 | #endif |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6988 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6989 | |
| 6990 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6991 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6992 | #endif |
| 6993 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6994 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6995 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6996 | conf->sig_hashes = ssl_preset_default_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6997 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6998 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6999 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 7000 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 7001 | else |
| 7002 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7003 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 7004 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 7005 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7006 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 7007 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 7008 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7009 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 7010 | |
| 7011 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 7012 | conf->dhm_min_bitlen = 1024; |
| 7013 | #endif |
| 7014 | } |
| 7015 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7016 | return( 0 ); |
| 7017 | } |
| 7018 | |
| 7019 | /* |
| 7020 | * Free mbedtls_ssl_config |
| 7021 | */ |
| 7022 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 7023 | { |
| 7024 | #if defined(MBEDTLS_DHM_C) |
| 7025 | mbedtls_mpi_free( &conf->dhm_P ); |
| 7026 | mbedtls_mpi_free( &conf->dhm_G ); |
| 7027 | #endif |
| 7028 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7029 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7030 | if( conf->psk != NULL ) |
| 7031 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7032 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7033 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 7034 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7035 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 7036 | } |
| 7037 | |
| 7038 | if( conf->psk_identity != NULL ) |
| 7039 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7040 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 7041 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 7042 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7043 | conf->psk_identity_len = 0; |
| 7044 | } |
| 7045 | #endif |
| 7046 | |
| 7047 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7048 | ssl_key_cert_free( conf->key_cert ); |
| 7049 | #endif |
| 7050 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 7051 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 7052 | } |
| 7053 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7054 | #if defined(MBEDTLS_PK_C) && \ |
| 7055 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7056 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7057 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7058 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7059 | 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] | 7060 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7061 | #if defined(MBEDTLS_RSA_C) |
| 7062 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 7063 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7064 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7065 | #if defined(MBEDTLS_ECDSA_C) |
| 7066 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 7067 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7068 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7069 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 7070 | } |
| 7071 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7072 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 7073 | { |
| 7074 | switch( type ) { |
| 7075 | case MBEDTLS_PK_RSA: |
| 7076 | return( MBEDTLS_SSL_SIG_RSA ); |
| 7077 | case MBEDTLS_PK_ECDSA: |
| 7078 | case MBEDTLS_PK_ECKEY: |
| 7079 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 7080 | default: |
| 7081 | return( MBEDTLS_SSL_SIG_ANON ); |
| 7082 | } |
| 7083 | } |
| 7084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7085 | 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] | 7086 | { |
| 7087 | switch( sig ) |
| 7088 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7089 | #if defined(MBEDTLS_RSA_C) |
| 7090 | case MBEDTLS_SSL_SIG_RSA: |
| 7091 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7092 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7093 | #if defined(MBEDTLS_ECDSA_C) |
| 7094 | case MBEDTLS_SSL_SIG_ECDSA: |
| 7095 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7096 | #endif |
| 7097 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7098 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7099 | } |
| 7100 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 7101 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7102 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7103 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7104 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7105 | |
| 7106 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 7107 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 7108 | mbedtls_pk_type_t sig_alg ) |
| 7109 | { |
| 7110 | switch( sig_alg ) |
| 7111 | { |
| 7112 | case MBEDTLS_PK_RSA: |
| 7113 | return( set->rsa ); |
| 7114 | case MBEDTLS_PK_ECDSA: |
| 7115 | return( set->ecdsa ); |
| 7116 | default: |
| 7117 | return( MBEDTLS_MD_NONE ); |
| 7118 | } |
| 7119 | } |
| 7120 | |
| 7121 | /* Add a signature-hash-pair to a signature-hash set */ |
| 7122 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 7123 | mbedtls_pk_type_t sig_alg, |
| 7124 | mbedtls_md_type_t md_alg ) |
| 7125 | { |
| 7126 | switch( sig_alg ) |
| 7127 | { |
| 7128 | case MBEDTLS_PK_RSA: |
| 7129 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 7130 | set->rsa = md_alg; |
| 7131 | break; |
| 7132 | |
| 7133 | case MBEDTLS_PK_ECDSA: |
| 7134 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 7135 | set->ecdsa = md_alg; |
| 7136 | break; |
| 7137 | |
| 7138 | default: |
| 7139 | break; |
| 7140 | } |
| 7141 | } |
| 7142 | |
| 7143 | /* Allow exactly one hash algorithm for each signature. */ |
| 7144 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 7145 | mbedtls_md_type_t md_alg ) |
| 7146 | { |
| 7147 | set->rsa = md_alg; |
| 7148 | set->ecdsa = md_alg; |
| 7149 | } |
| 7150 | |
| 7151 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 7152 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 7153 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7154 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7155 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 7156 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7157 | 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] | 7158 | { |
| 7159 | switch( hash ) |
| 7160 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7161 | #if defined(MBEDTLS_MD5_C) |
| 7162 | case MBEDTLS_SSL_HASH_MD5: |
| 7163 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7164 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7165 | #if defined(MBEDTLS_SHA1_C) |
| 7166 | case MBEDTLS_SSL_HASH_SHA1: |
| 7167 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7168 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7169 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7170 | case MBEDTLS_SSL_HASH_SHA224: |
| 7171 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7172 | #endif |
| 7173 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7174 | case MBEDTLS_SSL_HASH_SHA256: |
| 7175 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7176 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7177 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7178 | case MBEDTLS_SSL_HASH_SHA384: |
| 7179 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7180 | #endif |
| 7181 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7182 | case MBEDTLS_SSL_HASH_SHA512: |
| 7183 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7184 | #endif |
| 7185 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7186 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 7187 | } |
| 7188 | } |
| 7189 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7190 | /* |
| 7191 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 7192 | */ |
| 7193 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 7194 | { |
| 7195 | switch( md ) |
| 7196 | { |
| 7197 | #if defined(MBEDTLS_MD5_C) |
| 7198 | case MBEDTLS_MD_MD5: |
| 7199 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 7200 | #endif |
| 7201 | #if defined(MBEDTLS_SHA1_C) |
| 7202 | case MBEDTLS_MD_SHA1: |
| 7203 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 7204 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7205 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7206 | case MBEDTLS_MD_SHA224: |
| 7207 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7208 | #endif |
| 7209 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7210 | case MBEDTLS_MD_SHA256: |
| 7211 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 7212 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7213 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7214 | case MBEDTLS_MD_SHA384: |
| 7215 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7216 | #endif |
| 7217 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7218 | case MBEDTLS_MD_SHA512: |
| 7219 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 7220 | #endif |
| 7221 | default: |
| 7222 | return( MBEDTLS_SSL_HASH_NONE ); |
| 7223 | } |
| 7224 | } |
| 7225 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7226 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7227 | * 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] | 7228 | * 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] | 7229 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 7230 | 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] | 7231 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7232 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7233 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7234 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7235 | return( -1 ); |
| 7236 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7237 | for( ; *group_list != 0; group_list++ ) |
| 7238 | { |
| 7239 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7240 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7241 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7242 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7243 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7244 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 7245 | |
| 7246 | #if defined(MBEDTLS_ECP_C) |
| 7247 | /* |
| 7248 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 7249 | */ |
| 7250 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 7251 | { |
Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 7252 | 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] | 7253 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 7254 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7255 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7257 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7258 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 7259 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7260 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7261 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7262 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7263 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7264 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7265 | const char *ext_oid; |
| 7266 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7268 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7269 | { |
| 7270 | /* Server part of the key exchange */ |
| 7271 | switch( ciphersuite->key_exchange ) |
| 7272 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7273 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 7274 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7275 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7276 | break; |
| 7277 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7278 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 7279 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 7280 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 7281 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7282 | break; |
| 7283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7284 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 7285 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7286 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7287 | break; |
| 7288 | |
| 7289 | /* 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] | 7290 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 7291 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 7292 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 7293 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 7294 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7295 | usage = 0; |
| 7296 | } |
| 7297 | } |
| 7298 | else |
| 7299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7300 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 7301 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7302 | } |
| 7303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7304 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7305 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7306 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7307 | ret = -1; |
| 7308 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7310 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7311 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7312 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 7313 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7314 | } |
| 7315 | else |
| 7316 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7317 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 7318 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7319 | } |
| 7320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7321 | 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] | 7322 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7323 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7324 | ret = -1; |
| 7325 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7326 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7327 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7328 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7329 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 7330 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7331 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 7332 | { |
| 7333 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7334 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7335 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7336 | |
| 7337 | switch( md ) |
| 7338 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 7339 | #if defined(MBEDTLS_SHA384_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7340 | case MBEDTLS_SSL_HASH_SHA384: |
| 7341 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7342 | break; |
| 7343 | #endif |
| 7344 | #if defined(MBEDTLS_SHA256_C) |
| 7345 | case MBEDTLS_SSL_HASH_SHA256: |
| 7346 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7347 | break; |
| 7348 | #endif |
| 7349 | default: |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7350 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7351 | } |
| 7352 | |
| 7353 | return 0; |
| 7354 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7355 | (void) ssl; |
| 7356 | (void) md; |
| 7357 | |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7358 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7359 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7360 | } |
| 7361 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 7362 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7363 | |
| 7364 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7365 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7366 | unsigned char *hash, size_t *hashlen, |
| 7367 | unsigned char *data, size_t data_len, |
| 7368 | mbedtls_md_type_t md_alg ) |
| 7369 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7370 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7371 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7372 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7373 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7374 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7375 | |
| 7376 | if( ( status = psa_hash_setup( &hash_operation, |
| 7377 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7378 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7379 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7380 | goto exit; |
| 7381 | } |
| 7382 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7383 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7384 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7385 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7386 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7387 | goto exit; |
| 7388 | } |
| 7389 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7390 | if( ( status = psa_hash_update( &hash_operation, |
| 7391 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7392 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7393 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7394 | goto exit; |
| 7395 | } |
| 7396 | |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 7397 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7398 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7399 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7400 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7401 | goto exit; |
| 7402 | } |
| 7403 | |
| 7404 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7405 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7406 | { |
| 7407 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7408 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7409 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7410 | { |
| 7411 | case PSA_ERROR_NOT_SUPPORTED: |
| 7412 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7413 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7414 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7415 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7416 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7417 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7418 | default: |
TRodziewicz | b579ccd | 2021-04-13 14:28:28 +0200 | [diff] [blame] | 7419 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7420 | } |
| 7421 | } |
| 7422 | return( 0 ); |
| 7423 | } |
| 7424 | |
| 7425 | #else |
| 7426 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7427 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7428 | unsigned char *hash, size_t *hashlen, |
| 7429 | unsigned char *data, size_t data_len, |
| 7430 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7431 | { |
| 7432 | int ret = 0; |
| 7433 | mbedtls_md_context_t ctx; |
| 7434 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7435 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7436 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7437 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7438 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7439 | mbedtls_md_init( &ctx ); |
| 7440 | |
| 7441 | /* |
| 7442 | * digitally-signed struct { |
| 7443 | * opaque client_random[32]; |
| 7444 | * opaque server_random[32]; |
| 7445 | * ServerDHParams params; |
| 7446 | * }; |
| 7447 | */ |
| 7448 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7449 | { |
| 7450 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7451 | goto exit; |
| 7452 | } |
| 7453 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7454 | { |
| 7455 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7456 | goto exit; |
| 7457 | } |
| 7458 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7459 | { |
| 7460 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7461 | goto exit; |
| 7462 | } |
| 7463 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7464 | { |
| 7465 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7466 | goto exit; |
| 7467 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7468 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7469 | { |
| 7470 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7471 | goto exit; |
| 7472 | } |
| 7473 | |
| 7474 | exit: |
| 7475 | mbedtls_md_free( &ctx ); |
| 7476 | |
| 7477 | if( ret != 0 ) |
| 7478 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7479 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7480 | |
| 7481 | return( ret ); |
| 7482 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7483 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7484 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 7485 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7486 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7487 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7488 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 7489 | const mbedtls_md_type_t md, |
| 7490 | unsigned char *dst, |
| 7491 | size_t dst_len, |
| 7492 | size_t *olen ) |
| 7493 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 7494 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7495 | psa_hash_operation_t *hash_operation_to_clone; |
| 7496 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 7497 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7498 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 7499 | |
| 7500 | switch( md ) |
| 7501 | { |
| 7502 | #if defined(MBEDTLS_SHA384_C) |
| 7503 | case MBEDTLS_MD_SHA384: |
| 7504 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 7505 | break; |
| 7506 | #endif |
| 7507 | |
| 7508 | #if defined(MBEDTLS_SHA256_C) |
| 7509 | case MBEDTLS_MD_SHA256: |
| 7510 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 7511 | break; |
| 7512 | #endif |
| 7513 | |
| 7514 | default: |
| 7515 | goto exit; |
| 7516 | } |
| 7517 | |
| 7518 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 7519 | if( status != PSA_SUCCESS ) |
| 7520 | goto exit; |
| 7521 | |
| 7522 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 7523 | if( status != PSA_SUCCESS ) |
| 7524 | goto exit; |
| 7525 | |
| 7526 | exit: |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 7527 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7528 | } |
| 7529 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7530 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7531 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7532 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 7533 | unsigned char *dst, |
| 7534 | size_t dst_len, |
| 7535 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7536 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7537 | int ret; |
| 7538 | mbedtls_sha512_context sha512; |
| 7539 | |
| 7540 | if( dst_len < 48 ) |
| 7541 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7542 | |
| 7543 | mbedtls_sha512_init( &sha512 ); |
| 7544 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 7545 | |
| 7546 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 7547 | { |
| 7548 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 7549 | goto exit; |
| 7550 | } |
| 7551 | |
| 7552 | *olen = 48; |
| 7553 | |
| 7554 | exit: |
| 7555 | |
| 7556 | mbedtls_sha512_free( &sha512 ); |
| 7557 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7558 | } |
| 7559 | #endif /* MBEDTLS_SHA384_C */ |
| 7560 | |
| 7561 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7562 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 7563 | unsigned char *dst, |
| 7564 | size_t dst_len, |
| 7565 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7566 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7567 | int ret; |
| 7568 | mbedtls_sha256_context sha256; |
| 7569 | |
| 7570 | if( dst_len < 32 ) |
| 7571 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7572 | |
| 7573 | mbedtls_sha256_init( &sha256 ); |
| 7574 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7575 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7576 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 7577 | { |
| 7578 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 7579 | goto exit; |
| 7580 | } |
| 7581 | |
| 7582 | *olen = 32; |
| 7583 | |
| 7584 | exit: |
| 7585 | |
| 7586 | mbedtls_sha256_free( &sha256 ); |
| 7587 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7588 | } |
| 7589 | #endif /* MBEDTLS_SHA256_C */ |
| 7590 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7591 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 7592 | const mbedtls_md_type_t md, |
| 7593 | unsigned char *dst, |
| 7594 | size_t dst_len, |
| 7595 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7596 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7597 | switch( md ) |
| 7598 | { |
| 7599 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7600 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7601 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7602 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 7603 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7604 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7605 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7606 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7607 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 7608 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7609 | |
| 7610 | default: |
| 7611 | break; |
| 7612 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7613 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7614 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 7615 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7616 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7617 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7618 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \ |
| 7619 | defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 7620 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7621 | /* |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7622 | * Function for writing a supported groups (TLS 1.3) or supported elliptic |
| 7623 | * curves (TLS 1.2) extension. |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7624 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7625 | * The "extension_data" field of a supported groups extension contains a |
| 7626 | * "NamedGroupList" value (TLS 1.3 RFC8446): |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7627 | * enum { |
| 7628 | * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), |
| 7629 | * x25519(0x001D), x448(0x001E), |
| 7630 | * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), |
| 7631 | * ffdhe6144(0x0103), ffdhe8192(0x0104), |
| 7632 | * ffdhe_private_use(0x01FC..0x01FF), |
| 7633 | * ecdhe_private_use(0xFE00..0xFEFF), |
| 7634 | * (0xFFFF) |
| 7635 | * } NamedGroup; |
| 7636 | * struct { |
| 7637 | * NamedGroup named_group_list<2..2^16-1>; |
| 7638 | * } NamedGroupList; |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7639 | * |
| 7640 | * The "extension_data" field of a supported elliptic curves extension contains |
| 7641 | * a "NamedCurveList" value (TLS 1.2 RFC 8422): |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7642 | * enum { |
| 7643 | * deprecated(1..22), |
| 7644 | * secp256r1 (23), secp384r1 (24), secp521r1 (25), |
| 7645 | * x25519(29), x448(30), |
| 7646 | * reserved (0xFE00..0xFEFF), |
| 7647 | * deprecated(0xFF01..0xFF02), |
| 7648 | * (0xFFFF) |
| 7649 | * } NamedCurve; |
| 7650 | * struct { |
| 7651 | * NamedCurve named_curve_list<2..2^16-1> |
| 7652 | * } NamedCurveList; |
| 7653 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7654 | * The TLS 1.3 supported groups extension was defined to be a compatible |
| 7655 | * generalization of the TLS 1.2 supported elliptic curves extension. They both |
| 7656 | * share the same extension identifier. |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7657 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7658 | * DHE groups are not supported yet. |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7659 | */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7660 | int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
| 7661 | unsigned char *buf, |
Jerry Yu | 1753261 | 2021-12-20 22:32:09 +0800 | [diff] [blame] | 7662 | const unsigned char *end, |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7663 | size_t *out_len ) |
| 7664 | { |
| 7665 | unsigned char *p = buf ; |
| 7666 | unsigned char *named_group_list; /* Start of named_group_list */ |
| 7667 | size_t named_group_list_len; /* Length of named_group_list */ |
| 7668 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
| 7669 | |
| 7670 | *out_len = 0; |
Jerry Yu | f46b016 | 2022-01-11 16:28:00 +0800 | [diff] [blame] | 7671 | |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7672 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); |
| 7673 | |
| 7674 | /* Check if we have space for header and length fields: |
| 7675 | * - extension_type (2 bytes) |
| 7676 | * - extension_data_length (2 bytes) |
| 7677 | * - named_group_list_length (2 bytes) |
| 7678 | */ |
| 7679 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 7680 | p += 6; |
| 7681 | |
| 7682 | named_group_list = p; |
| 7683 | |
| 7684 | if( group_list == NULL ) |
| 7685 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 7686 | |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7687 | for( ; *group_list != 0; group_list++ ) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7688 | { |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7689 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) ); |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7690 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7691 | #if defined(MBEDTLS_ECP_C) |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7692 | if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) && |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7693 | mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) || |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7694 | ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) && |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7695 | mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) ) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7696 | { |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7697 | const mbedtls_ecp_curve_info *curve_info; |
| 7698 | curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list ); |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7699 | if( curve_info == NULL ) |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7700 | continue; |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7701 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 7702 | MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 ); |
| 7703 | p += 2; |
| 7704 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", |
| 7705 | curve_info->name, *group_list ) ); |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7706 | } |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7707 | #endif /* MBEDTLS_ECP_C */ |
| 7708 | /* Add DHE groups here */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7709 | |
| 7710 | } |
| 7711 | |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7712 | /* Length of named_group_list */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7713 | named_group_list_len = p - named_group_list; |
| 7714 | if( named_group_list_len == 0 ) |
| 7715 | { |
| 7716 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) ); |
| 7717 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7718 | } |
| 7719 | |
| 7720 | /* Write extension_type */ |
| 7721 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); |
| 7722 | /* Write extension_data_length */ |
| 7723 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); |
| 7724 | /* Write length of named_group_list */ |
| 7725 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); |
| 7726 | |
Jerry Yu | 7f029d8 | 2022-01-11 11:08:53 +0800 | [diff] [blame] | 7727 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", |
| 7728 | buf + 4, named_group_list_len + 2 ); |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7729 | |
| 7730 | *out_len = p - buf; |
| 7731 | |
| 7732 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 7733 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 7734 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7735 | |
| 7736 | return( 0 ); |
| 7737 | } |
| 7738 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7739 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED || |
| 7740 | MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 7741 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7742 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7743 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7744 | /* |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7745 | * Function for writing a signature algorithm extension. |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7746 | * |
Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 7747 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7748 | * value (TLS 1.3 RFC8446): |
| 7749 | * enum { |
| 7750 | * .... |
| 7751 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 7752 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 7753 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 7754 | * .... |
| 7755 | * } SignatureScheme; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7756 | * |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7757 | * struct { |
| 7758 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 7759 | * } SignatureSchemeList; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7760 | * |
Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 7761 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 7762 | * value (TLS 1.2 RFC5246): |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7763 | * enum { |
| 7764 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 7765 | * sha512(6), (255) |
| 7766 | * } HashAlgorithm; |
| 7767 | * |
| 7768 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 7769 | * SignatureAlgorithm; |
| 7770 | * |
| 7771 | * struct { |
| 7772 | * HashAlgorithm hash; |
| 7773 | * SignatureAlgorithm signature; |
| 7774 | * } SignatureAndHashAlgorithm; |
| 7775 | * |
| 7776 | * SignatureAndHashAlgorithm |
| 7777 | * supported_signature_algorithms<2..2^16-2>; |
| 7778 | * |
| 7779 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 7780 | * generalization of the TLS 1.2 signature algorithm extension. |
| 7781 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 7782 | * `SignatureScheme` field of TLS 1.3 |
| 7783 | * |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7784 | */ |
| 7785 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 7786 | const unsigned char *end, size_t *out_len ) |
| 7787 | { |
| 7788 | unsigned char *p = buf; |
| 7789 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 7790 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 7791 | |
| 7792 | *out_len = 0; |
| 7793 | |
| 7794 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 7795 | |
| 7796 | /* Check if we have space for header and length field: |
| 7797 | * - extension_type (2 bytes) |
| 7798 | * - extension_data_length (2 bytes) |
| 7799 | * - supported_signature_algorithms_length (2 bytes) |
| 7800 | */ |
| 7801 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 7802 | p += 6; |
| 7803 | |
| 7804 | /* |
| 7805 | * Write supported_signature_algorithms |
| 7806 | */ |
| 7807 | supported_sig_alg = p; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 7808 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 7809 | if( sig_alg == NULL ) |
| 7810 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 7811 | |
| 7812 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7813 | { |
Jerry Yu | 1bab301 | 2022-01-19 17:43:22 +0800 | [diff] [blame] | 7814 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 7815 | continue; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7816 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 7817 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 7818 | p += 2; |
| 7819 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); |
| 7820 | } |
| 7821 | |
| 7822 | /* Length of supported_signature_algorithms */ |
| 7823 | supported_sig_alg_len = p - supported_sig_alg; |
| 7824 | if( supported_sig_alg_len == 0 ) |
| 7825 | { |
| 7826 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 7827 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7828 | } |
| 7829 | |
| 7830 | /* Write extension_type */ |
| 7831 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
| 7832 | /* Write extension_data_length */ |
| 7833 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
| 7834 | /* Write length of supported_signature_algorithms */ |
| 7835 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 7836 | |
| 7837 | /* Output the total length of signature algorithms extension. */ |
| 7838 | *out_len = p - buf; |
| 7839 | |
| 7840 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 7841 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 7842 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7843 | return( 0 ); |
| 7844 | } |
| 7845 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame^] | 7846 | |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7847 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7848 | int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl, |
| 7849 | unsigned char *buf, |
| 7850 | const unsigned char *end, |
| 7851 | size_t *olen ) |
| 7852 | { |
| 7853 | unsigned char *p = buf; |
| 7854 | size_t hostname_len; |
| 7855 | |
| 7856 | *olen = 0; |
| 7857 | |
| 7858 | if( ssl->hostname == NULL ) |
| 7859 | return( 0 ); |
| 7860 | |
| 7861 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 7862 | ( "client hello, adding server name extension: %s", |
| 7863 | ssl->hostname ) ); |
| 7864 | |
| 7865 | hostname_len = strlen( ssl->hostname ); |
| 7866 | |
| 7867 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 ); |
| 7868 | |
| 7869 | /* |
| 7870 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) |
| 7871 | * |
| 7872 | * In order to provide any of the server names, clients MAY include an |
| 7873 | * extension of type "server_name" in the (extended) client hello. The |
| 7874 | * "extension_data" field of this extension SHALL contain |
| 7875 | * "ServerNameList" where: |
| 7876 | * |
| 7877 | * struct { |
| 7878 | * NameType name_type; |
| 7879 | * select (name_type) { |
| 7880 | * case host_name: HostName; |
| 7881 | * } name; |
| 7882 | * } ServerName; |
| 7883 | * |
| 7884 | * enum { |
| 7885 | * host_name(0), (255) |
| 7886 | * } NameType; |
| 7887 | * |
| 7888 | * opaque HostName<1..2^16-1>; |
| 7889 | * |
| 7890 | * struct { |
| 7891 | * ServerName server_name_list<1..2^16-1> |
| 7892 | * } ServerNameList; |
| 7893 | * |
| 7894 | */ |
| 7895 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 ); |
| 7896 | p += 2; |
| 7897 | |
| 7898 | MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 ); |
| 7899 | p += 2; |
| 7900 | |
| 7901 | MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 ); |
| 7902 | p += 2; |
| 7903 | |
| 7904 | *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); |
| 7905 | |
| 7906 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 7907 | p += 2; |
| 7908 | |
| 7909 | memcpy( p, ssl->hostname, hostname_len ); |
| 7910 | |
| 7911 | *olen = hostname_len + 9; |
| 7912 | |
| 7913 | return( 0 ); |
| 7914 | } |
| 7915 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7917 | #endif /* MBEDTLS_SSL_TLS_C */ |