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) |
Jerry Yu | ed14c93 | 2022-02-17 13:40:45 +0800 | [diff] [blame] | 337 | |
| 338 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 339 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 340 | const char *label, |
| 341 | const unsigned char *random, size_t rlen, |
| 342 | unsigned char *dstbuf, size_t dlen ); |
| 343 | |
| 344 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ); |
| 345 | |
| 346 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 347 | |
| 348 | /* Type for the TLS PRF */ |
| 349 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 350 | const unsigned char *, size_t, |
| 351 | unsigned char *, size_t); |
| 352 | |
| 353 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 354 | int ciphersuite, |
| 355 | const unsigned char master[48], |
| 356 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 357 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 358 | int encrypt_then_mac, |
| 359 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 360 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 361 | ssl_tls_prf_t tls_prf, |
| 362 | const unsigned char randbytes[64], |
| 363 | int minor_ver, |
| 364 | unsigned endpoint, |
| 365 | const mbedtls_ssl_context *ssl ); |
| 366 | |
| 367 | #if defined(MBEDTLS_SHA256_C) |
| 368 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 369 | const char *label, |
| 370 | const unsigned char *random, size_t rlen, |
| 371 | unsigned char *dstbuf, size_t dlen ); |
| 372 | static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char*, size_t * ); |
| 373 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
| 374 | |
| 375 | #endif /* MBEDTLS_SHA256_C */ |
| 376 | |
| 377 | #if defined(MBEDTLS_SHA384_C) |
| 378 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 379 | const char *label, |
| 380 | const unsigned char *random, size_t rlen, |
| 381 | unsigned char *dstbuf, size_t dlen ); |
| 382 | |
| 383 | static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char*, size_t * ); |
| 384 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
| 385 | #endif /* MBEDTLS_SHA384_C */ |
| 386 | |
| 387 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 388 | unsigned char *buf, |
| 389 | size_t buf_len ); |
| 390 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 391 | const unsigned char *buf, |
| 392 | size_t len ); |
| 393 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 394 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 395 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 396 | |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 397 | #if defined(MBEDTLS_SHA256_C) |
| 398 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 399 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | db8c48a | 2022-01-27 14:54:54 +0800 | [diff] [blame] | 400 | |
| 401 | #if defined(MBEDTLS_SHA384_C) |
| 402 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 403 | #endif /* MBEDTLS_SHA384_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 404 | |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 405 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 406 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 407 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 408 | { |
| 409 | if( ssl->conf->f_psk != NULL ) |
| 410 | { |
| 411 | /* If we've used a callback to select the PSK, |
| 412 | * the static configuration is irrelevant. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 413 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 414 | return( 1 ); |
| 415 | |
| 416 | return( 0 ); |
| 417 | } |
| 418 | |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 419 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 420 | return( 1 ); |
| 421 | |
| 422 | return( 0 ); |
| 423 | } |
| 424 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
Manuel Pégourié-Gonnard | 45be3d8 | 2019-02-18 23:35:14 +0100 | [diff] [blame] | 425 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
Hanno Becker | 7d0a569 | 2018-10-23 15:26:22 +0100 | [diff] [blame] | 426 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 427 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 428 | const unsigned char *secret, size_t slen, |
| 429 | const char *label, |
| 430 | const unsigned char *random, size_t rlen, |
| 431 | unsigned char *dstbuf, size_t dlen ) |
| 432 | { |
| 433 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 434 | |
| 435 | switch( prf ) |
| 436 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 437 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 438 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 439 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 440 | tls_prf = tls_prf_sha384; |
| 441 | break; |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 442 | #endif /* MBEDTLS_SHA384_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 443 | #if defined(MBEDTLS_SHA256_C) |
| 444 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 445 | tls_prf = tls_prf_sha256; |
| 446 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 447 | #endif /* MBEDTLS_SHA256_C */ |
| 448 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 449 | default: |
| 450 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 451 | } |
| 452 | |
| 453 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 454 | } |
| 455 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 456 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) || \ |
| 457 | defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 458 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 459 | { |
| 460 | ((void) tls_prf); |
| 461 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 462 | #if defined(MBEDTLS_SHA384_C) |
| 463 | if( tls_prf == tls_prf_sha384 ) |
| 464 | { |
| 465 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 466 | } |
| 467 | else |
| 468 | #endif |
| 469 | #if defined(MBEDTLS_SHA256_C) |
| 470 | if( tls_prf == tls_prf_sha256 ) |
| 471 | { |
| 472 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 473 | } |
| 474 | else |
| 475 | #endif |
| 476 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 477 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 478 | } |
| 479 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 480 | /* Type for the TLS PRF */ |
| 481 | typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *, |
| 482 | const unsigned char *, size_t, |
| 483 | unsigned char *, size_t); |
| 484 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 485 | /* |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 486 | * Populate a transform structure with session keys and all the other |
| 487 | * necessary information. |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 488 | * |
Manuel Pégourié-Gonnard | cba40d9 | 2019-05-06 12:55:40 +0200 | [diff] [blame] | 489 | * Parameters: |
| 490 | * - [in/out]: transform: structure to populate |
| 491 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 492 | * [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] | 493 | * - [in] ciphersuite |
| 494 | * - [in] master |
| 495 | * - [in] encrypt_then_mac |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 496 | * - [in] compression |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 497 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 498 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 499 | * - [in] minor_ver: SSL/TLS minor version |
| 500 | * - [in] endpoint: client or server |
Andrzej Kurek | 5902cd6 | 2021-09-28 10:00:32 -0400 | [diff] [blame] | 501 | * - [in] ssl: used for: |
| 502 | * - ssl->conf->{f,p}_export_keys |
| 503 | * [in] optionally used for: |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 504 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 505 | */ |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 506 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 507 | int ciphersuite, |
| 508 | const unsigned char master[48], |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 509 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 510 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 511 | int encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 512 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 513 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 514 | ssl_tls_prf_t tls_prf, |
| 515 | const unsigned char randbytes[64], |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 516 | int minor_ver, |
| 517 | unsigned endpoint, |
Mateusz Starzyk | e204dbf | 2021-03-15 17:57:20 +0100 | [diff] [blame] | 518 | const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 519 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 520 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 | unsigned char keyblk[256]; |
| 522 | unsigned char *key1; |
| 523 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 524 | unsigned char *mac_enc; |
| 525 | unsigned char *mac_dec; |
sander-visser | 3888b03 | 2020-05-06 21:49:46 +0200 | [diff] [blame] | 526 | size_t mac_key_len = 0; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 527 | size_t iv_copy_len; |
Gilles Peskine | 88d681c | 2021-09-01 11:19:33 +0200 | [diff] [blame] | 528 | size_t keylen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 529 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | const mbedtls_cipher_info_t *cipher_info; |
| 531 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 532 | |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 533 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 534 | psa_key_type_t key_type; |
| 535 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 536 | psa_algorithm_t alg; |
| 537 | size_t key_bits; |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 538 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 539 | #endif |
| 540 | |
Andrzej Kurek | 324f72e | 2021-09-29 04:21:21 -0400 | [diff] [blame] | 541 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 542 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Andrzej Kurek | a72fe64 | 2021-09-29 15:57:30 -0400 | [diff] [blame] | 543 | if( ssl->f_export_keys == NULL ) |
| 544 | { |
| 545 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 546 | (void) ssl; |
| 547 | } |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 548 | #endif |
| 549 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 550 | /* |
| 551 | * Some data just needs copying into the structure |
| 552 | */ |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 553 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 554 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 555 | transform->encrypt_then_mac = encrypt_then_mac; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 556 | #endif |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 557 | transform->minor_ver = minor_ver; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 558 | |
Manuel Pégourié-Gonnard | 96fb0ee | 2019-07-09 12:54:17 +0200 | [diff] [blame] | 559 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 560 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 561 | #endif |
| 562 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 563 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 564 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 565 | { |
| 566 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 567 | * generation separate. This should never happen. */ |
| 568 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 569 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 570 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 571 | |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 572 | /* |
| 573 | * Get various info structures |
| 574 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 575 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 576 | if( ciphersuite_info == NULL ) |
| 577 | { |
| 578 | 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] | 579 | ciphersuite ) ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 580 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 581 | } |
| 582 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 583 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 584 | if( cipher_info == NULL ) |
| 585 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 586 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 587 | ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 589 | } |
| 590 | |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 591 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 592 | if( md_info == NULL ) |
| 593 | { |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 594 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 595 | (unsigned) ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 599 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 600 | /* Copy own and peer's CID if the use of the CID |
| 601 | * extension has been negotiated. */ |
| 602 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 603 | { |
| 604 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
Hanno Becker | 8a7f972 | 2019-04-30 13:52:29 +0100 | [diff] [blame] | 605 | |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 606 | transform->in_cid_len = ssl->own_cid_len; |
Hanno Becker | 05154c3 | 2019-05-03 15:23:51 +0100 | [diff] [blame] | 607 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
Hanno Becker | 1c1f046 | 2019-05-03 12:55:51 +0100 | [diff] [blame] | 608 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 609 | transform->in_cid_len ); |
Hanno Becker | d1f2035 | 2019-05-15 10:21:55 +0100 | [diff] [blame] | 610 | |
| 611 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 612 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 613 | ssl->handshake->peer_cid_len ); |
| 614 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 615 | transform->out_cid_len ); |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 616 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 617 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 4bf7465 | 2019-04-26 16:22:27 +0100 | [diff] [blame] | 618 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 619 | /* |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 620 | * Compute key block using the PRF |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 621 | */ |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 622 | 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] | 623 | if( ret != 0 ) |
| 624 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 626 | return( ret ); |
| 627 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 628 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
Manuel Pégourié-Gonnard | d91efa4 | 2019-05-20 10:27:20 +0200 | [diff] [blame] | 630 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 631 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
Manuel Pégourié-Gonnard | 9b108c2 | 2019-05-06 13:32:17 +0200 | [diff] [blame] | 632 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 634 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 635 | /* |
| 636 | * Determine the appropriate key, IV and MAC length. |
| 637 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 638 | |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 639 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 640 | |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 641 | #if defined(MBEDTLS_GCM_C) || \ |
| 642 | defined(MBEDTLS_CCM_C) || \ |
| 643 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 644 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM || |
| 645 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM || |
| 646 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 647 | { |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 648 | size_t explicit_ivlen; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 649 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 650 | transform->maclen = 0; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 651 | mac_key_len = 0; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 652 | transform->taglen = |
| 653 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 654 | |
Hanno Becker | 447558d | 2020-05-28 07:36:33 +0100 | [diff] [blame] | 655 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 656 | * with mode and version: |
| 657 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 658 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
Hanno Becker | f93c2d7 | 2020-05-28 07:39:43 +0100 | [diff] [blame] | 659 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 660 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 661 | * sequence number). |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 662 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 663 | transform->ivlen = 12; |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 664 | 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] | 665 | transform->fixed_ivlen = 12; |
| 666 | else |
Hanno Becker | c0da10d | 2021-04-21 05:32:23 +0100 | [diff] [blame] | 667 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 668 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 669 | /* Minimum length of encrypted record */ |
| 670 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 671 | transform->minlen = explicit_ivlen + transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 672 | } |
| 673 | else |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 674 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 675 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 676 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM || |
| 677 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 678 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 679 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 681 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 682 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 683 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 684 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 685 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 686 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 687 | /* Get MAC length */ |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 688 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 689 | transform->maclen = mac_key_len; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 690 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 691 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 692 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 693 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 694 | /* Minimum length */ |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 695 | 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] | 696 | transform->minlen = transform->maclen; |
| 697 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 698 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 699 | /* |
| 700 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 701 | * 1. if EtM is in use: one block plus MAC |
| 702 | * otherwise: * first multiple of blocklen greater than maclen |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 703 | * 2. IV |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 704 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6fa57bf | 2019-05-10 10:50:04 +0200 | [diff] [blame] | 706 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 707 | { |
| 708 | transform->minlen = transform->maclen |
| 709 | + cipher_info->block_size; |
| 710 | } |
| 711 | else |
| 712 | #endif |
| 713 | { |
| 714 | transform->minlen = transform->maclen |
| 715 | + cipher_info->block_size |
| 716 | - transform->maclen % cipher_info->block_size; |
| 717 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 718 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 719 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 720 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 721 | { |
| 722 | transform->minlen += transform->ivlen; |
| 723 | } |
| 724 | else |
| 725 | #endif |
| 726 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 728 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 729 | goto end; |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 730 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 731 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 732 | } |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 733 | else |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 734 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | 8031d06 | 2018-01-03 15:32:31 +0000 | [diff] [blame] | 735 | { |
| 736 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 737 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 738 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 739 | |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 740 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 741 | (unsigned) keylen, |
| 742 | (unsigned) transform->minlen, |
| 743 | (unsigned) transform->ivlen, |
| 744 | (unsigned) transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 748 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 749 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 750 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 751 | { |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 752 | key1 = keyblk + mac_key_len * 2; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 753 | key2 = keyblk + mac_key_len * 2 + keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 754 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 755 | mac_enc = keyblk; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 756 | mac_dec = keyblk + mac_key_len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 757 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 758 | /* |
| 759 | * This is not used in TLS v1.1. |
| 760 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 761 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 762 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 763 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 764 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 765 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 766 | } |
| 767 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 768 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 769 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | c864f6a | 2019-05-06 13:48:22 +0200 | [diff] [blame] | 770 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 771 | { |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 772 | key1 = keyblk + mac_key_len * 2 + keylen; |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 773 | key2 = keyblk + mac_key_len * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 774 | |
Hanno Becker | 81c7b18 | 2017-11-09 18:39:33 +0000 | [diff] [blame] | 775 | mac_enc = keyblk + mac_key_len; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 776 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 777 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 778 | /* |
| 779 | * This is not used in TLS v1.1. |
| 780 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 781 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 782 | transform->fixed_ivlen : transform->ivlen; |
Hanno Becker | 88aaf65 | 2017-12-27 08:17:40 +0000 | [diff] [blame] | 783 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 784 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 785 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 786 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 787 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 788 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 791 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 792 | goto end; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 793 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 794 | |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 795 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 796 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 797 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 798 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 799 | if( mac_key_len != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 800 | { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 801 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 802 | if( ret != 0 ) |
| 803 | goto end; |
| 804 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 805 | if( ret != 0 ) |
| 806 | goto end; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 807 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 808 | #endif |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 809 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 810 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 811 | ((void) mac_dec); |
| 812 | ((void) mac_enc); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 813 | |
Andrzej Kurek | a72fe64 | 2021-09-29 15:57:30 -0400 | [diff] [blame] | 814 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 815 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 816 | ssl->f_export_keys( ssl->p_export_keys, |
| 817 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 818 | master, 48, |
| 819 | randbytes + 32, |
| 820 | randbytes, |
| 821 | tls_prf_get_type( tls_prf ) ); |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 822 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 823 | |
Hanno Becker | f704bef | 2018-11-16 15:21:18 +0000 | [diff] [blame] | 824 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 825 | if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type, |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 826 | transform->taglen, |
| 827 | &alg, |
| 828 | &key_type, |
| 829 | &key_bits ) ) != PSA_SUCCESS ) |
| 830 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 831 | ret = psa_ssl_status_to_mbedtls( status ); |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 832 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 833 | goto end; |
| 834 | } |
| 835 | |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 836 | transform->psa_alg = alg; |
| 837 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 838 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 839 | { |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 840 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 841 | psa_set_key_algorithm( &attributes, alg ); |
| 842 | psa_set_key_type( &attributes, key_type ); |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 843 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 844 | if( ( status = psa_import_key( &attributes, |
| 845 | key1, |
| 846 | PSA_BITS_TO_BYTES( key_bits ), |
| 847 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 848 | { |
| 849 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 850 | ret = psa_ssl_status_to_mbedtls( status ); |
| 851 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 852 | goto end; |
| 853 | } |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 854 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 855 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 856 | |
| 857 | if( ( status = psa_import_key( &attributes, |
| 858 | key2, |
| 859 | PSA_BITS_TO_BYTES( key_bits ), |
| 860 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 861 | { |
| 862 | ret = psa_ssl_status_to_mbedtls( status ); |
| 863 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 864 | goto end; |
| 865 | } |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 866 | } |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 867 | #else |
| 868 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 869 | cipher_info ) ) != 0 ) |
| 870 | { |
| 871 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 872 | goto end; |
| 873 | } |
| 874 | |
| 875 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 876 | cipher_info ) ) != 0 ) |
| 877 | { |
| 878 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 879 | goto end; |
| 880 | } |
| 881 | |
| 882 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 883 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 884 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 885 | { |
| 886 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 887 | goto end; |
| 888 | } |
| 889 | |
| 890 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 891 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 892 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 893 | { |
| 894 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 895 | goto end; |
| 896 | } |
Przemyslaw Stekiel | ffccda4 | 2022-01-11 14:44:01 +0100 | [diff] [blame] | 897 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 898 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 899 | 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] | 900 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 902 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 903 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 904 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 905 | goto end; |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 906 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 908 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 909 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 911 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 912 | goto end; |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 913 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 914 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 915 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 916 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 917 | |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 918 | end: |
Ron Eldor | a9f9a73 | 2019-05-07 18:29:02 +0300 | [diff] [blame] | 919 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
Ron Eldor | e699270 | 2019-05-07 18:27:13 +0300 | [diff] [blame] | 920 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 921 | } |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 922 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 || MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 923 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 924 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 925 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 926 | { |
| 927 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 928 | if( session->peer_cert != NULL ) |
| 929 | { |
| 930 | mbedtls_x509_crt_free( session->peer_cert ); |
| 931 | mbedtls_free( session->peer_cert ); |
| 932 | session->peer_cert = NULL; |
| 933 | } |
| 934 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 935 | if( session->peer_cert_digest != NULL ) |
| 936 | { |
| 937 | /* Zeroization is not necessary. */ |
| 938 | mbedtls_free( session->peer_cert_digest ); |
| 939 | session->peer_cert_digest = NULL; |
| 940 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 941 | session->peer_cert_digest_len = 0; |
| 942 | } |
| 943 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 944 | } |
| 945 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 946 | |
| 947 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 948 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 949 | { |
| 950 | ((void) ciphersuite_info); |
| 951 | |
| 952 | #if defined(MBEDTLS_SHA384_C) |
| 953 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 954 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 955 | else |
| 956 | #endif |
| 957 | #if defined(MBEDTLS_SHA256_C) |
| 958 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 959 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 960 | else |
| 961 | #endif |
| 962 | { |
| 963 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 964 | return; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 969 | { |
| 970 | ((void) ssl); |
| 971 | #if defined(MBEDTLS_SHA256_C) |
| 972 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 973 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 974 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 975 | #else |
| 976 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 977 | #endif |
| 978 | #endif |
| 979 | #if defined(MBEDTLS_SHA384_C) |
| 980 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 981 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 982 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 983 | #else |
| 984 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); |
| 985 | #endif |
| 986 | #endif |
| 987 | } |
| 988 | |
| 989 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 990 | const unsigned char *buf, size_t len ) |
| 991 | { |
| 992 | #if defined(MBEDTLS_SHA256_C) |
| 993 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 994 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 995 | #else |
| 996 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 997 | #endif |
| 998 | #endif |
| 999 | #if defined(MBEDTLS_SHA384_C) |
| 1000 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1001 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 1002 | #else |
| 1003 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 1004 | #endif |
| 1005 | #endif |
| 1006 | } |
| 1007 | |
| 1008 | #if defined(MBEDTLS_SHA256_C) |
| 1009 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 1010 | const unsigned char *buf, size_t len ) |
| 1011 | { |
| 1012 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1013 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 1014 | #else |
| 1015 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 1016 | #endif |
| 1017 | } |
| 1018 | #endif |
| 1019 | |
| 1020 | #if defined(MBEDTLS_SHA384_C) |
| 1021 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 1022 | const unsigned char *buf, size_t len ) |
| 1023 | { |
| 1024 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1025 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 1026 | #else |
| 1027 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 1028 | #endif |
| 1029 | } |
| 1030 | #endif |
| 1031 | |
Jerry Yu | bef175d | 2022-01-28 10:52:05 +0800 | [diff] [blame] | 1032 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1033 | /* |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1034 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1035 | * |
| 1036 | * Inputs: |
| 1037 | * - SSL/TLS minor version |
| 1038 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 1039 | * |
Manuel Pégourié-Gonnard | 31d3ef1 | 2019-05-10 10:25:00 +0200 | [diff] [blame] | 1040 | * Outputs: |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1041 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 1042 | */ |
| 1043 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 1044 | int minor_ver, |
| 1045 | mbedtls_md_type_t hash ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1046 | { |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1047 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1048 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1049 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 1050 | hash == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1051 | { |
| 1052 | handshake->tls_prf = tls_prf_sha384; |
| 1053 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 1054 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 1055 | } |
| 1056 | else |
| 1057 | #endif |
| 1058 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1059 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1060 | { |
| 1061 | handshake->tls_prf = tls_prf_sha256; |
| 1062 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 1063 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 1064 | } |
| 1065 | else |
| 1066 | #endif |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1067 | { |
Jerry Yu | bef175d | 2022-01-28 10:52:05 +0800 | [diff] [blame] | 1068 | (void) hash; |
| 1069 | (void) minor_ver; |
| 1070 | (void) handshake; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1071 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1072 | } |
Jerry Yu | bef175d | 2022-01-28 10:52:05 +0800 | [diff] [blame] | 1073 | |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1074 | |
| 1075 | return( 0 ); |
| 1076 | } |
| 1077 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1078 | /* |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1079 | * Compute master secret if needed |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1080 | * |
| 1081 | * Parameters: |
| 1082 | * [in/out] handshake |
| 1083 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1084 | * (PSA-PSK) ciphersuite_info, psk_opaque |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1085 | * [out] premaster (cleared) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1086 | * [out] master |
| 1087 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 1088 | * debug: conf->f_dbg, conf->p_dbg |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1089 | * EMS: passed to calc_verify (debug + session_negotiate) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1090 | * PSA-PSA: minor_ver, conf |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1091 | */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1092 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1093 | unsigned char *master, |
Manuel Pégourié-Gonnard | 0d56aaa | 2019-05-03 09:58:33 +0200 | [diff] [blame] | 1094 | const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1095 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1096 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1097 | |
| 1098 | /* cf. RFC 5246, Section 8.1: |
| 1099 | * "The master secret is always exactly 48 bytes in length." */ |
| 1100 | size_t const master_secret_len = 48; |
| 1101 | |
| 1102 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 1103 | unsigned char session_hash[48]; |
| 1104 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 1105 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1106 | /* The label for the KDF used for key expansion. |
| 1107 | * This is either "master secret" or "extended master secret" |
| 1108 | * depending on whether the Extended Master Secret extension |
| 1109 | * is used. */ |
| 1110 | char const *lbl = "master secret"; |
| 1111 | |
| 1112 | /* The salt for the KDF used for key expansion. |
| 1113 | * - If the Extended Master Secret extension is not used, |
| 1114 | * this is ClientHello.Random + ServerHello.Random |
| 1115 | * (see Sect. 8.1 in RFC 5246). |
| 1116 | * - If the Extended Master Secret extension is used, |
| 1117 | * this is the transcript of the handshake so far. |
| 1118 | * (see Sect. 4 in RFC 7627). */ |
| 1119 | unsigned char const *salt = handshake->randbytes; |
| 1120 | size_t salt_len = 64; |
| 1121 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1122 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 1123 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 1124 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 1125 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1126 | 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] | 1127 | (void) ssl; |
| 1128 | #endif |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1129 | |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1130 | if( handshake->resume != 0 ) |
| 1131 | { |
| 1132 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1133 | return( 0 ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1134 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1135 | |
| 1136 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1137 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1138 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1139 | lbl = "extended master secret"; |
| 1140 | salt = session_hash; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1141 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1142 | |
Manuel Pégourié-Gonnard | 8faa70e | 2019-05-20 12:09:50 +0200 | [diff] [blame] | 1143 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 1144 | session_hash, salt_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1145 | } |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1146 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 1147 | |
| 1148 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1149 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1150 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1151 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1152 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 1153 | { |
| 1154 | /* Perform PSK-to-MS expansion in a single step. */ |
| 1155 | psa_status_t status; |
| 1156 | psa_algorithm_t alg; |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1157 | mbedtls_svc_key_id_t psk; |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1158 | psa_key_derivation_operation_t derivation = |
| 1159 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1160 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1161 | |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1162 | 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] | 1163 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1164 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1165 | |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1166 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1167 | 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] | 1168 | else |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1169 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 1170 | |
k-stachowiak | 81053a5 | 2019-08-17 10:30:28 +0200 | [diff] [blame] | 1171 | status = setup_psa_key_derivation( &derivation, psk, alg, |
| 1172 | salt, salt_len, |
| 1173 | (unsigned char const *) lbl, |
| 1174 | (size_t) strlen( lbl ), |
| 1175 | master_secret_len ); |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1176 | if( status != PSA_SUCCESS ) |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1177 | { |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1178 | psa_key_derivation_abort( &derivation ); |
| 1179 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1180 | } |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1181 | |
| 1182 | status = psa_key_derivation_output_bytes( &derivation, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1183 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1184 | master_secret_len ); |
| 1185 | if( status != PSA_SUCCESS ) |
| 1186 | { |
| 1187 | psa_key_derivation_abort( &derivation ); |
| 1188 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1189 | } |
| 1190 | |
| 1191 | status = psa_key_derivation_abort( &derivation ); |
| 1192 | if( status != PSA_SUCCESS ) |
| 1193 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 1194 | } |
| 1195 | else |
| 1196 | #endif |
| 1197 | { |
| 1198 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 1199 | lbl, salt, salt_len, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1200 | master, |
Manuel Pégourié-Gonnard | 85680c4 | 2019-05-03 09:16:16 +0200 | [diff] [blame] | 1201 | master_secret_len ); |
| 1202 | if( ret != 0 ) |
| 1203 | { |
| 1204 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 1205 | return( ret ); |
| 1206 | } |
| 1207 | |
| 1208 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 1209 | handshake->premaster, |
| 1210 | handshake->pmslen ); |
| 1211 | |
| 1212 | mbedtls_platform_zeroize( handshake->premaster, |
| 1213 | sizeof(handshake->premaster) ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | return( 0 ); |
| 1217 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1218 | |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1219 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 1220 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1221 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1222 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 1223 | ssl->handshake->ciphersuite_info; |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1224 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1225 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 1226 | |
| 1227 | /* Set PRF, calc_verify and calc_finished function pointers */ |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1228 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 1229 | ssl->minor_ver, |
| 1230 | ciphersuite_info->mac ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1231 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1232 | { |
| 1233 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1234 | return( ret ); |
Manuel Pégourié-Gonnard | 8d2805c | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1235 | } |
Manuel Pégourié-Gonnard | 1b00c4f | 2019-04-30 11:54:22 +0200 | [diff] [blame] | 1236 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1237 | /* Compute master secret if needed */ |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1238 | ret = ssl_compute_master( ssl->handshake, |
Manuel Pégourié-Gonnard | de047ad | 2019-05-03 09:46:14 +0200 | [diff] [blame] | 1239 | ssl->session_negotiate->master, |
| 1240 | ssl ); |
Manuel Pégourié-Gonnard | 9951b71 | 2019-04-30 12:08:59 +0200 | [diff] [blame] | 1241 | if( ret != 0 ) |
| 1242 | { |
| 1243 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 1244 | return( ret ); |
| 1245 | } |
| 1246 | |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1247 | /* Swap the client and server random values: |
| 1248 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 1249 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 1250 | { |
| 1251 | unsigned char tmp[64]; |
| 1252 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 1253 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 1254 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 1255 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 1256 | } |
| 1257 | |
| 1258 | /* Populate transform structure */ |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 1259 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 1260 | ssl->session_negotiate->ciphersuite, |
| 1261 | ssl->session_negotiate->master, |
| 1262 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 1263 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1264 | ssl->session_negotiate->encrypt_then_mac, |
| 1265 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 1266 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 1267 | ssl->handshake->tls_prf, |
| 1268 | ssl->handshake->randbytes, |
| 1269 | ssl->minor_ver, |
| 1270 | ssl->conf->endpoint, |
| 1271 | ssl ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1272 | if( ret != 0 ) |
| 1273 | { |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 1274 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
Manuel Pégourié-Gonnard | 040a951 | 2019-05-06 12:05:58 +0200 | [diff] [blame] | 1275 | return( ret ); |
| 1276 | } |
| 1277 | |
| 1278 | /* We no longer need Server/ClientHello.random values */ |
| 1279 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1280 | sizeof( ssl->handshake->randbytes ) ); |
| 1281 | |
| 1282 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 1283 | |
| 1284 | return( 0 ); |
Manuel Pégourié-Gonnard | e59ae23 | 2019-04-30 11:41:40 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1288 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1289 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1290 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1291 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1292 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1293 | size_t hash_size; |
| 1294 | psa_status_t status; |
| 1295 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 1296 | |
| 1297 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 1298 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 1299 | if( status != PSA_SUCCESS ) |
| 1300 | { |
| 1301 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 1306 | if( status != PSA_SUCCESS ) |
| 1307 | { |
| 1308 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1309 | return; |
| 1310 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1311 | |
| 1312 | *hlen = 32; |
| 1313 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1314 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1315 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1316 | mbedtls_sha256_context sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1317 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1318 | mbedtls_sha256_init( &sha256 ); |
| 1319 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1320 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1321 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1322 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 1323 | mbedtls_sha256_finish( &sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1324 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1325 | *hlen = 32; |
| 1326 | |
| 1327 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1328 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1329 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1330 | mbedtls_sha256_free( &sha256 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1331 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1332 | return; |
| 1333 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1334 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1335 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1336 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1337 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
Rodrigo Dias Correa | 2c42457 | 2020-11-10 01:38:00 -0300 | [diff] [blame] | 1338 | unsigned char *hash, |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1339 | size_t *hlen ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1340 | { |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1341 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1342 | size_t hash_size; |
| 1343 | psa_status_t status; |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1344 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1345 | |
| 1346 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1347 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1348 | if( status != PSA_SUCCESS ) |
| 1349 | { |
| 1350 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 1351 | return; |
| 1352 | } |
| 1353 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 1354 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1355 | if( status != PSA_SUCCESS ) |
| 1356 | { |
| 1357 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 1358 | return; |
| 1359 | } |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1360 | |
| 1361 | *hlen = 48; |
| 1362 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1363 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 1364 | #else |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1365 | mbedtls_sha512_context sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1366 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 1367 | mbedtls_sha512_init( &sha512 ); |
| 1368 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1369 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1370 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1371 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 1372 | mbedtls_sha512_finish( &sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1373 | |
Manuel Pégourié-Gonnard | de718b9 | 2019-05-03 11:43:28 +0200 | [diff] [blame] | 1374 | *hlen = 48; |
| 1375 | |
| 1376 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1377 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1378 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 1379 | mbedtls_sha512_free( &sha512 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 1380 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1381 | return; |
| 1382 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 1383 | #endif /* MBEDTLS_SHA384_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1384 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1385 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1386 | 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] | 1387 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1388 | unsigned char *p = ssl->handshake->premaster; |
| 1389 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Guilhem Bryant | b5f04e4 | 2020-04-01 11:23:58 +0100 | [diff] [blame] | 1390 | const unsigned char *psk = NULL; |
Guilhem Bryant | 61b0fe6 | 2020-03-27 11:12:12 +0000 | [diff] [blame] | 1391 | size_t psk_len = 0; |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1392 | |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1393 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) |
| 1394 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1395 | { |
Guilhem Bryant | c5285d8 | 2020-03-25 17:08:15 +0000 | [diff] [blame] | 1396 | /* |
| 1397 | * This should never happen because the existence of a PSK is always |
| 1398 | * checked before calling this function |
| 1399 | */ |
Guilhem Bryant | 82194c8 | 2020-03-26 17:04:31 +0000 | [diff] [blame] | 1400 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Guilhem Bryant | 0c9b195 | 2020-04-08 11:02:38 +0100 | [diff] [blame] | 1401 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1402 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1403 | |
| 1404 | /* |
| 1405 | * PMS = struct { |
| 1406 | * opaque other_secret<0..2^16-1>; |
| 1407 | * opaque psk<0..2^16-1>; |
| 1408 | * }; |
| 1409 | * with "other_secret" depending on the particular key exchange |
| 1410 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1411 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1412 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1413 | { |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1414 | if( end - p < 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1415 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1416 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1417 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1418 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1419 | |
| 1420 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1421 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1422 | |
| 1423 | memset( p, 0, psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1424 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1425 | } |
| 1426 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1427 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1428 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1429 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1430 | { |
| 1431 | /* |
| 1432 | * other_secret already set by the ClientKeyExchange message, |
| 1433 | * and is 48 bytes long |
| 1434 | */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1435 | if( end - p < 2 ) |
| 1436 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1437 | |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1438 | *p++ = 0; |
| 1439 | *p++ = 48; |
| 1440 | p += 48; |
| 1441 | } |
| 1442 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1443 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1444 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1445 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1446 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1447 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1448 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1449 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1450 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1451 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1452 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1453 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1454 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1455 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1456 | return( ret ); |
| 1457 | } |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1458 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 1459 | p += 2 + len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1461 | 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] | 1462 | } |
| 1463 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1464 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1465 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1466 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1467 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1468 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1469 | size_t zlen; |
| 1470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1472 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1473 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1474 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1475 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1476 | return( ret ); |
| 1477 | } |
| 1478 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1479 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 1480 | p += 2 + zlen; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1481 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1482 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 1483 | MBEDTLS_DEBUG_ECDH_Z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1484 | } |
| 1485 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1486 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1487 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1488 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1489 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1493 | if( end - p < 2 ) |
| 1494 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1495 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 1496 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 1497 | p += 2; |
Manuel Pégourié-Gonnard | bc5e508 | 2015-10-21 12:35:29 +0200 | [diff] [blame] | 1498 | |
| 1499 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 1500 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1501 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1502 | memcpy( p, psk, psk_len ); |
| 1503 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1504 | |
| 1505 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1506 | |
| 1507 | return( 0 ); |
| 1508 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1509 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1511 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1512 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1514 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 1515 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1516 | { |
| 1517 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 1518 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1519 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1520 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1521 | 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] | 1522 | unsigned char doublings = 1; |
| 1523 | |
| 1524 | while( ratio != 0 ) |
| 1525 | { |
| 1526 | ++doublings; |
| 1527 | ratio >>= 1; |
| 1528 | } |
| 1529 | |
| 1530 | if( ++ssl->renego_records_seen > doublings ) |
| 1531 | { |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 1532 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 1533 | return( 0 ); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | return( ssl_write_hello_request( ssl ) ); |
| 1538 | } |
| 1539 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1540 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1541 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1542 | /* |
| 1543 | * Handshake functions |
| 1544 | */ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1545 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1546 | /* No certificate support -> dummy functions */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1547 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1548 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1549 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1550 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1551 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1552 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1553 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1554 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1555 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1557 | ssl->state++; |
| 1558 | return( 0 ); |
| 1559 | } |
| 1560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1561 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1562 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1565 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1566 | { |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1567 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1568 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1569 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1570 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1571 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1572 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1573 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1574 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1575 | ssl->state++; |
| 1576 | return( 0 ); |
| 1577 | } |
| 1578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1579 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1580 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1581 | } |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1582 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1583 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Gilles Peskine | f982852 | 2017-05-03 12:28:43 +0200 | [diff] [blame] | 1584 | /* Some certificate support -> implement write and parse */ |
| 1585 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1586 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1587 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1588 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1589 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1590 | const mbedtls_x509_crt *crt; |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1591 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 1592 | ssl->handshake->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1595 | |
Hanno Becker | 7177a88 | 2019-02-05 13:36:46 +0000 | [diff] [blame] | 1596 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1597 | { |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 1598 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 1599 | ssl->state++; |
| 1600 | return( 0 ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 1601 | } |
| 1602 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1603 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1604 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1605 | { |
Jerry Yu | fb28b88 | 2022-01-28 11:05:58 +0800 | [diff] [blame] | 1606 | if( ssl->handshake->client_auth == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1607 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1608 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1609 | ssl->state++; |
| 1610 | return( 0 ); |
| 1611 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1612 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1613 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1614 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1615 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1616 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1617 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1618 | { |
Hanno Becker | 9cfe6e9 | 2021-04-30 05:38:24 +0100 | [diff] [blame] | 1619 | /* Should never happen because we shouldn't have picked the |
| 1620 | * ciphersuite if we don't have a certificate. */ |
| 1621 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1622 | } |
| 1623 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 1624 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1625 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1626 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1627 | |
| 1628 | /* |
| 1629 | * 0 . 0 handshake type |
| 1630 | * 1 . 3 handshake length |
| 1631 | * 4 . 6 length of all certs |
| 1632 | * 7 . 9 length of cert. 1 |
| 1633 | * 10 . n-1 peer certificate |
| 1634 | * n . n+2 length of cert. 2 |
| 1635 | * n+3 . ... upper level cert, etc. |
| 1636 | */ |
| 1637 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1638 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1639 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 1640 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1641 | { |
| 1642 | n = crt->raw.len; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1643 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1644 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1645 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 1646 | " > %" MBEDTLS_PRINTF_SIZET, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 1647 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Hanno Becker | 91e1cc3 | 2021-04-30 05:28:49 +0100 | [diff] [blame] | 1648 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 1651 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 1652 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 1653 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1654 | |
| 1655 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 1656 | i += n; crt = crt->next; |
| 1657 | } |
| 1658 | |
Joe Subbiani | fbeb692 | 2021-07-16 14:27:50 +0100 | [diff] [blame] | 1659 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 1660 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 1661 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1662 | |
| 1663 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 1665 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1666 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1667 | ssl->state++; |
| 1668 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1669 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1670 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 1671 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1672 | return( ret ); |
| 1673 | } |
| 1674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1675 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1676 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1677 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 1680 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1681 | |
| 1682 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1683 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 1684 | unsigned char *crt_buf, |
| 1685 | size_t crt_buf_len ) |
| 1686 | { |
| 1687 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 1688 | |
| 1689 | if( peer_crt == NULL ) |
| 1690 | return( -1 ); |
| 1691 | |
| 1692 | if( peer_crt->raw.len != crt_buf_len ) |
| 1693 | return( -1 ); |
| 1694 | |
k-stachowiak | 95b68ef | 2019-09-16 12:21:00 +0200 | [diff] [blame] | 1695 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1696 | } |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1697 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1698 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 1699 | unsigned char *crt_buf, |
| 1700 | size_t crt_buf_len ) |
| 1701 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1702 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 177475a | 2019-02-05 17:02:46 +0000 | [diff] [blame] | 1703 | unsigned char const * const peer_cert_digest = |
| 1704 | ssl->session->peer_cert_digest; |
| 1705 | mbedtls_md_type_t const peer_cert_digest_type = |
| 1706 | ssl->session->peer_cert_digest_type; |
| 1707 | mbedtls_md_info_t const * const digest_info = |
| 1708 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 1709 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 1710 | size_t digest_len; |
| 1711 | |
| 1712 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 1713 | return( -1 ); |
| 1714 | |
| 1715 | digest_len = mbedtls_md_get_size( digest_info ); |
| 1716 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 1717 | return( -1 ); |
| 1718 | |
| 1719 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 1720 | if( ret != 0 ) |
| 1721 | return( -1 ); |
| 1722 | |
| 1723 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 1724 | } |
| 1725 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 84879e3 | 2019-01-31 07:44:03 +0000 | [diff] [blame] | 1726 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1727 | |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1728 | /* |
| 1729 | * Once the certificate message is read, parse it into a cert chain and |
| 1730 | * perform basic checks, but leave actual verification to the caller |
| 1731 | */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1732 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 1733 | mbedtls_x509_crt *chain ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1734 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1735 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1736 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1737 | int crt_cnt=0; |
| 1738 | #endif |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1739 | size_t i, n; |
Gilles Peskine | 064a85c | 2017-05-10 10:46:40 +0200 | [diff] [blame] | 1740 | uint8_t alert; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1742 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1743 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1744 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1745 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1746 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1747 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1750 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 1751 | { |
| 1752 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1753 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 1754 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 1755 | } |
| 1756 | |
| 1757 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1758 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1759 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1760 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1761 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1762 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1765 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1766 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1767 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1768 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1769 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1770 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1771 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 1772 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1773 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1774 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1775 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1776 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1777 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1778 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1781 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 1782 | i += 3; |
| 1783 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1784 | /* Iterate through and parse the CRTs in the provided chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1785 | while( i < ssl->in_hslen ) |
| 1786 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1787 | /* Check that there's room for the next CRT's length fields. */ |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1788 | if ( i + 3 > ssl->in_hslen ) { |
| 1789 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1790 | mbedtls_ssl_send_alert_message( ssl, |
| 1791 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1792 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1793 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Philippe Antoine | 747fd53 | 2018-05-30 09:13:21 +0200 | [diff] [blame] | 1794 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1795 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 1796 | * anything beyond 2**16 ~ 64K. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1797 | if( ssl->in_msg[i] != 0 ) |
| 1798 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1799 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1800 | mbedtls_ssl_send_alert_message( ssl, |
| 1801 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1802 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 1803 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1806 | /* Read length of the next CRT in the chain. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1807 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 1808 | | (unsigned int) ssl->in_msg[i + 2]; |
| 1809 | i += 3; |
| 1810 | |
| 1811 | if( n < 128 || i + n > ssl->in_hslen ) |
| 1812 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1813 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
Hanno Becker | e2734e2 | 2019-01-31 07:44:17 +0000 | [diff] [blame] | 1814 | mbedtls_ssl_send_alert_message( ssl, |
| 1815 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 1816 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 1817 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1820 | /* Check if we're handling the first CRT in the chain. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1821 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 1822 | if( crt_cnt++ == 0 && |
| 1823 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 1824 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1825 | { |
Hanno Becker | 46f34d0 | 2019-02-08 14:00:04 +0000 | [diff] [blame] | 1826 | /* During client-side renegotiation, check that the server's |
| 1827 | * end-CRTs hasn't changed compared to the initial handshake, |
| 1828 | * mitigating the triple handshake attack. On success, reuse |
| 1829 | * the original end-CRT instead of parsing it again. */ |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1830 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 1831 | if( ssl_check_peer_crt_unchanged( ssl, |
| 1832 | &ssl->in_msg[i], |
| 1833 | n ) != 0 ) |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1834 | { |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1835 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 1836 | mbedtls_ssl_send_alert_message( ssl, |
| 1837 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 1838 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 1839 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1840 | } |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1841 | |
| 1842 | /* Now we can safely free the original chain. */ |
| 1843 | ssl_clear_peer_cert( ssl->session ); |
| 1844 | } |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1845 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 1846 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1847 | /* Parse the next certificate in the chain. */ |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1848 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1849 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1850 | #else |
Hanno Becker | 353a6f0 | 2019-02-26 11:51:34 +0000 | [diff] [blame] | 1851 | /* If we don't need to store the CRT chain permanently, parse |
Hanno Becker | 0056eab | 2019-02-08 14:39:16 +0000 | [diff] [blame] | 1852 | * it in-place from the input buffer instead of making a copy. */ |
| 1853 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 1854 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1855 | switch( ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1856 | { |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1857 | case 0: /*ok*/ |
| 1858 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 1859 | /* Ignore certificate with an unknown algorithm: maybe a |
| 1860 | prior certificate was already trusted. */ |
| 1861 | break; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1862 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1863 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 1864 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 1865 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1866 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1867 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 1868 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 1869 | goto crt_parse_der_failed; |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 1870 | |
Hanno Becker | def9bdc | 2019-01-30 14:46:46 +0000 | [diff] [blame] | 1871 | default: |
| 1872 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 1873 | crt_parse_der_failed: |
| 1874 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 1875 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 1876 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | i += n; |
| 1880 | } |
| 1881 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 1882 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1883 | return( 0 ); |
| 1884 | } |
| 1885 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1886 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1887 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 1888 | { |
| 1889 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1890 | return( -1 ); |
| 1891 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1892 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 1893 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 1894 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 1895 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 1896 | { |
| 1897 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 1898 | return( 0 ); |
| 1899 | } |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 1900 | return( -1 ); |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 1901 | } |
| 1902 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1903 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1904 | /* Check if a certificate message is expected. |
| 1905 | * Return either |
| 1906 | * - SSL_CERTIFICATE_EXPECTED, or |
| 1907 | * - SSL_CERTIFICATE_SKIP |
| 1908 | * indicating whether a Certificate message is expected or not. |
| 1909 | */ |
| 1910 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 1911 | #define SSL_CERTIFICATE_SKIP 1 |
| 1912 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 1913 | int authmode ) |
| 1914 | { |
| 1915 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1916 | ssl->handshake->ciphersuite_info; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1917 | |
| 1918 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 1919 | return( SSL_CERTIFICATE_SKIP ); |
| 1920 | |
| 1921 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1922 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1923 | { |
| 1924 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 1925 | return( SSL_CERTIFICATE_SKIP ); |
| 1926 | |
| 1927 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 1928 | { |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1929 | ssl->session_negotiate->verify_result = |
| 1930 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 1931 | return( SSL_CERTIFICATE_SKIP ); |
| 1932 | } |
| 1933 | } |
Hanno Becker | 84d9d27 | 2019-03-01 08:10:46 +0000 | [diff] [blame] | 1934 | #else |
| 1935 | ((void) authmode); |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1936 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1937 | |
| 1938 | return( SSL_CERTIFICATE_EXPECTED ); |
| 1939 | } |
| 1940 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1941 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 1942 | int authmode, |
| 1943 | mbedtls_x509_crt *chain, |
| 1944 | void *rs_ctx ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 1945 | { |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 1946 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 1947 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1948 | ssl->handshake->ciphersuite_info; |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1949 | int have_ca_chain = 0; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1950 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1951 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 1952 | void *p_vrfy; |
| 1953 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1954 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 1955 | return( 0 ); |
| 1956 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1957 | if( ssl->f_vrfy != NULL ) |
| 1958 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 1959 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1960 | f_vrfy = ssl->f_vrfy; |
| 1961 | p_vrfy = ssl->p_vrfy; |
| 1962 | } |
| 1963 | else |
| 1964 | { |
Hanno Becker | efb440a | 2019-04-03 13:04:33 +0100 | [diff] [blame] | 1965 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1966 | f_vrfy = ssl->conf->f_vrfy; |
| 1967 | p_vrfy = ssl->conf->p_vrfy; |
| 1968 | } |
| 1969 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 1970 | /* |
| 1971 | * Main check: verify certificate |
| 1972 | */ |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1973 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1974 | if( ssl->conf->f_ca_cb != NULL ) |
| 1975 | { |
| 1976 | ((void) rs_ctx); |
| 1977 | have_ca_chain = 1; |
| 1978 | |
| 1979 | 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] | 1980 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1981 | chain, |
| 1982 | ssl->conf->f_ca_cb, |
| 1983 | ssl->conf->p_ca_cb, |
| 1984 | ssl->conf->cert_profile, |
| 1985 | ssl->hostname, |
| 1986 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 1987 | f_vrfy, p_vrfy ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1988 | } |
| 1989 | else |
| 1990 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 1991 | { |
| 1992 | mbedtls_x509_crt *ca_chain; |
| 1993 | mbedtls_x509_crl *ca_crl; |
| 1994 | |
| 1995 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1996 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 1997 | { |
| 1998 | ca_chain = ssl->handshake->sni_ca_chain; |
| 1999 | ca_crl = ssl->handshake->sni_ca_crl; |
| 2000 | } |
| 2001 | else |
| 2002 | #endif |
| 2003 | { |
| 2004 | ca_chain = ssl->conf->ca_chain; |
| 2005 | ca_crl = ssl->conf->ca_crl; |
| 2006 | } |
| 2007 | |
| 2008 | if( ca_chain != NULL ) |
| 2009 | have_ca_chain = 1; |
| 2010 | |
| 2011 | ret = mbedtls_x509_crt_verify_restartable( |
| 2012 | chain, |
| 2013 | ca_chain, ca_crl, |
| 2014 | ssl->conf->cert_profile, |
| 2015 | ssl->hostname, |
| 2016 | &ssl->session_negotiate->verify_result, |
Jaeden Amero | fe71067 | 2019-04-16 15:03:12 +0100 | [diff] [blame] | 2017 | f_vrfy, p_vrfy, rs_ctx ); |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2018 | } |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2019 | |
| 2020 | if( ret != 0 ) |
| 2021 | { |
| 2022 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 2023 | } |
| 2024 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2025 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2026 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 2027 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 2028 | #endif |
| 2029 | |
| 2030 | /* |
| 2031 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 2032 | */ |
| 2033 | |
| 2034 | #if defined(MBEDTLS_ECP_C) |
| 2035 | { |
| 2036 | const mbedtls_pk_context *pk = &chain->pk; |
| 2037 | |
| 2038 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 2039 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 2040 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 2041 | { |
| 2042 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 2043 | |
| 2044 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 2045 | if( ret == 0 ) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2046 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | #endif /* MBEDTLS_ECP_C */ |
| 2050 | |
| 2051 | if( mbedtls_ssl_check_cert_usage( chain, |
| 2052 | ciphersuite_info, |
| 2053 | ! ssl->conf->endpoint, |
| 2054 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 2055 | { |
| 2056 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 2057 | if( ret == 0 ) |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2058 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 2062 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 2063 | * with details encoded in the verification flags. All other kinds |
| 2064 | * of error codes, including those from the user provided f_vrfy |
| 2065 | * functions, are treated as fatal and lead to a failure of |
| 2066 | * ssl_parse_certificate even if verification was optional. */ |
| 2067 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 2068 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
Hanno Becker | 9ed1ba5 | 2021-06-24 11:03:13 +0100 | [diff] [blame] | 2069 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2070 | { |
| 2071 | ret = 0; |
| 2072 | } |
| 2073 | |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 2074 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2075 | { |
| 2076 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 2077 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 2078 | } |
| 2079 | |
| 2080 | if( ret != 0 ) |
| 2081 | { |
| 2082 | uint8_t alert; |
| 2083 | |
| 2084 | /* The certificate may have been rejected for several reasons. |
| 2085 | Pick one and send the corresponding alert. Which alert to send |
| 2086 | may be a subject of debate in some cases. */ |
| 2087 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 2088 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 2089 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 2090 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 2091 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 2092 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2093 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 2094 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2095 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 2096 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2097 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 2098 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2099 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 2100 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 2101 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 2102 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 2103 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 2104 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 2105 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 2106 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 2107 | else |
| 2108 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 2109 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2110 | alert ); |
| 2111 | } |
| 2112 | |
| 2113 | #if defined(MBEDTLS_DEBUG_C) |
| 2114 | if( ssl->session_negotiate->verify_result != 0 ) |
| 2115 | { |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2116 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2117 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2118 | } |
| 2119 | else |
| 2120 | { |
| 2121 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 2122 | } |
| 2123 | #endif /* MBEDTLS_DEBUG_C */ |
| 2124 | |
| 2125 | return( ret ); |
| 2126 | } |
| 2127 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2128 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2129 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 2130 | unsigned char *start, size_t len ) |
| 2131 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2132 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2133 | /* Remember digest of the peer's end-CRT. */ |
| 2134 | ssl->session_negotiate->peer_cert_digest = |
| 2135 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 2136 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 2137 | { |
| 2138 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
irwir | 40883e9 | 2019-09-21 17:55:33 +0300 | [diff] [blame] | 2139 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2140 | mbedtls_ssl_send_alert_message( ssl, |
| 2141 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2142 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 2143 | |
| 2144 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2145 | } |
| 2146 | |
| 2147 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 2148 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 2149 | start, len, |
| 2150 | ssl->session_negotiate->peer_cert_digest ); |
| 2151 | |
| 2152 | ssl->session_negotiate->peer_cert_digest_type = |
| 2153 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 2154 | ssl->session_negotiate->peer_cert_digest_len = |
| 2155 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 2156 | |
| 2157 | return( ret ); |
| 2158 | } |
| 2159 | |
| 2160 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 2161 | unsigned char *start, size_t len ) |
| 2162 | { |
| 2163 | unsigned char *end = start + len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2164 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2165 | |
| 2166 | /* Make a copy of the peer's raw public key. */ |
| 2167 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 2168 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 2169 | &ssl->handshake->peer_pubkey ); |
| 2170 | if( ret != 0 ) |
| 2171 | { |
| 2172 | /* We should have parsed the public key before. */ |
| 2173 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2174 | } |
| 2175 | |
| 2176 | return( 0 ); |
| 2177 | } |
| 2178 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2179 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2180 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 2181 | { |
| 2182 | int ret = 0; |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2183 | int crt_expected; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2184 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2185 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 2186 | ? ssl->handshake->sni_authmode |
| 2187 | : ssl->conf->authmode; |
| 2188 | #else |
Johan Pascal | 4f09926 | 2020-09-22 10:59:26 +0200 | [diff] [blame] | 2189 | const int authmode = ssl->conf->authmode; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2190 | #endif |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2191 | void *rs_ctx = NULL; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2192 | mbedtls_x509_crt *chain = NULL; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2193 | |
| 2194 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 2195 | |
Hanno Becker | 28f2fcd | 2019-02-07 10:11:07 +0000 | [diff] [blame] | 2196 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 2197 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2198 | { |
| 2199 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2200 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2201 | } |
| 2202 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2203 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2204 | if( ssl->handshake->ecrs_enabled && |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2205 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2206 | { |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2207 | chain = ssl->handshake->ecrs_peer_cert; |
| 2208 | ssl->handshake->ecrs_peer_cert = NULL; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2209 | goto crt_verify; |
| 2210 | } |
| 2211 | #endif |
| 2212 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 2213 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2214 | { |
| 2215 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 2216 | let it decide whether to alert. */ |
| 2217 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2218 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2219 | } |
| 2220 | |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2221 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2222 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 2223 | { |
| 2224 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2225 | |
irwir | d3085ab | 2020-04-21 22:26:05 +0300 | [diff] [blame] | 2226 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2227 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2228 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2229 | goto exit; |
Hanno Becker | 4a55f63 | 2019-02-05 12:49:06 +0000 | [diff] [blame] | 2230 | } |
| 2231 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 2232 | |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2233 | /* Clear existing peer CRT structure in case we tried to |
| 2234 | * reuse a session but it failed, and allocate a new one. */ |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2235 | ssl_clear_peer_cert( ssl->session_negotiate ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2236 | |
| 2237 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 2238 | if( chain == NULL ) |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2239 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2240 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2241 | sizeof( mbedtls_x509_crt ) ) ); |
| 2242 | mbedtls_ssl_send_alert_message( ssl, |
| 2243 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2244 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Hanno Becker | 7a955a0 | 2019-02-05 13:08:01 +0000 | [diff] [blame] | 2245 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2246 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 2247 | goto exit; |
| 2248 | } |
| 2249 | mbedtls_x509_crt_init( chain ); |
| 2250 | |
| 2251 | ret = ssl_parse_certificate_chain( ssl, chain ); |
Hanno Becker | c7bd780 | 2019-02-05 15:37:23 +0000 | [diff] [blame] | 2252 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2253 | goto exit; |
Manuel Pégourié-Gonnard | fed37ed | 2017-08-15 13:27:41 +0200 | [diff] [blame] | 2254 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2255 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2256 | if( ssl->handshake->ecrs_enabled) |
Manuel Pégourié-Gonnard | 0b23f16 | 2017-08-24 12:08:33 +0200 | [diff] [blame] | 2257 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
Manuel Pégourié-Gonnard | 3bf49c4 | 2017-08-15 13:47:06 +0200 | [diff] [blame] | 2258 | |
| 2259 | crt_verify: |
| 2260 | if( ssl->handshake->ecrs_enabled) |
| 2261 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 2262 | #endif |
| 2263 | |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2264 | ret = ssl_parse_certificate_verify( ssl, authmode, |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2265 | chain, rs_ctx ); |
Hanno Becker | 6863619 | 2019-02-05 14:36:34 +0000 | [diff] [blame] | 2266 | if( ret != 0 ) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2267 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2268 | |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2269 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2270 | { |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2271 | unsigned char *crt_start, *pk_start; |
| 2272 | size_t crt_len, pk_len; |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2273 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2274 | /* We parse the CRT chain without copying, so |
| 2275 | * these pointers point into the input buffer, |
| 2276 | * and are hence still valid after freeing the |
| 2277 | * CRT chain. */ |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2278 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2279 | crt_start = chain->raw.p; |
| 2280 | crt_len = chain->raw.len; |
Hanno Becker | 6bbd94c | 2019-02-05 17:02:28 +0000 | [diff] [blame] | 2281 | |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2282 | pk_start = chain->pk_raw.p; |
| 2283 | pk_len = chain->pk_raw.len; |
| 2284 | |
| 2285 | /* Free the CRT structures before computing |
| 2286 | * digest and copying the peer's public key. */ |
| 2287 | mbedtls_x509_crt_free( chain ); |
| 2288 | mbedtls_free( chain ); |
| 2289 | chain = NULL; |
| 2290 | |
| 2291 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2292 | if( ret != 0 ) |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2293 | goto exit; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2294 | |
| 2295 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 2296 | if( ret != 0 ) |
| 2297 | goto exit; |
Hanno Becker | a274753 | 2019-02-06 16:19:04 +0000 | [diff] [blame] | 2298 | } |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2299 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 2300 | /* Pass ownership to session structure. */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2301 | ssl->session_negotiate->peer_cert = chain; |
| 2302 | chain = NULL; |
Hanno Becker | 6b8fbab | 2019-02-08 14:59:05 +0000 | [diff] [blame] | 2303 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2306 | |
Hanno Becker | 6bdfab2 | 2019-02-05 13:11:17 +0000 | [diff] [blame] | 2307 | exit: |
| 2308 | |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2309 | if( ret == 0 ) |
| 2310 | ssl->state++; |
| 2311 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2312 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 2313 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 2314 | { |
| 2315 | ssl->handshake->ecrs_peer_cert = chain; |
| 2316 | chain = NULL; |
| 2317 | } |
| 2318 | #endif |
| 2319 | |
| 2320 | if( chain != NULL ) |
| 2321 | { |
| 2322 | mbedtls_x509_crt_free( chain ); |
| 2323 | mbedtls_free( chain ); |
| 2324 | } |
| 2325 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2326 | return( ret ); |
| 2327 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2328 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2330 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2331 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2332 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2333 | { |
| 2334 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2335 | const char *sender; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2336 | unsigned char padbuf[32]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2337 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2338 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2339 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2340 | psa_status_t status; |
| 2341 | #else |
| 2342 | mbedtls_sha256_context sha256; |
| 2343 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2345 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2346 | if( !session ) |
| 2347 | session = ssl->session; |
| 2348 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2349 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 2350 | ? "client finished" |
| 2351 | : "server finished"; |
| 2352 | |
| 2353 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2354 | sha256_psa = psa_hash_operation_init(); |
| 2355 | |
| 2356 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 2357 | |
| 2358 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 2359 | if( status != PSA_SUCCESS ) |
| 2360 | { |
| 2361 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2362 | return; |
| 2363 | } |
| 2364 | |
| 2365 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 2366 | if( status != PSA_SUCCESS ) |
| 2367 | { |
| 2368 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2369 | return; |
| 2370 | } |
| 2371 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 2372 | #else |
| 2373 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2374 | mbedtls_sha256_init( &sha256 ); |
| 2375 | |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2376 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2377 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2378 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2379 | |
| 2380 | /* |
| 2381 | * TLSv1.2: |
| 2382 | * hash = PRF( master, finished_label, |
| 2383 | * Hash( handshake ) )[0.11] |
| 2384 | */ |
| 2385 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2386 | #if !defined(MBEDTLS_SHA256_ALT) |
| 2387 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2388 | sha256.state, sizeof( sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 2389 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2390 | |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2391 | mbedtls_sha256_finish( &sha256, padbuf ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2392 | mbedtls_sha256_free( &sha256 ); |
| 2393 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2394 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2395 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2396 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2397 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2398 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2399 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2400 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2402 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2403 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2404 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2405 | |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2406 | #if defined(MBEDTLS_SHA384_C) |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2407 | |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2408 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2409 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2410 | { |
| 2411 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 2412 | const char *sender; |
Rodrigo Dias Correa | d596ca8 | 2020-11-25 00:42:28 -0300 | [diff] [blame] | 2413 | unsigned char padbuf[48]; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2414 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2415 | size_t hash_size; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 2416 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2417 | psa_status_t status; |
| 2418 | #else |
| 2419 | mbedtls_sha512_context sha512; |
| 2420 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2423 | if( !session ) |
| 2424 | session = ssl->session; |
| 2425 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2426 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 2427 | ? "client finished" |
| 2428 | : "server finished"; |
| 2429 | |
| 2430 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2431 | sha384_psa = psa_hash_operation_init(); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2432 | |
| 2433 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 2434 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2435 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2436 | if( status != PSA_SUCCESS ) |
| 2437 | { |
| 2438 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 2439 | return; |
| 2440 | } |
| 2441 | |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2442 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2443 | if( status != PSA_SUCCESS ) |
| 2444 | { |
| 2445 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 2446 | return; |
| 2447 | } |
| 2448 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 2449 | #else |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2450 | mbedtls_sha512_init( &sha512 ); |
| 2451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2452 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2453 | |
Manuel Pégourié-Gonnard | 001f2b6 | 2015-07-06 16:21:13 +0200 | [diff] [blame] | 2454 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2455 | |
| 2456 | /* |
| 2457 | * TLSv1.2: |
| 2458 | * hash = PRF( master, finished_label, |
| 2459 | * Hash( handshake ) )[0.11] |
| 2460 | */ |
| 2461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | #if !defined(MBEDTLS_SHA512_ALT) |
Manuel Pégourié-Gonnard | c0bf01e | 2015-07-06 16:11:18 +0200 | [diff] [blame] | 2463 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 2464 | sha512.state, sizeof( sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 2465 | #endif |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2466 | mbedtls_sha512_finish( &sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2467 | |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2468 | mbedtls_sha512_free( &sha512 ); |
| 2469 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2470 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 2471 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2472 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2474 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2475 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2476 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2477 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2478 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 2479 | } |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2480 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 2481 | |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 2482 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2483 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2484 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2485 | |
| 2486 | /* |
| 2487 | * Free our handshake params |
| 2488 | */ |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2489 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2490 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2491 | ssl->handshake = NULL; |
| 2492 | |
| 2493 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2494 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2495 | */ |
| 2496 | if( ssl->transform ) |
| 2497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2498 | mbedtls_ssl_transform_free( ssl->transform ); |
| 2499 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2500 | } |
| 2501 | ssl->transform = ssl->transform_negotiate; |
| 2502 | ssl->transform_negotiate = NULL; |
| 2503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2504 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2505 | } |
| 2506 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2507 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2508 | { |
| 2509 | int resume = ssl->handshake->resume; |
| 2510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2511 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2512 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2513 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2514 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2515 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2516 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2517 | ssl->renego_records_seen = 0; |
| 2518 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2519 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2520 | |
| 2521 | /* |
| 2522 | * Free the previous session and switch in the current one |
| 2523 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2524 | if( ssl->session ) |
| 2525 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2526 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 2527 | /* RFC 7366 3.1: keep the EtM state */ |
| 2528 | ssl->session_negotiate->encrypt_then_mac = |
| 2529 | ssl->session->encrypt_then_mac; |
| 2530 | #endif |
| 2531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2532 | mbedtls_ssl_session_free( ssl->session ); |
| 2533 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2534 | } |
| 2535 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2536 | ssl->session_negotiate = NULL; |
| 2537 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2538 | /* |
| 2539 | * Add cache entry |
| 2540 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2541 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 2542 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2543 | resume == 0 ) |
| 2544 | { |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 2545 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 2546 | ssl->session->id, |
| 2547 | ssl->session->id_len, |
| 2548 | ssl->session ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2549 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 2550 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2551 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2552 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2553 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2554 | ssl->handshake->flight != NULL ) |
| 2555 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2556 | /* Cancel handshake timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 2557 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2558 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2559 | /* Keep last flight around in case we need to resend it: |
| 2560 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2561 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2562 | } |
| 2563 | else |
| 2564 | #endif |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 2565 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 2566 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2567 | ssl->state++; |
| 2568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2569 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2572 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2573 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2574 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2575 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2576 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2577 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 2578 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 2579 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2580 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2581 | |
Manuel Pégourié-Gonnard | 214a848 | 2016-02-22 11:27:26 +0100 | [diff] [blame] | 2582 | /* |
| 2583 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 2584 | * may define some other value. Currently (early 2016), no defined |
| 2585 | * ciphersuite does this (and this is unlikely to change as activity has |
| 2586 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 2587 | */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2588 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2590 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2591 | ssl->verify_data_len = hash_len; |
| 2592 | 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] | 2593 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2594 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2595 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2596 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2597 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2598 | |
| 2599 | /* |
| 2600 | * In case of session resuming, invert the client and server |
| 2601 | * ChangeCipherSpec messages order. |
| 2602 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2603 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2604 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2605 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2606 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2607 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2608 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2609 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2610 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2611 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2612 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2613 | } |
| 2614 | else |
| 2615 | ssl->state++; |
| 2616 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2617 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2618 | * Switch to our negotiated transform and session parameters for outbound |
| 2619 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2620 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2621 | 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] | 2622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2623 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2624 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2625 | { |
| 2626 | unsigned char i; |
| 2627 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2628 | /* Remember current epoch settings for resending */ |
| 2629 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 2630 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
Jerry Yu | d96a5c2 | 2021-09-29 17:46:51 +0800 | [diff] [blame] | 2631 | sizeof( ssl->handshake->alt_out_ctr ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2632 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2633 | /* Set sequence_number to zero */ |
Jerry Yu | fd320e9 | 2021-10-08 21:52:41 +0800 | [diff] [blame] | 2634 | 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] | 2635 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2636 | |
| 2637 | /* Increment epoch */ |
| 2638 | for( i = 2; i > 0; i-- ) |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 2639 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2640 | break; |
| 2641 | |
| 2642 | /* The loop goes to its end iff the counter is wrapping */ |
| 2643 | if( i == 0 ) |
| 2644 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2645 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 2646 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2650 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Jerry Yu | fd320e9 | 2021-10-08 21:52:41 +0800 | [diff] [blame] | 2651 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2652 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2653 | ssl->transform_out = ssl->transform_negotiate; |
| 2654 | ssl->session_out = ssl->session_negotiate; |
| 2655 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2656 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2657 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2658 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2659 | #endif |
| 2660 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2661 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2662 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2663 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2664 | return( ret ); |
| 2665 | } |
| 2666 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2667 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2668 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2669 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2670 | { |
| 2671 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 2672 | return( ret ); |
| 2673 | } |
| 2674 | #endif |
| 2675 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2676 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2677 | |
| 2678 | return( 0 ); |
| 2679 | } |
| 2680 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2681 | #define SSL_MAX_HASH_LEN 12 |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2682 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2684 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2685 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a417431 | 2021-12-13 14:38:40 +0100 | [diff] [blame] | 2686 | unsigned int hash_len = 12; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 2687 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2689 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2690 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2691 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2692 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 2693 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2694 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2695 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2696 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2699 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2700 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2701 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2702 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2703 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2704 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 2705 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2708 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 2709 | { |
| 2710 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2711 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2712 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 2713 | goto exit; |
Hanno Becker | a0ca87e | 2021-06-24 10:27:37 +0100 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2717 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2718 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2719 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 2720 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2721 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 2722 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 2725 | 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] | 2726 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2727 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2728 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 2729 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
Dave Rodgman | 43fcb8d | 2021-06-28 21:49:15 +0100 | [diff] [blame] | 2730 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2731 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 2732 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2735 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2736 | ssl->verify_data_len = hash_len; |
| 2737 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2738 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2739 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 2740 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2741 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2742 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2743 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2744 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2745 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2746 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2747 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2748 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 2749 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2750 | } |
| 2751 | else |
| 2752 | ssl->state++; |
| 2753 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2754 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2755 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2756 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2757 | #endif |
| 2758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2759 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2760 | |
Gilles Peskine | f0fd4c3 | 2021-12-13 12:36:15 +0100 | [diff] [blame] | 2761 | exit: |
| 2762 | mbedtls_platform_zeroize( buf, hash_len ); |
| 2763 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 2766 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2767 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2768 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2769 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2770 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2772 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2773 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2774 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 2775 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 2776 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2777 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2778 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2779 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2780 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 2781 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2782 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 2783 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 2784 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2785 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2786 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 2787 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2788 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 2789 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2790 | |
| 2791 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2792 | |
| 2793 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2794 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 2795 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 2796 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2797 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2798 | #if defined(MBEDTLS_DHM_C) |
| 2799 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2800 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2801 | #if defined(MBEDTLS_ECDH_C) |
| 2802 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2803 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 2804 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 2805 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 2806 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2807 | handshake->ecjpake_cache = NULL; |
| 2808 | handshake->ecjpake_cache_len = 0; |
| 2809 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 2810 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2811 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2812 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 2813 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 2814 | #endif |
| 2815 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 2816 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 2817 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 2818 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 2819 | |
| 2820 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 2821 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 2822 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 2823 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2824 | } |
| 2825 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2826 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2827 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2828 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 2829 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 2830 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2831 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 2832 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 2833 | #else |
| 2834 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 2835 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 2836 | #endif |
| 2837 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 2838 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2839 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 2840 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 2841 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2842 | } |
| 2843 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2844 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2845 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2846 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2849 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2850 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2851 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2852 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2853 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2854 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2855 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2856 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2857 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2858 | |
| 2859 | /* |
| 2860 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 2861 | * Now allocate missing structures. |
| 2862 | */ |
| 2863 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2864 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2865 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2866 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2867 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2868 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2869 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2870 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2871 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2872 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 2873 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2874 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2875 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2876 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2877 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2878 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 2879 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 2880 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 2881 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2882 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2883 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2884 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2885 | if( ssl->handshake == NULL || |
| 2886 | ssl->transform_negotiate == NULL || |
| 2887 | ssl->session_negotiate == NULL ) |
| 2888 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2889 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2890 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2891 | mbedtls_free( ssl->handshake ); |
| 2892 | mbedtls_free( ssl->transform_negotiate ); |
| 2893 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2894 | |
| 2895 | ssl->handshake = NULL; |
| 2896 | ssl->transform_negotiate = NULL; |
| 2897 | ssl->session_negotiate = NULL; |
| 2898 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2899 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2902 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2903 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2904 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 2905 | ssl_handshake_params_init( ssl->handshake ); |
| 2906 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2907 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2908 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 2909 | { |
| 2910 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2911 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2912 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 2913 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 2914 | else |
| 2915 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 2916 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 2917 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 2918 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2919 | #endif |
| 2920 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2921 | /* |
| 2922 | * curve_list is translated to IANA TLS group identifiers here because |
| 2923 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 2924 | * any error codes. |
| 2925 | */ |
| 2926 | #if defined(MBEDTLS_ECP_C) |
| 2927 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2928 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 2929 | if ( ssl->conf->curve_list != NULL ) |
| 2930 | { |
| 2931 | size_t length; |
| 2932 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 2933 | |
| 2934 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 2935 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 2936 | |
| 2937 | /* Leave room for zero termination */ |
| 2938 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 2939 | if ( group_list == NULL ) |
| 2940 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 2941 | |
| 2942 | for( size_t i = 0; i < length; i++ ) |
| 2943 | { |
| 2944 | const mbedtls_ecp_curve_info *info = |
| 2945 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 2946 | if ( info == NULL ) |
| 2947 | { |
| 2948 | mbedtls_free( group_list ); |
| 2949 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 2950 | } |
| 2951 | group_list[i] = info->tls_id; |
| 2952 | } |
| 2953 | |
| 2954 | group_list[length] = 0; |
| 2955 | |
| 2956 | ssl->handshake->group_list = group_list; |
| 2957 | ssl->handshake->group_list_heap_allocated = 1; |
| 2958 | } |
| 2959 | else |
| 2960 | { |
| 2961 | ssl->handshake->group_list = ssl->conf->group_list; |
| 2962 | ssl->handshake->group_list_heap_allocated = 0; |
| 2963 | } |
| 2964 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 2965 | #endif /* MBEDTLS_ECP_C */ |
| 2966 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2967 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 2968 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 2969 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 2970 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 2971 | signature algorithms IANA identifiers. */ |
| 2972 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2973 | ssl->conf->sig_hashes != NULL ) |
| 2974 | { |
| 2975 | const int *md; |
| 2976 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 2977 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2978 | uint16_t *p; |
| 2979 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 2980 | #if defined(static_assert) |
| 2981 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 2982 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 2983 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 2984 | #endif |
| 2985 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2986 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 2987 | { |
| 2988 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 2989 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 2990 | #if defined(MBEDTLS_ECDSA_C) |
| 2991 | sig_algs_len += sizeof( uint16_t ); |
| 2992 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 2993 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 2994 | #if defined(MBEDTLS_RSA_C) |
| 2995 | sig_algs_len += sizeof( uint16_t ); |
| 2996 | #endif |
| 2997 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 2998 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2999 | } |
| 3000 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3001 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3002 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3003 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 3004 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 3005 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3006 | if( ssl->handshake->sig_algs == NULL ) |
| 3007 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3008 | |
| 3009 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 3010 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 3011 | { |
| 3012 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 3013 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 3014 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3015 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3016 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 3017 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3018 | #endif |
| 3019 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3020 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 3021 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3022 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3023 | } |
| 3024 | *p = MBEDTLS_TLS1_3_SIG_NONE; |
| 3025 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 3026 | } |
| 3027 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 3028 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3029 | { |
| 3030 | ssl->handshake->sig_algs = ssl->conf->sig_algs; |
| 3031 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 3032 | } |
| 3033 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 3034 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3035 | return( 0 ); |
| 3036 | } |
| 3037 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3038 | #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] | 3039 | /* Dummy cookie callbacks for defaults */ |
| 3040 | static int ssl_cookie_write_dummy( void *ctx, |
| 3041 | unsigned char **p, unsigned char *end, |
| 3042 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3043 | { |
| 3044 | ((void) ctx); |
| 3045 | ((void) p); |
| 3046 | ((void) end); |
| 3047 | ((void) cli_id); |
| 3048 | ((void) cli_id_len); |
| 3049 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3050 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | static int ssl_cookie_check_dummy( void *ctx, |
| 3054 | const unsigned char *cookie, size_t cookie_len, |
| 3055 | const unsigned char *cli_id, size_t cli_id_len ) |
| 3056 | { |
| 3057 | ((void) ctx); |
| 3058 | ((void) cookie); |
| 3059 | ((void) cookie_len); |
| 3060 | ((void) cli_id); |
| 3061 | ((void) cli_id_len); |
| 3062 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3063 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3064 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3065 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 3066 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3067 | /* |
| 3068 | * Initialize an SSL context |
| 3069 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3070 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 3071 | { |
| 3072 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 3073 | } |
| 3074 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3075 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 3076 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3077 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3078 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
| 3079 | { |
| 3080 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3081 | { |
| 3082 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) ); |
| 3083 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3084 | } |
| 3085 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 3086 | return( 0 ); |
| 3087 | } |
| 3088 | #endif |
| 3089 | |
| 3090 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3091 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 3092 | { |
| 3093 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 3094 | return( 0 ); |
| 3095 | } |
| 3096 | #endif |
| 3097 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3098 | #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] | 3099 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) ) |
| 3100 | { |
| 3101 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) ); |
| 3102 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3103 | } |
| 3104 | #endif |
| 3105 | |
| 3106 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 3107 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 3108 | } |
| 3109 | |
| 3110 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 3111 | { |
| 3112 | int ret; |
| 3113 | ret = ssl_conf_version_check( ssl ); |
| 3114 | if( ret != 0 ) |
| 3115 | return( ret ); |
| 3116 | |
| 3117 | /* Space for further checks */ |
| 3118 | |
| 3119 | return( 0 ); |
| 3120 | } |
| 3121 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3122 | /* |
| 3123 | * Setup an SSL context |
| 3124 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 3125 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3126 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 3127 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3128 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3129 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3130 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3131 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3132 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3133 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3134 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 3135 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 3136 | return( ret ); |
| 3137 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3138 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 3139 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 3140 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 3141 | |
| 3142 | /* Set to NULL in case of an error condition */ |
| 3143 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3144 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3145 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3146 | ssl->in_buf_len = in_buf_len; |
| 3147 | #endif |
| 3148 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3149 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3150 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3151 | 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] | 3152 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3153 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3154 | } |
| 3155 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3156 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3157 | ssl->out_buf_len = out_buf_len; |
| 3158 | #endif |
| 3159 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3160 | if( ssl->out_buf == NULL ) |
| 3161 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3162 | 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] | 3163 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3164 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3167 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3168 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3169 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 3170 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 3171 | #endif |
| 3172 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3173 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3174 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3175 | |
| 3176 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3177 | |
| 3178 | error: |
| 3179 | mbedtls_free( ssl->in_buf ); |
| 3180 | mbedtls_free( ssl->out_buf ); |
| 3181 | |
| 3182 | ssl->conf = NULL; |
| 3183 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3184 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3185 | ssl->in_buf_len = 0; |
| 3186 | ssl->out_buf_len = 0; |
| 3187 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 3188 | ssl->in_buf = NULL; |
| 3189 | ssl->out_buf = NULL; |
| 3190 | |
| 3191 | ssl->in_hdr = NULL; |
| 3192 | ssl->in_ctr = NULL; |
| 3193 | ssl->in_len = NULL; |
| 3194 | ssl->in_iv = NULL; |
| 3195 | ssl->in_msg = NULL; |
| 3196 | |
| 3197 | ssl->out_hdr = NULL; |
| 3198 | ssl->out_ctr = NULL; |
| 3199 | ssl->out_len = NULL; |
| 3200 | ssl->out_iv = NULL; |
| 3201 | ssl->out_msg = NULL; |
| 3202 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 3203 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3207 | * Reset an initialized and used SSL context for re-use while retaining |
| 3208 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3209 | * |
| 3210 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 3211 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3212 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 3213 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 3214 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3215 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3216 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3217 | size_t in_buf_len = ssl->in_buf_len; |
| 3218 | size_t out_buf_len = ssl->out_buf_len; |
| 3219 | #else |
| 3220 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3221 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3222 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3223 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3224 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 3225 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 3226 | #endif |
| 3227 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3228 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3229 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3230 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3231 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 3232 | |
| 3233 | /* Reset incoming message parsing */ |
| 3234 | ssl->in_offt = NULL; |
| 3235 | ssl->nb_zero = 0; |
| 3236 | ssl->in_msgtype = 0; |
| 3237 | ssl->in_msglen = 0; |
| 3238 | ssl->in_hslen = 0; |
| 3239 | ssl->keep_current_message = 0; |
| 3240 | ssl->transform_in = NULL; |
| 3241 | |
| 3242 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3243 | ssl->next_record_offset = 0; |
| 3244 | ssl->in_epoch = 0; |
| 3245 | #endif |
| 3246 | |
| 3247 | /* Keep current datagram if partial == 1 */ |
| 3248 | if( partial == 0 ) |
| 3249 | { |
| 3250 | ssl->in_left = 0; |
| 3251 | memset( ssl->in_buf, 0, in_buf_len ); |
| 3252 | } |
| 3253 | |
| 3254 | /* Reset outgoing message writing */ |
| 3255 | ssl->out_msgtype = 0; |
| 3256 | ssl->out_msglen = 0; |
| 3257 | ssl->out_left = 0; |
| 3258 | memset( ssl->out_buf, 0, out_buf_len ); |
| 3259 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 3260 | ssl->transform_out = NULL; |
| 3261 | |
| 3262 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3263 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 3264 | #endif |
| 3265 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3266 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3267 | if( ssl->transform ) |
| 3268 | { |
| 3269 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3270 | mbedtls_free( ssl->transform ); |
| 3271 | ssl->transform = NULL; |
| 3272 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3273 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3274 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3275 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 3276 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 3277 | mbedtls_free( ssl->transform_application ); |
| 3278 | ssl->transform_application = NULL; |
| 3279 | |
| 3280 | if( ssl->handshake != NULL ) |
| 3281 | { |
| 3282 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 3283 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 3284 | ssl->handshake->transform_earlydata = NULL; |
| 3285 | |
| 3286 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 3287 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 3288 | ssl->handshake->transform_handshake = NULL; |
| 3289 | } |
| 3290 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 3291 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 3295 | { |
| 3296 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 3297 | |
| 3298 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 3299 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 3300 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3301 | |
| 3302 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3303 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3304 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 3305 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3306 | |
| 3307 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3308 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 3309 | 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] | 3310 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3311 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3312 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 3313 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 3314 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3315 | if( ssl->session ) |
| 3316 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3317 | mbedtls_ssl_session_free( ssl->session ); |
| 3318 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3319 | ssl->session = NULL; |
| 3320 | } |
| 3321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3322 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 3323 | ssl->alpn_chosen = NULL; |
| 3324 | #endif |
| 3325 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3326 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3327 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3328 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 3329 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3330 | { |
| 3331 | mbedtls_free( ssl->cli_id ); |
| 3332 | ssl->cli_id = NULL; |
| 3333 | ssl->cli_id_len = 0; |
| 3334 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3335 | #endif |
| 3336 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3337 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 3338 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3339 | |
| 3340 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 3343 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3344 | * Reset an initialized and used SSL context for re-use while retaining |
| 3345 | * all application-set variables, function pointers and data. |
| 3346 | */ |
| 3347 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 3348 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3349 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3353 | * SSL set accessors |
| 3354 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3355 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3356 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3357 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 3360 | 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] | 3361 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3362 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3365 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3366 | 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] | 3367 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3368 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3369 | } |
| 3370 | #endif |
| 3371 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3372 | 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] | 3373 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3374 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3375 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3377 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3378 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 3379 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 3380 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 3381 | { |
| 3382 | ssl->disable_datagram_packing = !allow_packing; |
| 3383 | } |
| 3384 | |
| 3385 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 3386 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3387 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3388 | conf->hs_timeout_min = min; |
| 3389 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 3390 | } |
| 3391 | #endif |
| 3392 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3393 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3394 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3395 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3398 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3399 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 3400 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3401 | void *p_vrfy ) |
| 3402 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3403 | conf->f_vrfy = f_vrfy; |
| 3404 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3405 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3406 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 3407 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3408 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 3409 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3410 | void *p_rng ) |
| 3411 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 3412 | conf->f_rng = f_rng; |
| 3413 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3416 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 3417 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3418 | void *p_dbg ) |
| 3419 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3420 | conf->f_dbg = f_dbg; |
| 3421 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3424 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3425 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 3426 | mbedtls_ssl_send_t *f_send, |
| 3427 | mbedtls_ssl_recv_t *f_recv, |
| 3428 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3429 | { |
| 3430 | ssl->p_bio = p_bio; |
| 3431 | ssl->f_send = f_send; |
| 3432 | ssl->f_recv = f_recv; |
| 3433 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 3434 | } |
| 3435 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 3436 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3437 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 3438 | { |
| 3439 | ssl->mtu = mtu; |
| 3440 | } |
| 3441 | #endif |
| 3442 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3443 | 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] | 3444 | { |
| 3445 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 3446 | } |
| 3447 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3448 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 3449 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 3450 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 3451 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3452 | { |
| 3453 | ssl->p_timer = p_timer; |
| 3454 | ssl->f_set_timer = f_set_timer; |
| 3455 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 3456 | |
| 3457 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 3458 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 3459 | } |
| 3460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3461 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3462 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 3463 | void *p_cache, |
| 3464 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 3465 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3466 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 3467 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3468 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 3469 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3470 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3471 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3473 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3474 | 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] | 3475 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3476 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3477 | |
| 3478 | if( ssl == NULL || |
| 3479 | session == NULL || |
| 3480 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3481 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3482 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3483 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3484 | } |
| 3485 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 3486 | if( ssl->handshake->resume == 1 ) |
| 3487 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 3488 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 3489 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 3490 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3491 | return( ret ); |
| 3492 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3493 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 3494 | |
| 3495 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3496 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3497 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3498 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 3499 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 3500 | const int *ciphersuites ) |
| 3501 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 3502 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3505 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 3506 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3507 | const int kex_modes ) |
| 3508 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3509 | 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] | 3510 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3511 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 3512 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3513 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3514 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 3515 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 3516 | { |
| 3517 | conf->cert_profile = profile; |
| 3518 | } |
| 3519 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3520 | /* Append a new keycert entry to a (possibly empty) list */ |
| 3521 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 3522 | mbedtls_x509_crt *cert, |
| 3523 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3524 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3525 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3526 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3527 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 3528 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3529 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3530 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3531 | new_cert->cert = cert; |
| 3532 | new_cert->key = key; |
| 3533 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3534 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3535 | /* Update head is the list was null, else add to the end */ |
| 3536 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 3537 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3538 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 3539 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3540 | else |
| 3541 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3542 | mbedtls_ssl_key_cert *cur = *head; |
| 3543 | while( cur->next != NULL ) |
| 3544 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 3545 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 3546 | } |
| 3547 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3548 | return( 0 ); |
| 3549 | } |
| 3550 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3551 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 3552 | mbedtls_x509_crt *own_cert, |
| 3553 | mbedtls_pk_context *pk_key ) |
| 3554 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 3555 | 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] | 3556 | } |
| 3557 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 3558 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3559 | mbedtls_x509_crt *ca_chain, |
| 3560 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3561 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 3562 | conf->ca_chain = ca_chain; |
| 3563 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3564 | |
| 3565 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3566 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3567 | * cannot be used together. */ |
| 3568 | conf->f_ca_cb = NULL; |
| 3569 | conf->p_ca_cb = NULL; |
| 3570 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3571 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3572 | |
| 3573 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3574 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 3575 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 3576 | void *p_ca_cb ) |
| 3577 | { |
| 3578 | conf->f_ca_cb = f_ca_cb; |
| 3579 | conf->p_ca_cb = p_ca_cb; |
| 3580 | |
| 3581 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 3582 | * cannot be used together. */ |
| 3583 | conf->ca_chain = NULL; |
| 3584 | conf->ca_crl = NULL; |
| 3585 | } |
| 3586 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3587 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 3588 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3589 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3590 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 3591 | mbedtls_x509_crt *own_cert, |
| 3592 | mbedtls_pk_context *pk_key ) |
| 3593 | { |
| 3594 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 3595 | own_cert, pk_key ) ); |
| 3596 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 3597 | |
| 3598 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 3599 | mbedtls_x509_crt *ca_chain, |
| 3600 | mbedtls_x509_crl *ca_crl ) |
| 3601 | { |
| 3602 | ssl->handshake->sni_ca_chain = ca_chain; |
| 3603 | ssl->handshake->sni_ca_crl = ca_crl; |
| 3604 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3605 | |
| 3606 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 3607 | int authmode ) |
| 3608 | { |
| 3609 | ssl->handshake->sni_authmode = authmode; |
| 3610 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 3611 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 3612 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 3613 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 3614 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 3615 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3616 | void *p_vrfy ) |
| 3617 | { |
| 3618 | ssl->f_vrfy = f_vrfy; |
| 3619 | ssl->p_vrfy = p_vrfy; |
| 3620 | } |
| 3621 | #endif |
| 3622 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3623 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3624 | /* |
| 3625 | * Set EC J-PAKE password for current handshake |
| 3626 | */ |
| 3627 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 3628 | const unsigned char *pw, |
| 3629 | size_t pw_len ) |
| 3630 | { |
| 3631 | mbedtls_ecjpake_role role; |
| 3632 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 3633 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3634 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3635 | |
| 3636 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 3637 | role = MBEDTLS_ECJPAKE_SERVER; |
| 3638 | else |
| 3639 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 3640 | |
| 3641 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 3642 | role, |
| 3643 | MBEDTLS_MD_SHA256, |
| 3644 | MBEDTLS_ECP_DP_SECP256R1, |
| 3645 | pw, pw_len ) ); |
| 3646 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3647 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 3648 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3649 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3650 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3651 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 3652 | { |
| 3653 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3654 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 3655 | return( 1 ); |
| 3656 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3657 | |
| 3658 | if( conf->psk != NULL ) |
| 3659 | return( 1 ); |
| 3660 | |
| 3661 | return( 0 ); |
| 3662 | } |
| 3663 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3664 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 3665 | { |
| 3666 | /* Remove reference to existing PSK, if any. */ |
| 3667 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3668 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3669 | { |
| 3670 | /* The maintenance of the PSK key slot is the |
| 3671 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3672 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3673 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 3674 | /* This and the following branch should never |
| 3675 | * be taken simultaenously as we maintain the |
| 3676 | * invariant that raw and opaque PSKs are never |
| 3677 | * configured simultaneously. As a safeguard, |
| 3678 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3679 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3680 | if( conf->psk != NULL ) |
| 3681 | { |
| 3682 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 3683 | |
| 3684 | mbedtls_free( conf->psk ); |
| 3685 | conf->psk = NULL; |
| 3686 | conf->psk_len = 0; |
| 3687 | } |
| 3688 | |
| 3689 | /* Remove reference to PSK identity, if any. */ |
| 3690 | if( conf->psk_identity != NULL ) |
| 3691 | { |
| 3692 | mbedtls_free( conf->psk_identity ); |
| 3693 | conf->psk_identity = NULL; |
| 3694 | conf->psk_identity_len = 0; |
| 3695 | } |
| 3696 | } |
| 3697 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3698 | /* This function assumes that PSK identity in the SSL config is unset. |
| 3699 | * It checks that the provided identity is well-formed and attempts |
| 3700 | * to make a copy of it in the SSL config. |
| 3701 | * On failure, the PSK identity in the config remains unset. */ |
| 3702 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 3703 | unsigned char const *psk_identity, |
| 3704 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3705 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3706 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3707 | if( psk_identity == NULL || |
| 3708 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3709 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 3710 | { |
| 3711 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3712 | } |
| 3713 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3714 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 3715 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3716 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3717 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3718 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 3719 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 3720 | |
| 3721 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 3722 | } |
| 3723 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3724 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 3725 | const unsigned char *psk, size_t psk_len, |
| 3726 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 3727 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3728 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3729 | |
| 3730 | /* We currently only support one PSK, raw or opaque. */ |
| 3731 | if( ssl_conf_psk_is_configured( conf ) ) |
| 3732 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3733 | |
| 3734 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3735 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3736 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 3737 | if( psk_len == 0 ) |
| 3738 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3739 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 3740 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3741 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3742 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 3743 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 3744 | conf->psk_len = psk_len; |
| 3745 | memcpy( conf->psk, psk, conf->psk_len ); |
| 3746 | |
| 3747 | /* Check and set PSK Identity */ |
| 3748 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 3749 | if( ret != 0 ) |
| 3750 | ssl_conf_remove_psk( conf ); |
| 3751 | |
| 3752 | return( ret ); |
| 3753 | } |
| 3754 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3755 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 3756 | { |
| 3757 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3758 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3759 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3760 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3761 | } |
| 3762 | else |
| 3763 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3764 | if( ssl->handshake->psk != NULL ) |
| 3765 | { |
| 3766 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 3767 | ssl->handshake->psk_len ); |
| 3768 | mbedtls_free( ssl->handshake->psk ); |
| 3769 | ssl->handshake->psk_len = 0; |
| 3770 | } |
| 3771 | } |
| 3772 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3773 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 3774 | const unsigned char *psk, size_t psk_len ) |
| 3775 | { |
| 3776 | if( psk == NULL || ssl->handshake == NULL ) |
| 3777 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3778 | |
| 3779 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 3780 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3781 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3782 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3783 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 3784 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 3785 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3786 | |
| 3787 | ssl->handshake->psk_len = psk_len; |
| 3788 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 3789 | |
| 3790 | return( 0 ); |
| 3791 | } |
| 3792 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3793 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3794 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 3795 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3796 | const unsigned char *psk_identity, |
| 3797 | size_t psk_identity_len ) |
| 3798 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3799 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 3800 | |
| 3801 | /* We currently only support one PSK, raw or opaque. */ |
| 3802 | if( ssl_conf_psk_is_configured( conf ) ) |
| 3803 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3804 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3805 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3806 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3807 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 3808 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 3809 | |
| 3810 | /* Check and set PSK Identity */ |
| 3811 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 3812 | psk_identity_len ); |
| 3813 | if( ret != 0 ) |
| 3814 | ssl_conf_remove_psk( conf ); |
| 3815 | |
| 3816 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3817 | } |
| 3818 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 3819 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 3820 | mbedtls_svc_key_id_t psk ) |
| 3821 | { |
| 3822 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 3823 | ( ssl->handshake == NULL ) ) |
| 3824 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3825 | |
| 3826 | ssl_remove_psk( ssl ); |
| 3827 | ssl->handshake->psk_opaque = psk; |
| 3828 | return( 0 ); |
| 3829 | } |
| 3830 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3831 | |
| 3832 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 3833 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 3834 | size_t), |
| 3835 | void *p_psk ) |
| 3836 | { |
| 3837 | conf->f_psk = f_psk; |
| 3838 | conf->p_psk = p_psk; |
| 3839 | } |
| 3840 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 3841 | |
| 3842 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 3843 | 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] | 3844 | size_t taglen, |
| 3845 | psa_algorithm_t *alg, |
| 3846 | psa_key_type_t *key_type, |
| 3847 | size_t *key_size ) |
| 3848 | { |
| 3849 | switch ( mbedtls_cipher_type ) |
| 3850 | { |
| 3851 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 3852 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 3853 | *key_type = PSA_KEY_TYPE_AES; |
| 3854 | *key_size = 128; |
| 3855 | break; |
| 3856 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 3857 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3858 | *key_type = PSA_KEY_TYPE_AES; |
| 3859 | *key_size = 128; |
| 3860 | break; |
| 3861 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 3862 | *alg = PSA_ALG_GCM; |
| 3863 | *key_type = PSA_KEY_TYPE_AES; |
| 3864 | *key_size = 128; |
| 3865 | break; |
| 3866 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 3867 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3868 | *key_type = PSA_KEY_TYPE_AES; |
| 3869 | *key_size = 192; |
| 3870 | break; |
| 3871 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 3872 | *alg = PSA_ALG_GCM; |
| 3873 | *key_type = PSA_KEY_TYPE_AES; |
| 3874 | *key_size = 192; |
| 3875 | break; |
| 3876 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 3877 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 3878 | *key_type = PSA_KEY_TYPE_AES; |
| 3879 | *key_size = 256; |
| 3880 | break; |
| 3881 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 3882 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3883 | *key_type = PSA_KEY_TYPE_AES; |
| 3884 | *key_size = 256; |
| 3885 | break; |
| 3886 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 3887 | *alg = PSA_ALG_GCM; |
| 3888 | *key_type = PSA_KEY_TYPE_AES; |
| 3889 | *key_size = 256; |
| 3890 | break; |
| 3891 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 3892 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 3893 | *key_type = PSA_KEY_TYPE_ARIA; |
| 3894 | *key_size = 128; |
| 3895 | break; |
| 3896 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 3897 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3898 | *key_type = PSA_KEY_TYPE_ARIA; |
| 3899 | *key_size = 128; |
| 3900 | break; |
| 3901 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 3902 | *alg = PSA_ALG_GCM; |
| 3903 | *key_type = PSA_KEY_TYPE_ARIA; |
| 3904 | *key_size = 128; |
| 3905 | break; |
| 3906 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 3907 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3908 | *key_type = PSA_KEY_TYPE_ARIA; |
| 3909 | *key_size = 192; |
| 3910 | break; |
| 3911 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 3912 | *alg = PSA_ALG_GCM; |
| 3913 | *key_type = PSA_KEY_TYPE_ARIA; |
| 3914 | *key_size = 192; |
| 3915 | break; |
| 3916 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 3917 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 3918 | *key_type = PSA_KEY_TYPE_ARIA; |
| 3919 | *key_size = 256; |
| 3920 | break; |
| 3921 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 3922 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3923 | *key_type = PSA_KEY_TYPE_ARIA; |
| 3924 | *key_size = 256; |
| 3925 | break; |
| 3926 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 3927 | *alg = PSA_ALG_GCM; |
| 3928 | *key_type = PSA_KEY_TYPE_ARIA; |
| 3929 | *key_size = 256; |
| 3930 | break; |
| 3931 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 3932 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 3933 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 3934 | *key_size = 128; |
| 3935 | break; |
| 3936 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 3937 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3938 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 3939 | *key_size = 128; |
| 3940 | break; |
| 3941 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 3942 | *alg = PSA_ALG_GCM; |
| 3943 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 3944 | *key_size = 128; |
| 3945 | break; |
| 3946 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 3947 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3948 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 3949 | *key_size = 192; |
| 3950 | break; |
| 3951 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 3952 | *alg = PSA_ALG_GCM; |
| 3953 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 3954 | *key_size = 192; |
| 3955 | break; |
| 3956 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 3957 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 3958 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 3959 | *key_size = 256; |
| 3960 | break; |
| 3961 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 3962 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 3963 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 3964 | *key_size = 256; |
| 3965 | break; |
| 3966 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 3967 | *alg = PSA_ALG_GCM; |
| 3968 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 3969 | *key_size = 256; |
| 3970 | break; |
| 3971 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 3972 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 3973 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 3974 | *key_size = 256; |
| 3975 | break; |
| 3976 | case MBEDTLS_CIPHER_NULL: |
| 3977 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 3978 | *key_type = 0; |
| 3979 | *key_size = 0; |
| 3980 | break; |
| 3981 | default: |
| 3982 | return PSA_ERROR_NOT_SUPPORTED; |
| 3983 | } |
| 3984 | |
| 3985 | return PSA_SUCCESS; |
| 3986 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 3987 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3988 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 3989 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3990 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 3991 | const unsigned char *dhm_P, size_t P_len, |
| 3992 | const unsigned char *dhm_G, size_t G_len ) |
| 3993 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3994 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3995 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 3996 | mbedtls_mpi_free( &conf->dhm_P ); |
| 3997 | mbedtls_mpi_free( &conf->dhm_G ); |
| 3998 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 3999 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 4000 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 4001 | { |
| 4002 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4003 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4004 | return( ret ); |
| 4005 | } |
| 4006 | |
| 4007 | return( 0 ); |
| 4008 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4009 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4010 | 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] | 4011 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4012 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4013 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 4014 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4015 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4016 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 4017 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 4018 | &conf->dhm_P ) ) != 0 || |
| 4019 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 4020 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4021 | { |
| 4022 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4023 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4024 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 4025 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4026 | |
| 4027 | return( 0 ); |
| 4028 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 4029 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 4030 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4031 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4032 | /* |
| 4033 | * Set the minimum length for Diffie-Hellman parameters |
| 4034 | */ |
| 4035 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 4036 | unsigned int bitlen ) |
| 4037 | { |
| 4038 | conf->dhm_min_bitlen = bitlen; |
| 4039 | } |
| 4040 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 4041 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4042 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4043 | #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] | 4044 | /* |
| 4045 | * Set allowed/preferred hashes for handshake signatures |
| 4046 | */ |
| 4047 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 4048 | const int *hashes ) |
| 4049 | { |
| 4050 | conf->sig_hashes = hashes; |
| 4051 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4052 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4053 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4054 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4055 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 4056 | const uint16_t* sig_algs ) |
| 4057 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4058 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4059 | conf->sig_hashes = NULL; |
| 4060 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 4061 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 4062 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4063 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 4064 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4065 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4066 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4067 | /* |
| 4068 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4069 | * |
| 4070 | * mbedtls_ssl_setup() takes the provided list |
| 4071 | * and translates it to a list of IANA TLS group identifiers, |
| 4072 | * stored in ssl->handshake->group_list. |
| 4073 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4074 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4075 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4076 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4077 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4078 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4079 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4080 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4081 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4082 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 4083 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4084 | /* |
| 4085 | * Set the allowed groups |
| 4086 | */ |
| 4087 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 4088 | const uint16_t *group_list ) |
| 4089 | { |
| 4090 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4091 | conf->curve_list = NULL; |
| 4092 | #endif |
| 4093 | conf->group_list = group_list; |
| 4094 | } |
| 4095 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4096 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4097 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4098 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4099 | /* Initialize to suppress unnecessary compiler warning */ |
| 4100 | size_t hostname_len = 0; |
| 4101 | |
| 4102 | /* Check if new hostname is valid before |
| 4103 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4104 | if( hostname != NULL ) |
| 4105 | { |
| 4106 | hostname_len = strlen( hostname ); |
| 4107 | |
| 4108 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 4109 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4110 | } |
| 4111 | |
| 4112 | /* Now it's clear that we will overwrite the old hostname, |
| 4113 | * so we can free it safely */ |
| 4114 | |
| 4115 | if( ssl->hostname != NULL ) |
| 4116 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4117 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4118 | mbedtls_free( ssl->hostname ); |
| 4119 | } |
| 4120 | |
| 4121 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 4122 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4123 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4124 | { |
| 4125 | ssl->hostname = NULL; |
| 4126 | } |
| 4127 | else |
| 4128 | { |
| 4129 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4130 | if( ssl->hostname == NULL ) |
| 4131 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4132 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4133 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 4134 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 4135 | ssl->hostname[hostname_len] = '\0'; |
| 4136 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4137 | |
| 4138 | return( 0 ); |
| 4139 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 4140 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4141 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 4142 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4143 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4144 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4145 | const unsigned char *, size_t), |
| 4146 | void *p_sni ) |
| 4147 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4148 | conf->f_sni = f_sni; |
| 4149 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4150 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4151 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 4152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4153 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4154 | 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] | 4155 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4156 | size_t cur_len, tot_len; |
| 4157 | const char **p; |
| 4158 | |
| 4159 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 4160 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 4161 | * MUST NOT be truncated." |
| 4162 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4163 | */ |
| 4164 | tot_len = 0; |
| 4165 | for( p = protos; *p != NULL; p++ ) |
| 4166 | { |
| 4167 | cur_len = strlen( *p ); |
| 4168 | tot_len += cur_len; |
| 4169 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 4170 | if( ( cur_len == 0 ) || |
| 4171 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 4172 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4173 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4174 | } |
| 4175 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4176 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 4177 | |
| 4178 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4181 | 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] | 4182 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4183 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4184 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4185 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 4186 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4187 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4188 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 4189 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4190 | { |
| 4191 | conf->dtls_srtp_mki_support = support_mki_value; |
| 4192 | } |
| 4193 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4194 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 4195 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 4196 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4197 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4198 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4199 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4200 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4201 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4202 | |
| 4203 | 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] | 4204 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4205 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4206 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4207 | |
| 4208 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 4209 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 4210 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 4211 | } |
| 4212 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 4213 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4214 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4215 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4216 | const mbedtls_ssl_srtp_profile *p; |
| 4217 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4218 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4219 | /* check the profiles list: all entry must be valid, |
| 4220 | * 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] | 4221 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 4222 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 4223 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4224 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4225 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 4226 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 4227 | list_size++; |
| 4228 | } |
| 4229 | else |
| 4230 | { |
| 4231 | /* unsupported value, stop parsing and set the size to an error value */ |
| 4232 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4233 | } |
| 4234 | } |
| 4235 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4236 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 4237 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4238 | conf->dtls_srtp_profile_list = NULL; |
| 4239 | conf->dtls_srtp_profile_list_len = 0; |
| 4240 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4241 | } |
| 4242 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 4243 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 4244 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4245 | |
| 4246 | return( 0 ); |
| 4247 | } |
| 4248 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4249 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 4250 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4251 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4252 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 4253 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4254 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4255 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4256 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 4257 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4258 | else |
| 4259 | { |
| 4260 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 4261 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 4262 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 4263 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4264 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 4265 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 4266 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4267 | 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] | 4268 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4269 | conf->max_major_ver = major; |
| 4270 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 4273 | 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] | 4274 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4275 | conf->min_major_ver = major; |
| 4276 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4279 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4280 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 4281 | char cert_req_ca_list ) |
| 4282 | { |
| 4283 | conf->cert_req_ca_list = cert_req_ca_list; |
| 4284 | } |
| 4285 | #endif |
| 4286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4287 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4288 | 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] | 4289 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4290 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 4291 | } |
| 4292 | #endif |
| 4293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4294 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4295 | 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] | 4296 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4297 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 4298 | } |
| 4299 | #endif |
| 4300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4301 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4302 | 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] | 4303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4304 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4305 | 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] | 4306 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4307 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4308 | } |
| 4309 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 4310 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4311 | |
| 4312 | return( 0 ); |
| 4313 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4314 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 4315 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4316 | 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] | 4317 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4318 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4319 | } |
| 4320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4321 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4322 | 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] | 4323 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4324 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4325 | } |
| 4326 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4327 | 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] | 4328 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4329 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 4330 | } |
| 4331 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 4332 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4333 | const unsigned char period[8] ) |
| 4334 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 4335 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 4336 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4337 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4339 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4340 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4341 | 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] | 4342 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 4343 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4344 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4345 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4346 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4347 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4348 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 4349 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 4350 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 4351 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4352 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 4353 | conf->f_ticket_write = f_ticket_write; |
| 4354 | conf->f_ticket_parse = f_ticket_parse; |
| 4355 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 4356 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 4357 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4358 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 4359 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 4360 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 4361 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 4362 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4363 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 4364 | ssl->f_export_keys = f_export_keys; |
| 4365 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 4366 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 4367 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4368 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4369 | void mbedtls_ssl_conf_async_private_cb( |
| 4370 | mbedtls_ssl_config *conf, |
| 4371 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 4372 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 4373 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 4374 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4375 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4376 | { |
| 4377 | conf->f_async_sign_start = f_async_sign; |
| 4378 | conf->f_async_decrypt_start = f_async_decrypt; |
| 4379 | conf->f_async_resume = f_async_resume; |
| 4380 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4381 | conf->p_async_config_data = async_config_data; |
| 4382 | } |
| 4383 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 4384 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 4385 | { |
| 4386 | return( conf->p_async_config_data ); |
| 4387 | } |
| 4388 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4389 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4390 | { |
| 4391 | if( ssl->handshake == NULL ) |
| 4392 | return( NULL ); |
| 4393 | else |
| 4394 | return( ssl->handshake->user_async_ctx ); |
| 4395 | } |
| 4396 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 4397 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 4398 | void *ctx ) |
| 4399 | { |
| 4400 | if( ssl->handshake != NULL ) |
| 4401 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4402 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 4403 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 4404 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4405 | /* |
| 4406 | * SSL get accessors |
| 4407 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4408 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4409 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 4410 | if( ssl->session != NULL ) |
| 4411 | return( ssl->session->verify_result ); |
| 4412 | |
| 4413 | if( ssl->session_negotiate != NULL ) |
| 4414 | return( ssl->session_negotiate->verify_result ); |
| 4415 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 4416 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 4419 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 4420 | { |
| 4421 | if( ssl == NULL || ssl->session == NULL ) |
| 4422 | return( 0 ); |
| 4423 | |
| 4424 | return( ssl->session->ciphersuite ); |
| 4425 | } |
| 4426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4427 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4428 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4429 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4430 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 4431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4432 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4435 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4436 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4437 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4438 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4439 | { |
| 4440 | switch( ssl->minor_ver ) |
| 4441 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4442 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4443 | return( "DTLSv1.2" ); |
| 4444 | |
| 4445 | default: |
| 4446 | return( "unknown (DTLS)" ); |
| 4447 | } |
| 4448 | } |
| 4449 | #endif |
| 4450 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4451 | switch( ssl->minor_ver ) |
| 4452 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4453 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4454 | return( "TLSv1.2" ); |
| 4455 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4456 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 4457 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4458 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4461 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4462 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 4463 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 4464 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4465 | size_t read_mfl; |
| 4466 | |
| 4467 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 4468 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4469 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 4470 | { |
| 4471 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 4472 | } |
| 4473 | |
| 4474 | /* Check if a smaller max length was negotiated */ |
| 4475 | if( ssl->session_out != NULL ) |
| 4476 | { |
| 4477 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 4478 | if( read_mfl < max_len ) |
| 4479 | { |
| 4480 | max_len = read_mfl; |
| 4481 | } |
| 4482 | } |
| 4483 | |
| 4484 | // During a handshake, use the value being negotiated |
| 4485 | if( ssl->session_negotiate != NULL ) |
| 4486 | { |
| 4487 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4488 | if( read_mfl < max_len ) |
| 4489 | { |
| 4490 | max_len = read_mfl; |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | return( max_len ); |
| 4495 | } |
| 4496 | |
| 4497 | 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] | 4498 | { |
| 4499 | size_t max_len; |
| 4500 | |
| 4501 | /* |
| 4502 | * Assume mfl_code is correct since it was checked when set |
| 4503 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4504 | 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] | 4505 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4506 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4507 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4508 | 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] | 4509 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 4510 | 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] | 4511 | } |
| 4512 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 4513 | /* During a handshake, use the value being negotiated */ |
| 4514 | if( ssl->session_negotiate != NULL && |
| 4515 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 4516 | { |
| 4517 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 4518 | } |
| 4519 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4520 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 4521 | } |
| 4522 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4523 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4524 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4525 | 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] | 4526 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 4527 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 4528 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4529 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 4530 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 4531 | return ( 0 ); |
| 4532 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4533 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 4534 | return( ssl->mtu ); |
| 4535 | |
| 4536 | if( ssl->mtu == 0 ) |
| 4537 | return( ssl->handshake->mtu ); |
| 4538 | |
| 4539 | return( ssl->mtu < ssl->handshake->mtu ? |
| 4540 | ssl->mtu : ssl->handshake->mtu ); |
| 4541 | } |
| 4542 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4543 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4544 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 4545 | { |
| 4546 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 4547 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 4548 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4549 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4550 | (void) ssl; |
| 4551 | #endif |
| 4552 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4553 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 4554 | 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] | 4555 | |
| 4556 | if( max_len > mfl ) |
| 4557 | max_len = mfl; |
| 4558 | #endif |
| 4559 | |
| 4560 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4561 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4562 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 4563 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4564 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 4565 | const size_t overhead = (size_t) ret; |
| 4566 | |
| 4567 | if( ret < 0 ) |
| 4568 | return( ret ); |
| 4569 | |
| 4570 | if( mtu <= overhead ) |
| 4571 | { |
| 4572 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 4573 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4574 | } |
| 4575 | |
| 4576 | if( max_len > mtu - overhead ) |
| 4577 | max_len = mtu - overhead; |
| 4578 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 4579 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4580 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 4581 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 4582 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4583 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 4584 | #endif |
| 4585 | |
| 4586 | return( (int) max_len ); |
| 4587 | } |
| 4588 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 4589 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 4590 | { |
| 4591 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 4592 | |
| 4593 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4594 | (void) ssl; |
| 4595 | #endif |
| 4596 | |
| 4597 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4598 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 4599 | |
| 4600 | if( max_len > mfl ) |
| 4601 | max_len = mfl; |
| 4602 | #endif |
| 4603 | |
| 4604 | return( (int) max_len ); |
| 4605 | } |
| 4606 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4607 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4608 | 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] | 4609 | { |
| 4610 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4611 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4612 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 4613 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 4614 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 4615 | #else |
| 4616 | return( NULL ); |
| 4617 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4618 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4619 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 4620 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4621 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 4622 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 4623 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4624 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4625 | int ret; |
| 4626 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4627 | if( ssl == NULL || |
| 4628 | dst == NULL || |
| 4629 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4630 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4632 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4633 | } |
| 4634 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 4635 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 4636 | * idempotent: Each session can only be exported once. |
| 4637 | * |
| 4638 | * (This is in preparation for TLS 1.3 support where we will |
| 4639 | * need the ability to export multiple sessions (aka tickets), |
| 4640 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 4641 | * multiple times until it fails.) |
| 4642 | * |
| 4643 | * Check whether we have already exported the current session, |
| 4644 | * and fail if so. |
| 4645 | */ |
| 4646 | if( ssl->session->exported == 1 ) |
| 4647 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4648 | |
| 4649 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 4650 | if( ret != 0 ) |
| 4651 | return( ret ); |
| 4652 | |
| 4653 | /* Remember that we've exported the session. */ |
| 4654 | ssl->session->exported = 1; |
| 4655 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4656 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4657 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 4658 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4659 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4660 | * Define ticket header determining Mbed TLS version |
| 4661 | * and structure of the ticket. |
| 4662 | */ |
| 4663 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4664 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4665 | * Define bitflag determining compile-time settings influencing |
| 4666 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4667 | */ |
| 4668 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4669 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4670 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4671 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4672 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4673 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4674 | |
| 4675 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4676 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4677 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4678 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4679 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4680 | |
| 4681 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4682 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4683 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4684 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4685 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 4686 | |
| 4687 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4688 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4689 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4690 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4691 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 4692 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4693 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4694 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4695 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4696 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4697 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 4698 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4699 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4700 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 4701 | #else |
| 4702 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 4703 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 4704 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4705 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 4706 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 4707 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 4708 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 4709 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 4710 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4711 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 4712 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4713 | ( (uint16_t) ( \ |
| 4714 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 4715 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 4716 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 4717 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 4718 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 4719 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4720 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4721 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 4722 | MBEDTLS_VERSION_MAJOR, |
| 4723 | MBEDTLS_VERSION_MINOR, |
| 4724 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4725 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 4726 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 4727 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4728 | |
| 4729 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4730 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4731 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4732 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4733 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4734 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 4735 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 4736 | * opaque session_format[2]; // library-version specific 16-bit field |
| 4737 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4738 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 4739 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4740 | * Note: When updating the format, remember to keep |
| 4741 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4742 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4743 | * // In this version, `session_format` determines |
| 4744 | * // the setting of those compile-time |
| 4745 | * // configuration options which influence |
| 4746 | * // the structure of mbedtls_ssl_session. |
| 4747 | * |
Hanno Becker | fa0d61e | 2021-08-02 08:56:14 +0100 | [diff] [blame] | 4748 | * uint8_t minor_ver; // Protocol-version. Possible values: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4749 | * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3) |
| 4750 | * |
| 4751 | * select (serialized_session.minor_ver) { |
| 4752 | * |
| 4753 | * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2 |
| 4754 | * serialized_session_tls12 data; |
| 4755 | * |
| 4756 | * }; |
| 4757 | * |
| 4758 | * } serialized_session; |
| 4759 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4760 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4761 | |
| 4762 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4763 | /* Serialization of TLS 1.2 sessions: |
| 4764 | * |
| 4765 | * struct { |
| 4766 | * uint64 start_time; |
| 4767 | * uint8 ciphersuite[2]; // defined by the standard |
| 4768 | * uint8 compression; // 0 or 1 |
| 4769 | * uint8 session_id_len; // at most 32 |
| 4770 | * opaque session_id[32]; |
| 4771 | * opaque master[48]; // fixed length in the standard |
| 4772 | * uint32 verify_result; |
| 4773 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 4774 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 4775 | * uint32 ticket_lifetime; |
| 4776 | * uint8 mfl_code; // up to 255 according to standard |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4777 | * uint8 encrypt_then_mac; // 0 or 1 |
| 4778 | * } serialized_session_tls12; |
| 4779 | * |
| 4780 | */ |
| 4781 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 4782 | unsigned char *buf, |
| 4783 | size_t buf_len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4784 | { |
| 4785 | unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4786 | size_t used = 0; |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4787 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4788 | #if defined(MBEDTLS_HAVE_TIME) |
| 4789 | uint64_t start; |
| 4790 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4791 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4792 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4793 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4794 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4795 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4796 | |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 4797 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4798 | * Time |
| 4799 | */ |
| 4800 | #if defined(MBEDTLS_HAVE_TIME) |
| 4801 | used += 8; |
| 4802 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4803 | if( used <= buf_len ) |
| 4804 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4805 | start = (uint64_t) session->start; |
| 4806 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4807 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 4808 | p += 8; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4809 | } |
| 4810 | #endif /* MBEDTLS_HAVE_TIME */ |
| 4811 | |
| 4812 | /* |
| 4813 | * Basic mandatory fields |
| 4814 | */ |
| 4815 | used += 2 /* ciphersuite */ |
| 4816 | + 1 /* compression */ |
| 4817 | + 1 /* id_len */ |
| 4818 | + sizeof( session->id ) |
| 4819 | + sizeof( session->master ) |
| 4820 | + 4; /* verify_result */ |
| 4821 | |
| 4822 | if( used <= buf_len ) |
| 4823 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4824 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 4825 | p += 2; |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4826 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4827 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4828 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4829 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4830 | memcpy( p, session->id, 32 ); |
| 4831 | p += 32; |
| 4832 | |
| 4833 | memcpy( p, session->master, 48 ); |
| 4834 | p += 48; |
| 4835 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4836 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 4837 | p += 4; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4838 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4839 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4840 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4841 | * Peer's end-entity certificate |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4842 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4843 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4844 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 4845 | if( session->peer_cert == NULL ) |
| 4846 | cert_len = 0; |
| 4847 | else |
| 4848 | cert_len = session->peer_cert->raw.len; |
| 4849 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4850 | used += 3 + cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4851 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4852 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4853 | { |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4854 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 4855 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 4856 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4857 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4858 | if( session->peer_cert != NULL ) |
| 4859 | { |
| 4860 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 4861 | p += cert_len; |
| 4862 | } |
| 4863 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4864 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4865 | if( session->peer_cert_digest != NULL ) |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4866 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4867 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 4868 | if( used <= buf_len ) |
| 4869 | { |
| 4870 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 4871 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 4872 | memcpy( p, session->peer_cert_digest, |
| 4873 | session->peer_cert_digest_len ); |
| 4874 | p += session->peer_cert_digest_len; |
| 4875 | } |
| 4876 | } |
| 4877 | else |
| 4878 | { |
| 4879 | used += 2; |
| 4880 | if( used <= buf_len ) |
| 4881 | { |
| 4882 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 4883 | *p++ = 0; |
| 4884 | } |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4885 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4886 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4887 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 4888 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4889 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4890 | * Session ticket if any, plus associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4891 | */ |
| 4892 | #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] | 4893 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4894 | |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4895 | if( used <= buf_len ) |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4896 | { |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4897 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 4898 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 4899 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4900 | |
| 4901 | if( session->ticket != NULL ) |
| 4902 | { |
| 4903 | memcpy( p, session->ticket, session->ticket_len ); |
| 4904 | p += session->ticket_len; |
| 4905 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4906 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 4907 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 4908 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 4909 | } |
| 4910 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 4911 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4912 | /* |
| 4913 | * Misc extension-related info |
| 4914 | */ |
| 4915 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4916 | used += 1; |
| 4917 | |
| 4918 | if( used <= buf_len ) |
| 4919 | *p++ = session->mfl_code; |
| 4920 | #endif |
| 4921 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4922 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4923 | used += 1; |
| 4924 | |
| 4925 | if( used <= buf_len ) |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 4926 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 4927 | #endif |
| 4928 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4929 | return( used ); |
| 4930 | } |
| 4931 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4932 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4933 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 4934 | unsigned char omit_header, |
| 4935 | unsigned char *buf, |
| 4936 | size_t buf_len, |
| 4937 | size_t *olen ) |
| 4938 | { |
| 4939 | unsigned char *p = buf; |
| 4940 | size_t used = 0; |
| 4941 | |
| 4942 | if( !omit_header ) |
| 4943 | { |
| 4944 | /* |
| 4945 | * Add Mbed TLS version identifier |
| 4946 | */ |
| 4947 | |
| 4948 | used += sizeof( ssl_serialized_session_header ); |
| 4949 | |
| 4950 | if( used <= buf_len ) |
| 4951 | { |
| 4952 | memcpy( p, ssl_serialized_session_header, |
| 4953 | sizeof( ssl_serialized_session_header ) ); |
| 4954 | p += sizeof( ssl_serialized_session_header ); |
| 4955 | } |
| 4956 | } |
| 4957 | |
| 4958 | /* |
| 4959 | * TLS version identifier |
| 4960 | */ |
| 4961 | used += 1; |
| 4962 | if( used <= buf_len ) |
| 4963 | { |
| 4964 | *p++ = session->minor_ver; |
| 4965 | } |
| 4966 | |
| 4967 | /* Forward to version-specific serialization routine. */ |
| 4968 | switch( session->minor_ver ) |
| 4969 | { |
| 4970 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4971 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 4972 | { |
| 4973 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 4974 | used += ssl_session_save_tls12( session, p, remaining_len ); |
| 4975 | break; |
| 4976 | } |
| 4977 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4978 | |
| 4979 | default: |
| 4980 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4981 | } |
| 4982 | |
| 4983 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 4984 | if( used > buf_len ) |
| 4985 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 4986 | |
| 4987 | return( 0 ); |
| 4988 | } |
| 4989 | |
| 4990 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 4991 | * Public wrapper for ssl_session_save() |
| 4992 | */ |
| 4993 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 4994 | unsigned char *buf, |
| 4995 | size_t buf_len, |
| 4996 | size_t *olen ) |
| 4997 | { |
| 4998 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 4999 | } |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 5000 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5001 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5002 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5003 | * |
| 5004 | * 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] | 5005 | * case of error, and has an extra option omit_header. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5006 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5007 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 5008 | const unsigned char *buf, |
| 5009 | size_t len ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5010 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5011 | #if defined(MBEDTLS_HAVE_TIME) |
| 5012 | uint64_t start; |
| 5013 | #endif |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5014 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5015 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5016 | size_t cert_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5017 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5018 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5019 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5020 | const unsigned char *p = buf; |
| 5021 | const unsigned char * const end = buf + len; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 5022 | |
| 5023 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5024 | * Time |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5025 | */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5026 | #if defined(MBEDTLS_HAVE_TIME) |
| 5027 | if( 8 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5028 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5029 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5030 | start = ( (uint64_t) p[0] << 56 ) | |
| 5031 | ( (uint64_t) p[1] << 48 ) | |
| 5032 | ( (uint64_t) p[2] << 40 ) | |
| 5033 | ( (uint64_t) p[3] << 32 ) | |
| 5034 | ( (uint64_t) p[4] << 24 ) | |
| 5035 | ( (uint64_t) p[5] << 16 ) | |
| 5036 | ( (uint64_t) p[6] << 8 ) | |
| 5037 | ( (uint64_t) p[7] ); |
| 5038 | p += 8; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5039 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5040 | session->start = (time_t) start; |
| 5041 | #endif /* MBEDTLS_HAVE_TIME */ |
| 5042 | |
| 5043 | /* |
| 5044 | * Basic mandatory fields |
| 5045 | */ |
| 5046 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 5047 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5048 | |
| 5049 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 5050 | p += 2; |
| 5051 | |
| 5052 | session->compression = *p++; |
| 5053 | |
| 5054 | session->id_len = *p++; |
| 5055 | memcpy( session->id, p, 32 ); |
| 5056 | p += 32; |
| 5057 | |
| 5058 | memcpy( session->master, p, 48 ); |
| 5059 | p += 48; |
| 5060 | |
| 5061 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 5062 | ( (uint32_t) p[1] << 16 ) | |
| 5063 | ( (uint32_t) p[2] << 8 ) | |
| 5064 | ( (uint32_t) p[3] ); |
| 5065 | p += 4; |
| 5066 | |
| 5067 | /* Immediately clear invalid pointer values that have been read, in case |
| 5068 | * we exit early before we replaced them with valid ones. */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5069 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5070 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5071 | session->peer_cert = NULL; |
| 5072 | #else |
| 5073 | session->peer_cert_digest = NULL; |
| 5074 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5075 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5076 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5077 | session->ticket = NULL; |
| 5078 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5079 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5080 | /* |
| 5081 | * Peer certificate |
| 5082 | */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5083 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5084 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5085 | /* Deserialize CRT from the end of the ticket. */ |
| 5086 | if( 3 > (size_t)( end - p ) ) |
| 5087 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5088 | |
| 5089 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5090 | p += 3; |
| 5091 | |
| 5092 | if( cert_len != 0 ) |
| 5093 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5094 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5095 | |
| 5096 | if( cert_len > (size_t)( end - p ) ) |
| 5097 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5098 | |
| 5099 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 5100 | |
| 5101 | if( session->peer_cert == NULL ) |
| 5102 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5103 | |
| 5104 | mbedtls_x509_crt_init( session->peer_cert ); |
| 5105 | |
| 5106 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 5107 | p, cert_len ) ) != 0 ) |
| 5108 | { |
| 5109 | mbedtls_x509_crt_free( session->peer_cert ); |
| 5110 | mbedtls_free( session->peer_cert ); |
| 5111 | session->peer_cert = NULL; |
| 5112 | return( ret ); |
| 5113 | } |
| 5114 | |
| 5115 | p += cert_len; |
| 5116 | } |
| 5117 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5118 | /* Deserialize CRT digest from the end of the ticket. */ |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5119 | if( 2 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5120 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5121 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5122 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 5123 | session->peer_cert_digest_len = (size_t) *p++; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5124 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5125 | if( session->peer_cert_digest_len != 0 ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5126 | { |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5127 | const mbedtls_md_info_t *md_info = |
| 5128 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 5129 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5130 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5131 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 5132 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5133 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5134 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 5135 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5136 | |
| 5137 | session->peer_cert_digest = |
| 5138 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5139 | if( session->peer_cert_digest == NULL ) |
| 5140 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5141 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5142 | memcpy( session->peer_cert_digest, p, |
| 5143 | session->peer_cert_digest_len ); |
| 5144 | p += session->peer_cert_digest_len; |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5145 | } |
| 5146 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5147 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 5148 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5149 | /* |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5150 | * Session ticket and associated data |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5151 | */ |
| 5152 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 5153 | if( 3 > (size_t)( end - p ) ) |
| 5154 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5155 | |
| 5156 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 5157 | p += 3; |
| 5158 | |
| 5159 | if( session->ticket_len != 0 ) |
| 5160 | { |
| 5161 | if( session->ticket_len > (size_t)( end - p ) ) |
| 5162 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5163 | |
| 5164 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 5165 | if( session->ticket == NULL ) |
| 5166 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 5167 | |
| 5168 | memcpy( session->ticket, p, session->ticket_len ); |
| 5169 | p += session->ticket_len; |
| 5170 | } |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5171 | |
| 5172 | if( 4 > (size_t)( end - p ) ) |
| 5173 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5174 | |
| 5175 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 5176 | ( (uint32_t) p[1] << 16 ) | |
| 5177 | ( (uint32_t) p[2] << 8 ) | |
| 5178 | ( (uint32_t) p[3] ); |
| 5179 | p += 4; |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5180 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 5181 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5182 | /* |
| 5183 | * Misc extension-related info |
| 5184 | */ |
| 5185 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 5186 | if( 1 > (size_t)( end - p ) ) |
| 5187 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5188 | |
| 5189 | session->mfl_code = *p++; |
| 5190 | #endif |
| 5191 | |
Manuel Pégourié-Gonnard | f743c03 | 2019-05-24 12:06:29 +0200 | [diff] [blame] | 5192 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5193 | if( 1 > (size_t)( end - p ) ) |
| 5194 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5195 | |
| 5196 | session->encrypt_then_mac = *p++; |
| 5197 | #endif |
| 5198 | |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 5199 | /* Done, should have consumed entire buffer */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5200 | if( p != end ) |
| 5201 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5202 | |
| 5203 | return( 0 ); |
| 5204 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 5205 | #endif /*MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 5206 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 5207 | unsigned char omit_header, |
| 5208 | const unsigned char *buf, |
| 5209 | size_t len ) |
| 5210 | { |
| 5211 | const unsigned char *p = buf; |
| 5212 | const unsigned char * const end = buf + len; |
| 5213 | |
| 5214 | if( !omit_header ) |
| 5215 | { |
| 5216 | /* |
| 5217 | * Check Mbed TLS version identifier |
| 5218 | */ |
| 5219 | |
| 5220 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 5221 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5222 | |
| 5223 | if( memcmp( p, ssl_serialized_session_header, |
| 5224 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 5225 | { |
| 5226 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 5227 | } |
| 5228 | p += sizeof( ssl_serialized_session_header ); |
| 5229 | } |
| 5230 | |
| 5231 | /* |
| 5232 | * TLS version identifier |
| 5233 | */ |
| 5234 | if( 1 > (size_t)( end - p ) ) |
| 5235 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5236 | session->minor_ver = *p++; |
| 5237 | |
| 5238 | /* Dispatch according to TLS version. */ |
| 5239 | switch( session->minor_ver ) |
| 5240 | { |
| 5241 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5242 | case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */ |
| 5243 | { |
| 5244 | size_t remaining_len = ( end - p ); |
| 5245 | return( ssl_session_load_tls12( session, p, remaining_len ) ); |
| 5246 | } |
| 5247 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5248 | |
| 5249 | default: |
| 5250 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5251 | } |
| 5252 | } |
| 5253 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 5254 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 5255 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5256 | */ |
| 5257 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 5258 | const unsigned char *buf, |
| 5259 | size_t len ) |
| 5260 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5261 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 5262 | |
| 5263 | if( ret != 0 ) |
| 5264 | mbedtls_ssl_session_free( session ); |
| 5265 | |
| 5266 | return( ret ); |
| 5267 | } |
| 5268 | |
| 5269 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5270 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5271 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5272 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 5273 | { |
| 5274 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5275 | |
| 5276 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 5277 | return( ret ); |
| 5278 | |
| 5279 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5280 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5281 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 5282 | { |
| 5283 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 5284 | return( ret ); |
| 5285 | } |
| 5286 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5287 | |
| 5288 | return( ret ); |
| 5289 | } |
| 5290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5291 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5292 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5293 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5294 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5295 | if( ssl == NULL || |
| 5296 | ssl->conf == NULL || |
| 5297 | ssl->handshake == NULL || |
| 5298 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5299 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5300 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 5301 | } |
| 5302 | |
| 5303 | ret = ssl_prepare_handshake_step( ssl ); |
| 5304 | if( ret != 0 ) |
| 5305 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5306 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5307 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 5308 | if( ret != 0 ) |
| 5309 | goto cleanup; |
| 5310 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5311 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5312 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5313 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5314 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5315 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 5316 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5317 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5318 | |
| 5319 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5320 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 5321 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 5322 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5323 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5324 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5325 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5326 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5327 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5328 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5329 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 5330 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5331 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 5332 | |
| 5333 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5334 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 5335 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 5336 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5337 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5338 | #endif |
| 5339 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5340 | if( ret != 0 ) |
| 5341 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 5342 | /* handshake_step return error. And it is same |
| 5343 | * with alert_reason. |
| 5344 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 5345 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5346 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 5347 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 5348 | goto cleanup; |
| 5349 | } |
| 5350 | } |
| 5351 | |
| 5352 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5353 | return( ret ); |
| 5354 | } |
| 5355 | |
| 5356 | /* |
| 5357 | * Perform the SSL handshake |
| 5358 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5359 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5360 | { |
| 5361 | int ret = 0; |
| 5362 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5363 | /* Sanity checks */ |
| 5364 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5365 | if( ssl == NULL || ssl->conf == NULL ) |
| 5366 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5367 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5368 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5369 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5370 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 5371 | { |
| 5372 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 5373 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 5374 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5375 | } |
| 5376 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5378 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5379 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 5380 | /* Main handshake loop */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5381 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5383 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5384 | |
| 5385 | if( ret != 0 ) |
| 5386 | break; |
| 5387 | } |
| 5388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5389 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5390 | |
| 5391 | return( ret ); |
| 5392 | } |
| 5393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5394 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5395 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5396 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5397 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5398 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5399 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5400 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5401 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5403 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5404 | |
| 5405 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5406 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5407 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5408 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5409 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5410 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 5411 | 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] | 5412 | return( ret ); |
| 5413 | } |
| 5414 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5415 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5416 | |
| 5417 | return( 0 ); |
| 5418 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5419 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5420 | |
| 5421 | /* |
| 5422 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5423 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 5424 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 5425 | * - 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] | 5426 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5427 | * 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] | 5428 | * 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] | 5429 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5430 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5431 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5432 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5433 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5434 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5435 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5436 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5437 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5438 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5439 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 5440 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5441 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5442 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5443 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5444 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5445 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 5446 | ssl->handshake->out_msg_seq = 1; |
| 5447 | else |
| 5448 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5449 | } |
| 5450 | #endif |
| 5451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5452 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 5453 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5455 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5456 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5457 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5458 | return( ret ); |
| 5459 | } |
| 5460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5461 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5462 | |
| 5463 | return( 0 ); |
| 5464 | } |
| 5465 | |
| 5466 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5467 | * Renegotiate current connection on client, |
| 5468 | * or request renegotiation on server |
| 5469 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5470 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5471 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5472 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5473 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5474 | if( ssl == NULL || ssl->conf == NULL ) |
| 5475 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5477 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5478 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5479 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5481 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5482 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5484 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5485 | |
| 5486 | /* Did we already try/start sending HelloRequest? */ |
| 5487 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5488 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5489 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5490 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5491 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5492 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5493 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5494 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5495 | /* |
| 5496 | * On client, either start the renegotiation process or, |
| 5497 | * if already in progress, continue the handshake |
| 5498 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5499 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5500 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5501 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5502 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5503 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5504 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5505 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 5506 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5507 | return( ret ); |
| 5508 | } |
| 5509 | } |
| 5510 | else |
| 5511 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5512 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5513 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5514 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5515 | return( ret ); |
| 5516 | } |
| 5517 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5518 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5519 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 5520 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5521 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5522 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5523 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5524 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5525 | 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] | 5526 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5527 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5528 | |
| 5529 | while( cur != NULL ) |
| 5530 | { |
| 5531 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5532 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5533 | cur = next; |
| 5534 | } |
| 5535 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5536 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 5537 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5538 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5539 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 5540 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 5541 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5542 | if( handshake == NULL ) |
| 5543 | return; |
| 5544 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 5545 | #if defined(MBEDTLS_ECP_C) |
| 5546 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5547 | if ( ssl->handshake->group_list_heap_allocated ) |
| 5548 | mbedtls_free( (void*) handshake->group_list ); |
| 5549 | handshake->group_list = NULL; |
| 5550 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 5551 | #endif /* MBEDTLS_ECP_C */ |
| 5552 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 5553 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5554 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 5555 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 5556 | mbedtls_free( (void*) handshake->sig_algs ); |
| 5557 | handshake->sig_algs = NULL; |
| 5558 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 5559 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5560 | if( ssl->handshake->certificate_request_context ) |
| 5561 | { |
| 5562 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 5563 | } |
| 5564 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 5565 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5566 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5567 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 5568 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 5569 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 5570 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 5571 | handshake->async_in_progress = 0; |
| 5572 | } |
| 5573 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 5574 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5575 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5576 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5577 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 5578 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5579 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 5580 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5581 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 5582 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5583 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 5584 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5585 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5586 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 5587 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 5588 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 5589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5590 | #if defined(MBEDTLS_DHM_C) |
| 5591 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5592 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5593 | #if defined(MBEDTLS_ECDH_C) |
| 5594 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5595 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 5596 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5597 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 5598 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5599 | mbedtls_free( handshake->ecjpake_cache ); |
| 5600 | handshake->ecjpake_cache = NULL; |
| 5601 | handshake->ecjpake_cache_len = 0; |
| 5602 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 5603 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5604 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 5605 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 5606 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 5607 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5608 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 5609 | #endif |
| 5610 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5611 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5612 | if( handshake->psk != NULL ) |
| 5613 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5614 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5615 | mbedtls_free( handshake->psk ); |
| 5616 | } |
| 5617 | #endif |
| 5618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5619 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5620 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5621 | /* |
| 5622 | * Free only the linked list wrapper, not the keys themselves |
| 5623 | * since the belong to the SNI callback |
| 5624 | */ |
| 5625 | if( handshake->sni_key_cert != NULL ) |
| 5626 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5627 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5628 | |
| 5629 | while( cur != NULL ) |
| 5630 | { |
| 5631 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5632 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 5633 | cur = next; |
| 5634 | } |
| 5635 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5636 | #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] | 5637 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 5638 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 5639 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 5640 | if( handshake->ecrs_peer_cert != NULL ) |
| 5641 | { |
| 5642 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 5643 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 5644 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 5645 | #endif |
| 5646 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 5647 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 5648 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5649 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 5650 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5651 | |
XiaokangQian | 3490974 | 2022-01-27 02:25:04 +0000 | [diff] [blame] | 5652 | #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] | 5653 | mbedtls_free( handshake->verify_cookie ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 5654 | #endif /* MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 5655 | |
| 5656 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 5657 | mbedtls_ssl_flight_free( handshake->flight ); |
| 5658 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 5659 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 5660 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 5661 | #if defined(MBEDTLS_ECDH_C) && \ |
| 5662 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5663 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 5664 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 5665 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5666 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 5667 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 5668 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5669 | mbedtls_free( handshake->transform_earlydata ); |
| 5670 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5671 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5672 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5673 | |
| 5674 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 5675 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 5676 | * processes datagrams and the fact that a datagram is allowed to have |
| 5677 | * several records in it, it is possible that the I/O buffers are not |
| 5678 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 5679 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 5680 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5681 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 5682 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 5683 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 5684 | mbedtls_platform_zeroize( handshake, |
| 5685 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5688 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5689 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5690 | if( session == NULL ) |
| 5691 | return; |
| 5692 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5693 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 5694 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 5695 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5696 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5697 | #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] | 5698 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 5699 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 5700 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5701 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 5704 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5705 | |
| 5706 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5707 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 5708 | #else |
| 5709 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 5710 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5711 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5712 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5713 | |
| 5714 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5715 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 5716 | #else |
| 5717 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 5718 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5719 | |
| 5720 | #if defined(MBEDTLS_SSL_ALPN) |
| 5721 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 5722 | #else |
| 5723 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 5724 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5725 | |
| 5726 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 5727 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 5728 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 5729 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 5730 | |
| 5731 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 5732 | ( (uint32_t) ( \ |
| 5733 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 5734 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 5735 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 5736 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 5737 | 0u ) ) |
| 5738 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5739 | static unsigned char ssl_serialized_context_header[] = { |
| 5740 | MBEDTLS_VERSION_MAJOR, |
| 5741 | MBEDTLS_VERSION_MINOR, |
| 5742 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 5743 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5744 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 5745 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 5746 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 5747 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5748 | }; |
| 5749 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5750 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5751 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5752 | * |
| 5753 | * The format of the serialized data is: |
| 5754 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 5755 | * |
| 5756 | * // header |
| 5757 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5758 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 5759 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5760 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 5761 | * Note: When updating the format, remember to keep these |
| 5762 | * 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] | 5763 | * |
| 5764 | * // session sub-structure |
| 5765 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 5766 | * // transform sub-structure |
| 5767 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 5768 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 5769 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 5770 | * // fields from ssl_context |
| 5771 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 5772 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 5773 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 5774 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 5775 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 5776 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 5777 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 5778 | * |
| 5779 | * Note that many fields of the ssl_context or sub-structures are not |
| 5780 | * serialized, as they fall in one of the following categories: |
| 5781 | * |
| 5782 | * 1. forced value (eg in_left must be 0) |
| 5783 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 5784 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 5785 | * 4. value was temporary (eg content of input buffer) |
| 5786 | * 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] | 5787 | */ |
| 5788 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 5789 | unsigned char *buf, |
| 5790 | size_t buf_len, |
| 5791 | size_t *olen ) |
| 5792 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5793 | unsigned char *p = buf; |
| 5794 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5795 | size_t session_len; |
| 5796 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5797 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5798 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5799 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 5800 | * this function's documentation. |
| 5801 | * |
| 5802 | * These are due to assumptions/limitations in the implementation. Some of |
| 5803 | * them are likely to stay (no handshake in progress) some might go away |
| 5804 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5805 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5806 | /* The initial handshake must be over */ |
| 5807 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5808 | { |
| 5809 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 5810 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5811 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5812 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5813 | { |
| 5814 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5815 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5816 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5817 | /* Double-check that sub-structures are indeed ready */ |
| 5818 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5819 | { |
| 5820 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5821 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5822 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5823 | /* There must be no pending incoming or outgoing data */ |
| 5824 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5825 | { |
| 5826 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5827 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5828 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5829 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5830 | { |
| 5831 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5832 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5833 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5834 | /* Protocol must be DLTS, not TLS */ |
| 5835 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5836 | { |
| 5837 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5838 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5839 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5840 | /* Version must be 1.2 */ |
| 5841 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5842 | { |
| 5843 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5844 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5845 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5846 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5847 | { |
| 5848 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5849 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5850 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5851 | /* We must be using an AEAD ciphersuite */ |
| 5852 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5853 | { |
| 5854 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5855 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5856 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5857 | /* Renegotiation must not be enabled */ |
| 5858 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5859 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5860 | { |
| 5861 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5862 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 5863 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 5864 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5865 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5866 | /* |
| 5867 | * Version and format identifier |
| 5868 | */ |
| 5869 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5870 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5871 | if( used <= buf_len ) |
| 5872 | { |
| 5873 | memcpy( p, ssl_serialized_context_header, |
| 5874 | sizeof( ssl_serialized_context_header ) ); |
| 5875 | p += sizeof( ssl_serialized_context_header ); |
| 5876 | } |
| 5877 | |
| 5878 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5879 | * Session (length + data) |
| 5880 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5881 | 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] | 5882 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 5883 | return( ret ); |
| 5884 | |
| 5885 | used += 4 + session_len; |
| 5886 | if( used <= buf_len ) |
| 5887 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5888 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 5889 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5890 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 5891 | ret = ssl_session_save( ssl->session, 1, |
| 5892 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5893 | if( ret != 0 ) |
| 5894 | return( ret ); |
| 5895 | |
| 5896 | p += session_len; |
| 5897 | } |
| 5898 | |
| 5899 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 5900 | * Transform |
| 5901 | */ |
| 5902 | used += sizeof( ssl->transform->randbytes ); |
| 5903 | if( used <= buf_len ) |
| 5904 | { |
| 5905 | memcpy( p, ssl->transform->randbytes, |
| 5906 | sizeof( ssl->transform->randbytes ) ); |
| 5907 | p += sizeof( ssl->transform->randbytes ); |
| 5908 | } |
| 5909 | |
| 5910 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5911 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 5912 | if( used <= buf_len ) |
| 5913 | { |
| 5914 | *p++ = ssl->transform->in_cid_len; |
| 5915 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 5916 | p += ssl->transform->in_cid_len; |
| 5917 | |
| 5918 | *p++ = ssl->transform->out_cid_len; |
| 5919 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 5920 | p += ssl->transform->out_cid_len; |
| 5921 | } |
| 5922 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5923 | |
| 5924 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5925 | * Saved fields from top-level ssl_context structure |
| 5926 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5927 | used += 4; |
| 5928 | if( used <= buf_len ) |
| 5929 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5930 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 5931 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5932 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5933 | |
| 5934 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 5935 | used += 16; |
| 5936 | if( used <= buf_len ) |
| 5937 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5938 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 5939 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5940 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5941 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 5942 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5943 | } |
| 5944 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 5945 | |
| 5946 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5947 | used += 1; |
| 5948 | if( used <= buf_len ) |
| 5949 | { |
| 5950 | *p++ = ssl->disable_datagram_packing; |
| 5951 | } |
| 5952 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5953 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5954 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5955 | if( used <= buf_len ) |
| 5956 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5957 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 5958 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5959 | } |
| 5960 | |
| 5961 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5962 | used += 2; |
| 5963 | if( used <= buf_len ) |
| 5964 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 5965 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 5966 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5967 | } |
| 5968 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5969 | |
| 5970 | #if defined(MBEDTLS_SSL_ALPN) |
| 5971 | { |
| 5972 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 5973 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 5974 | : 0; |
| 5975 | |
| 5976 | used += 1 + alpn_len; |
| 5977 | if( used <= buf_len ) |
| 5978 | { |
| 5979 | *p++ = alpn_len; |
| 5980 | |
| 5981 | if( ssl->alpn_chosen != NULL ) |
| 5982 | { |
| 5983 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 5984 | p += alpn_len; |
| 5985 | } |
| 5986 | } |
| 5987 | } |
| 5988 | #endif /* MBEDTLS_SSL_ALPN */ |
| 5989 | |
| 5990 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 5991 | * Done |
| 5992 | */ |
| 5993 | *olen = used; |
| 5994 | |
| 5995 | if( used > buf_len ) |
| 5996 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 5997 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 5998 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 5999 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6000 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6004 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 6005 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 6006 | */ |
| 6007 | typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen, |
| 6008 | const char *label, |
| 6009 | const unsigned char *random, size_t rlen, |
| 6010 | unsigned char *dstbuf, size_t dlen ); |
| 6011 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 6012 | { |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 6013 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 6014 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6015 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6016 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 6017 | |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6018 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 6019 | return( tls_prf_sha384 ); |
Jarno Lamsa | b7b486c | 2019-08-21 15:30:44 +0300 | [diff] [blame] | 6020 | #else |
| 6021 | (void) ciphersuite_id; |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6022 | #endif |
| 6023 | return( tls_prf_sha256 ); |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 6024 | #else /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 6025 | (void) ciphersuite_id; |
| 6026 | return( NULL ); |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 6027 | #endif /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | c3091b1 | 2021-12-23 14:57:39 +0800 | [diff] [blame] | 6028 | |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6029 | } |
| 6030 | |
| 6031 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6032 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6033 | * |
| 6034 | * This internal version is wrapped by a public function that cleans up in |
| 6035 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6036 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6037 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 6038 | const unsigned char *buf, |
| 6039 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6040 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6041 | const unsigned char *p = buf; |
| 6042 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6043 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6044 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6045 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6046 | /* |
| 6047 | * The context should have been freshly setup or reset. |
| 6048 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 6049 | * (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] | 6050 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 6051 | */ |
| 6052 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 6053 | ssl->session != NULL ) |
| 6054 | { |
| 6055 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6056 | } |
| 6057 | |
| 6058 | /* |
| 6059 | * We can't check that the config matches the initial one, but we can at |
| 6060 | * least check it matches the requirements for serializing. |
| 6061 | */ |
| 6062 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 6063 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6064 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 6065 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6066 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
| 6067 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6068 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6069 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 6070 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 6071 | { |
| 6072 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6073 | } |
| 6074 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6075 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 6076 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6077 | /* |
| 6078 | * Check version identifier |
| 6079 | */ |
| 6080 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 6081 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6082 | |
| 6083 | if( memcmp( p, ssl_serialized_context_header, |
| 6084 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 6085 | { |
| 6086 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 6087 | } |
| 6088 | p += sizeof( ssl_serialized_context_header ); |
| 6089 | |
| 6090 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6091 | * Session |
| 6092 | */ |
| 6093 | if( (size_t)( end - p ) < 4 ) |
| 6094 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6095 | |
| 6096 | session_len = ( (size_t) p[0] << 24 ) | |
| 6097 | ( (size_t) p[1] << 16 ) | |
| 6098 | ( (size_t) p[2] << 8 ) | |
| 6099 | ( (size_t) p[3] ); |
| 6100 | p += 4; |
| 6101 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6102 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6103 | * 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] | 6104 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6105 | ssl->session_in = ssl->session; |
| 6106 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6107 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6108 | |
| 6109 | if( (size_t)( end - p ) < session_len ) |
| 6110 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6111 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6112 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6113 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6114 | { |
| 6115 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6116 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 6117 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6118 | |
| 6119 | p += session_len; |
| 6120 | |
| 6121 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6122 | * Transform |
| 6123 | */ |
| 6124 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6125 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 6126 | * 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] | 6127 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6128 | ssl->transform_in = ssl->transform; |
| 6129 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 6130 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6131 | |
| 6132 | /* Read random bytes and populate structure */ |
| 6133 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 6134 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6135 | |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6136 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6137 | ssl->session->ciphersuite, |
| 6138 | ssl->session->master, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6139 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 6140 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6141 | ssl->session->encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 6142 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 6143 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 6144 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 6145 | p, /* currently pointing to randbytes */ |
| 6146 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 6147 | ssl->conf->endpoint, |
| 6148 | ssl ); |
| 6149 | if( ret != 0 ) |
| 6150 | return( ret ); |
| 6151 | |
| 6152 | p += sizeof( ssl->transform->randbytes ); |
| 6153 | |
| 6154 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 6155 | /* Read connection IDs and store them */ |
| 6156 | if( (size_t)( end - p ) < 1 ) |
| 6157 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6158 | |
| 6159 | ssl->transform->in_cid_len = *p++; |
| 6160 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 6161 | 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] | 6162 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6163 | |
| 6164 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 6165 | p += ssl->transform->in_cid_len; |
| 6166 | |
| 6167 | ssl->transform->out_cid_len = *p++; |
| 6168 | |
| 6169 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 6170 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6171 | |
| 6172 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 6173 | p += ssl->transform->out_cid_len; |
| 6174 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 6175 | |
| 6176 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6177 | * Saved fields from top-level ssl_context structure |
| 6178 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6179 | if( (size_t)( end - p ) < 4 ) |
| 6180 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6181 | |
| 6182 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 6183 | ( (uint32_t) p[1] << 16 ) | |
| 6184 | ( (uint32_t) p[2] << 8 ) | |
| 6185 | ( (uint32_t) p[3] ); |
| 6186 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6187 | |
| 6188 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6189 | if( (size_t)( end - p ) < 16 ) |
| 6190 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6191 | |
| 6192 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 6193 | ( (uint64_t) p[1] << 48 ) | |
| 6194 | ( (uint64_t) p[2] << 40 ) | |
| 6195 | ( (uint64_t) p[3] << 32 ) | |
| 6196 | ( (uint64_t) p[4] << 24 ) | |
| 6197 | ( (uint64_t) p[5] << 16 ) | |
| 6198 | ( (uint64_t) p[6] << 8 ) | |
| 6199 | ( (uint64_t) p[7] ); |
| 6200 | p += 8; |
| 6201 | |
| 6202 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 6203 | ( (uint64_t) p[1] << 48 ) | |
| 6204 | ( (uint64_t) p[2] << 40 ) | |
| 6205 | ( (uint64_t) p[3] << 32 ) | |
| 6206 | ( (uint64_t) p[4] << 24 ) | |
| 6207 | ( (uint64_t) p[5] << 16 ) | |
| 6208 | ( (uint64_t) p[6] << 8 ) | |
| 6209 | ( (uint64_t) p[7] ); |
| 6210 | p += 8; |
| 6211 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 6212 | |
| 6213 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6214 | if( (size_t)( end - p ) < 1 ) |
| 6215 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6216 | |
| 6217 | ssl->disable_datagram_packing = *p++; |
| 6218 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6219 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 6220 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6221 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 6222 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 6223 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 6224 | |
| 6225 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6226 | if( (size_t)( end - p ) < 2 ) |
| 6227 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6228 | |
| 6229 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 6230 | p += 2; |
| 6231 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6232 | |
| 6233 | #if defined(MBEDTLS_SSL_ALPN) |
| 6234 | { |
| 6235 | uint8_t alpn_len; |
| 6236 | const char **cur; |
| 6237 | |
| 6238 | if( (size_t)( end - p ) < 1 ) |
| 6239 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6240 | |
| 6241 | alpn_len = *p++; |
| 6242 | |
| 6243 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 6244 | { |
| 6245 | /* alpn_chosen should point to an item in the configured list */ |
| 6246 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 6247 | { |
| 6248 | if( strlen( *cur ) == alpn_len && |
| 6249 | memcmp( p, cur, alpn_len ) == 0 ) |
| 6250 | { |
| 6251 | ssl->alpn_chosen = *cur; |
| 6252 | break; |
| 6253 | } |
| 6254 | } |
| 6255 | } |
| 6256 | |
| 6257 | /* can only happen on conf mismatch */ |
| 6258 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 6259 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 6260 | |
| 6261 | p += alpn_len; |
| 6262 | } |
| 6263 | #endif /* MBEDTLS_SSL_ALPN */ |
| 6264 | |
| 6265 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6266 | * Forced fields from top-level ssl_context structure |
| 6267 | * |
| 6268 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 6269 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 6270 | */ |
| 6271 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 6272 | |
| 6273 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6274 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6275 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6276 | /* Adjust pointers for header fields of outgoing records to |
| 6277 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 6278 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 6279 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6280 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6281 | ssl->in_epoch = 1; |
| 6282 | #endif |
| 6283 | |
| 6284 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 6285 | * 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] | 6286 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 6287 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 6288 | if( ssl->handshake != NULL ) |
| 6289 | { |
| 6290 | mbedtls_ssl_handshake_free( ssl ); |
| 6291 | mbedtls_free( ssl->handshake ); |
| 6292 | ssl->handshake = NULL; |
| 6293 | } |
| 6294 | |
| 6295 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 6296 | * Done - should have consumed entire buffer |
| 6297 | */ |
| 6298 | if( p != end ) |
| 6299 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 6300 | |
| 6301 | return( 0 ); |
| 6302 | } |
| 6303 | |
| 6304 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 6305 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6306 | */ |
| 6307 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 6308 | const unsigned char *buf, |
| 6309 | size_t len ) |
| 6310 | { |
| 6311 | int ret = ssl_context_load( context, buf, len ); |
| 6312 | |
| 6313 | if( ret != 0 ) |
| 6314 | mbedtls_ssl_free( context ); |
| 6315 | |
| 6316 | return( ret ); |
| 6317 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 6318 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 6319 | |
| 6320 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6321 | * Free an SSL context |
| 6322 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6323 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6324 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6325 | if( ssl == NULL ) |
| 6326 | return; |
| 6327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6328 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6329 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6330 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6331 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6332 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6333 | size_t out_buf_len = ssl->out_buf_len; |
| 6334 | #else |
| 6335 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 6336 | #endif |
| 6337 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6338 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6339 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6340 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6341 | } |
| 6342 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6343 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6344 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 6345 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 6346 | size_t in_buf_len = ssl->in_buf_len; |
| 6347 | #else |
| 6348 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 6349 | #endif |
| 6350 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 6351 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6352 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 6353 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6356 | if( ssl->transform ) |
| 6357 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6358 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6359 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
| 6362 | if( ssl->handshake ) |
| 6363 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 6364 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6365 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 6366 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6368 | mbedtls_free( ssl->handshake ); |
| 6369 | mbedtls_free( ssl->transform_negotiate ); |
| 6370 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6373 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 6374 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 6375 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6376 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 6377 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6378 | if( ssl->session ) |
| 6379 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6380 | mbedtls_ssl_session_free( ssl->session ); |
| 6381 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6382 | } |
| 6383 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 6384 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 6385 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6386 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6387 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6388 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6389 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 6390 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6391 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6392 | #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] | 6393 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 6394 | #endif |
| 6395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6396 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 6397 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 6398 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6399 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6402 | /* |
| 6403 | * Initialze mbedtls_ssl_config |
| 6404 | */ |
| 6405 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 6406 | { |
| 6407 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 6408 | } |
| 6409 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6410 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6411 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6412 | /* 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] | 6413 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 6414 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6415 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 6416 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6417 | static int ssl_preset_default_hashes[] = { |
| 6418 | #if defined(MBEDTLS_SHA512_C) |
| 6419 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6420 | #endif |
| 6421 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6422 | MBEDTLS_MD_SHA384, |
| 6423 | #endif |
| 6424 | #if defined(MBEDTLS_SHA256_C) |
| 6425 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6426 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6427 | MBEDTLS_MD_NONE |
| 6428 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6429 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 6430 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6431 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6432 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 6433 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6434 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6435 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6436 | * about this list. |
| 6437 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6438 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6439 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6440 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6441 | #endif |
| 6442 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6443 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6444 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6445 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6446 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6447 | #endif |
| 6448 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6449 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6450 | #endif |
| 6451 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6452 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6453 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6454 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6455 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6456 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6457 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6458 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6459 | #endif |
| 6460 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6461 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 6462 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6463 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6464 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 6465 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6466 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 6467 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 6468 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 6469 | 0 |
| 6470 | }; |
| 6471 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6472 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6473 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6474 | static int ssl_preset_suiteb_hashes[] = { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6475 | #if defined(MBEDTLS_SHA256_C) |
| 6476 | MBEDTLS_MD_SHA256, |
| 6477 | #endif |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6478 | #if defined(MBEDTLS_SHA384_C) |
| 6479 | MBEDTLS_MD_SHA384, |
| 6480 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6481 | MBEDTLS_MD_NONE |
| 6482 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6483 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6484 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6485 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 6486 | * 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] | 6487 | * rules SHOULD be upheld. |
| 6488 | * - No duplicate entries. |
| 6489 | * - But if there is a good reason, do not change the order of the algorithms. |
| 6490 | * - ssl_tls12_present* is for TLS 1.2 use only. |
| 6491 | * - 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] | 6492 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6493 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6494 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 6495 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 6496 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 6497 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 6498 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 6499 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6500 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6501 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 6502 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 6503 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 6504 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6505 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 6506 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 6507 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ |
| 6508 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 6509 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
| 6510 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6511 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6512 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6513 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 6514 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 6515 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
| 6516 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6517 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 6518 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 6519 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 6520 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6521 | MBEDTLS_TLS1_3_SIG_NONE |
| 6522 | }; |
| 6523 | |
| 6524 | /* NOTICE: see above */ |
| 6525 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6526 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6527 | #if defined(MBEDTLS_SHA512_C) |
| 6528 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) |
| 6529 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 6530 | #if defined(MBEDTLS_SHA384_C) |
| 6531 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 6532 | #endif |
| 6533 | #if defined(MBEDTLS_SHA256_C) |
| 6534 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 6535 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6536 | MBEDTLS_TLS1_3_SIG_NONE |
| 6537 | }; |
| 6538 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6539 | /* NOTICE: see above */ |
| 6540 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 6541 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6542 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 6543 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 6544 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 6545 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 6546 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 6547 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6548 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 6549 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 6550 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 6551 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 6552 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6553 | |
| 6554 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 6555 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 6556 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6557 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 6558 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 6559 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 6560 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 6561 | |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6562 | MBEDTLS_TLS1_3_SIG_NONE |
| 6563 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 6564 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6565 | /* NOTICE: see above */ |
| 6566 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6567 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 6568 | #if defined(MBEDTLS_SHA256_C) |
| 6569 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 6570 | #endif |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 6571 | #if defined(MBEDTLS_SHA384_C) |
| 6572 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 6573 | #endif |
Xiaofei Bai | 9539501 | 2021-11-25 08:51:30 +0000 | [diff] [blame] | 6574 | MBEDTLS_TLS1_3_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 6575 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6576 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6577 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6578 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6579 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6580 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6581 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6582 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6583 | #endif |
| 6584 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6585 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 6586 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6587 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6588 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6589 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6590 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6591 | /* 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] | 6592 | * to make sure there are no duplicated signature algorithm entries. */ |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6593 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6594 | { |
| 6595 | size_t i, j; |
| 6596 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6597 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6598 | for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6599 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6600 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6601 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6602 | if( sig_algs[i] != sig_algs[j] ) |
| 6603 | continue; |
| 6604 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 6605 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 6606 | sig_algs[i], j, i ); |
| 6607 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6608 | } |
| 6609 | } |
| 6610 | return( ret ); |
| 6611 | } |
| 6612 | |
| 6613 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6614 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6615 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 6616 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6617 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6618 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6619 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6620 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6621 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6622 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6623 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6624 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6625 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6626 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6627 | { |
| 6628 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 6629 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6630 | } |
| 6631 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6632 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6633 | { |
| 6634 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 6635 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6636 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6637 | |
| 6638 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6639 | 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] | 6640 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6641 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6642 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6643 | } |
| 6644 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 6645 | 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] | 6646 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6647 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6648 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 6649 | } |
| 6650 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 6651 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6652 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 6653 | /* Use the functions here so that they are covered in tests, |
| 6654 | * but otherwise access member directly for efficiency */ |
| 6655 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 6656 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6657 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6658 | /* |
| 6659 | * Things that are common to all presets |
| 6660 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6661 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6662 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6663 | { |
| 6664 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 6665 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 6666 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 6667 | #endif |
| 6668 | } |
| 6669 | #endif |
| 6670 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6671 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6672 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 6673 | #endif |
| 6674 | |
| 6675 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6676 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 6677 | #endif |
| 6678 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6679 | #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] | 6680 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 6681 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 6682 | #endif |
| 6683 | |
| 6684 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6685 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 6686 | #endif |
| 6687 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6688 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6689 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 6690 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 6691 | #endif |
| 6692 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6693 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6694 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 6695 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 6696 | #endif |
| 6697 | |
| 6698 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6699 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 6700 | memset( conf->renego_period, 0x00, 2 ); |
| 6701 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6702 | #endif |
| 6703 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6704 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6705 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6706 | { |
| 6707 | const unsigned char dhm_p[] = |
| 6708 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 6709 | const unsigned char dhm_g[] = |
| 6710 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 6711 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 6712 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 6713 | dhm_p, sizeof( dhm_p ), |
| 6714 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 6715 | { |
| 6716 | return( ret ); |
| 6717 | } |
| 6718 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6719 | #endif |
| 6720 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6721 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6722 | /* |
| 6723 | * Allow all TLS 1.3 key exchange modes by default. |
| 6724 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 6725 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 6726 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 6727 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6728 | /* |
| 6729 | * Preset-specific defaults |
| 6730 | */ |
| 6731 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6732 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6733 | /* |
| 6734 | * NSA Suite B |
| 6735 | */ |
| 6736 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6737 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6738 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 6739 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 6740 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 6741 | /* Hybrid TLS 1.2/1.3 is not supported yet */ |
| 6742 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6743 | #else |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6744 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 6745 | #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] | 6746 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6747 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6748 | |
| 6749 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6750 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6751 | #endif |
| 6752 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6753 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6754 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6755 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6756 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6757 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6758 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 6759 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 6760 | else |
| 6761 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6762 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6763 | #endif |
| 6764 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6765 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6766 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6767 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6768 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 6769 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6770 | |
| 6771 | /* |
| 6772 | * Default |
| 6773 | */ |
| 6774 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 6775 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 6776 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 6777 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 6778 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 6779 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 6780 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 6781 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 6782 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6783 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 6784 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 6785 | /* Hybrid TLS 1.2/1.3 is not supported yet */ |
| 6786 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 6787 | #else |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6788 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 6789 | #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] | 6790 | |
| 6791 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6792 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 6793 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6794 | #endif |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 6795 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6796 | |
| 6797 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6798 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6799 | #endif |
| 6800 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6801 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6802 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 6803 | conf->sig_hashes = ssl_preset_default_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 6804 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 6805 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6806 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 6807 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 6808 | else |
| 6809 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 6810 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 6811 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6812 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6813 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 6814 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6815 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 6816 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6817 | |
| 6818 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6819 | conf->dhm_min_bitlen = 1024; |
| 6820 | #endif |
| 6821 | } |
| 6822 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6823 | return( 0 ); |
| 6824 | } |
| 6825 | |
| 6826 | /* |
| 6827 | * Free mbedtls_ssl_config |
| 6828 | */ |
| 6829 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 6830 | { |
| 6831 | #if defined(MBEDTLS_DHM_C) |
| 6832 | mbedtls_mpi_free( &conf->dhm_P ); |
| 6833 | mbedtls_mpi_free( &conf->dhm_G ); |
| 6834 | #endif |
| 6835 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6836 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6837 | if( conf->psk != NULL ) |
| 6838 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6839 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6840 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6841 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6842 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | if( conf->psk_identity != NULL ) |
| 6846 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6847 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 6848 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 6849 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6850 | conf->psk_identity_len = 0; |
| 6851 | } |
| 6852 | #endif |
| 6853 | |
| 6854 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6855 | ssl_key_cert_free( conf->key_cert ); |
| 6856 | #endif |
| 6857 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 6858 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6859 | } |
| 6860 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 6861 | #if defined(MBEDTLS_PK_C) && \ |
| 6862 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6863 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6864 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6865 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6866 | 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] | 6867 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6868 | #if defined(MBEDTLS_RSA_C) |
| 6869 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 6870 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6871 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6872 | #if defined(MBEDTLS_ECDSA_C) |
| 6873 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 6874 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6875 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6876 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6877 | } |
| 6878 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6879 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 6880 | { |
| 6881 | switch( type ) { |
| 6882 | case MBEDTLS_PK_RSA: |
| 6883 | return( MBEDTLS_SSL_SIG_RSA ); |
| 6884 | case MBEDTLS_PK_ECDSA: |
| 6885 | case MBEDTLS_PK_ECKEY: |
| 6886 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 6887 | default: |
| 6888 | return( MBEDTLS_SSL_SIG_ANON ); |
| 6889 | } |
| 6890 | } |
| 6891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6892 | 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] | 6893 | { |
| 6894 | switch( sig ) |
| 6895 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6896 | #if defined(MBEDTLS_RSA_C) |
| 6897 | case MBEDTLS_SSL_SIG_RSA: |
| 6898 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6899 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6900 | #if defined(MBEDTLS_ECDSA_C) |
| 6901 | case MBEDTLS_SSL_SIG_ECDSA: |
| 6902 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6903 | #endif |
| 6904 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6905 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6906 | } |
| 6907 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 6908 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6909 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6910 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6911 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6912 | |
| 6913 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 6914 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 6915 | mbedtls_pk_type_t sig_alg ) |
| 6916 | { |
| 6917 | switch( sig_alg ) |
| 6918 | { |
| 6919 | case MBEDTLS_PK_RSA: |
| 6920 | return( set->rsa ); |
| 6921 | case MBEDTLS_PK_ECDSA: |
| 6922 | return( set->ecdsa ); |
| 6923 | default: |
| 6924 | return( MBEDTLS_MD_NONE ); |
| 6925 | } |
| 6926 | } |
| 6927 | |
| 6928 | /* Add a signature-hash-pair to a signature-hash set */ |
| 6929 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 6930 | mbedtls_pk_type_t sig_alg, |
| 6931 | mbedtls_md_type_t md_alg ) |
| 6932 | { |
| 6933 | switch( sig_alg ) |
| 6934 | { |
| 6935 | case MBEDTLS_PK_RSA: |
| 6936 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 6937 | set->rsa = md_alg; |
| 6938 | break; |
| 6939 | |
| 6940 | case MBEDTLS_PK_ECDSA: |
| 6941 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 6942 | set->ecdsa = md_alg; |
| 6943 | break; |
| 6944 | |
| 6945 | default: |
| 6946 | break; |
| 6947 | } |
| 6948 | } |
| 6949 | |
| 6950 | /* Allow exactly one hash algorithm for each signature. */ |
| 6951 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 6952 | mbedtls_md_type_t md_alg ) |
| 6953 | { |
| 6954 | set->rsa = md_alg; |
| 6955 | set->ecdsa = md_alg; |
| 6956 | } |
| 6957 | |
| 6958 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2) && |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 6959 | MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 6960 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6961 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6962 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6963 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6964 | 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] | 6965 | { |
| 6966 | switch( hash ) |
| 6967 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6968 | #if defined(MBEDTLS_MD5_C) |
| 6969 | case MBEDTLS_SSL_HASH_MD5: |
| 6970 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6971 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6972 | #if defined(MBEDTLS_SHA1_C) |
| 6973 | case MBEDTLS_SSL_HASH_SHA1: |
| 6974 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6975 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6976 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6977 | case MBEDTLS_SSL_HASH_SHA224: |
| 6978 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 6979 | #endif |
| 6980 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6981 | case MBEDTLS_SSL_HASH_SHA256: |
| 6982 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6983 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6984 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6985 | case MBEDTLS_SSL_HASH_SHA384: |
| 6986 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 6987 | #endif |
| 6988 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6989 | case MBEDTLS_SSL_HASH_SHA512: |
| 6990 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6991 | #endif |
| 6992 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6993 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6994 | } |
| 6995 | } |
| 6996 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6997 | /* |
| 6998 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 6999 | */ |
| 7000 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 7001 | { |
| 7002 | switch( md ) |
| 7003 | { |
| 7004 | #if defined(MBEDTLS_MD5_C) |
| 7005 | case MBEDTLS_MD_MD5: |
| 7006 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 7007 | #endif |
| 7008 | #if defined(MBEDTLS_SHA1_C) |
| 7009 | case MBEDTLS_MD_SHA1: |
| 7010 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 7011 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7012 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7013 | case MBEDTLS_MD_SHA224: |
| 7014 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 7015 | #endif |
| 7016 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7017 | case MBEDTLS_MD_SHA256: |
| 7018 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 7019 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7020 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7021 | case MBEDTLS_MD_SHA384: |
| 7022 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 7023 | #endif |
| 7024 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7025 | case MBEDTLS_MD_SHA512: |
| 7026 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 7027 | #endif |
| 7028 | default: |
| 7029 | return( MBEDTLS_SSL_HASH_NONE ); |
| 7030 | } |
| 7031 | } |
| 7032 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7033 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 7034 | * 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] | 7035 | * 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] | 7036 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 7037 | 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] | 7038 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7039 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7040 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7041 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7042 | return( -1 ); |
| 7043 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7044 | for( ; *group_list != 0; group_list++ ) |
| 7045 | { |
| 7046 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7047 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 7048 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7049 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 7050 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 7051 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 7052 | |
| 7053 | #if defined(MBEDTLS_ECP_C) |
| 7054 | /* |
| 7055 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 7056 | */ |
| 7057 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 7058 | { |
Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 7059 | 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] | 7060 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 7061 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 7062 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7063 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7064 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7065 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 7066 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7067 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7068 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7069 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7070 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7071 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7072 | const char *ext_oid; |
| 7073 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7074 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7075 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7076 | { |
| 7077 | /* Server part of the key exchange */ |
| 7078 | switch( ciphersuite->key_exchange ) |
| 7079 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7080 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 7081 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7082 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7083 | break; |
| 7084 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7085 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 7086 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 7087 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 7088 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7089 | break; |
| 7090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7091 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 7092 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7093 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7094 | break; |
| 7095 | |
| 7096 | /* 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] | 7097 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 7098 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 7099 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 7100 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 7101 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7102 | usage = 0; |
| 7103 | } |
| 7104 | } |
| 7105 | else |
| 7106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7107 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 7108 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7109 | } |
| 7110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7111 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7112 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7113 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7114 | ret = -1; |
| 7115 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7117 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7118 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7119 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 7120 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7121 | } |
| 7122 | else |
| 7123 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7124 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 7125 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7126 | } |
| 7127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7128 | 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] | 7129 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7130 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7131 | ret = -1; |
| 7132 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7133 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7134 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7135 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7136 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 7137 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7138 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 7139 | { |
| 7140 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7141 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7142 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7143 | |
| 7144 | switch( md ) |
| 7145 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 7146 | #if defined(MBEDTLS_SHA384_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7147 | case MBEDTLS_SSL_HASH_SHA384: |
| 7148 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 7149 | break; |
| 7150 | #endif |
| 7151 | #if defined(MBEDTLS_SHA256_C) |
| 7152 | case MBEDTLS_SSL_HASH_SHA256: |
| 7153 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 7154 | break; |
| 7155 | #endif |
| 7156 | default: |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7157 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7158 | } |
| 7159 | |
| 7160 | return 0; |
| 7161 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7162 | (void) ssl; |
| 7163 | (void) md; |
| 7164 | |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 7165 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 7166 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7167 | } |
| 7168 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 7169 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7170 | |
| 7171 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7172 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7173 | unsigned char *hash, size_t *hashlen, |
| 7174 | unsigned char *data, size_t data_len, |
| 7175 | mbedtls_md_type_t md_alg ) |
| 7176 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7177 | psa_status_t status; |
Jaeden Amero | 3497323 | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 7178 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7179 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7180 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7181 | 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] | 7182 | |
| 7183 | if( ( status = psa_hash_setup( &hash_operation, |
| 7184 | hash_alg ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7185 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7186 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7187 | goto exit; |
| 7188 | } |
| 7189 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7190 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7191 | 64 ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7192 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7193 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7194 | goto exit; |
| 7195 | } |
| 7196 | |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7197 | if( ( status = psa_hash_update( &hash_operation, |
| 7198 | data, data_len ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7199 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7200 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7201 | goto exit; |
| 7202 | } |
| 7203 | |
Ronald Cron | 69a6342 | 2021-10-18 09:47:58 +0200 | [diff] [blame] | 7204 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7205 | hashlen ) ) != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7206 | { |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7207 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7208 | goto exit; |
| 7209 | } |
| 7210 | |
| 7211 | exit: |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7212 | if( status != PSA_SUCCESS ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7213 | { |
| 7214 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7215 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7216 | switch( status ) |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7217 | { |
| 7218 | case PSA_ERROR_NOT_SUPPORTED: |
| 7219 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Andrzej Kurek | 814feff | 2019-01-14 04:35:19 -0500 | [diff] [blame] | 7220 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7221 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7222 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7223 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7224 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7225 | default: |
TRodziewicz | b579ccd | 2021-04-13 14:28:28 +0200 | [diff] [blame] | 7226 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7227 | } |
| 7228 | } |
| 7229 | return( 0 ); |
| 7230 | } |
| 7231 | |
| 7232 | #else |
| 7233 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7234 | 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] | 7235 | unsigned char *hash, size_t *hashlen, |
| 7236 | unsigned char *data, size_t data_len, |
| 7237 | mbedtls_md_type_t md_alg ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7238 | { |
| 7239 | int ret = 0; |
| 7240 | mbedtls_md_context_t ctx; |
| 7241 | 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] | 7242 | *hashlen = mbedtls_md_get_size( md_info ); |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7243 | |
Hanno Becker | 4c8c7aa | 2019-04-10 09:25:41 +0100 | [diff] [blame] | 7244 | 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] | 7245 | |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7246 | mbedtls_md_init( &ctx ); |
| 7247 | |
| 7248 | /* |
| 7249 | * digitally-signed struct { |
| 7250 | * opaque client_random[32]; |
| 7251 | * opaque server_random[32]; |
| 7252 | * ServerDHParams params; |
| 7253 | * }; |
| 7254 | */ |
| 7255 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7256 | { |
| 7257 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7258 | goto exit; |
| 7259 | } |
| 7260 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7261 | { |
| 7262 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7263 | goto exit; |
| 7264 | } |
| 7265 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7266 | { |
| 7267 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7268 | goto exit; |
| 7269 | } |
| 7270 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7271 | { |
| 7272 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7273 | goto exit; |
| 7274 | } |
Gilles Peskine | ca1d742 | 2018-04-24 11:53:22 +0200 | [diff] [blame] | 7275 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7276 | { |
| 7277 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7278 | goto exit; |
| 7279 | } |
| 7280 | |
| 7281 | exit: |
| 7282 | mbedtls_md_free( &ctx ); |
| 7283 | |
| 7284 | if( ret != 0 ) |
| 7285 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7286 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7287 | |
| 7288 | return( ret ); |
| 7289 | } |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 7290 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7291 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 7292 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Andres Amaya Garcia | 46f5a3e | 2017-07-20 16:17:51 +0100 | [diff] [blame] | 7293 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7294 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7295 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 7296 | const mbedtls_md_type_t md, |
| 7297 | unsigned char *dst, |
| 7298 | size_t dst_len, |
| 7299 | size_t *olen ) |
| 7300 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 7301 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7302 | psa_hash_operation_t *hash_operation_to_clone; |
| 7303 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 7304 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7305 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 7306 | |
| 7307 | switch( md ) |
| 7308 | { |
| 7309 | #if defined(MBEDTLS_SHA384_C) |
| 7310 | case MBEDTLS_MD_SHA384: |
| 7311 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 7312 | break; |
| 7313 | #endif |
| 7314 | |
| 7315 | #if defined(MBEDTLS_SHA256_C) |
| 7316 | case MBEDTLS_MD_SHA256: |
| 7317 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 7318 | break; |
| 7319 | #endif |
| 7320 | |
| 7321 | default: |
| 7322 | goto exit; |
| 7323 | } |
| 7324 | |
| 7325 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 7326 | if( status != PSA_SUCCESS ) |
| 7327 | goto exit; |
| 7328 | |
| 7329 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 7330 | if( status != PSA_SUCCESS ) |
| 7331 | goto exit; |
| 7332 | |
| 7333 | exit: |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 7334 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7335 | } |
| 7336 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7337 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7338 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7339 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 7340 | unsigned char *dst, |
| 7341 | size_t dst_len, |
| 7342 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7343 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7344 | int ret; |
| 7345 | mbedtls_sha512_context sha512; |
| 7346 | |
| 7347 | if( dst_len < 48 ) |
| 7348 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7349 | |
| 7350 | mbedtls_sha512_init( &sha512 ); |
| 7351 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 7352 | |
| 7353 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 7354 | { |
| 7355 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 7356 | goto exit; |
| 7357 | } |
| 7358 | |
| 7359 | *olen = 48; |
| 7360 | |
| 7361 | exit: |
| 7362 | |
| 7363 | mbedtls_sha512_free( &sha512 ); |
| 7364 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7365 | } |
| 7366 | #endif /* MBEDTLS_SHA384_C */ |
| 7367 | |
| 7368 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7369 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 7370 | unsigned char *dst, |
| 7371 | size_t dst_len, |
| 7372 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7373 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7374 | int ret; |
| 7375 | mbedtls_sha256_context sha256; |
| 7376 | |
| 7377 | if( dst_len < 32 ) |
| 7378 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7379 | |
| 7380 | mbedtls_sha256_init( &sha256 ); |
| 7381 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7382 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7383 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 7384 | { |
| 7385 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 7386 | goto exit; |
| 7387 | } |
| 7388 | |
| 7389 | *olen = 32; |
| 7390 | |
| 7391 | exit: |
| 7392 | |
| 7393 | mbedtls_sha256_free( &sha256 ); |
| 7394 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7395 | } |
| 7396 | #endif /* MBEDTLS_SHA256_C */ |
| 7397 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 7398 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 7399 | const mbedtls_md_type_t md, |
| 7400 | unsigned char *dst, |
| 7401 | size_t dst_len, |
| 7402 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7403 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7404 | switch( md ) |
| 7405 | { |
| 7406 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7407 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7408 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7409 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 7410 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7411 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7412 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7413 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7414 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 7415 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 7416 | |
| 7417 | default: |
| 7418 | break; |
| 7419 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7420 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7421 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 7422 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 7423 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 7424 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7425 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \ |
| 7426 | defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 7427 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7428 | /* |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7429 | * Function for writing a supported groups (TLS 1.3) or supported elliptic |
| 7430 | * curves (TLS 1.2) extension. |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7431 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7432 | * The "extension_data" field of a supported groups extension contains a |
| 7433 | * "NamedGroupList" value (TLS 1.3 RFC8446): |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7434 | * enum { |
| 7435 | * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), |
| 7436 | * x25519(0x001D), x448(0x001E), |
| 7437 | * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), |
| 7438 | * ffdhe6144(0x0103), ffdhe8192(0x0104), |
| 7439 | * ffdhe_private_use(0x01FC..0x01FF), |
| 7440 | * ecdhe_private_use(0xFE00..0xFEFF), |
| 7441 | * (0xFFFF) |
| 7442 | * } NamedGroup; |
| 7443 | * struct { |
| 7444 | * NamedGroup named_group_list<2..2^16-1>; |
| 7445 | * } NamedGroupList; |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7446 | * |
| 7447 | * The "extension_data" field of a supported elliptic curves extension contains |
| 7448 | * a "NamedCurveList" value (TLS 1.2 RFC 8422): |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7449 | * enum { |
| 7450 | * deprecated(1..22), |
| 7451 | * secp256r1 (23), secp384r1 (24), secp521r1 (25), |
| 7452 | * x25519(29), x448(30), |
| 7453 | * reserved (0xFE00..0xFEFF), |
| 7454 | * deprecated(0xFF01..0xFF02), |
| 7455 | * (0xFFFF) |
| 7456 | * } NamedCurve; |
| 7457 | * struct { |
| 7458 | * NamedCurve named_curve_list<2..2^16-1> |
| 7459 | * } NamedCurveList; |
| 7460 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7461 | * The TLS 1.3 supported groups extension was defined to be a compatible |
| 7462 | * generalization of the TLS 1.2 supported elliptic curves extension. They both |
| 7463 | * share the same extension identifier. |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7464 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7465 | * DHE groups are not supported yet. |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7466 | */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7467 | int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
| 7468 | unsigned char *buf, |
Jerry Yu | 1753261 | 2021-12-20 22:32:09 +0800 | [diff] [blame] | 7469 | const unsigned char *end, |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7470 | size_t *out_len ) |
| 7471 | { |
| 7472 | unsigned char *p = buf ; |
| 7473 | unsigned char *named_group_list; /* Start of named_group_list */ |
| 7474 | size_t named_group_list_len; /* Length of named_group_list */ |
| 7475 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
| 7476 | |
| 7477 | *out_len = 0; |
Jerry Yu | f46b016 | 2022-01-11 16:28:00 +0800 | [diff] [blame] | 7478 | |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7479 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); |
| 7480 | |
| 7481 | /* Check if we have space for header and length fields: |
| 7482 | * - extension_type (2 bytes) |
| 7483 | * - extension_data_length (2 bytes) |
| 7484 | * - named_group_list_length (2 bytes) |
| 7485 | */ |
| 7486 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 7487 | p += 6; |
| 7488 | |
| 7489 | named_group_list = p; |
| 7490 | |
| 7491 | if( group_list == NULL ) |
| 7492 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 7493 | |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7494 | for( ; *group_list != 0; group_list++ ) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7495 | { |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7496 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) ); |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7497 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7498 | #if defined(MBEDTLS_ECP_C) |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7499 | if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) && |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7500 | mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) || |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7501 | ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) && |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7502 | mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) ) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7503 | { |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7504 | const mbedtls_ecp_curve_info *curve_info; |
| 7505 | curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list ); |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7506 | if( curve_info == NULL ) |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 7507 | continue; |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 7508 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 7509 | MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 ); |
| 7510 | p += 2; |
| 7511 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", |
| 7512 | curve_info->name, *group_list ) ); |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7513 | } |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 7514 | #endif /* MBEDTLS_ECP_C */ |
| 7515 | /* Add DHE groups here */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7516 | |
| 7517 | } |
| 7518 | |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 7519 | /* Length of named_group_list */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7520 | named_group_list_len = p - named_group_list; |
| 7521 | if( named_group_list_len == 0 ) |
| 7522 | { |
| 7523 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) ); |
| 7524 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7525 | } |
| 7526 | |
| 7527 | /* Write extension_type */ |
| 7528 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); |
| 7529 | /* Write extension_data_length */ |
| 7530 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); |
| 7531 | /* Write length of named_group_list */ |
| 7532 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); |
| 7533 | |
Jerry Yu | 7f029d8 | 2022-01-11 11:08:53 +0800 | [diff] [blame] | 7534 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", |
| 7535 | buf + 4, named_group_list_len + 2 ); |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7536 | |
| 7537 | *out_len = p - buf; |
| 7538 | |
| 7539 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 7540 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 7541 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7542 | |
| 7543 | return( 0 ); |
| 7544 | } |
| 7545 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 7546 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED || |
| 7547 | MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 7548 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 7549 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7550 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7551 | /* |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7552 | * Function for writing a signature algorithm extension. |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7553 | * |
Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 7554 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7555 | * value (TLS 1.3 RFC8446): |
| 7556 | * enum { |
| 7557 | * .... |
| 7558 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 7559 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 7560 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 7561 | * .... |
| 7562 | * } SignatureScheme; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7563 | * |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7564 | * struct { |
| 7565 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 7566 | * } SignatureSchemeList; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7567 | * |
Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 7568 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 7569 | * value (TLS 1.2 RFC5246): |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 7570 | * enum { |
| 7571 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 7572 | * sha512(6), (255) |
| 7573 | * } HashAlgorithm; |
| 7574 | * |
| 7575 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 7576 | * SignatureAlgorithm; |
| 7577 | * |
| 7578 | * struct { |
| 7579 | * HashAlgorithm hash; |
| 7580 | * SignatureAlgorithm signature; |
| 7581 | * } SignatureAndHashAlgorithm; |
| 7582 | * |
| 7583 | * SignatureAndHashAlgorithm |
| 7584 | * supported_signature_algorithms<2..2^16-2>; |
| 7585 | * |
| 7586 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 7587 | * generalization of the TLS 1.2 signature algorithm extension. |
| 7588 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 7589 | * `SignatureScheme` field of TLS 1.3 |
| 7590 | * |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7591 | */ |
| 7592 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 7593 | const unsigned char *end, size_t *out_len ) |
| 7594 | { |
| 7595 | unsigned char *p = buf; |
| 7596 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 7597 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 7598 | |
| 7599 | *out_len = 0; |
| 7600 | |
| 7601 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 7602 | |
| 7603 | /* Check if we have space for header and length field: |
| 7604 | * - extension_type (2 bytes) |
| 7605 | * - extension_data_length (2 bytes) |
| 7606 | * - supported_signature_algorithms_length (2 bytes) |
| 7607 | */ |
| 7608 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 7609 | p += 6; |
| 7610 | |
| 7611 | /* |
| 7612 | * Write supported_signature_algorithms |
| 7613 | */ |
| 7614 | supported_sig_alg = p; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 7615 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 7616 | if( sig_alg == NULL ) |
| 7617 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 7618 | |
| 7619 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7620 | { |
Jerry Yu | 1bab301 | 2022-01-19 17:43:22 +0800 | [diff] [blame] | 7621 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 7622 | continue; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7623 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 7624 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 7625 | p += 2; |
| 7626 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); |
| 7627 | } |
| 7628 | |
| 7629 | /* Length of supported_signature_algorithms */ |
| 7630 | supported_sig_alg_len = p - supported_sig_alg; |
| 7631 | if( supported_sig_alg_len == 0 ) |
| 7632 | { |
| 7633 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 7634 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7635 | } |
| 7636 | |
| 7637 | /* Write extension_type */ |
| 7638 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
| 7639 | /* Write extension_data_length */ |
| 7640 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
| 7641 | /* Write length of supported_signature_algorithms */ |
| 7642 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 7643 | |
| 7644 | /* Output the total length of signature algorithms extension. */ |
| 7645 | *out_len = p - buf; |
| 7646 | |
| 7647 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 7648 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 7649 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7650 | return( 0 ); |
| 7651 | } |
| 7652 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 7653 | |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 7654 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 7655 | int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl, |
| 7656 | unsigned char *buf, |
| 7657 | const unsigned char *end, |
| 7658 | size_t *olen ) |
| 7659 | { |
| 7660 | unsigned char *p = buf; |
| 7661 | size_t hostname_len; |
| 7662 | |
| 7663 | *olen = 0; |
| 7664 | |
| 7665 | if( ssl->hostname == NULL ) |
| 7666 | return( 0 ); |
| 7667 | |
| 7668 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 7669 | ( "client hello, adding server name extension: %s", |
| 7670 | ssl->hostname ) ); |
| 7671 | |
| 7672 | hostname_len = strlen( ssl->hostname ); |
| 7673 | |
| 7674 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 ); |
| 7675 | |
| 7676 | /* |
| 7677 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) |
| 7678 | * |
| 7679 | * In order to provide any of the server names, clients MAY include an |
| 7680 | * extension of type "server_name" in the (extended) client hello. The |
| 7681 | * "extension_data" field of this extension SHALL contain |
| 7682 | * "ServerNameList" where: |
| 7683 | * |
| 7684 | * struct { |
| 7685 | * NameType name_type; |
| 7686 | * select (name_type) { |
| 7687 | * case host_name: HostName; |
| 7688 | * } name; |
| 7689 | * } ServerName; |
| 7690 | * |
| 7691 | * enum { |
| 7692 | * host_name(0), (255) |
| 7693 | * } NameType; |
| 7694 | * |
| 7695 | * opaque HostName<1..2^16-1>; |
| 7696 | * |
| 7697 | * struct { |
| 7698 | * ServerName server_name_list<1..2^16-1> |
| 7699 | * } ServerNameList; |
| 7700 | * |
| 7701 | */ |
| 7702 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 ); |
| 7703 | p += 2; |
| 7704 | |
| 7705 | MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 ); |
| 7706 | p += 2; |
| 7707 | |
| 7708 | MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 ); |
| 7709 | p += 2; |
| 7710 | |
| 7711 | *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); |
| 7712 | |
| 7713 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 7714 | p += 2; |
| 7715 | |
| 7716 | memcpy( p, ssl->hostname, hostname_len ); |
| 7717 | |
| 7718 | *olen = hostname_len + 9; |
| 7719 | |
| 7720 | return( 0 ); |
| 7721 | } |
| 7722 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 7723 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame^] | 7724 | |
| 7725 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 7726 | |
| 7727 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7728 | |
| 7729 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 7730 | mbedtls_svc_key_id_t key, |
| 7731 | psa_algorithm_t alg, |
| 7732 | const unsigned char* seed, size_t seed_length, |
| 7733 | const unsigned char* label, size_t label_length, |
| 7734 | size_t capacity ) |
| 7735 | { |
| 7736 | psa_status_t status; |
| 7737 | |
| 7738 | status = psa_key_derivation_setup( derivation, alg ); |
| 7739 | if( status != PSA_SUCCESS ) |
| 7740 | return( status ); |
| 7741 | |
| 7742 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 7743 | { |
| 7744 | status = psa_key_derivation_input_bytes( derivation, |
| 7745 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 7746 | seed, seed_length ); |
| 7747 | if( status != PSA_SUCCESS ) |
| 7748 | return( status ); |
| 7749 | |
| 7750 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 7751 | { |
| 7752 | status = psa_key_derivation_input_bytes( |
| 7753 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 7754 | NULL, 0 ); |
| 7755 | } |
| 7756 | else |
| 7757 | { |
| 7758 | status = psa_key_derivation_input_key( |
| 7759 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 7760 | } |
| 7761 | if( status != PSA_SUCCESS ) |
| 7762 | return( status ); |
| 7763 | |
| 7764 | status = psa_key_derivation_input_bytes( derivation, |
| 7765 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 7766 | label, label_length ); |
| 7767 | if( status != PSA_SUCCESS ) |
| 7768 | return( status ); |
| 7769 | } |
| 7770 | else |
| 7771 | { |
| 7772 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 7773 | } |
| 7774 | |
| 7775 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 7776 | if( status != PSA_SUCCESS ) |
| 7777 | return( status ); |
| 7778 | |
| 7779 | return( PSA_SUCCESS ); |
| 7780 | } |
| 7781 | |
| 7782 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 7783 | const unsigned char *secret, size_t slen, |
| 7784 | const char *label, |
| 7785 | const unsigned char *random, size_t rlen, |
| 7786 | unsigned char *dstbuf, size_t dlen ) |
| 7787 | { |
| 7788 | psa_status_t status; |
| 7789 | psa_algorithm_t alg; |
| 7790 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7791 | psa_key_derivation_operation_t derivation = |
| 7792 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7793 | |
| 7794 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 7795 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 7796 | else |
| 7797 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 7798 | |
| 7799 | /* Normally a "secret" should be long enough to be impossible to |
| 7800 | * find by brute force, and in particular should not be empty. But |
| 7801 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 7802 | * and for this use case it makes sense to have a 0-length "secret". |
| 7803 | * Since the key API doesn't allow importing a key of length 0, |
| 7804 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 7805 | * to mean a 0-length "secret" input. */ |
| 7806 | if( slen != 0 ) |
| 7807 | { |
| 7808 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 7809 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7810 | psa_set_key_algorithm( &key_attributes, alg ); |
| 7811 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 7812 | |
| 7813 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 7814 | if( status != PSA_SUCCESS ) |
| 7815 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 7816 | } |
| 7817 | |
| 7818 | status = setup_psa_key_derivation( &derivation, |
| 7819 | master_key, alg, |
| 7820 | random, rlen, |
| 7821 | (unsigned char const *) label, |
| 7822 | (size_t) strlen( label ), |
| 7823 | dlen ); |
| 7824 | if( status != PSA_SUCCESS ) |
| 7825 | { |
| 7826 | psa_key_derivation_abort( &derivation ); |
| 7827 | psa_destroy_key( master_key ); |
| 7828 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 7829 | } |
| 7830 | |
| 7831 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 7832 | if( status != PSA_SUCCESS ) |
| 7833 | { |
| 7834 | psa_key_derivation_abort( &derivation ); |
| 7835 | psa_destroy_key( master_key ); |
| 7836 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 7837 | } |
| 7838 | |
| 7839 | status = psa_key_derivation_abort( &derivation ); |
| 7840 | if( status != PSA_SUCCESS ) |
| 7841 | { |
| 7842 | psa_destroy_key( master_key ); |
| 7843 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 7844 | } |
| 7845 | |
| 7846 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 7847 | status = psa_destroy_key( master_key ); |
| 7848 | if( status != PSA_SUCCESS ) |
| 7849 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 7850 | |
| 7851 | return( 0 ); |
| 7852 | } |
| 7853 | |
| 7854 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7855 | |
| 7856 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 7857 | const unsigned char *secret, size_t slen, |
| 7858 | const char *label, |
| 7859 | const unsigned char *random, size_t rlen, |
| 7860 | unsigned char *dstbuf, size_t dlen ) |
| 7861 | { |
| 7862 | size_t nb; |
| 7863 | size_t i, j, k, md_len; |
| 7864 | unsigned char *tmp; |
| 7865 | size_t tmp_len = 0; |
| 7866 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 7867 | const mbedtls_md_info_t *md_info; |
| 7868 | mbedtls_md_context_t md_ctx; |
| 7869 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7870 | |
| 7871 | mbedtls_md_init( &md_ctx ); |
| 7872 | |
| 7873 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 7874 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7875 | |
| 7876 | md_len = mbedtls_md_get_size( md_info ); |
| 7877 | |
| 7878 | tmp_len = md_len + strlen( label ) + rlen; |
| 7879 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 7880 | if( tmp == NULL ) |
| 7881 | { |
| 7882 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 7883 | goto exit; |
| 7884 | } |
| 7885 | |
| 7886 | nb = strlen( label ); |
| 7887 | memcpy( tmp + md_len, label, nb ); |
| 7888 | memcpy( tmp + md_len + nb, random, rlen ); |
| 7889 | nb += rlen; |
| 7890 | |
| 7891 | /* |
| 7892 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 7893 | */ |
| 7894 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 7895 | goto exit; |
| 7896 | |
| 7897 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 7898 | if( ret != 0 ) |
| 7899 | goto exit; |
| 7900 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 7901 | if( ret != 0 ) |
| 7902 | goto exit; |
| 7903 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 7904 | if( ret != 0 ) |
| 7905 | goto exit; |
| 7906 | |
| 7907 | for( i = 0; i < dlen; i += md_len ) |
| 7908 | { |
| 7909 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 7910 | if( ret != 0 ) |
| 7911 | goto exit; |
| 7912 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 7913 | if( ret != 0 ) |
| 7914 | goto exit; |
| 7915 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 7916 | if( ret != 0 ) |
| 7917 | goto exit; |
| 7918 | |
| 7919 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 7920 | if( ret != 0 ) |
| 7921 | goto exit; |
| 7922 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 7923 | if( ret != 0 ) |
| 7924 | goto exit; |
| 7925 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 7926 | if( ret != 0 ) |
| 7927 | goto exit; |
| 7928 | |
| 7929 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 7930 | |
| 7931 | for( j = 0; j < k; j++ ) |
| 7932 | dstbuf[i + j] = h_i[j]; |
| 7933 | } |
| 7934 | |
| 7935 | exit: |
| 7936 | mbedtls_md_free( &md_ctx ); |
| 7937 | |
| 7938 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 7939 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 7940 | |
| 7941 | mbedtls_free( tmp ); |
| 7942 | |
| 7943 | return( ret ); |
| 7944 | } |
| 7945 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7946 | |
| 7947 | #if defined(MBEDTLS_SHA256_C) |
| 7948 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 7949 | const char *label, |
| 7950 | const unsigned char *random, size_t rlen, |
| 7951 | unsigned char *dstbuf, size_t dlen ) |
| 7952 | { |
| 7953 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 7954 | label, random, rlen, dstbuf, dlen ) ); |
| 7955 | } |
| 7956 | #endif /* MBEDTLS_SHA256_C */ |
| 7957 | |
| 7958 | #if defined(MBEDTLS_SHA384_C) |
| 7959 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 7960 | const char *label, |
| 7961 | const unsigned char *random, size_t rlen, |
| 7962 | unsigned char *dstbuf, size_t dlen ) |
| 7963 | { |
| 7964 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 7965 | label, random, rlen, dstbuf, dlen ) ); |
| 7966 | } |
| 7967 | #endif /* MBEDTLS_SHA384_C */ |
| 7968 | |
| 7969 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7970 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7971 | #endif /* MBEDTLS_SSL_TLS_C */ |