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 | |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 405 | int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf, |
| 406 | const unsigned char *secret, size_t slen, |
| 407 | const char *label, |
| 408 | const unsigned char *random, size_t rlen, |
| 409 | unsigned char *dstbuf, size_t dlen ) |
| 410 | { |
| 411 | mbedtls_ssl_tls_prf_cb *tls_prf = NULL; |
| 412 | |
| 413 | switch( prf ) |
| 414 | { |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 415 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 416 | #if defined(MBEDTLS_SHA384_C) |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 417 | case MBEDTLS_SSL_TLS_PRF_SHA384: |
| 418 | tls_prf = tls_prf_sha384; |
| 419 | break; |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 420 | #endif /* MBEDTLS_SHA384_C */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 421 | #if defined(MBEDTLS_SHA256_C) |
| 422 | case MBEDTLS_SSL_TLS_PRF_SHA256: |
| 423 | tls_prf = tls_prf_sha256; |
| 424 | break; |
Ron Eldor | d2f25f7 | 2019-05-15 14:54:22 +0300 | [diff] [blame] | 425 | #endif /* MBEDTLS_SHA256_C */ |
| 426 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 51d3ab5 | 2019-05-12 14:54:30 +0300 | [diff] [blame] | 427 | default: |
| 428 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 429 | } |
| 430 | |
| 431 | return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) ); |
| 432 | } |
| 433 | |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 434 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 435 | static void ssl_clear_peer_cert( mbedtls_ssl_session *session ) |
| 436 | { |
| 437 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 438 | if( session->peer_cert != NULL ) |
| 439 | { |
| 440 | mbedtls_x509_crt_free( session->peer_cert ); |
| 441 | mbedtls_free( session->peer_cert ); |
| 442 | session->peer_cert = NULL; |
| 443 | } |
| 444 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 445 | if( session->peer_cert_digest != NULL ) |
| 446 | { |
| 447 | /* Zeroization is not necessary. */ |
| 448 | mbedtls_free( session->peer_cert_digest ); |
| 449 | session->peer_cert_digest = NULL; |
| 450 | session->peer_cert_digest_type = MBEDTLS_MD_NONE; |
| 451 | session->peer_cert_digest_len = 0; |
| 452 | } |
| 453 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 454 | } |
| 455 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 456 | |
| 457 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 458 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
| 459 | { |
| 460 | ((void) ciphersuite_info); |
| 461 | |
| 462 | #if defined(MBEDTLS_SHA384_C) |
| 463 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 464 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 465 | else |
| 466 | #endif |
| 467 | #if defined(MBEDTLS_SHA256_C) |
| 468 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
| 469 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
| 470 | else |
| 471 | #endif |
| 472 | { |
| 473 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 474 | return; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
| 479 | { |
| 480 | ((void) ssl); |
| 481 | #if defined(MBEDTLS_SHA256_C) |
| 482 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 483 | psa_hash_abort( &ssl->handshake->fin_sha256_psa ); |
| 484 | psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 485 | #else |
| 486 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
| 487 | #endif |
| 488 | #endif |
| 489 | #if defined(MBEDTLS_SHA384_C) |
| 490 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 491 | psa_hash_abort( &ssl->handshake->fin_sha384_psa ); |
| 492 | psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
| 493 | #else |
| 494 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); |
| 495 | #endif |
| 496 | #endif |
| 497 | } |
| 498 | |
| 499 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
| 500 | const unsigned char *buf, size_t len ) |
| 501 | { |
| 502 | #if defined(MBEDTLS_SHA256_C) |
| 503 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 504 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 505 | #else |
| 506 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 507 | #endif |
| 508 | #endif |
| 509 | #if defined(MBEDTLS_SHA384_C) |
| 510 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 511 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 512 | #else |
| 513 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 514 | #endif |
| 515 | #endif |
| 516 | } |
| 517 | |
| 518 | #if defined(MBEDTLS_SHA256_C) |
| 519 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
| 520 | const unsigned char *buf, size_t len ) |
| 521 | { |
| 522 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 523 | psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); |
| 524 | #else |
| 525 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
| 526 | #endif |
| 527 | } |
| 528 | #endif |
| 529 | |
| 530 | #if defined(MBEDTLS_SHA384_C) |
| 531 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
| 532 | const unsigned char *buf, size_t len ) |
| 533 | { |
| 534 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 535 | psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); |
| 536 | #else |
| 537 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
| 538 | #endif |
| 539 | } |
| 540 | #endif |
| 541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 543 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 545 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 547 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 548 | handshake->fin_sha256_psa = psa_hash_operation_init(); |
| 549 | psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); |
| 550 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 551 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 552 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 553 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 554 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 555 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 556 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 557 | handshake->fin_sha384_psa = psa_hash_operation_init(); |
| 558 | psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 559 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 561 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 562 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 563 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 564 | |
| 565 | handshake->update_checksum = ssl_update_checksum_start; |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 566 | |
| 567 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 568 | defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 569 | mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs ); |
| 570 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 571 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | #if defined(MBEDTLS_DHM_C) |
| 573 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 574 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | #if defined(MBEDTLS_ECDH_C) |
| 576 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 577 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 578 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 579 | mbedtls_ecjpake_init( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 580 | #if defined(MBEDTLS_SSL_CLI_C) |
| 581 | handshake->ecjpake_cache = NULL; |
| 582 | handshake->ecjpake_cache_len = 0; |
| 583 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 584 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 585 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 586 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 587 | mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx ); |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 588 | #endif |
| 589 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 590 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 591 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 592 | #endif |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 593 | |
| 594 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 595 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 596 | mbedtls_pk_init( &handshake->peer_pubkey ); |
| 597 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 600 | void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 601 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 603 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 604 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 605 | transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 606 | transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 607 | #else |
| 608 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 609 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 610 | #endif |
| 611 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 612 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 614 | mbedtls_md_init( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 615 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 616 | } |
| 617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 619 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 624 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 625 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 626 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 627 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 628 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 630 | if( ssl->handshake ) |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 631 | mbedtls_ssl_handshake_free( ssl ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 635 | * Now allocate missing structures. |
| 636 | */ |
| 637 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 638 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 639 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 640 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 641 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 642 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 643 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 644 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 645 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 646 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 647 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 648 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 649 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 650 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 651 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 652 | /* If the buffers are too small - reallocate */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 653 | |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 654 | handle_buffer_resizing( ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN, |
| 655 | MBEDTLS_SSL_OUT_BUFFER_LEN ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 656 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 657 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 658 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 659 | if( ssl->handshake == NULL || |
| 660 | ssl->transform_negotiate == NULL || |
| 661 | ssl->session_negotiate == NULL ) |
| 662 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 663 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | mbedtls_free( ssl->handshake ); |
| 666 | mbedtls_free( ssl->transform_negotiate ); |
| 667 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 668 | |
| 669 | ssl->handshake = NULL; |
| 670 | ssl->transform_negotiate = NULL; |
| 671 | ssl->session_negotiate = NULL; |
| 672 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 673 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 676 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 678 | mbedtls_ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 679 | ssl_handshake_params_init( ssl->handshake ); |
| 680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 681 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 682 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 683 | { |
| 684 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 685 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 686 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 687 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 688 | else |
| 689 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 690 | |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 691 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 692 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 693 | #endif |
| 694 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 695 | /* |
| 696 | * curve_list is translated to IANA TLS group identifiers here because |
| 697 | * mbedtls_ssl_conf_curves returns void and so can't return |
| 698 | * any error codes. |
| 699 | */ |
| 700 | #if defined(MBEDTLS_ECP_C) |
| 701 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 702 | /* Heap allocate and translate curve_list from internal to IANA group ids */ |
| 703 | if ( ssl->conf->curve_list != NULL ) |
| 704 | { |
| 705 | size_t length; |
| 706 | const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list; |
| 707 | |
| 708 | for( length = 0; ( curve_list[length] != MBEDTLS_ECP_DP_NONE ) && |
| 709 | ( length < MBEDTLS_ECP_DP_MAX ); length++ ) {} |
| 710 | |
| 711 | /* Leave room for zero termination */ |
| 712 | uint16_t *group_list = mbedtls_calloc( length + 1, sizeof(uint16_t) ); |
| 713 | if ( group_list == NULL ) |
| 714 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 715 | |
| 716 | for( size_t i = 0; i < length; i++ ) |
| 717 | { |
| 718 | const mbedtls_ecp_curve_info *info = |
| 719 | mbedtls_ecp_curve_info_from_grp_id( curve_list[i] ); |
| 720 | if ( info == NULL ) |
| 721 | { |
| 722 | mbedtls_free( group_list ); |
| 723 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 724 | } |
| 725 | group_list[i] = info->tls_id; |
| 726 | } |
| 727 | |
| 728 | group_list[length] = 0; |
| 729 | |
| 730 | ssl->handshake->group_list = group_list; |
| 731 | ssl->handshake->group_list_heap_allocated = 1; |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | ssl->handshake->group_list = ssl->conf->group_list; |
| 736 | ssl->handshake->group_list_heap_allocated = 0; |
| 737 | } |
| 738 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 739 | #endif /* MBEDTLS_ECP_C */ |
| 740 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 741 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 742 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 743 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 744 | /* Heap allocate and translate sig_hashes from internal hash identifiers to |
| 745 | signature algorithms IANA identifiers. */ |
| 746 | if ( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) && |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 747 | ssl->conf->sig_hashes != NULL ) |
| 748 | { |
| 749 | const int *md; |
| 750 | const int *sig_hashes = ssl->conf->sig_hashes; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 751 | size_t sig_algs_len = 0; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 752 | uint16_t *p; |
| 753 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 754 | #if defined(static_assert) |
| 755 | static_assert( MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN |
| 756 | <= ( SIZE_MAX - ( 2 * sizeof(uint16_t) ) ), |
| 757 | "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big" ); |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 758 | #endif |
| 759 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 760 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 761 | { |
| 762 | if( mbedtls_ssl_hash_from_md_alg( *md ) == MBEDTLS_SSL_HASH_NONE ) |
| 763 | continue; |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 764 | #if defined(MBEDTLS_ECDSA_C) |
| 765 | sig_algs_len += sizeof( uint16_t ); |
| 766 | #endif |
Jerry Yu | a68dca2 | 2022-01-20 16:28:27 +0800 | [diff] [blame] | 767 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 768 | #if defined(MBEDTLS_RSA_C) |
| 769 | sig_algs_len += sizeof( uint16_t ); |
| 770 | #endif |
| 771 | if( sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN ) |
| 772 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 773 | } |
| 774 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 775 | if( sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 776 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 777 | |
Jerry Yu | b476a44 | 2022-01-21 18:14:45 +0800 | [diff] [blame] | 778 | ssl->handshake->sig_algs = mbedtls_calloc( 1, sig_algs_len + |
| 779 | sizeof( uint16_t )); |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 780 | if( ssl->handshake->sig_algs == NULL ) |
| 781 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 782 | |
| 783 | p = (uint16_t *)ssl->handshake->sig_algs; |
| 784 | for( md = sig_hashes; *md != MBEDTLS_MD_NONE; md++ ) |
| 785 | { |
| 786 | unsigned char hash = mbedtls_ssl_hash_from_md_alg( *md ); |
| 787 | if( hash == MBEDTLS_SSL_HASH_NONE ) |
| 788 | continue; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 789 | #if defined(MBEDTLS_ECDSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 790 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_ECDSA); |
| 791 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 792 | #endif |
| 793 | #if defined(MBEDTLS_RSA_C) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 794 | *p = (( hash << 8 ) | MBEDTLS_SSL_SIG_RSA); |
| 795 | p++; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 796 | #endif |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 797 | } |
| 798 | *p = MBEDTLS_TLS1_3_SIG_NONE; |
| 799 | ssl->handshake->sig_algs_heap_allocated = 1; |
| 800 | } |
| 801 | else |
Jerry Yu | a69269a | 2022-01-17 21:06:01 +0800 | [diff] [blame] | 802 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 803 | { |
| 804 | ssl->handshake->sig_algs = ssl->conf->sig_algs; |
| 805 | ssl->handshake->sig_algs_heap_allocated = 0; |
| 806 | } |
| 807 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 808 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 809 | return( 0 ); |
| 810 | } |
| 811 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 812 | #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] | 813 | /* Dummy cookie callbacks for defaults */ |
| 814 | static int ssl_cookie_write_dummy( void *ctx, |
| 815 | unsigned char **p, unsigned char *end, |
| 816 | const unsigned char *cli_id, size_t cli_id_len ) |
| 817 | { |
| 818 | ((void) ctx); |
| 819 | ((void) p); |
| 820 | ((void) end); |
| 821 | ((void) cli_id); |
| 822 | ((void) cli_id_len); |
| 823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 824 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static int ssl_cookie_check_dummy( void *ctx, |
| 828 | const unsigned char *cookie, size_t cookie_len, |
| 829 | const unsigned char *cli_id, size_t cli_id_len ) |
| 830 | { |
| 831 | ((void) ctx); |
| 832 | ((void) cookie); |
| 833 | ((void) cookie_len); |
| 834 | ((void) cli_id); |
| 835 | ((void) cli_id_len); |
| 836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 838 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 839 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 840 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 841 | /* |
| 842 | * Initialize an SSL context |
| 843 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 844 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 845 | { |
| 846 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 847 | } |
| 848 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 849 | static int ssl_conf_version_check( const mbedtls_ssl_context *ssl ) |
| 850 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 851 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 852 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
| 853 | { |
| 854 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 855 | { |
| 856 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS 1.3 is not yet supported" ) ); |
| 857 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 858 | } |
| 859 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls13 only." ) ); |
| 860 | return( 0 ); |
| 861 | } |
| 862 | #endif |
| 863 | |
| 864 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 865 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 866 | { |
| 867 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "The SSL configuration is tls12 only." ) ); |
| 868 | return( 0 ); |
| 869 | } |
| 870 | #endif |
| 871 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 872 | #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] | 873 | if( mbedtls_ssl_conf_is_hybrid_tls12_tls13( ssl->conf ) ) |
| 874 | { |
| 875 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Hybrid TLS 1.2 + TLS 1.3 configurations are not yet supported" ) ); |
| 876 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 877 | } |
| 878 | #endif |
| 879 | |
| 880 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "The SSL configuration is invalid." ) ); |
| 881 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 882 | } |
| 883 | |
| 884 | static int ssl_conf_check(const mbedtls_ssl_context *ssl) |
| 885 | { |
| 886 | int ret; |
| 887 | ret = ssl_conf_version_check( ssl ); |
| 888 | if( ret != 0 ) |
| 889 | return( ret ); |
| 890 | |
| 891 | /* Space for further checks */ |
| 892 | |
| 893 | return( 0 ); |
| 894 | } |
| 895 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 896 | /* |
| 897 | * Setup an SSL context |
| 898 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 899 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 900 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 901 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 902 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 903 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 904 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 905 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 906 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 907 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 908 | |
Jerry Yu | 60835a8 | 2021-08-04 10:13:52 +0800 | [diff] [blame] | 909 | if( ( ret = ssl_conf_check( ssl ) ) != 0 ) |
| 910 | return( ret ); |
| 911 | |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 912 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 913 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 914 | */ |
k-stachowiak | c9a5f02 | 2018-07-24 13:53:31 +0200 | [diff] [blame] | 915 | |
| 916 | /* Set to NULL in case of an error condition */ |
| 917 | ssl->out_buf = NULL; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 918 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 919 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 920 | ssl->in_buf_len = in_buf_len; |
| 921 | #endif |
| 922 | ssl->in_buf = mbedtls_calloc( 1, in_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 923 | if( ssl->in_buf == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 924 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 925 | 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] | 926 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 927 | goto error; |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 928 | } |
| 929 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 930 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 931 | ssl->out_buf_len = out_buf_len; |
| 932 | #endif |
| 933 | ssl->out_buf = mbedtls_calloc( 1, out_buf_len ); |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 934 | if( ssl->out_buf == NULL ) |
| 935 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 936 | 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] | 937 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 938 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 941 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 942 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 943 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | 3adb992 | 2017-12-21 10:15:08 +0200 | [diff] [blame] | 944 | memset( &ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info) ); |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 945 | #endif |
| 946 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 947 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 948 | goto error; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 949 | |
| 950 | return( 0 ); |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 951 | |
| 952 | error: |
| 953 | mbedtls_free( ssl->in_buf ); |
| 954 | mbedtls_free( ssl->out_buf ); |
| 955 | |
| 956 | ssl->conf = NULL; |
| 957 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 958 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 959 | ssl->in_buf_len = 0; |
| 960 | ssl->out_buf_len = 0; |
| 961 | #endif |
k-stachowiak | a47911c | 2018-07-04 17:41:58 +0200 | [diff] [blame] | 962 | ssl->in_buf = NULL; |
| 963 | ssl->out_buf = NULL; |
| 964 | |
| 965 | ssl->in_hdr = NULL; |
| 966 | ssl->in_ctr = NULL; |
| 967 | ssl->in_len = NULL; |
| 968 | ssl->in_iv = NULL; |
| 969 | ssl->in_msg = NULL; |
| 970 | |
| 971 | ssl->out_hdr = NULL; |
| 972 | ssl->out_ctr = NULL; |
| 973 | ssl->out_len = NULL; |
| 974 | ssl->out_iv = NULL; |
| 975 | ssl->out_msg = NULL; |
| 976 | |
k-stachowiak | 9f7798e | 2018-07-31 16:52:32 +0200 | [diff] [blame] | 977 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 981 | * Reset an initialized and used SSL context for re-use while retaining |
| 982 | * all application-set variables, function pointers and data. |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 983 | * |
| 984 | * If partial is non-zero, keep data in the input buffer and client ID. |
| 985 | * (Use when a DTLS client reconnects from the same port.) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 986 | */ |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 987 | void mbedtls_ssl_session_reset_msg_layer( mbedtls_ssl_context *ssl, |
| 988 | int partial ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 989 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 990 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 991 | size_t in_buf_len = ssl->in_buf_len; |
| 992 | size_t out_buf_len = ssl->out_buf_len; |
| 993 | #else |
| 994 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 995 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 996 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 997 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 998 | #if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C) |
| 999 | partial = 0; |
Hanno Becker | 7e77213 | 2018-08-10 12:38:21 +0100 | [diff] [blame] | 1000 | #endif |
| 1001 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1002 | /* Cancel any possibly running timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1003 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1004 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1005 | mbedtls_ssl_reset_in_out_pointers( ssl ); |
| 1006 | |
| 1007 | /* Reset incoming message parsing */ |
| 1008 | ssl->in_offt = NULL; |
| 1009 | ssl->nb_zero = 0; |
| 1010 | ssl->in_msgtype = 0; |
| 1011 | ssl->in_msglen = 0; |
| 1012 | ssl->in_hslen = 0; |
| 1013 | ssl->keep_current_message = 0; |
| 1014 | ssl->transform_in = NULL; |
| 1015 | |
| 1016 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1017 | ssl->next_record_offset = 0; |
| 1018 | ssl->in_epoch = 0; |
| 1019 | #endif |
| 1020 | |
| 1021 | /* Keep current datagram if partial == 1 */ |
| 1022 | if( partial == 0 ) |
| 1023 | { |
| 1024 | ssl->in_left = 0; |
| 1025 | memset( ssl->in_buf, 0, in_buf_len ); |
| 1026 | } |
| 1027 | |
| 1028 | /* Reset outgoing message writing */ |
| 1029 | ssl->out_msgtype = 0; |
| 1030 | ssl->out_msglen = 0; |
| 1031 | ssl->out_left = 0; |
| 1032 | memset( ssl->out_buf, 0, out_buf_len ); |
| 1033 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 1034 | ssl->transform_out = NULL; |
| 1035 | |
| 1036 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 1037 | mbedtls_ssl_dtls_replay_reset( ssl ); |
| 1038 | #endif |
| 1039 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1040 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1041 | if( ssl->transform ) |
| 1042 | { |
| 1043 | mbedtls_ssl_transform_free( ssl->transform ); |
| 1044 | mbedtls_free( ssl->transform ); |
| 1045 | ssl->transform = NULL; |
| 1046 | } |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1047 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 1048 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1049 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1050 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 1051 | mbedtls_free( ssl->transform_application ); |
| 1052 | ssl->transform_application = NULL; |
| 1053 | |
| 1054 | if( ssl->handshake != NULL ) |
| 1055 | { |
| 1056 | mbedtls_ssl_transform_free( ssl->handshake->transform_earlydata ); |
| 1057 | mbedtls_free( ssl->handshake->transform_earlydata ); |
| 1058 | ssl->handshake->transform_earlydata = NULL; |
| 1059 | |
| 1060 | mbedtls_ssl_transform_free( ssl->handshake->transform_handshake ); |
| 1061 | mbedtls_free( ssl->handshake->transform_handshake ); |
| 1062 | ssl->handshake->transform_handshake = NULL; |
| 1063 | } |
| 1064 | |
XiaokangQian | 2b01dc3 | 2022-01-21 02:53:13 +0000 | [diff] [blame] | 1065 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial ) |
| 1069 | { |
| 1070 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1071 | |
| 1072 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 1073 | |
XiaokangQian | 78b1fa7 | 2022-01-19 06:56:30 +0000 | [diff] [blame] | 1074 | mbedtls_ssl_session_reset_msg_layer( ssl, partial ); |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1075 | |
| 1076 | /* Reset renegotiation state */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1078 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 1079 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1080 | |
| 1081 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 1083 | 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] | 1084 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1086 | |
Hanno Becker | b0302c4 | 2021-08-03 09:39:42 +0100 | [diff] [blame] | 1087 | ssl->session_in = NULL; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 1088 | ssl->session_out = NULL; |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1089 | if( ssl->session ) |
| 1090 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | mbedtls_ssl_session_free( ssl->session ); |
| 1092 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 1093 | ssl->session = NULL; |
| 1094 | } |
| 1095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1096 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1097 | ssl->alpn_chosen = NULL; |
| 1098 | #endif |
| 1099 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 1100 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1101 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1102 | if( partial == 0 ) |
Hanno Becker | 4ccbf06 | 2018-08-10 11:20:38 +0100 | [diff] [blame] | 1103 | #endif |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1104 | { |
| 1105 | mbedtls_free( ssl->cli_id ); |
| 1106 | ssl->cli_id = NULL; |
| 1107 | ssl->cli_id_len = 0; |
| 1108 | } |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 1109 | #endif |
| 1110 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1111 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 1112 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1113 | |
| 1114 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 1117 | /* |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1118 | * Reset an initialized and used SSL context for re-use while retaining |
| 1119 | * all application-set variables, function pointers and data. |
| 1120 | */ |
| 1121 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
| 1122 | { |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 1123 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | 3f09b6d | 2015-09-08 11:58:14 +0200 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1127 | * SSL set accessors |
| 1128 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1129 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1130 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1131 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 1134 | 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] | 1135 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1136 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1140 | 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] | 1141 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1142 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 1143 | } |
| 1144 | #endif |
| 1145 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1146 | 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] | 1147 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1148 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1149 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 1150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1151 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1152 | |
Hanno Becker | 1841b0a | 2018-08-24 11:13:57 +0100 | [diff] [blame] | 1153 | void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl, |
| 1154 | unsigned allow_packing ) |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 1155 | { |
| 1156 | ssl->disable_datagram_packing = !allow_packing; |
| 1157 | } |
| 1158 | |
| 1159 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, |
| 1160 | uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1161 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1162 | conf->hs_timeout_min = min; |
| 1163 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 1164 | } |
| 1165 | #endif |
| 1166 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1167 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1168 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1169 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1172 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1173 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 1174 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1175 | void *p_vrfy ) |
| 1176 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1177 | conf->f_vrfy = f_vrfy; |
| 1178 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1179 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1180 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 1181 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1182 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1183 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1184 | void *p_rng ) |
| 1185 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1186 | conf->f_rng = f_rng; |
| 1187 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1190 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 1191 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1192 | void *p_dbg ) |
| 1193 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1194 | conf->f_dbg = f_dbg; |
| 1195 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1199 | void *p_bio, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1200 | mbedtls_ssl_send_t *f_send, |
| 1201 | mbedtls_ssl_recv_t *f_recv, |
| 1202 | mbedtls_ssl_recv_timeout_t *f_recv_timeout ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1203 | { |
| 1204 | ssl->p_bio = p_bio; |
| 1205 | ssl->f_send = f_send; |
| 1206 | ssl->f_recv = f_recv; |
| 1207 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Manuel Pégourié-Gonnard | 6e7aaca | 2018-08-20 10:37:23 +0200 | [diff] [blame] | 1210 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1211 | void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu ) |
| 1212 | { |
| 1213 | ssl->mtu = mtu; |
| 1214 | } |
| 1215 | #endif |
| 1216 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1217 | 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] | 1218 | { |
| 1219 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 1220 | } |
| 1221 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1222 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 1223 | void *p_timer, |
Simon Butcher | e846b51 | 2016-03-01 17:31:49 +0000 | [diff] [blame] | 1224 | mbedtls_ssl_set_timer_t *f_set_timer, |
| 1225 | mbedtls_ssl_get_timer_t *f_get_timer ) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1226 | { |
| 1227 | ssl->p_timer = p_timer; |
| 1228 | ssl->f_set_timer = f_set_timer; |
| 1229 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1230 | |
| 1231 | /* Make sure we start with no timer running */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1232 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 1233 | } |
| 1234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1235 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1236 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Hanno Becker | a637ff6 | 2021-04-15 08:42:48 +0100 | [diff] [blame] | 1237 | void *p_cache, |
| 1238 | mbedtls_ssl_cache_get_t *f_get_cache, |
| 1239 | mbedtls_ssl_cache_set_t *f_set_cache ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1240 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 1241 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1242 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1243 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1244 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1247 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1248 | 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] | 1249 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1250 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1251 | |
| 1252 | if( ssl == NULL || |
| 1253 | session == NULL || |
| 1254 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1255 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1256 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1257 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 1260 | if( ssl->handshake->resume == 1 ) |
| 1261 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 1262 | |
Hanno Becker | 52055ae | 2019-02-06 14:30:46 +0000 | [diff] [blame] | 1263 | if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate, |
| 1264 | session ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1265 | return( ret ); |
| 1266 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1267 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 1268 | |
| 1269 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1270 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1271 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1272 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1273 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
| 1274 | const int *ciphersuites ) |
| 1275 | { |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 1276 | conf->ciphersuite_list = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1279 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | cadebe5 | 2021-08-24 10:36:45 +0800 | [diff] [blame] | 1280 | void mbedtls_ssl_conf_tls13_key_exchange_modes( mbedtls_ssl_config *conf, |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1281 | const int kex_modes ) |
| 1282 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1283 | 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] | 1284 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1285 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 1286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1288 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
Nicholas Wilson | 2088e2e | 2015-09-08 16:53:18 +0100 | [diff] [blame] | 1289 | const mbedtls_x509_crt_profile *profile ) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 1290 | { |
| 1291 | conf->cert_profile = profile; |
| 1292 | } |
| 1293 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1294 | /* Append a new keycert entry to a (possibly empty) list */ |
| 1295 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 1296 | mbedtls_x509_crt *cert, |
| 1297 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1298 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1299 | mbedtls_ssl_key_cert *new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1300 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1301 | new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
| 1302 | if( new_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1303 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1304 | |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1305 | new_cert->cert = cert; |
| 1306 | new_cert->key = key; |
| 1307 | new_cert->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1308 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1309 | /* Update head is the list was null, else add to the end */ |
| 1310 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1311 | { |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1312 | *head = new_cert; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 1313 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1314 | else |
| 1315 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1316 | mbedtls_ssl_key_cert *cur = *head; |
| 1317 | while( cur->next != NULL ) |
| 1318 | cur = cur->next; |
niisato | 8ee2422 | 2018-06-25 19:05:48 +0900 | [diff] [blame] | 1319 | cur->next = new_cert; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1322 | return( 0 ); |
| 1323 | } |
| 1324 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1325 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 1326 | mbedtls_x509_crt *own_cert, |
| 1327 | mbedtls_pk_context *pk_key ) |
| 1328 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 1329 | 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] | 1330 | } |
| 1331 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1332 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1333 | mbedtls_x509_crt *ca_chain, |
| 1334 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1335 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1336 | conf->ca_chain = ca_chain; |
| 1337 | conf->ca_crl = ca_crl; |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1338 | |
| 1339 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1340 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1341 | * cannot be used together. */ |
| 1342 | conf->f_ca_cb = NULL; |
| 1343 | conf->p_ca_cb = NULL; |
| 1344 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1345 | } |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1346 | |
| 1347 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 1348 | void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf, |
Hanno Becker | afd0b0a | 2019-03-27 16:55:01 +0000 | [diff] [blame] | 1349 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
Hanno Becker | 5adaad9 | 2019-03-27 16:54:37 +0000 | [diff] [blame] | 1350 | void *p_ca_cb ) |
| 1351 | { |
| 1352 | conf->f_ca_cb = f_ca_cb; |
| 1353 | conf->p_ca_cb = p_ca_cb; |
| 1354 | |
| 1355 | /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb() |
| 1356 | * cannot be used together. */ |
| 1357 | conf->ca_chain = NULL; |
| 1358 | conf->ca_crl = NULL; |
| 1359 | } |
| 1360 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1361 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 1362 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1363 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 1364 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 1365 | mbedtls_x509_crt *own_cert, |
| 1366 | mbedtls_pk_context *pk_key ) |
| 1367 | { |
| 1368 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 1369 | own_cert, pk_key ) ); |
| 1370 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 1371 | |
| 1372 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 1373 | mbedtls_x509_crt *ca_chain, |
| 1374 | mbedtls_x509_crl *ca_crl ) |
| 1375 | { |
| 1376 | ssl->handshake->sni_ca_chain = ca_chain; |
| 1377 | ssl->handshake->sni_ca_crl = ca_crl; |
| 1378 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 1379 | |
| 1380 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 1381 | int authmode ) |
| 1382 | { |
| 1383 | ssl->handshake->sni_authmode = authmode; |
| 1384 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 1385 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 1386 | |
Hanno Becker | 8927c83 | 2019-04-03 12:52:50 +0100 | [diff] [blame] | 1387 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 1388 | void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl, |
| 1389 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 1390 | void *p_vrfy ) |
| 1391 | { |
| 1392 | ssl->f_vrfy = f_vrfy; |
| 1393 | ssl->p_vrfy = p_vrfy; |
| 1394 | } |
| 1395 | #endif |
| 1396 | |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1397 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1398 | /* |
| 1399 | * Set EC J-PAKE password for current handshake |
| 1400 | */ |
| 1401 | int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl, |
| 1402 | const unsigned char *pw, |
| 1403 | size_t pw_len ) |
| 1404 | { |
| 1405 | mbedtls_ecjpake_role role; |
| 1406 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 1407 | if( ssl->handshake == NULL || ssl->conf == NULL ) |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1408 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1409 | |
| 1410 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1411 | role = MBEDTLS_ECJPAKE_SERVER; |
| 1412 | else |
| 1413 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 1414 | |
| 1415 | return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx, |
| 1416 | role, |
| 1417 | MBEDTLS_MD_SHA256, |
| 1418 | MBEDTLS_ECP_DP_SECP256R1, |
| 1419 | pw, pw_len ) ); |
| 1420 | } |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 1421 | #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Manuel Pégourié-Gonnard | 7002f4a | 2015-09-15 12:43:43 +0200 | [diff] [blame] | 1422 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1423 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1424 | |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1425 | static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf ) |
| 1426 | { |
| 1427 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1428 | if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
| 1429 | return( 1 ); |
| 1430 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1431 | |
| 1432 | if( conf->psk != NULL ) |
| 1433 | return( 1 ); |
| 1434 | |
| 1435 | return( 0 ); |
| 1436 | } |
| 1437 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1438 | static void ssl_conf_remove_psk( mbedtls_ssl_config *conf ) |
| 1439 | { |
| 1440 | /* Remove reference to existing PSK, if any. */ |
| 1441 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1442 | if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1443 | { |
| 1444 | /* The maintenance of the PSK key slot is the |
| 1445 | * user's responsibility. */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1446 | conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1447 | } |
Hanno Becker | a63ac3f | 2018-11-05 12:47:16 +0000 | [diff] [blame] | 1448 | /* This and the following branch should never |
| 1449 | * be taken simultaenously as we maintain the |
| 1450 | * invariant that raw and opaque PSKs are never |
| 1451 | * configured simultaneously. As a safeguard, |
| 1452 | * though, `else` is omitted here. */ |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1453 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1454 | if( conf->psk != NULL ) |
| 1455 | { |
| 1456 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
| 1457 | |
| 1458 | mbedtls_free( conf->psk ); |
| 1459 | conf->psk = NULL; |
| 1460 | conf->psk_len = 0; |
| 1461 | } |
| 1462 | |
| 1463 | /* Remove reference to PSK identity, if any. */ |
| 1464 | if( conf->psk_identity != NULL ) |
| 1465 | { |
| 1466 | mbedtls_free( conf->psk_identity ); |
| 1467 | conf->psk_identity = NULL; |
| 1468 | conf->psk_identity_len = 0; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1472 | /* This function assumes that PSK identity in the SSL config is unset. |
| 1473 | * It checks that the provided identity is well-formed and attempts |
| 1474 | * to make a copy of it in the SSL config. |
| 1475 | * On failure, the PSK identity in the config remains unset. */ |
| 1476 | static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf, |
| 1477 | unsigned char const *psk_identity, |
| 1478 | size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 1479 | { |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1480 | /* Identity len will be encoded on two bytes */ |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1481 | if( psk_identity == NULL || |
| 1482 | ( psk_identity_len >> 16 ) != 0 || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 1483 | psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | c6b5d83 | 2015-08-27 16:37:35 +0200 | [diff] [blame] | 1484 | { |
| 1485 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1486 | } |
| 1487 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1488 | conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ); |
| 1489 | if( conf->psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1490 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1491 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1492 | conf->psk_identity_len = psk_identity_len; |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 1493 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 5ad403f | 2013-09-18 21:21:30 +0200 | [diff] [blame] | 1494 | |
| 1495 | return( 0 ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 1496 | } |
| 1497 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1498 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
| 1499 | const unsigned char *psk, size_t psk_len, |
| 1500 | const unsigned char *psk_identity, size_t psk_identity_len ) |
| 1501 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1502 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1503 | |
| 1504 | /* We currently only support one PSK, raw or opaque. */ |
| 1505 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1506 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1507 | |
| 1508 | /* Check and set raw PSK */ |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1509 | if( psk == NULL ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Piotr Nowicki | 9926eaf | 2019-11-20 14:54:36 +0100 | [diff] [blame] | 1511 | if( psk_len == 0 ) |
| 1512 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1513 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1514 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1515 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1516 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
| 1517 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 1518 | conf->psk_len = psk_len; |
| 1519 | memcpy( conf->psk, psk, conf->psk_len ); |
| 1520 | |
| 1521 | /* Check and set PSK Identity */ |
| 1522 | ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len ); |
| 1523 | if( ret != 0 ) |
| 1524 | ssl_conf_remove_psk( conf ); |
| 1525 | |
| 1526 | return( ret ); |
| 1527 | } |
| 1528 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1529 | static void ssl_remove_psk( mbedtls_ssl_context *ssl ) |
| 1530 | { |
| 1531 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1532 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1533 | { |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1534 | ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1535 | } |
| 1536 | else |
| 1537 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1538 | if( ssl->handshake->psk != NULL ) |
| 1539 | { |
| 1540 | mbedtls_platform_zeroize( ssl->handshake->psk, |
| 1541 | ssl->handshake->psk_len ); |
| 1542 | mbedtls_free( ssl->handshake->psk ); |
| 1543 | ssl->handshake->psk_len = 0; |
| 1544 | } |
| 1545 | } |
| 1546 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1547 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 1548 | const unsigned char *psk, size_t psk_len ) |
| 1549 | { |
| 1550 | if( psk == NULL || ssl->handshake == NULL ) |
| 1551 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1552 | |
| 1553 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 1554 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1555 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1556 | ssl_remove_psk( ssl ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1557 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1558 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1559 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1560 | |
| 1561 | ssl->handshake->psk_len = psk_len; |
| 1562 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 1563 | |
| 1564 | return( 0 ); |
| 1565 | } |
| 1566 | |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1567 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1568 | int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf, |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1569 | mbedtls_svc_key_id_t psk, |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1570 | const unsigned char *psk_identity, |
| 1571 | size_t psk_identity_len ) |
| 1572 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1573 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 2ed3dce | 2021-04-19 21:59:14 +0100 | [diff] [blame] | 1574 | |
| 1575 | /* We currently only support one PSK, raw or opaque. */ |
| 1576 | if( ssl_conf_psk_is_configured( conf ) ) |
| 1577 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1578 | |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1579 | /* Check and set opaque PSK */ |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1580 | if( mbedtls_svc_key_id_is_null( psk ) ) |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1581 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 1582 | conf->psk_opaque = psk; |
Hanno Becker | 7390c71 | 2018-11-15 13:33:04 +0000 | [diff] [blame] | 1583 | |
| 1584 | /* Check and set PSK Identity */ |
| 1585 | ret = ssl_conf_set_psk_identity( conf, psk_identity, |
| 1586 | psk_identity_len ); |
| 1587 | if( ret != 0 ) |
| 1588 | ssl_conf_remove_psk( conf ); |
| 1589 | |
| 1590 | return( ret ); |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
Przemyslaw Stekiel | 6928a51 | 2022-02-03 13:50:35 +0100 | [diff] [blame] | 1593 | int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl, |
| 1594 | mbedtls_svc_key_id_t psk ) |
| 1595 | { |
| 1596 | if( ( mbedtls_svc_key_id_is_null( psk ) ) || |
| 1597 | ( ssl->handshake == NULL ) ) |
| 1598 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1599 | |
| 1600 | ssl_remove_psk( ssl ); |
| 1601 | ssl->handshake->psk_opaque = psk; |
| 1602 | return( 0 ); |
| 1603 | } |
| 1604 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1605 | |
| 1606 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
| 1607 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
| 1608 | size_t), |
| 1609 | void *p_psk ) |
| 1610 | { |
| 1611 | conf->f_psk = f_psk; |
| 1612 | conf->p_psk = p_psk; |
| 1613 | } |
| 1614 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 1615 | |
| 1616 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1617 | 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] | 1618 | size_t taglen, |
| 1619 | psa_algorithm_t *alg, |
| 1620 | psa_key_type_t *key_type, |
| 1621 | size_t *key_size ) |
| 1622 | { |
| 1623 | switch ( mbedtls_cipher_type ) |
| 1624 | { |
| 1625 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 1626 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1627 | *key_type = PSA_KEY_TYPE_AES; |
| 1628 | *key_size = 128; |
| 1629 | break; |
| 1630 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 1631 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1632 | *key_type = PSA_KEY_TYPE_AES; |
| 1633 | *key_size = 128; |
| 1634 | break; |
| 1635 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 1636 | *alg = PSA_ALG_GCM; |
| 1637 | *key_type = PSA_KEY_TYPE_AES; |
| 1638 | *key_size = 128; |
| 1639 | break; |
| 1640 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 1641 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1642 | *key_type = PSA_KEY_TYPE_AES; |
| 1643 | *key_size = 192; |
| 1644 | break; |
| 1645 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 1646 | *alg = PSA_ALG_GCM; |
| 1647 | *key_type = PSA_KEY_TYPE_AES; |
| 1648 | *key_size = 192; |
| 1649 | break; |
| 1650 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 1651 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1652 | *key_type = PSA_KEY_TYPE_AES; |
| 1653 | *key_size = 256; |
| 1654 | break; |
| 1655 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 1656 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1657 | *key_type = PSA_KEY_TYPE_AES; |
| 1658 | *key_size = 256; |
| 1659 | break; |
| 1660 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 1661 | *alg = PSA_ALG_GCM; |
| 1662 | *key_type = PSA_KEY_TYPE_AES; |
| 1663 | *key_size = 256; |
| 1664 | break; |
| 1665 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 1666 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1667 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1668 | *key_size = 128; |
| 1669 | break; |
| 1670 | case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 1671 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1672 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1673 | *key_size = 128; |
| 1674 | break; |
| 1675 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 1676 | *alg = PSA_ALG_GCM; |
| 1677 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1678 | *key_size = 128; |
| 1679 | break; |
| 1680 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 1681 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1682 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1683 | *key_size = 192; |
| 1684 | break; |
| 1685 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 1686 | *alg = PSA_ALG_GCM; |
| 1687 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1688 | *key_size = 192; |
| 1689 | break; |
| 1690 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 1691 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1692 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1693 | *key_size = 256; |
| 1694 | break; |
| 1695 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 1696 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1697 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1698 | *key_size = 256; |
| 1699 | break; |
| 1700 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 1701 | *alg = PSA_ALG_GCM; |
| 1702 | *key_type = PSA_KEY_TYPE_ARIA; |
| 1703 | *key_size = 256; |
| 1704 | break; |
| 1705 | case MBEDTLS_CIPHER_CAMELLIA_128_CBC: |
| 1706 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1707 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1708 | *key_size = 128; |
| 1709 | break; |
| 1710 | case MBEDTLS_CIPHER_CAMELLIA_128_CCM: |
| 1711 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1712 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1713 | *key_size = 128; |
| 1714 | break; |
| 1715 | case MBEDTLS_CIPHER_CAMELLIA_128_GCM: |
| 1716 | *alg = PSA_ALG_GCM; |
| 1717 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1718 | *key_size = 128; |
| 1719 | break; |
| 1720 | case MBEDTLS_CIPHER_CAMELLIA_192_CCM: |
| 1721 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1722 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1723 | *key_size = 192; |
| 1724 | break; |
| 1725 | case MBEDTLS_CIPHER_CAMELLIA_192_GCM: |
| 1726 | *alg = PSA_ALG_GCM; |
| 1727 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1728 | *key_size = 192; |
| 1729 | break; |
| 1730 | case MBEDTLS_CIPHER_CAMELLIA_256_CBC: |
| 1731 | *alg = PSA_ALG_CBC_NO_PADDING; |
| 1732 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1733 | *key_size = 256; |
| 1734 | break; |
| 1735 | case MBEDTLS_CIPHER_CAMELLIA_256_CCM: |
| 1736 | *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, taglen ) : PSA_ALG_CCM; |
| 1737 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1738 | *key_size = 256; |
| 1739 | break; |
| 1740 | case MBEDTLS_CIPHER_CAMELLIA_256_GCM: |
| 1741 | *alg = PSA_ALG_GCM; |
| 1742 | *key_type = PSA_KEY_TYPE_CAMELLIA; |
| 1743 | *key_size = 256; |
| 1744 | break; |
| 1745 | case MBEDTLS_CIPHER_CHACHA20_POLY1305: |
| 1746 | *alg = PSA_ALG_CHACHA20_POLY1305; |
| 1747 | *key_type = PSA_KEY_TYPE_CHACHA20; |
| 1748 | *key_size = 256; |
| 1749 | break; |
| 1750 | case MBEDTLS_CIPHER_NULL: |
| 1751 | *alg = MBEDTLS_SSL_NULL_CIPHER; |
| 1752 | *key_type = 0; |
| 1753 | *key_size = 0; |
| 1754 | break; |
| 1755 | default: |
| 1756 | return PSA_ERROR_NOT_SUPPORTED; |
| 1757 | } |
| 1758 | |
| 1759 | return PSA_SUCCESS; |
| 1760 | } |
Hanno Becker | d20a8ca | 2018-10-22 15:31:26 +0100 | [diff] [blame] | 1761 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1762 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1763 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1764 | int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf, |
| 1765 | const unsigned char *dhm_P, size_t P_len, |
| 1766 | const unsigned char *dhm_G, size_t G_len ) |
| 1767 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1768 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1769 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1770 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1771 | mbedtls_mpi_free( &conf->dhm_G ); |
| 1772 | |
Hanno Becker | a90658f | 2017-10-04 15:29:08 +0100 | [diff] [blame] | 1773 | if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 || |
| 1774 | ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 ) |
| 1775 | { |
| 1776 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1777 | mbedtls_mpi_free( &conf->dhm_G ); |
| 1778 | return( ret ); |
| 1779 | } |
| 1780 | |
| 1781 | return( 0 ); |
| 1782 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1783 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1784 | 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] | 1785 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1786 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1787 | |
Glenn Strauss | cee1129 | 2021-12-20 01:43:17 -0500 | [diff] [blame] | 1788 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1789 | mbedtls_mpi_free( &conf->dhm_G ); |
| 1790 | |
Gilles Peskine | e570248 | 2021-06-11 21:59:08 +0200 | [diff] [blame] | 1791 | if( ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_P, |
| 1792 | &conf->dhm_P ) ) != 0 || |
| 1793 | ( ret = mbedtls_dhm_get_value( dhm_ctx, MBEDTLS_DHM_PARAM_G, |
| 1794 | &conf->dhm_G ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 1795 | { |
| 1796 | mbedtls_mpi_free( &conf->dhm_P ); |
| 1797 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1798 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 1799 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1800 | |
| 1801 | return( 0 ); |
| 1802 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 1803 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 1804 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 1805 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 1806 | /* |
| 1807 | * Set the minimum length for Diffie-Hellman parameters |
| 1808 | */ |
| 1809 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 1810 | unsigned int bitlen ) |
| 1811 | { |
| 1812 | conf->dhm_min_bitlen = bitlen; |
| 1813 | } |
| 1814 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 1815 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1816 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 1817 | #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] | 1818 | /* |
| 1819 | * Set allowed/preferred hashes for handshake signatures |
| 1820 | */ |
| 1821 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 1822 | const int *hashes ) |
| 1823 | { |
| 1824 | conf->sig_hashes = hashes; |
| 1825 | } |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 1826 | #endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1827 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1828 | /* Configure allowed signature algorithms for handshake */ |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1829 | void mbedtls_ssl_conf_sig_algs( mbedtls_ssl_config *conf, |
| 1830 | const uint16_t* sig_algs ) |
| 1831 | { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 1832 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1833 | conf->sig_hashes = NULL; |
| 1834 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 1835 | conf->sig_algs = sig_algs; |
Hanno Becker | 1cd6e00 | 2021-08-10 13:27:10 +0100 | [diff] [blame] | 1836 | } |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1837 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 1838 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 1839 | #if defined(MBEDTLS_ECP_C) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1840 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1841 | /* |
| 1842 | * Set the allowed elliptic curves |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1843 | * |
| 1844 | * mbedtls_ssl_setup() takes the provided list |
| 1845 | * and translates it to a list of IANA TLS group identifiers, |
| 1846 | * stored in ssl->handshake->group_list. |
| 1847 | * |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1848 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1849 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1850 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1851 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1852 | conf->curve_list = curve_list; |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1853 | conf->group_list = NULL; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1854 | } |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1855 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1856 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 1857 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 1858 | /* |
| 1859 | * Set the allowed groups |
| 1860 | */ |
| 1861 | void mbedtls_ssl_conf_groups( mbedtls_ssl_config *conf, |
| 1862 | const uint16_t *group_list ) |
| 1863 | { |
| 1864 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 1865 | conf->curve_list = NULL; |
| 1866 | #endif |
| 1867 | conf->group_list = group_list; |
| 1868 | } |
| 1869 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1870 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1871 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1872 | { |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1873 | /* Initialize to suppress unnecessary compiler warning */ |
| 1874 | size_t hostname_len = 0; |
| 1875 | |
| 1876 | /* Check if new hostname is valid before |
| 1877 | * making any change to current one */ |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1878 | if( hostname != NULL ) |
| 1879 | { |
| 1880 | hostname_len = strlen( hostname ); |
| 1881 | |
| 1882 | if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN ) |
| 1883 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1884 | } |
| 1885 | |
| 1886 | /* Now it's clear that we will overwrite the old hostname, |
| 1887 | * so we can free it safely */ |
| 1888 | |
| 1889 | if( ssl->hostname != NULL ) |
| 1890 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1891 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1892 | mbedtls_free( ssl->hostname ); |
| 1893 | } |
| 1894 | |
| 1895 | /* Passing NULL as hostname shall clear the old one */ |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 1896 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1897 | if( hostname == NULL ) |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1898 | { |
| 1899 | ssl->hostname = NULL; |
| 1900 | } |
| 1901 | else |
| 1902 | { |
| 1903 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1904 | if( ssl->hostname == NULL ) |
| 1905 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 1906 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1907 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 1908 | |
Hanno Becker | 947194e | 2017-04-07 13:25:49 +0100 | [diff] [blame] | 1909 | ssl->hostname[hostname_len] = '\0'; |
| 1910 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1911 | |
| 1912 | return( 0 ); |
| 1913 | } |
Hanno Becker | 1a9a51c | 2017-04-07 13:02:16 +0100 | [diff] [blame] | 1914 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1915 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 1916 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1917 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1918 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1919 | const unsigned char *, size_t), |
| 1920 | void *p_sni ) |
| 1921 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1922 | conf->f_sni = f_sni; |
| 1923 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1924 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1925 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 1926 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1927 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1928 | 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] | 1929 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1930 | size_t cur_len, tot_len; |
| 1931 | const char **p; |
| 1932 | |
| 1933 | /* |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 1934 | * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings |
| 1935 | * MUST NOT be truncated." |
| 1936 | * We check lengths now rather than later. |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1937 | */ |
| 1938 | tot_len = 0; |
| 1939 | for( p = protos; *p != NULL; p++ ) |
| 1940 | { |
| 1941 | cur_len = strlen( *p ); |
| 1942 | tot_len += cur_len; |
| 1943 | |
Ronald Cron | 8216dd3 | 2020-04-23 16:41:44 +0200 | [diff] [blame] | 1944 | if( ( cur_len == 0 ) || |
| 1945 | ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || |
| 1946 | ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1947 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1948 | } |
| 1949 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 1950 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 1951 | |
| 1952 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1953 | } |
| 1954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1955 | 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] | 1956 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1957 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1958 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1959 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 1960 | |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1961 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1962 | void mbedtls_ssl_conf_srtp_mki_value_supported( mbedtls_ssl_config *conf, |
| 1963 | int support_mki_value ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1964 | { |
| 1965 | conf->dtls_srtp_mki_support = support_mki_value; |
| 1966 | } |
| 1967 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1968 | int mbedtls_ssl_dtls_srtp_set_mki_value( mbedtls_ssl_context *ssl, |
| 1969 | unsigned char *mki_value, |
Johan Pascal | f6417ec | 2020-09-22 15:15:19 +0200 | [diff] [blame] | 1970 | uint16_t mki_len ) |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1971 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 1972 | if( mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ) |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1973 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 1974 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1975 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1976 | |
| 1977 | 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] | 1978 | { |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 1979 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1980 | } |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1981 | |
| 1982 | memcpy( ssl->dtls_srtp_info.mki_value, mki_value, mki_len ); |
| 1983 | ssl->dtls_srtp_info.mki_len = mki_len; |
Ron Eldor | a978804 | 2018-12-05 11:04:31 +0200 | [diff] [blame] | 1984 | return( 0 ); |
Ron Eldor | 591f162 | 2018-01-22 12:30:04 +0200 | [diff] [blame] | 1985 | } |
| 1986 | |
Ron Eldor | ef72faf | 2018-07-12 11:54:20 +0300 | [diff] [blame] | 1987 | int mbedtls_ssl_conf_dtls_srtp_protection_profiles( mbedtls_ssl_config *conf, |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 1988 | const mbedtls_ssl_srtp_profile *profiles ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1989 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 1990 | const mbedtls_ssl_srtp_profile *p; |
| 1991 | size_t list_size = 0; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 1992 | |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 1993 | /* check the profiles list: all entry must be valid, |
| 1994 | * 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] | 1995 | for( p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET && |
| 1996 | list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH; |
| 1997 | p++ ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 1998 | { |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 1999 | if( mbedtls_ssl_check_srtp_profile_value( *p ) != MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | d576fdb | 2020-09-22 10:39:53 +0200 | [diff] [blame] | 2000 | { |
Johan Pascal | 76fdf1d | 2020-10-22 23:31:00 +0200 | [diff] [blame] | 2001 | list_size++; |
| 2002 | } |
| 2003 | else |
| 2004 | { |
| 2005 | /* unsupported value, stop parsing and set the size to an error value */ |
| 2006 | list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2010 | if( list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH ) |
Johan Pascal | d387aa0 | 2020-09-23 18:47:56 +0200 | [diff] [blame] | 2011 | { |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2012 | conf->dtls_srtp_profile_list = NULL; |
| 2013 | conf->dtls_srtp_profile_list_len = 0; |
| 2014 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2015 | } |
| 2016 | |
Johan Pascal | 9bc97ca | 2020-09-21 23:44:45 +0200 | [diff] [blame] | 2017 | conf->dtls_srtp_profile_list = profiles; |
Johan Pascal | 253d026 | 2020-09-22 13:04:45 +0200 | [diff] [blame] | 2018 | conf->dtls_srtp_profile_list_len = list_size; |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2019 | |
| 2020 | return( 0 ); |
| 2021 | } |
| 2022 | |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2023 | void mbedtls_ssl_get_dtls_srtp_negotiation_result( const mbedtls_ssl_context *ssl, |
| 2024 | mbedtls_dtls_srtp_info *dtls_srtp_info ) |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2025 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2026 | dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile; |
| 2027 | /* do not copy the mki value if there is no chosen profile */ |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2028 | if( dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET ) |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2029 | { |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2030 | dtls_srtp_info->mki_len = 0; |
Johan Pascal | 0dbcd1d | 2020-10-28 11:03:07 +0100 | [diff] [blame] | 2031 | } |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2032 | else |
| 2033 | { |
| 2034 | dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len; |
Johan Pascal | 5ef72d2 | 2020-10-28 17:05:47 +0100 | [diff] [blame] | 2035 | memcpy( dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value, |
| 2036 | ssl->dtls_srtp_info.mki_len ); |
Johan Pascal | 2258a4f | 2020-10-28 13:53:09 +0100 | [diff] [blame] | 2037 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2038 | } |
Johan Pascal | b62bb51 | 2015-12-03 21:56:45 +0100 | [diff] [blame] | 2039 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 2040 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2041 | 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] | 2042 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2043 | conf->max_major_ver = major; |
| 2044 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 2047 | 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] | 2048 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2049 | conf->min_major_ver = major; |
| 2050 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 2053 | #if defined(MBEDTLS_SSL_SRV_C) |
| 2054 | void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf, |
| 2055 | char cert_req_ca_list ) |
| 2056 | { |
| 2057 | conf->cert_req_ca_list = cert_req_ca_list; |
| 2058 | } |
| 2059 | #endif |
| 2060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2061 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2062 | 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] | 2063 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2064 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 2065 | } |
| 2066 | #endif |
| 2067 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2069 | 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] | 2070 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2071 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 2072 | } |
| 2073 | #endif |
| 2074 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2075 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2076 | 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] | 2077 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2078 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2079 | 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] | 2080 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2081 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2082 | } |
| 2083 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 2084 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2085 | |
| 2086 | return( 0 ); |
| 2087 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2088 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 2089 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2090 | 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] | 2091 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2092 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2095 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2096 | 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] | 2097 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2098 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 2099 | } |
| 2100 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2101 | 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] | 2102 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2103 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 2104 | } |
| 2105 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 2106 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2107 | const unsigned char period[8] ) |
| 2108 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 2109 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 2110 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2111 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2113 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2114 | #if defined(MBEDTLS_SSL_CLI_C) |
| 2115 | 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] | 2116 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 2117 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2118 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2119 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2120 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2121 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2122 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 2123 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 2124 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 2125 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2126 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 2127 | conf->f_ticket_write = f_ticket_write; |
| 2128 | conf->f_ticket_parse = f_ticket_parse; |
| 2129 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 2130 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 2131 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2132 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 2133 | |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2134 | void mbedtls_ssl_set_export_keys_cb( mbedtls_ssl_context *ssl, |
| 2135 | mbedtls_ssl_export_keys_t *f_export_keys, |
| 2136 | void *p_export_keys ) |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2137 | { |
Hanno Becker | 7e6c178 | 2021-06-08 09:24:55 +0100 | [diff] [blame] | 2138 | ssl->f_export_keys = f_export_keys; |
| 2139 | ssl->p_export_keys = p_export_keys; |
Ron Eldor | f5cc10d | 2019-05-07 18:33:40 +0300 | [diff] [blame] | 2140 | } |
Robert Cragie | 4feb7ae | 2015-10-02 13:33:37 +0100 | [diff] [blame] | 2141 | |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2142 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2143 | void mbedtls_ssl_conf_async_private_cb( |
| 2144 | mbedtls_ssl_config *conf, |
| 2145 | mbedtls_ssl_async_sign_t *f_async_sign, |
| 2146 | mbedtls_ssl_async_decrypt_t *f_async_decrypt, |
| 2147 | mbedtls_ssl_async_resume_t *f_async_resume, |
| 2148 | mbedtls_ssl_async_cancel_t *f_async_cancel, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2149 | void *async_config_data ) |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2150 | { |
| 2151 | conf->f_async_sign_start = f_async_sign; |
| 2152 | conf->f_async_decrypt_start = f_async_decrypt; |
| 2153 | conf->f_async_resume = f_async_resume; |
| 2154 | conf->f_async_cancel = f_async_cancel; |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2155 | conf->p_async_config_data = async_config_data; |
| 2156 | } |
| 2157 | |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 2158 | void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf ) |
| 2159 | { |
| 2160 | return( conf->p_async_config_data ); |
| 2161 | } |
| 2162 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2163 | void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl ) |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2164 | { |
| 2165 | if( ssl->handshake == NULL ) |
| 2166 | return( NULL ); |
| 2167 | else |
| 2168 | return( ssl->handshake->user_async_ctx ); |
| 2169 | } |
| 2170 | |
Gilles Peskine | 1febfef | 2018-04-30 11:54:39 +0200 | [diff] [blame] | 2171 | void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl, |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 2172 | void *ctx ) |
| 2173 | { |
| 2174 | if( ssl->handshake != NULL ) |
| 2175 | ssl->handshake->user_async_ctx = ctx; |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2176 | } |
Gilles Peskine | b74a1c7 | 2018-04-24 13:09:22 +0200 | [diff] [blame] | 2177 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
Gilles Peskine | 8bf79f6 | 2018-01-05 21:11:53 +0100 | [diff] [blame] | 2178 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2179 | /* |
| 2180 | * SSL get accessors |
| 2181 | */ |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 2182 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2183 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 2184 | if( ssl->session != NULL ) |
| 2185 | return( ssl->session->verify_result ); |
| 2186 | |
| 2187 | if( ssl->session_negotiate != NULL ) |
| 2188 | return( ssl->session_negotiate->verify_result ); |
| 2189 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 2190 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Glenn Strauss | 8f52690 | 2022-01-13 00:04:49 -0500 | [diff] [blame] | 2193 | int mbedtls_ssl_get_ciphersuite_id_from_ssl( const mbedtls_ssl_context *ssl ) |
| 2194 | { |
| 2195 | if( ssl == NULL || ssl->session == NULL ) |
| 2196 | return( 0 ); |
| 2197 | |
| 2198 | return( ssl->session->ciphersuite ); |
| 2199 | } |
| 2200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2201 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2202 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2203 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2204 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 2205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2206 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Gilles Peskine | e1a0c25 | 2022-01-13 01:08:03 +0100 | [diff] [blame] | 2209 | mbedtls_ssl_protocol_version mbedtls_ssl_get_version_number( |
| 2210 | const mbedtls_ssl_context *ssl ) |
| 2211 | { |
| 2212 | /* For major_ver, only 3 is supported, so skip checking it. */ |
| 2213 | switch( ssl->minor_ver ) |
| 2214 | { |
| 2215 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 2216 | return( MBEDTLS_SSL_VERSION_1_2 ); |
| 2217 | case MBEDTLS_SSL_MINOR_VERSION_4: |
| 2218 | return( MBEDTLS_SSL_VERSION_1_3 ); |
| 2219 | default: |
| 2220 | return( MBEDTLS_SSL_VERSION_UNKNOWN ); |
| 2221 | } |
| 2222 | } |
| 2223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2224 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2225 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2226 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2227 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2228 | { |
| 2229 | switch( ssl->minor_ver ) |
| 2230 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2231 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2232 | return( "DTLSv1.2" ); |
| 2233 | |
| 2234 | default: |
| 2235 | return( "unknown (DTLS)" ); |
| 2236 | } |
| 2237 | } |
| 2238 | #endif |
| 2239 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2240 | switch( ssl->minor_ver ) |
| 2241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2242 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 2243 | return( "TLSv1.2" ); |
Gilles Peskine | c63a1e0 | 2022-01-13 01:10:24 +0100 | [diff] [blame] | 2244 | case MBEDTLS_SSL_MINOR_VERSION_4: |
| 2245 | return( "TLSv1.3" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2246 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 2247 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2248 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2251 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2252 | size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl ) |
| 2253 | { |
David Horstmann | 95d516f | 2021-05-04 18:36:56 +0100 | [diff] [blame] | 2254 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2255 | size_t read_mfl; |
| 2256 | |
| 2257 | /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */ |
| 2258 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2259 | ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE ) |
| 2260 | { |
| 2261 | return ssl_mfl_code_to_length( ssl->conf->mfl_code ); |
| 2262 | } |
| 2263 | |
| 2264 | /* Check if a smaller max length was negotiated */ |
| 2265 | if( ssl->session_out != NULL ) |
| 2266 | { |
| 2267 | read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code ); |
| 2268 | if( read_mfl < max_len ) |
| 2269 | { |
| 2270 | max_len = read_mfl; |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | // During a handshake, use the value being negotiated |
| 2275 | if( ssl->session_negotiate != NULL ) |
| 2276 | { |
| 2277 | read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2278 | if( read_mfl < max_len ) |
| 2279 | { |
| 2280 | max_len = read_mfl; |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | return( max_len ); |
| 2285 | } |
| 2286 | |
| 2287 | 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] | 2288 | { |
| 2289 | size_t max_len; |
| 2290 | |
| 2291 | /* |
| 2292 | * Assume mfl_code is correct since it was checked when set |
| 2293 | */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2294 | 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] | 2295 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2296 | /* Check if a smaller max length was negotiated */ |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2297 | if( ssl->session_out != NULL && |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2298 | 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] | 2299 | { |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2300 | 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] | 2301 | } |
| 2302 | |
Manuel Pégourié-Gonnard | 2cb17e2 | 2017-09-19 13:00:47 +0200 | [diff] [blame] | 2303 | /* During a handshake, use the value being negotiated */ |
| 2304 | if( ssl->session_negotiate != NULL && |
| 2305 | ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len ) |
| 2306 | { |
| 2307 | max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ); |
| 2308 | } |
| 2309 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2310 | return( max_len ); |
Manuel Pégourié-Gonnard | a2cda6b | 2015-08-31 18:30:52 +0200 | [diff] [blame] | 2311 | } |
| 2312 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2313 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2314 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2315 | 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] | 2316 | { |
Andrzej Kurek | ef43ce6 | 2018-10-09 08:24:12 -0400 | [diff] [blame] | 2317 | /* Return unlimited mtu for client hello messages to avoid fragmentation. */ |
| 2318 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 2319 | ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO || |
| 2320 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) ) |
| 2321 | return ( 0 ); |
| 2322 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2323 | if( ssl->handshake == NULL || ssl->handshake->mtu == 0 ) |
| 2324 | return( ssl->mtu ); |
| 2325 | |
| 2326 | if( ssl->mtu == 0 ) |
| 2327 | return( ssl->handshake->mtu ); |
| 2328 | |
| 2329 | return( ssl->mtu < ssl->handshake->mtu ? |
| 2330 | ssl->mtu : ssl->handshake->mtu ); |
| 2331 | } |
| 2332 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2333 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2334 | int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl ) |
| 2335 | { |
| 2336 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
| 2337 | |
Manuel Pégourié-Gonnard | 000281e | 2018-08-21 11:20:58 +0200 | [diff] [blame] | 2338 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2339 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2340 | (void) ssl; |
| 2341 | #endif |
| 2342 | |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2343 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 2344 | 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] | 2345 | |
| 2346 | if( max_len > mfl ) |
| 2347 | max_len = mfl; |
| 2348 | #endif |
| 2349 | |
| 2350 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2351 | if( mbedtls_ssl_get_current_mtu( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2352 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 2353 | const size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2354 | const int ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 2355 | const size_t overhead = (size_t) ret; |
| 2356 | |
| 2357 | if( ret < 0 ) |
| 2358 | return( ret ); |
| 2359 | |
| 2360 | if( mtu <= overhead ) |
| 2361 | { |
| 2362 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) ); |
| 2363 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2364 | } |
| 2365 | |
| 2366 | if( max_len > mtu - overhead ) |
| 2367 | max_len = mtu - overhead; |
| 2368 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 2369 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2370 | |
Hanno Becker | 0defedb | 2018-08-10 12:35:02 +0100 | [diff] [blame] | 2371 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \ |
| 2372 | !defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2373 | ((void) ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 2374 | #endif |
| 2375 | |
| 2376 | return( (int) max_len ); |
| 2377 | } |
| 2378 | |
Hanno Becker | 2d8e99b | 2021-04-21 06:19:50 +0100 | [diff] [blame] | 2379 | int mbedtls_ssl_get_max_in_record_payload( const mbedtls_ssl_context *ssl ) |
| 2380 | { |
| 2381 | size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN; |
| 2382 | |
| 2383 | #if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2384 | (void) ssl; |
| 2385 | #endif |
| 2386 | |
| 2387 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2388 | const size_t mfl = mbedtls_ssl_get_input_max_frag_len( ssl ); |
| 2389 | |
| 2390 | if( max_len > mfl ) |
| 2391 | max_len = mfl; |
| 2392 | #endif |
| 2393 | |
| 2394 | return( (int) max_len ); |
| 2395 | } |
| 2396 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2397 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2398 | 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] | 2399 | { |
| 2400 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2401 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2402 | |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2403 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 2404 | return( ssl->session->peer_cert ); |
Hanno Becker | e682457 | 2019-02-07 13:18:46 +0000 | [diff] [blame] | 2405 | #else |
| 2406 | return( NULL ); |
| 2407 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2408 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2409 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 2410 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2411 | #if defined(MBEDTLS_SSL_CLI_C) |
Hanno Becker | f852b1c | 2019-02-05 11:42:30 +0000 | [diff] [blame] | 2412 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, |
| 2413 | mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2414 | { |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2415 | int ret; |
| 2416 | |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2417 | if( ssl == NULL || |
| 2418 | dst == NULL || |
| 2419 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2420 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2421 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
Hanno Becker | e810bbc | 2021-05-14 16:01:05 +0100 | [diff] [blame] | 2425 | /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer |
| 2426 | * idempotent: Each session can only be exported once. |
| 2427 | * |
| 2428 | * (This is in preparation for TLS 1.3 support where we will |
| 2429 | * need the ability to export multiple sessions (aka tickets), |
| 2430 | * which will be achieved by calling mbedtls_ssl_get_session() |
| 2431 | * multiple times until it fails.) |
| 2432 | * |
| 2433 | * Check whether we have already exported the current session, |
| 2434 | * and fail if so. |
| 2435 | */ |
| 2436 | if( ssl->session->exported == 1 ) |
| 2437 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2438 | |
| 2439 | ret = mbedtls_ssl_session_copy( dst, ssl->session ); |
| 2440 | if( ret != 0 ) |
| 2441 | return( ret ); |
| 2442 | |
| 2443 | /* Remember that we've exported the session. */ |
| 2444 | ssl->session->exported = 1; |
| 2445 | return( 0 ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2446 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2447 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 2448 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2449 | /* |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2450 | * Define ticket header determining Mbed TLS version |
| 2451 | * and structure of the ticket. |
| 2452 | */ |
| 2453 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2454 | /* |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2455 | * Define bitflag determining compile-time settings influencing |
| 2456 | * structure of serialized SSL sessions. |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2457 | */ |
| 2458 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2459 | #if defined(MBEDTLS_HAVE_TIME) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2460 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 1 |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2461 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2462 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2463 | #endif /* MBEDTLS_HAVE_TIME */ |
| 2464 | |
| 2465 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2466 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2467 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2468 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2469 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 2470 | |
| 2471 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2472 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2473 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2474 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2475 | #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */ |
| 2476 | |
| 2477 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2478 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2479 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2480 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2481 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2482 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2483 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2484 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 1 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2485 | #else |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2486 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM 0 |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2487 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 2488 | |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2489 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 2490 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1 |
| 2491 | #else |
| 2492 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0 |
| 2493 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 2494 | |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2495 | #define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0 |
| 2496 | #define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1 |
| 2497 | #define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2 |
| 2498 | #define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3 |
Hanno Becker | 37bdbe6 | 2021-08-01 05:38:58 +0100 | [diff] [blame] | 2499 | #define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4 |
| 2500 | #define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5 |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2501 | |
Hanno Becker | 50b5966 | 2019-05-28 14:30:45 +0100 | [diff] [blame] | 2502 | #define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2503 | ( (uint16_t) ( \ |
| 2504 | ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \ |
| 2505 | ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \ |
| 2506 | ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \ |
| 2507 | ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \ |
Hanno Becker | 3e08866 | 2019-05-29 11:10:18 +0100 | [diff] [blame] | 2508 | ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \ |
Hanno Becker | be34e8e | 2019-06-04 09:43:16 +0100 | [diff] [blame] | 2509 | ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) ) |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2510 | |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2511 | static unsigned char ssl_serialized_session_header[] = { |
Hanno Becker | 94ef3b3 | 2019-05-16 12:50:45 +0100 | [diff] [blame] | 2512 | MBEDTLS_VERSION_MAJOR, |
| 2513 | MBEDTLS_VERSION_MINOR, |
| 2514 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 2515 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 2516 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
Hanno Becker | f878707 | 2019-05-16 12:41:07 +0100 | [diff] [blame] | 2517 | }; |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 2518 | |
| 2519 | /* |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2520 | * Serialize a session in the following format: |
Manuel Pégourié-Gonnard | 35eb802 | 2019-05-16 11:11:08 +0200 | [diff] [blame] | 2521 | * (in the presentation language of TLS, RFC 8446 section 3) |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2522 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2523 | * struct { |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2524 | * |
Hanno Becker | dce5097 | 2021-08-01 05:39:23 +0100 | [diff] [blame] | 2525 | * opaque mbedtls_version[3]; // library version: major, minor, patch |
| 2526 | * opaque session_format[2]; // library-version specific 16-bit field |
| 2527 | * // determining the format of the remaining |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2528 | * // serialized data. |
Hanno Becker | dc28b6c | 2019-05-29 11:08:00 +0100 | [diff] [blame] | 2529 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2530 | * Note: When updating the format, remember to keep |
| 2531 | * these version+format bytes. |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2532 | * |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2533 | * // In this version, `session_format` determines |
| 2534 | * // the setting of those compile-time |
| 2535 | * // configuration options which influence |
| 2536 | * // the structure of mbedtls_ssl_session. |
| 2537 | * |
Hanno Becker | fa0d61e | 2021-08-02 08:56:14 +0100 | [diff] [blame] | 2538 | * uint8_t minor_ver; // Protocol-version. Possible values: |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2539 | * // - TLS 1.2 (MBEDTLS_SSL_MINOR_VERSION_3) |
| 2540 | * |
| 2541 | * select (serialized_session.minor_ver) { |
| 2542 | * |
| 2543 | * case MBEDTLS_SSL_MINOR_VERSION_3: // TLS 1.2 |
| 2544 | * serialized_session_tls12 data; |
| 2545 | * |
| 2546 | * }; |
| 2547 | * |
| 2548 | * } serialized_session; |
| 2549 | * |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2550 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2551 | |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2552 | static int ssl_session_save( const mbedtls_ssl_session *session, |
| 2553 | unsigned char omit_header, |
| 2554 | unsigned char *buf, |
| 2555 | size_t buf_len, |
| 2556 | size_t *olen ) |
| 2557 | { |
| 2558 | unsigned char *p = buf; |
| 2559 | size_t used = 0; |
| 2560 | |
| 2561 | if( !omit_header ) |
| 2562 | { |
| 2563 | /* |
| 2564 | * Add Mbed TLS version identifier |
| 2565 | */ |
| 2566 | |
| 2567 | used += sizeof( ssl_serialized_session_header ); |
| 2568 | |
| 2569 | if( used <= buf_len ) |
| 2570 | { |
| 2571 | memcpy( p, ssl_serialized_session_header, |
| 2572 | sizeof( ssl_serialized_session_header ) ); |
| 2573 | p += sizeof( ssl_serialized_session_header ); |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | /* |
| 2578 | * TLS version identifier |
| 2579 | */ |
| 2580 | used += 1; |
| 2581 | if( used <= buf_len ) |
| 2582 | { |
| 2583 | *p++ = session->minor_ver; |
| 2584 | } |
| 2585 | |
| 2586 | /* Forward to version-specific serialization routine. */ |
| 2587 | switch( session->minor_ver ) |
| 2588 | { |
| 2589 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2590 | case MBEDTLS_SSL_MINOR_VERSION_3: |
| 2591 | { |
| 2592 | size_t remaining_len = used <= buf_len ? buf_len - used : 0; |
| 2593 | used += ssl_session_save_tls12( session, p, remaining_len ); |
| 2594 | break; |
| 2595 | } |
| 2596 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2597 | |
| 2598 | default: |
| 2599 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 2600 | } |
| 2601 | |
| 2602 | *olen = used; |
Manuel Pégourié-Gonnard | 26f982f | 2019-05-21 11:01:32 +0200 | [diff] [blame] | 2603 | if( used > buf_len ) |
| 2604 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2605 | |
| 2606 | return( 0 ); |
| 2607 | } |
| 2608 | |
| 2609 | /* |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2610 | * Public wrapper for ssl_session_save() |
| 2611 | */ |
| 2612 | int mbedtls_ssl_session_save( const mbedtls_ssl_session *session, |
| 2613 | unsigned char *buf, |
| 2614 | size_t buf_len, |
| 2615 | size_t *olen ) |
| 2616 | { |
| 2617 | return( ssl_session_save( session, 0, buf, buf_len, olen ) ); |
| 2618 | } |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2619 | |
Jerry Yu | f1b23ca | 2022-02-18 11:48:47 +0800 | [diff] [blame] | 2620 | /* |
| 2621 | * Deserialize session, see mbedtls_ssl_session_save() for format. |
| 2622 | * |
| 2623 | * This internal version is wrapped by a public function that cleans up in |
| 2624 | * case of error, and has an extra option omit_header. |
| 2625 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 2626 | static int ssl_session_load( mbedtls_ssl_session *session, |
| 2627 | unsigned char omit_header, |
| 2628 | const unsigned char *buf, |
| 2629 | size_t len ) |
| 2630 | { |
| 2631 | const unsigned char *p = buf; |
| 2632 | const unsigned char * const end = buf + len; |
| 2633 | |
| 2634 | if( !omit_header ) |
| 2635 | { |
| 2636 | /* |
| 2637 | * Check Mbed TLS version identifier |
| 2638 | */ |
| 2639 | |
| 2640 | if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) ) |
| 2641 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2642 | |
| 2643 | if( memcmp( p, ssl_serialized_session_header, |
| 2644 | sizeof( ssl_serialized_session_header ) ) != 0 ) |
| 2645 | { |
| 2646 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 2647 | } |
| 2648 | p += sizeof( ssl_serialized_session_header ); |
| 2649 | } |
| 2650 | |
| 2651 | /* |
| 2652 | * TLS version identifier |
| 2653 | */ |
| 2654 | if( 1 > (size_t)( end - p ) ) |
| 2655 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2656 | session->minor_ver = *p++; |
| 2657 | |
| 2658 | /* Dispatch according to TLS version. */ |
| 2659 | switch( session->minor_ver ) |
| 2660 | { |
| 2661 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2662 | case MBEDTLS_SSL_MINOR_VERSION_3: /* TLS 1.2 */ |
| 2663 | { |
| 2664 | size_t remaining_len = ( end - p ); |
| 2665 | return( ssl_session_load_tls12( session, p, remaining_len ) ); |
| 2666 | } |
| 2667 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2668 | |
| 2669 | default: |
| 2670 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2671 | } |
| 2672 | } |
| 2673 | |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 2674 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 2675 | * Deserialize session: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2676 | */ |
| 2677 | int mbedtls_ssl_session_load( mbedtls_ssl_session *session, |
| 2678 | const unsigned char *buf, |
| 2679 | size_t len ) |
| 2680 | { |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 2681 | int ret = ssl_session_load( session, 0, buf, len ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2682 | |
| 2683 | if( ret != 0 ) |
| 2684 | mbedtls_ssl_session_free( session ); |
| 2685 | |
| 2686 | return( ret ); |
| 2687 | } |
| 2688 | |
| 2689 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2690 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2691 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2692 | static int ssl_prepare_handshake_step( mbedtls_ssl_context *ssl ) |
| 2693 | { |
| 2694 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2695 | |
Ronald Cron | 66dbf91 | 2022-02-02 15:33:46 +0100 | [diff] [blame^] | 2696 | /* |
| 2697 | * We may have not been able to send to the peer all the handshake data |
| 2698 | * that were written into the output buffer by the previous handshake step: |
| 2699 | * the write to the network callback returned with the |
| 2700 | * #MBEDTLS_ERR_SSL_WANT_WRITE error code. |
| 2701 | * We proceed to the next handshake step only when all data from the |
| 2702 | * previous one have been sent to the peer, thus we make sure that this is |
| 2703 | * the case here by calling `mbedtls_ssl_flush_output()`. The function may |
| 2704 | * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case |
| 2705 | * we have to wait before to go ahead. |
| 2706 | * In the case of TLS 1.3, handshake step handlers do not send data to the |
| 2707 | * peer. Data are only sent here and through |
| 2708 | * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an |
| 2709 | * alert occured. |
| 2710 | */ |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2711 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 2712 | return( ret ); |
| 2713 | |
| 2714 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2715 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2716 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
| 2717 | { |
| 2718 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 2719 | return( ret ); |
| 2720 | } |
| 2721 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2722 | |
| 2723 | return( ret ); |
| 2724 | } |
| 2725 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2726 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2727 | { |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2728 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2729 | |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2730 | if( ssl == NULL || |
| 2731 | ssl->conf == NULL || |
| 2732 | ssl->handshake == NULL || |
| 2733 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 2734 | { |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2735 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | 41934dd | 2021-08-07 19:13:43 +0100 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | ret = ssl_prepare_handshake_step( ssl ); |
| 2739 | if( ret != 0 ) |
| 2740 | return( ret ); |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2741 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2742 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
| 2743 | if( ret != 0 ) |
| 2744 | goto cleanup; |
| 2745 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2746 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2747 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2748 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2749 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2750 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | f443681 | 2021-08-26 22:59:56 +0800 | [diff] [blame] | 2751 | ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2752 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2753 | |
| 2754 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2755 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 2756 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
| 2757 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2758 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2759 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2760 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2761 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2762 | { |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2763 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2764 | if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) |
Jerry Yu | 2756193 | 2021-08-27 17:07:38 +0800 | [diff] [blame] | 2765 | ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2766 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | b9930e7 | 2021-08-06 17:11:51 +0800 | [diff] [blame] | 2767 | |
| 2768 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2769 | if( mbedtls_ssl_conf_is_tls12_only( ssl->conf ) ) |
| 2770 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
| 2771 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2772 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2773 | #endif |
| 2774 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2775 | if( ret != 0 ) |
| 2776 | { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 2777 | /* handshake_step return error. And it is same |
| 2778 | * with alert_reason. |
| 2779 | */ |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 2780 | if( ssl->send_alert ) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2781 | { |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 2782 | ret = mbedtls_ssl_handle_pending_alert( ssl ); |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 2783 | goto cleanup; |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | cleanup: |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2788 | return( ret ); |
| 2789 | } |
| 2790 | |
| 2791 | /* |
| 2792 | * Perform the SSL handshake |
| 2793 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2794 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2795 | { |
| 2796 | int ret = 0; |
| 2797 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2798 | /* Sanity checks */ |
| 2799 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2800 | if( ssl == NULL || ssl->conf == NULL ) |
| 2801 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2802 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2803 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2804 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2805 | ( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) ) |
| 2806 | { |
| 2807 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 2808 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 2809 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2810 | } |
| 2811 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2813 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2814 | |
Hanno Becker | a817ea4 | 2020-10-20 15:20:23 +0100 | [diff] [blame] | 2815 | /* Main handshake loop */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2816 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2817 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2818 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 2819 | |
| 2820 | if( ret != 0 ) |
| 2821 | break; |
| 2822 | } |
| 2823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2824 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2825 | |
| 2826 | return( ret ); |
| 2827 | } |
| 2828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2829 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2830 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2831 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2832 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2833 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2834 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2835 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2836 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2837 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2838 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2839 | |
| 2840 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2841 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2842 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2843 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2844 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2845 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2846 | 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] | 2847 | return( ret ); |
| 2848 | } |
| 2849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2850 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2851 | |
| 2852 | return( 0 ); |
| 2853 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2854 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2855 | |
| 2856 | /* |
| 2857 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2858 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 2859 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 2860 | * - 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] | 2861 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2862 | * 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] | 2863 | * 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] | 2864 | */ |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 2865 | int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2866 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2867 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2868 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2869 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2870 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2871 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 2872 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2873 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2874 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 2875 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2876 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2877 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2878 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2879 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2880 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2881 | ssl->handshake->out_msg_seq = 1; |
| 2882 | else |
| 2883 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 2884 | } |
| 2885 | #endif |
| 2886 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2887 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 2888 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2889 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2890 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2891 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2892 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2893 | return( ret ); |
| 2894 | } |
| 2895 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2896 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2897 | |
| 2898 | return( 0 ); |
| 2899 | } |
| 2900 | |
| 2901 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2902 | * Renegotiate current connection on client, |
| 2903 | * or request renegotiation on server |
| 2904 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2905 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2906 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2907 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2908 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 2909 | if( ssl == NULL || ssl->conf == NULL ) |
| 2910 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2911 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2912 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2913 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2914 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2915 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2916 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 2917 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2919 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 2920 | |
| 2921 | /* Did we already try/start sending HelloRequest? */ |
| 2922 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2923 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 2924 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2925 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2926 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2927 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2929 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2930 | /* |
| 2931 | * On client, either start the renegotiation process or, |
| 2932 | * if already in progress, continue the handshake |
| 2933 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2934 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2935 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2936 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 2937 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2938 | |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 2939 | if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2940 | { |
Hanno Becker | 40cdaa1 | 2020-02-05 10:48:27 +0000 | [diff] [blame] | 2941 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2942 | return( ret ); |
| 2943 | } |
| 2944 | } |
| 2945 | else |
| 2946 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2947 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2949 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2950 | return( ret ); |
| 2951 | } |
| 2952 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2953 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 2954 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 2955 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2956 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2957 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 2958 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2959 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 2960 | 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] | 2961 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2962 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 2963 | |
| 2964 | while( cur != NULL ) |
| 2965 | { |
| 2966 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2967 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 2968 | cur = next; |
| 2969 | } |
| 2970 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2971 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 2972 | |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2973 | void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2974 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 2975 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 2976 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 2977 | if( handshake == NULL ) |
| 2978 | return; |
| 2979 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 2980 | #if defined(MBEDTLS_ECP_C) |
| 2981 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2982 | if ( ssl->handshake->group_list_heap_allocated ) |
| 2983 | mbedtls_free( (void*) handshake->group_list ); |
| 2984 | handshake->group_list = NULL; |
| 2985 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 2986 | #endif /* MBEDTLS_ECP_C */ |
| 2987 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 2988 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 2989 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 2990 | if ( ssl->handshake->sig_algs_heap_allocated ) |
| 2991 | mbedtls_free( (void*) handshake->sig_algs ); |
| 2992 | handshake->sig_algs = NULL; |
| 2993 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Xiaofei Bai | c234ecf | 2022-02-08 09:59:23 +0000 | [diff] [blame] | 2994 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 2995 | if( ssl->handshake->certificate_request_context ) |
| 2996 | { |
| 2997 | mbedtls_free( (void*) handshake->certificate_request_context ); |
| 2998 | } |
| 2999 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3000 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 3001 | |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3002 | #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) |
| 3003 | if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 ) |
| 3004 | { |
Gilles Peskine | 8f97af7 | 2018-04-26 11:46:10 +0200 | [diff] [blame] | 3005 | ssl->conf->f_async_cancel( ssl ); |
Gilles Peskine | df13d5c | 2018-04-25 20:39:48 +0200 | [diff] [blame] | 3006 | handshake->async_in_progress = 0; |
| 3007 | } |
| 3008 | #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ |
| 3009 | |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3010 | #if defined(MBEDTLS_SHA256_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3011 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3012 | psa_hash_abort( &handshake->fin_sha256_psa ); |
| 3013 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3014 | mbedtls_sha256_free( &handshake->fin_sha256 ); |
| 3015 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3016 | #endif |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 3017 | #if defined(MBEDTLS_SHA384_C) |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3018 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 972fba5 | 2019-01-30 03:29:12 -0500 | [diff] [blame] | 3019 | psa_hash_abort( &handshake->fin_sha384_psa ); |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3020 | #else |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3021 | mbedtls_sha512_free( &handshake->fin_sha512 ); |
| 3022 | #endif |
Andrzej Kurek | eb34224 | 2019-01-29 09:14:33 -0500 | [diff] [blame] | 3023 | #endif |
Manuel Pégourié-Gonnard | b9d64e5 | 2015-07-06 14:18:56 +0200 | [diff] [blame] | 3024 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3025 | #if defined(MBEDTLS_DHM_C) |
| 3026 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3027 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3028 | #if defined(MBEDTLS_ECDH_C) |
| 3029 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3030 | #endif |
Manuel Pégourié-Gonnard | eef142d | 2015-09-16 10:05:04 +0200 | [diff] [blame] | 3031 | #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3032 | mbedtls_ecjpake_free( &handshake->ecjpake_ctx ); |
Manuel Pégourié-Gonnard | 77c0646 | 2015-09-17 13:59:49 +0200 | [diff] [blame] | 3033 | #if defined(MBEDTLS_SSL_CLI_C) |
| 3034 | mbedtls_free( handshake->ecjpake_cache ); |
| 3035 | handshake->ecjpake_cache = NULL; |
| 3036 | handshake->ecjpake_cache_len = 0; |
| 3037 | #endif |
Manuel Pégourié-Gonnard | 76cfd3f | 2015-09-15 12:10:54 +0200 | [diff] [blame] | 3038 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 3039 | |
Janos Follath | 4ae5c29 | 2016-02-10 11:27:43 +0000 | [diff] [blame] | 3040 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 3041 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3042 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3043 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 3044 | #endif |
| 3045 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3046 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3047 | if( handshake->psk != NULL ) |
| 3048 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3049 | mbedtls_platform_zeroize( handshake->psk, handshake->psk_len ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 3050 | mbedtls_free( handshake->psk ); |
| 3051 | } |
| 3052 | #endif |
| 3053 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3054 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3055 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3056 | /* |
| 3057 | * Free only the linked list wrapper, not the keys themselves |
| 3058 | * since the belong to the SNI callback |
| 3059 | */ |
| 3060 | if( handshake->sni_key_cert != NULL ) |
| 3061 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3062 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3063 | |
| 3064 | while( cur != NULL ) |
| 3065 | { |
| 3066 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3067 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 3068 | cur = next; |
| 3069 | } |
| 3070 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3071 | #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] | 3072 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3073 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
Manuel Pégourié-Gonnard | 6b7301c | 2017-08-15 12:08:45 +0200 | [diff] [blame] | 3074 | mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx ); |
Hanno Becker | 3dad311 | 2019-02-05 17:19:52 +0000 | [diff] [blame] | 3075 | if( handshake->ecrs_peer_cert != NULL ) |
| 3076 | { |
| 3077 | mbedtls_x509_crt_free( handshake->ecrs_peer_cert ); |
| 3078 | mbedtls_free( handshake->ecrs_peer_cert ); |
| 3079 | } |
Manuel Pégourié-Gonnard | 862cde5 | 2017-05-17 11:56:15 +0200 | [diff] [blame] | 3080 | #endif |
| 3081 | |
Hanno Becker | 7517312 | 2019-02-06 16:18:31 +0000 | [diff] [blame] | 3082 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 3083 | !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 3084 | mbedtls_pk_free( &handshake->peer_pubkey ); |
| 3085 | #endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 3086 | |
XiaokangQian | 3490974 | 2022-01-27 02:25:04 +0000 | [diff] [blame] | 3087 | #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] | 3088 | mbedtls_free( handshake->verify_cookie ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3089 | #endif /* MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 3090 | |
| 3091 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 3092 | mbedtls_ssl_flight_free( handshake->flight ); |
| 3093 | mbedtls_ssl_buffering_free( ssl ); |
XiaokangQian | 8499b6c | 2022-01-27 09:00:11 +0000 | [diff] [blame] | 3094 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 3095 | |
Hanno Becker | 4a63ed4 | 2019-01-08 11:39:35 +0000 | [diff] [blame] | 3096 | #if defined(MBEDTLS_ECDH_C) && \ |
| 3097 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3098 | psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 3099 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ |
| 3100 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3101 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | a1a568c | 2021-11-09 10:17:21 +0800 | [diff] [blame] | 3102 | mbedtls_ssl_transform_free( handshake->transform_handshake ); |
| 3103 | mbedtls_ssl_transform_free( handshake->transform_earlydata ); |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3104 | mbedtls_free( handshake->transform_earlydata ); |
| 3105 | mbedtls_free( handshake->transform_handshake ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3106 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3107 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3108 | |
| 3109 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3110 | /* If the buffers are too big - reallocate. Because of the way Mbed TLS |
| 3111 | * processes datagrams and the fact that a datagram is allowed to have |
| 3112 | * several records in it, it is possible that the I/O buffers are not |
| 3113 | * empty at this stage */ |
Andrzej Kurek | 4a06379 | 2020-10-21 15:08:44 +0200 | [diff] [blame] | 3114 | handle_buffer_resizing( ssl, 1, mbedtls_ssl_get_input_buflen( ssl ), |
| 3115 | mbedtls_ssl_get_output_buflen( ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3116 | #endif |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3117 | |
Jerry Yu | ba9c727 | 2021-10-30 11:54:10 +0800 | [diff] [blame] | 3118 | /* mbedtls_platform_zeroize MUST be last one in this function */ |
| 3119 | mbedtls_platform_zeroize( handshake, |
| 3120 | sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3123 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3124 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3125 | if( session == NULL ) |
| 3126 | return; |
| 3127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3128 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 1294a0b | 2019-02-05 12:38:15 +0000 | [diff] [blame] | 3129 | ssl_clear_peer_cert( session ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3130 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 3131 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 3132 | #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] | 3133 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 3134 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 3135 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3136 | mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3139 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3140 | |
| 3141 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3142 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u |
| 3143 | #else |
| 3144 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u |
| 3145 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3146 | |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3147 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3148 | |
| 3149 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3150 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u |
| 3151 | #else |
| 3152 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u |
| 3153 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3154 | |
| 3155 | #if defined(MBEDTLS_SSL_ALPN) |
| 3156 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u |
| 3157 | #else |
| 3158 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u |
| 3159 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3160 | |
| 3161 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0 |
| 3162 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1 |
| 3163 | #define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2 |
| 3164 | #define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3 |
| 3165 | |
| 3166 | #define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \ |
| 3167 | ( (uint32_t) ( \ |
| 3168 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \ |
| 3169 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \ |
| 3170 | ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \ |
| 3171 | ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \ |
| 3172 | 0u ) ) |
| 3173 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3174 | static unsigned char ssl_serialized_context_header[] = { |
| 3175 | MBEDTLS_VERSION_MAJOR, |
| 3176 | MBEDTLS_VERSION_MINOR, |
| 3177 | MBEDTLS_VERSION_PATCH, |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 3178 | MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3179 | MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), |
| 3180 | MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3181 | MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
| 3182 | MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3183 | }; |
| 3184 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3185 | /* |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3186 | * Serialize a full SSL context |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3187 | * |
| 3188 | * The format of the serialized data is: |
| 3189 | * (in the presentation language of TLS, RFC 8446 section 3) |
| 3190 | * |
| 3191 | * // header |
| 3192 | * opaque mbedtls_version[3]; // major, minor, patch |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3193 | * opaque context_format[5]; // version-specific field determining |
Manuel Pégourié-Gonnard | 00400c2 | 2019-07-10 14:58:45 +0200 | [diff] [blame] | 3194 | * // the format of the remaining |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3195 | * // serialized data. |
Manuel Pégourié-Gonnard | 4e9370b | 2019-07-23 16:31:16 +0200 | [diff] [blame] | 3196 | * Note: When updating the format, remember to keep these |
| 3197 | * 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] | 3198 | * |
| 3199 | * // session sub-structure |
| 3200 | * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save() |
| 3201 | * // transform sub-structure |
| 3202 | * uint8 random[64]; // ServerHello.random+ClientHello.random |
| 3203 | * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value |
| 3204 | * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use |
| 3205 | * // fields from ssl_context |
| 3206 | * uint32 badmac_seen; // DTLS: number of records with failing MAC |
| 3207 | * uint64 in_window_top; // DTLS: last validated record seq_num |
| 3208 | * uint64 in_window; // DTLS: bitmask for replay protection |
| 3209 | * uint8 disable_datagram_packing; // DTLS: only one record per datagram |
| 3210 | * uint64 cur_out_ctr; // Record layer: outgoing sequence number |
| 3211 | * uint16 mtu; // DTLS: path mtu (max outgoing fragment size) |
| 3212 | * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol |
| 3213 | * |
| 3214 | * Note that many fields of the ssl_context or sub-structures are not |
| 3215 | * serialized, as they fall in one of the following categories: |
| 3216 | * |
| 3217 | * 1. forced value (eg in_left must be 0) |
| 3218 | * 2. pointer to dynamically-allocated memory (eg session, transform) |
| 3219 | * 3. value can be re-derived from other data (eg session keys from MS) |
| 3220 | * 4. value was temporary (eg content of input buffer) |
| 3221 | * 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] | 3222 | */ |
| 3223 | int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, |
| 3224 | unsigned char *buf, |
| 3225 | size_t buf_len, |
| 3226 | size_t *olen ) |
| 3227 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3228 | unsigned char *p = buf; |
| 3229 | size_t used = 0; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3230 | size_t session_len; |
| 3231 | int ret = 0; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3232 | |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3233 | /* |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3234 | * Enforce usage restrictions, see "return BAD_INPUT_DATA" in |
| 3235 | * this function's documentation. |
| 3236 | * |
| 3237 | * These are due to assumptions/limitations in the implementation. Some of |
| 3238 | * them are likely to stay (no handshake in progress) some might go away |
| 3239 | * (only DTLS) but are currently used to simplify the implementation. |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3240 | */ |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3241 | /* The initial handshake must be over */ |
| 3242 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3243 | { |
| 3244 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) ); |
Manuel Pégourié-Gonnard | 1aaf669 | 2019-07-10 14:14:05 +0200 | [diff] [blame] | 3245 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3246 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3247 | if( ssl->handshake != NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3248 | { |
| 3249 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3250 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3251 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3252 | /* Double-check that sub-structures are indeed ready */ |
| 3253 | if( ssl->transform == NULL || ssl->session == NULL ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3254 | { |
| 3255 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3256 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3257 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3258 | /* There must be no pending incoming or outgoing data */ |
| 3259 | if( mbedtls_ssl_check_pending( ssl ) != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3260 | { |
| 3261 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3262 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3263 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3264 | if( ssl->out_left != 0 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3265 | { |
| 3266 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3267 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3268 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3269 | /* Protocol must be DLTS, not TLS */ |
| 3270 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3271 | { |
| 3272 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3273 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3274 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3275 | /* Version must be 1.2 */ |
| 3276 | if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3277 | { |
| 3278 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3279 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3280 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3281 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3282 | { |
| 3283 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3284 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3285 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3286 | /* We must be using an AEAD ciphersuite */ |
| 3287 | if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3288 | { |
| 3289 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3290 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3291 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3292 | /* Renegotiation must not be enabled */ |
| 3293 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3294 | if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3295 | { |
| 3296 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) ); |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3297 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jarno Lamsa | 8c51b7c | 2019-08-21 13:45:05 +0300 | [diff] [blame] | 3298 | } |
Manuel Pégourié-Gonnard | e458869 | 2019-07-29 12:28:52 +0200 | [diff] [blame] | 3299 | #endif |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3300 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3301 | /* |
| 3302 | * Version and format identifier |
| 3303 | */ |
| 3304 | used += sizeof( ssl_serialized_context_header ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3305 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3306 | if( used <= buf_len ) |
| 3307 | { |
| 3308 | memcpy( p, ssl_serialized_context_header, |
| 3309 | sizeof( ssl_serialized_context_header ) ); |
| 3310 | p += sizeof( ssl_serialized_context_header ); |
| 3311 | } |
| 3312 | |
| 3313 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3314 | * Session (length + data) |
| 3315 | */ |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3316 | 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] | 3317 | if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ) |
| 3318 | return( ret ); |
| 3319 | |
| 3320 | used += 4 + session_len; |
| 3321 | if( used <= buf_len ) |
| 3322 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3323 | MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); |
| 3324 | p += 4; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3325 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3326 | ret = ssl_session_save( ssl->session, 1, |
| 3327 | p, session_len, &session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3328 | if( ret != 0 ) |
| 3329 | return( ret ); |
| 3330 | |
| 3331 | p += session_len; |
| 3332 | } |
| 3333 | |
| 3334 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3335 | * Transform |
| 3336 | */ |
| 3337 | used += sizeof( ssl->transform->randbytes ); |
| 3338 | if( used <= buf_len ) |
| 3339 | { |
| 3340 | memcpy( p, ssl->transform->randbytes, |
| 3341 | sizeof( ssl->transform->randbytes ) ); |
| 3342 | p += sizeof( ssl->transform->randbytes ); |
| 3343 | } |
| 3344 | |
| 3345 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3346 | used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len; |
| 3347 | if( used <= buf_len ) |
| 3348 | { |
| 3349 | *p++ = ssl->transform->in_cid_len; |
| 3350 | memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len ); |
| 3351 | p += ssl->transform->in_cid_len; |
| 3352 | |
| 3353 | *p++ = ssl->transform->out_cid_len; |
| 3354 | memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len ); |
| 3355 | p += ssl->transform->out_cid_len; |
| 3356 | } |
| 3357 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3358 | |
| 3359 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3360 | * Saved fields from top-level ssl_context structure |
| 3361 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3362 | used += 4; |
| 3363 | if( used <= buf_len ) |
| 3364 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3365 | MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); |
| 3366 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3367 | } |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3368 | |
| 3369 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3370 | used += 16; |
| 3371 | if( used <= buf_len ) |
| 3372 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3373 | MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); |
| 3374 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3375 | |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3376 | MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); |
| 3377 | p += 8; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3378 | } |
| 3379 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3380 | |
| 3381 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3382 | used += 1; |
| 3383 | if( used <= buf_len ) |
| 3384 | { |
| 3385 | *p++ = ssl->disable_datagram_packing; |
| 3386 | } |
| 3387 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3388 | |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3389 | used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3390 | if( used <= buf_len ) |
| 3391 | { |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 3392 | memcpy( p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN ); |
| 3393 | p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3397 | used += 2; |
| 3398 | if( used <= buf_len ) |
| 3399 | { |
Joe Subbiani | 1f6c3ae | 2021-08-20 11:44:44 +0100 | [diff] [blame] | 3400 | MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); |
| 3401 | p += 2; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3402 | } |
| 3403 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3404 | |
| 3405 | #if defined(MBEDTLS_SSL_ALPN) |
| 3406 | { |
| 3407 | const uint8_t alpn_len = ssl->alpn_chosen |
Manuel Pégourié-Gonnard | f041f4e | 2019-07-24 00:58:27 +0200 | [diff] [blame] | 3408 | ? (uint8_t) strlen( ssl->alpn_chosen ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3409 | : 0; |
| 3410 | |
| 3411 | used += 1 + alpn_len; |
| 3412 | if( used <= buf_len ) |
| 3413 | { |
| 3414 | *p++ = alpn_len; |
| 3415 | |
| 3416 | if( ssl->alpn_chosen != NULL ) |
| 3417 | { |
| 3418 | memcpy( p, ssl->alpn_chosen, alpn_len ); |
| 3419 | p += alpn_len; |
| 3420 | } |
| 3421 | } |
| 3422 | } |
| 3423 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3424 | |
| 3425 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3426 | * Done |
| 3427 | */ |
| 3428 | *olen = used; |
| 3429 | |
| 3430 | if( used > buf_len ) |
| 3431 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3432 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3433 | MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used ); |
| 3434 | |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3435 | return( mbedtls_ssl_session_reset_int( ssl, 0 ) ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3439 | * Deserialize context, see mbedtls_ssl_context_save() for format. |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3440 | * |
| 3441 | * This internal version is wrapped by a public function that cleans up in |
| 3442 | * case of error. |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3443 | */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3444 | static int ssl_context_load( mbedtls_ssl_context *ssl, |
| 3445 | const unsigned char *buf, |
| 3446 | size_t len ) |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3447 | { |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3448 | const unsigned char *p = buf; |
| 3449 | const unsigned char * const end = buf + len; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3450 | size_t session_len; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3451 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3452 | |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3453 | /* |
| 3454 | * The context should have been freshly setup or reset. |
| 3455 | * Give the user an error in case of obvious misuse. |
Manuel Pégourié-Gonnard | 4ca930f | 2019-07-26 16:31:53 +0200 | [diff] [blame] | 3456 | * (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] | 3457 | * renegotiating, or if the user mistakenly loaded a session first.) |
| 3458 | */ |
| 3459 | if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST || |
| 3460 | ssl->session != NULL ) |
| 3461 | { |
| 3462 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3463 | } |
| 3464 | |
| 3465 | /* |
| 3466 | * We can't check that the config matches the initial one, but we can at |
| 3467 | * least check it matches the requirements for serializing. |
| 3468 | */ |
| 3469 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM || |
| 3470 | ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 3471 | ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 || |
| 3472 | ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 || |
| 3473 | ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 || |
| 3474 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3475 | ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3476 | #endif |
Manuel Pégourié-Gonnard | 9a96fd7 | 2019-07-23 17:11:24 +0200 | [diff] [blame] | 3477 | 0 ) |
Manuel Pégourié-Gonnard | 0ff7640 | 2019-07-11 09:56:30 +0200 | [diff] [blame] | 3478 | { |
| 3479 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3480 | } |
| 3481 | |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3482 | MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len ); |
| 3483 | |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3484 | /* |
| 3485 | * Check version identifier |
| 3486 | */ |
| 3487 | if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) ) |
| 3488 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3489 | |
| 3490 | if( memcmp( p, ssl_serialized_context_header, |
| 3491 | sizeof( ssl_serialized_context_header ) ) != 0 ) |
| 3492 | { |
| 3493 | return( MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
| 3494 | } |
| 3495 | p += sizeof( ssl_serialized_context_header ); |
| 3496 | |
| 3497 | /* |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3498 | * Session |
| 3499 | */ |
| 3500 | if( (size_t)( end - p ) < 4 ) |
| 3501 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3502 | |
| 3503 | session_len = ( (size_t) p[0] << 24 ) | |
| 3504 | ( (size_t) p[1] << 16 ) | |
| 3505 | ( (size_t) p[2] << 8 ) | |
| 3506 | ( (size_t) p[3] ); |
| 3507 | p += 4; |
| 3508 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3509 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3510 | * 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] | 3511 | ssl->session = ssl->session_negotiate; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3512 | ssl->session_in = ssl->session; |
| 3513 | ssl->session_out = ssl->session; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3514 | ssl->session_negotiate = NULL; |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3515 | |
| 3516 | if( (size_t)( end - p ) < session_len ) |
| 3517 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3518 | |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3519 | ret = ssl_session_load( ssl->session, 1, p, session_len ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3520 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3521 | { |
| 3522 | mbedtls_ssl_session_free( ssl->session ); |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3523 | return( ret ); |
Manuel Pégourié-Gonnard | 45ac1f0 | 2019-07-23 16:52:45 +0200 | [diff] [blame] | 3524 | } |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3525 | |
| 3526 | p += session_len; |
| 3527 | |
| 3528 | /* |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3529 | * Transform |
| 3530 | */ |
| 3531 | |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3532 | /* This has been allocated by ssl_handshake_init(), called by |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3533 | * 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] | 3534 | ssl->transform = ssl->transform_negotiate; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3535 | ssl->transform_in = ssl->transform; |
| 3536 | ssl->transform_out = ssl->transform; |
Manuel Pégourié-Gonnard | 142ba73 | 2019-07-23 14:43:30 +0200 | [diff] [blame] | 3537 | ssl->transform_negotiate = NULL; |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3538 | |
| 3539 | /* Read random bytes and populate structure */ |
| 3540 | if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) ) |
| 3541 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3542 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3543 | ret = ssl_tls12_populate_transform( ssl->transform, |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3544 | ssl->session->ciphersuite, |
| 3545 | ssl->session->master, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3546 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 3547 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3548 | ssl->session->encrypt_then_mac, |
Hanno Becker | bd25755 | 2021-03-22 06:59:27 +0000 | [diff] [blame] | 3549 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 3550 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3551 | ssl_tls12prf_from_cs( ssl->session->ciphersuite ), |
| 3552 | p, /* currently pointing to randbytes */ |
| 3553 | MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */ |
| 3554 | ssl->conf->endpoint, |
| 3555 | ssl ); |
| 3556 | if( ret != 0 ) |
| 3557 | return( ret ); |
Jerry Yu | 840fbb2 | 2022-02-17 14:59:29 +0800 | [diff] [blame] | 3558 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | c2a7b89 | 2019-07-15 09:04:11 +0200 | [diff] [blame] | 3559 | p += sizeof( ssl->transform->randbytes ); |
| 3560 | |
| 3561 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3562 | /* Read connection IDs and store them */ |
| 3563 | if( (size_t)( end - p ) < 1 ) |
| 3564 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3565 | |
| 3566 | ssl->transform->in_cid_len = *p++; |
| 3567 | |
Manuel Pégourié-Gonnard | 5ea13b8 | 2019-07-23 15:02:54 +0200 | [diff] [blame] | 3568 | 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] | 3569 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3570 | |
| 3571 | memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len ); |
| 3572 | p += ssl->transform->in_cid_len; |
| 3573 | |
| 3574 | ssl->transform->out_cid_len = *p++; |
| 3575 | |
| 3576 | if( (size_t)( end - p ) < ssl->transform->out_cid_len ) |
| 3577 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3578 | |
| 3579 | memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len ); |
| 3580 | p += ssl->transform->out_cid_len; |
| 3581 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3582 | |
| 3583 | /* |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3584 | * Saved fields from top-level ssl_context structure |
| 3585 | */ |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3586 | if( (size_t)( end - p ) < 4 ) |
| 3587 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3588 | |
| 3589 | ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) | |
| 3590 | ( (uint32_t) p[1] << 16 ) | |
| 3591 | ( (uint32_t) p[2] << 8 ) | |
| 3592 | ( (uint32_t) p[3] ); |
| 3593 | p += 4; |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3594 | |
| 3595 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3596 | if( (size_t)( end - p ) < 16 ) |
| 3597 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3598 | |
| 3599 | ssl->in_window_top = ( (uint64_t) p[0] << 56 ) | |
| 3600 | ( (uint64_t) p[1] << 48 ) | |
| 3601 | ( (uint64_t) p[2] << 40 ) | |
| 3602 | ( (uint64_t) p[3] << 32 ) | |
| 3603 | ( (uint64_t) p[4] << 24 ) | |
| 3604 | ( (uint64_t) p[5] << 16 ) | |
| 3605 | ( (uint64_t) p[6] << 8 ) | |
| 3606 | ( (uint64_t) p[7] ); |
| 3607 | p += 8; |
| 3608 | |
| 3609 | ssl->in_window = ( (uint64_t) p[0] << 56 ) | |
| 3610 | ( (uint64_t) p[1] << 48 ) | |
| 3611 | ( (uint64_t) p[2] << 40 ) | |
| 3612 | ( (uint64_t) p[3] << 32 ) | |
| 3613 | ( (uint64_t) p[4] << 24 ) | |
| 3614 | ( (uint64_t) p[5] << 16 ) | |
| 3615 | ( (uint64_t) p[6] << 8 ) | |
| 3616 | ( (uint64_t) p[7] ); |
| 3617 | p += 8; |
| 3618 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
| 3619 | |
| 3620 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3621 | if( (size_t)( end - p ) < 1 ) |
| 3622 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3623 | |
| 3624 | ssl->disable_datagram_packing = *p++; |
| 3625 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3626 | |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3627 | if( (size_t)( end - p ) < sizeof( ssl->cur_out_ctr ) ) |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3628 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 3629 | memcpy( ssl->cur_out_ctr, p, sizeof( ssl->cur_out_ctr ) ); |
| 3630 | p += sizeof( ssl->cur_out_ctr ); |
Manuel Pégourié-Gonnard | c86c5df | 2019-07-15 11:23:03 +0200 | [diff] [blame] | 3631 | |
| 3632 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3633 | if( (size_t)( end - p ) < 2 ) |
| 3634 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3635 | |
| 3636 | ssl->mtu = ( p[0] << 8 ) | p[1]; |
| 3637 | p += 2; |
| 3638 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3639 | |
| 3640 | #if defined(MBEDTLS_SSL_ALPN) |
| 3641 | { |
| 3642 | uint8_t alpn_len; |
| 3643 | const char **cur; |
| 3644 | |
| 3645 | if( (size_t)( end - p ) < 1 ) |
| 3646 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3647 | |
| 3648 | alpn_len = *p++; |
| 3649 | |
| 3650 | if( alpn_len != 0 && ssl->conf->alpn_list != NULL ) |
| 3651 | { |
| 3652 | /* alpn_chosen should point to an item in the configured list */ |
| 3653 | for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ ) |
| 3654 | { |
| 3655 | if( strlen( *cur ) == alpn_len && |
| 3656 | memcmp( p, cur, alpn_len ) == 0 ) |
| 3657 | { |
| 3658 | ssl->alpn_chosen = *cur; |
| 3659 | break; |
| 3660 | } |
| 3661 | } |
| 3662 | } |
| 3663 | |
| 3664 | /* can only happen on conf mismatch */ |
| 3665 | if( alpn_len != 0 && ssl->alpn_chosen == NULL ) |
| 3666 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 3667 | |
| 3668 | p += alpn_len; |
| 3669 | } |
| 3670 | #endif /* MBEDTLS_SSL_ALPN */ |
| 3671 | |
| 3672 | /* |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3673 | * Forced fields from top-level ssl_context structure |
| 3674 | * |
| 3675 | * Most of them already set to the correct value by mbedtls_ssl_init() and |
| 3676 | * mbedtls_ssl_reset(), so we only need to set the remaining ones. |
| 3677 | */ |
| 3678 | ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER; |
| 3679 | |
| 3680 | ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 3681 | ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 3682 | |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3683 | /* Adjust pointers for header fields of outgoing records to |
| 3684 | * the given transform, accounting for explicit IV and CID. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 3685 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform ); |
Hanno Becker | 361b10d | 2019-08-30 10:42:49 +0100 | [diff] [blame] | 3686 | |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3687 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3688 | ssl->in_epoch = 1; |
| 3689 | #endif |
| 3690 | |
| 3691 | /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated, |
| 3692 | * 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] | 3693 | * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform() |
| 3694 | * inappropriately. */ |
Manuel Pégourié-Gonnard | 0eb3eac | 2019-07-15 11:53:51 +0200 | [diff] [blame] | 3695 | if( ssl->handshake != NULL ) |
| 3696 | { |
| 3697 | mbedtls_ssl_handshake_free( ssl ); |
| 3698 | mbedtls_free( ssl->handshake ); |
| 3699 | ssl->handshake = NULL; |
| 3700 | } |
| 3701 | |
| 3702 | /* |
Manuel Pégourié-Gonnard | 4c90e85 | 2019-07-11 10:58:10 +0200 | [diff] [blame] | 3703 | * Done - should have consumed entire buffer |
| 3704 | */ |
| 3705 | if( p != end ) |
| 3706 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ac87e28 | 2019-05-28 13:02:16 +0200 | [diff] [blame] | 3707 | |
| 3708 | return( 0 ); |
| 3709 | } |
| 3710 | |
| 3711 | /* |
Manuel Pégourié-Gonnard | b9dfc9f | 2019-07-12 10:50:19 +0200 | [diff] [blame] | 3712 | * Deserialize context: public wrapper for error cleaning |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3713 | */ |
| 3714 | int mbedtls_ssl_context_load( mbedtls_ssl_context *context, |
| 3715 | const unsigned char *buf, |
| 3716 | size_t len ) |
| 3717 | { |
| 3718 | int ret = ssl_context_load( context, buf, len ); |
| 3719 | |
| 3720 | if( ret != 0 ) |
| 3721 | mbedtls_ssl_free( context ); |
| 3722 | |
| 3723 | return( ret ); |
| 3724 | } |
Manuel Pégourié-Gonnard | 5c0e377 | 2019-07-23 16:13:17 +0200 | [diff] [blame] | 3725 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Manuel Pégourié-Gonnard | 4b7e6b9 | 2019-07-11 12:50:53 +0200 | [diff] [blame] | 3726 | |
| 3727 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3728 | * Free an SSL context |
| 3729 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3730 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3731 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 3732 | if( ssl == NULL ) |
| 3733 | return; |
| 3734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3735 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3736 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3737 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3738 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3739 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3740 | size_t out_buf_len = ssl->out_buf_len; |
| 3741 | #else |
| 3742 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 3743 | #endif |
| 3744 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3745 | mbedtls_platform_zeroize( ssl->out_buf, out_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3746 | mbedtls_free( ssl->out_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3747 | ssl->out_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 3750 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3751 | { |
sander-visser | b8aa207 | 2020-05-06 22:05:13 +0200 | [diff] [blame] | 3752 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 3753 | size_t in_buf_len = ssl->in_buf_len; |
| 3754 | #else |
| 3755 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 3756 | #endif |
| 3757 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3758 | mbedtls_platform_zeroize( ssl->in_buf, in_buf_len ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3759 | mbedtls_free( ssl->in_buf ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 3760 | ssl->in_buf = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3763 | if( ssl->transform ) |
| 3764 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3765 | mbedtls_ssl_transform_free( ssl->transform ); |
| 3766 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | if( ssl->handshake ) |
| 3770 | { |
Gilles Peskine | 9b562d5 | 2018-04-25 20:32:43 +0200 | [diff] [blame] | 3771 | mbedtls_ssl_handshake_free( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3772 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 3773 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3774 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3775 | mbedtls_free( ssl->handshake ); |
| 3776 | mbedtls_free( ssl->transform_negotiate ); |
| 3777 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3780 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3781 | mbedtls_ssl_transform_free( ssl->transform_application ); |
| 3782 | mbedtls_free( ssl->transform_application ); |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3783 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 3aa186f | 2021-08-10 09:24:19 +0100 | [diff] [blame] | 3784 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3785 | if( ssl->session ) |
| 3786 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3787 | mbedtls_ssl_session_free( ssl->session ); |
| 3788 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 3789 | } |
| 3790 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 3791 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 3792 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3793 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3794 | mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3795 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3796 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 3797 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3798 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 3799 | #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] | 3800 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 3801 | #endif |
| 3802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3803 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 3804 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 3805 | /* Actually clear after last debug message */ |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 3806 | mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 3809 | /* |
| 3810 | * Initialze mbedtls_ssl_config |
| 3811 | */ |
| 3812 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 3813 | { |
| 3814 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 3815 | } |
| 3816 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3817 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3818 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3819 | /* 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] | 3820 | * x509_crt.c. Here, the order matters. Currently we favor stronger hashes, |
| 3821 | * for no fundamental reason. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3822 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
| 3823 | * about this list. */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3824 | static int ssl_preset_default_hashes[] = { |
| 3825 | #if defined(MBEDTLS_SHA512_C) |
| 3826 | MBEDTLS_MD_SHA512, |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 3827 | #endif |
| 3828 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3829 | MBEDTLS_MD_SHA384, |
| 3830 | #endif |
| 3831 | #if defined(MBEDTLS_SHA256_C) |
| 3832 | MBEDTLS_MD_SHA256, |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 3833 | #endif |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3834 | MBEDTLS_MD_NONE |
| 3835 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3836 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
| 3837 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 3838 | |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3839 | /* The selection should be the same as mbedtls_x509_crt_profile_default in |
| 3840 | * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters: |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3841 | * curves with a lower resource usage come first. |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3842 | * See the documentation of mbedtls_ssl_conf_curves() for what we promise |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3843 | * about this list. |
| 3844 | */ |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3845 | static uint16_t ssl_preset_default_groups[] = { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3846 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3847 | MBEDTLS_SSL_IANA_TLS_GROUP_X25519, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3848 | #endif |
| 3849 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3850 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3851 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3852 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3853 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3854 | #endif |
| 3855 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3856 | MBEDTLS_SSL_IANA_TLS_GROUP_X448, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3857 | #endif |
| 3858 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3859 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3860 | #endif |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3861 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3862 | MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3863 | #endif |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3864 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3865 | MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3866 | #endif |
| 3867 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3868 | MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, |
Gilles Peskine | b1940a7 | 2021-06-02 15:18:12 +0200 | [diff] [blame] | 3869 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3870 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3871 | }; |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 3872 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3873 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 3874 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 3875 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 3876 | 0 |
| 3877 | }; |
| 3878 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 3879 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3880 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3881 | static int ssl_preset_suiteb_hashes[] = { |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3882 | #if defined(MBEDTLS_SHA256_C) |
| 3883 | MBEDTLS_MD_SHA256, |
| 3884 | #endif |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3885 | #if defined(MBEDTLS_SHA384_C) |
| 3886 | MBEDTLS_MD_SHA384, |
| 3887 | #endif |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3888 | MBEDTLS_MD_NONE |
| 3889 | }; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 3890 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3891 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3892 | /* NOTICE: |
Jerry Yu | 0b994b8 | 2022-01-25 17:22:12 +0800 | [diff] [blame] | 3893 | * 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] | 3894 | * rules SHOULD be upheld. |
| 3895 | * - No duplicate entries. |
| 3896 | * - But if there is a good reason, do not change the order of the algorithms. |
| 3897 | * - ssl_tls12_present* is for TLS 1.2 use only. |
| 3898 | * - 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] | 3899 | */ |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3900 | static uint16_t ssl_preset_default_sig_algs[] = { |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3901 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 3902 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 3903 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 3904 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 3905 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 3906 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3907 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3908 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 3909 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 3910 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 3911 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 3912 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 3913 | |
Jerry Yu | ed5e9f4 | 2022-01-26 11:21:34 +0800 | [diff] [blame] | 3914 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA512_C) && \ |
| 3915 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 3916 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512, |
| 3917 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 3918 | MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3919 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3920 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 3921 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 3922 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
| 3923 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 3924 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 3925 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 3926 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 3927 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3928 | MBEDTLS_TLS1_3_SIG_NONE |
| 3929 | }; |
| 3930 | |
| 3931 | /* NOTICE: see above */ |
| 3932 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3933 | static uint16_t ssl_tls12_preset_default_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3934 | #if defined(MBEDTLS_SHA512_C) |
| 3935 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA512 ) |
| 3936 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 3937 | #if defined(MBEDTLS_SHA384_C) |
| 3938 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 3939 | #endif |
| 3940 | #if defined(MBEDTLS_SHA256_C) |
| 3941 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 3942 | #endif |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3943 | MBEDTLS_TLS1_3_SIG_NONE |
| 3944 | }; |
| 3945 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3946 | /* NOTICE: see above */ |
| 3947 | static uint16_t ssl_preset_suiteb_sig_algs[] = { |
| 3948 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3949 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && \ |
| 3950 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 3951 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256, |
| 3952 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA256_C && |
| 3953 | MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 3954 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 3955 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA384_C) && \ |
| 3956 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 3957 | MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384, |
| 3958 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA384_C && |
| 3959 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3960 | |
| 3961 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_SHA256_C) |
| 3962 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
| 3963 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_SHA256_C */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3964 | |
Jerry Yu | 5303789 | 2022-01-25 11:02:06 +0800 | [diff] [blame] | 3965 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) |
| 3966 | MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256, |
| 3967 | #endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */ |
| 3968 | |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3969 | MBEDTLS_TLS1_3_SIG_NONE |
| 3970 | }; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 3971 | |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3972 | /* NOTICE: see above */ |
| 3973 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3974 | static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = { |
Jerry Yu | 713013f | 2022-01-17 18:16:35 +0800 | [diff] [blame] | 3975 | #if defined(MBEDTLS_SHA256_C) |
| 3976 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA256 ) |
| 3977 | #endif |
Jerry Yu | 18c833e | 2022-01-25 10:55:47 +0800 | [diff] [blame] | 3978 | #if defined(MBEDTLS_SHA384_C) |
| 3979 | MBEDTLS_SSL_SIG_ALG( MBEDTLS_SSL_HASH_SHA384 ) |
| 3980 | #endif |
Xiaofei Bai | 9539501 | 2021-11-25 08:51:30 +0000 | [diff] [blame] | 3981 | MBEDTLS_TLS1_3_SIG_NONE |
Hanno Becker | 9c6aa7b | 2021-08-10 13:50:43 +0100 | [diff] [blame] | 3982 | }; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3983 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3984 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3985 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3986 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3987 | static uint16_t ssl_preset_suiteb_groups[] = { |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 3988 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3989 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 3990 | #endif |
| 3991 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3992 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, |
Jaeden Amero | d431104 | 2019-06-03 08:27:16 +0100 | [diff] [blame] | 3993 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 3994 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3995 | }; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3996 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 3997 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 3998 | /* 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] | 3999 | * to make sure there are no duplicated signature algorithm entries. */ |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4000 | static int ssl_check_no_sig_alg_duplication( uint16_t * sig_algs ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4001 | { |
| 4002 | size_t i, j; |
| 4003 | int ret = 0; |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4004 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4005 | for( i = 0; sig_algs[i] != MBEDTLS_TLS1_3_SIG_NONE; i++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4006 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4007 | for( j = 0; j < i; j++ ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4008 | { |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4009 | if( sig_algs[i] != sig_algs[j] ) |
| 4010 | continue; |
| 4011 | mbedtls_printf( " entry(%04x,%" MBEDTLS_PRINTF_SIZET |
| 4012 | ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n", |
| 4013 | sig_algs[i], j, i ); |
| 4014 | ret = -1; |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | return( ret ); |
| 4018 | } |
| 4019 | |
| 4020 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4021 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4022 | /* |
Tillmann Karras | 588ad50 | 2015-09-25 04:27:22 +0200 | [diff] [blame] | 4023 | * Load default in mbedtls_ssl_config |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4024 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4025 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4026 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4027 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4028 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4029 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4030 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4031 | |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4032 | #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4033 | if( ssl_check_no_sig_alg_duplication( ssl_preset_suiteb_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4034 | { |
| 4035 | mbedtls_printf( "ssl_preset_suiteb_sig_algs has duplicated entries\n" ); |
| 4036 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4037 | } |
| 4038 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4039 | if( ssl_check_no_sig_alg_duplication( ssl_preset_default_sig_algs ) ) |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4040 | { |
| 4041 | mbedtls_printf( "ssl_preset_default_sig_algs has duplicated entries\n" ); |
| 4042 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4043 | } |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4044 | |
| 4045 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4046 | 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] | 4047 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4048 | mbedtls_printf( "ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4049 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4050 | } |
| 4051 | |
Jerry Yu | f377d64 | 2022-01-25 10:43:59 +0800 | [diff] [blame] | 4052 | 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] | 4053 | { |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4054 | mbedtls_printf( "ssl_tls12_preset_default_sig_algs has duplicated entries\n" ); |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4055 | return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ); |
| 4056 | } |
| 4057 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Jerry Yu | 1a8b481 | 2022-01-20 17:56:50 +0800 | [diff] [blame] | 4058 | #endif /* MBEDTLS_DEBUG_C && MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 4059 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 4060 | /* Use the functions here so that they are covered in tests, |
| 4061 | * but otherwise access member directly for efficiency */ |
| 4062 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 4063 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4064 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4065 | /* |
| 4066 | * Things that are common to all presets |
| 4067 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 4068 | #if defined(MBEDTLS_SSL_CLI_C) |
| 4069 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4070 | { |
| 4071 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4072 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 4073 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 4074 | #endif |
| 4075 | } |
| 4076 | #endif |
| 4077 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4078 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4079 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 4080 | #endif |
| 4081 | |
| 4082 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 4083 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 4084 | #endif |
| 4085 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4086 | #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] | 4087 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 4088 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 4089 | #endif |
| 4090 | |
| 4091 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4092 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 4093 | #endif |
| 4094 | |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4095 | #if defined(MBEDTLS_SSL_SRV_C) |
| 4096 | conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED; |
TRodziewicz | 3946f79 | 2021-06-14 12:11:18 +0200 | [diff] [blame] | 4097 | conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER; |
Janos Follath | 088ce43 | 2017-04-10 12:42:31 +0100 | [diff] [blame] | 4098 | #endif |
| 4099 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4100 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4101 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 4102 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 4103 | #endif |
| 4104 | |
| 4105 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4106 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4107 | memset( conf->renego_period, 0x00, 2 ); |
| 4108 | memset( conf->renego_period + 2, 0xFF, 6 ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4109 | #endif |
| 4110 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4111 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4112 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4113 | { |
| 4114 | const unsigned char dhm_p[] = |
| 4115 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; |
| 4116 | const unsigned char dhm_g[] = |
| 4117 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; |
Hanno Becker | 00d0a68 | 2017-10-04 13:14:29 +0100 | [diff] [blame] | 4118 | |
Hanno Becker | e2defad | 2021-07-24 05:59:17 +0100 | [diff] [blame] | 4119 | if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf, |
| 4120 | dhm_p, sizeof( dhm_p ), |
| 4121 | dhm_g, sizeof( dhm_g ) ) ) != 0 ) |
| 4122 | { |
| 4123 | return( ret ); |
| 4124 | } |
| 4125 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 4126 | #endif |
| 4127 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4128 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4129 | /* |
| 4130 | * Allow all TLS 1.3 key exchange modes by default. |
| 4131 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4132 | conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4133 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 71f1ed6 | 2021-07-24 06:01:47 +0100 | [diff] [blame] | 4134 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4135 | /* |
| 4136 | * Preset-specific defaults |
| 4137 | */ |
| 4138 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4139 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4140 | /* |
| 4141 | * NSA Suite B |
| 4142 | */ |
| 4143 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 4144 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 4145 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 4146 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4147 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4148 | /* Hybrid TLS 1.2/1.3 is not supported yet */ |
| 4149 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 4150 | #else |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4151 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4152 | #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] | 4153 | |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4154 | conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4155 | |
| 4156 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4157 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4158 | #endif |
| 4159 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4160 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4161 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4162 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4163 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4164 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4165 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4166 | conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs; |
| 4167 | else |
| 4168 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4169 | conf->sig_algs = ssl_preset_suiteb_sig_algs; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4170 | #endif |
| 4171 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4172 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4173 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4174 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4175 | conf->group_list = ssl_preset_suiteb_groups; |
Manuel Pégourié-Gonnard | c98204e | 2015-08-11 04:21:01 +0200 | [diff] [blame] | 4176 | break; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4177 | |
| 4178 | /* |
| 4179 | * Default |
| 4180 | */ |
| 4181 | default: |
Ron Eldor | 5e9f14d | 2017-05-28 10:46:38 +0300 | [diff] [blame] | 4182 | conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION > |
| 4183 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ? |
| 4184 | MBEDTLS_SSL_MIN_MAJOR_VERSION : |
| 4185 | MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION; |
| 4186 | conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION > |
| 4187 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ? |
| 4188 | MBEDTLS_SSL_MIN_MINOR_VERSION : |
| 4189 | MBEDTLS_SSL_MIN_VALID_MINOR_VERSION; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4190 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4191 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4192 | /* Hybrid TLS 1.2/1.3 is not supported yet */ |
| 4193 | conf->max_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 4194 | #else |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4195 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
Jerry Yu | 7d23963 | 2022-01-27 14:16:44 +0800 | [diff] [blame] | 4196 | #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] | 4197 | |
| 4198 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4199 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 4200 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4201 | #endif |
Hanno Becker | d60b6c6 | 2021-04-29 12:04:11 +0100 | [diff] [blame] | 4202 | conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites(); |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4203 | |
| 4204 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4205 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 4206 | #endif |
| 4207 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4208 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4209 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 47229c7 | 2015-12-04 15:02:56 +0100 | [diff] [blame] | 4210 | conf->sig_hashes = ssl_preset_default_hashes; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4211 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Jerry Yu | 909df7b | 2022-01-22 11:56:27 +0800 | [diff] [blame] | 4212 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4213 | if( mbedtls_ssl_conf_is_tls12_only( conf ) ) |
| 4214 | conf->sig_algs = ssl_tls12_preset_default_sig_algs; |
| 4215 | else |
| 4216 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4217 | conf->sig_algs = ssl_preset_default_sig_algs; |
Hanno Becker | deb68ce | 2021-08-10 16:04:05 +0100 | [diff] [blame] | 4218 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4219 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4220 | #if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 4221 | conf->curve_list = NULL; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4222 | #endif |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4223 | conf->group_list = ssl_preset_default_groups; |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 4224 | |
| 4225 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 4226 | conf->dhm_min_bitlen = 1024; |
| 4227 | #endif |
| 4228 | } |
| 4229 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4230 | return( 0 ); |
| 4231 | } |
| 4232 | |
| 4233 | /* |
| 4234 | * Free mbedtls_ssl_config |
| 4235 | */ |
| 4236 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 4237 | { |
| 4238 | #if defined(MBEDTLS_DHM_C) |
| 4239 | mbedtls_mpi_free( &conf->dhm_P ); |
| 4240 | mbedtls_mpi_free( &conf->dhm_G ); |
| 4241 | #endif |
| 4242 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 4243 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4244 | if( conf->psk != NULL ) |
| 4245 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4246 | mbedtls_platform_zeroize( conf->psk, conf->psk_len ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4247 | mbedtls_free( conf->psk ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4248 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4249 | conf->psk_len = 0; |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | if( conf->psk_identity != NULL ) |
| 4253 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4254 | mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len ); |
junyeonLEE | 316b162 | 2017-12-20 16:29:30 +0900 | [diff] [blame] | 4255 | mbedtls_free( conf->psk_identity ); |
Azim Khan | 27e8a12 | 2018-03-21 14:24:11 +0000 | [diff] [blame] | 4256 | conf->psk_identity = NULL; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4257 | conf->psk_identity_len = 0; |
| 4258 | } |
| 4259 | #endif |
| 4260 | |
| 4261 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4262 | ssl_key_cert_free( conf->key_cert ); |
| 4263 | #endif |
| 4264 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 4265 | mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 4266 | } |
| 4267 | |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4268 | #if defined(MBEDTLS_PK_C) && \ |
| 4269 | ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4270 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4271 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4272 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4273 | 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] | 4274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4275 | #if defined(MBEDTLS_RSA_C) |
| 4276 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 4277 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4278 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4279 | #if defined(MBEDTLS_ECDSA_C) |
| 4280 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 4281 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4282 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4283 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 4284 | } |
| 4285 | |
Hanno Becker | 7e5437a | 2017-04-28 17:15:26 +0100 | [diff] [blame] | 4286 | unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type ) |
| 4287 | { |
| 4288 | switch( type ) { |
| 4289 | case MBEDTLS_PK_RSA: |
| 4290 | return( MBEDTLS_SSL_SIG_RSA ); |
| 4291 | case MBEDTLS_PK_ECDSA: |
| 4292 | case MBEDTLS_PK_ECKEY: |
| 4293 | return( MBEDTLS_SSL_SIG_ECDSA ); |
| 4294 | default: |
| 4295 | return( MBEDTLS_SSL_SIG_ANON ); |
| 4296 | } |
| 4297 | } |
| 4298 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4299 | 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] | 4300 | { |
| 4301 | switch( sig ) |
| 4302 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4303 | #if defined(MBEDTLS_RSA_C) |
| 4304 | case MBEDTLS_SSL_SIG_RSA: |
| 4305 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4306 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4307 | #if defined(MBEDTLS_ECDSA_C) |
| 4308 | case MBEDTLS_SSL_SIG_ECDSA: |
| 4309 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4310 | #endif |
| 4311 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4312 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4313 | } |
| 4314 | } |
Manuel Pégourié-Gonnard | 5674a97 | 2015-10-19 15:14:03 +0200 | [diff] [blame] | 4315 | #endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4316 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4317 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4318 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 4319 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4320 | 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] | 4321 | { |
| 4322 | switch( hash ) |
| 4323 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4324 | #if defined(MBEDTLS_MD5_C) |
| 4325 | case MBEDTLS_SSL_HASH_MD5: |
| 4326 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4327 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4328 | #if defined(MBEDTLS_SHA1_C) |
| 4329 | case MBEDTLS_SSL_HASH_SHA1: |
| 4330 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4331 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4332 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4333 | case MBEDTLS_SSL_HASH_SHA224: |
| 4334 | return( MBEDTLS_MD_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4335 | #endif |
| 4336 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4337 | case MBEDTLS_SSL_HASH_SHA256: |
| 4338 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4339 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4340 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4341 | case MBEDTLS_SSL_HASH_SHA384: |
| 4342 | return( MBEDTLS_MD_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4343 | #endif |
| 4344 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4345 | case MBEDTLS_SSL_HASH_SHA512: |
| 4346 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4347 | #endif |
| 4348 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4349 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 4350 | } |
| 4351 | } |
| 4352 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4353 | /* |
| 4354 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 4355 | */ |
| 4356 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 4357 | { |
| 4358 | switch( md ) |
| 4359 | { |
| 4360 | #if defined(MBEDTLS_MD5_C) |
| 4361 | case MBEDTLS_MD_MD5: |
| 4362 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 4363 | #endif |
| 4364 | #if defined(MBEDTLS_SHA1_C) |
| 4365 | case MBEDTLS_MD_SHA1: |
| 4366 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 4367 | #endif |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4368 | #if defined(MBEDTLS_SHA224_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4369 | case MBEDTLS_MD_SHA224: |
| 4370 | return( MBEDTLS_SSL_HASH_SHA224 ); |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 4371 | #endif |
| 4372 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4373 | case MBEDTLS_MD_SHA256: |
| 4374 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 4375 | #endif |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4376 | #if defined(MBEDTLS_SHA384_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4377 | case MBEDTLS_MD_SHA384: |
| 4378 | return( MBEDTLS_SSL_HASH_SHA384 ); |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 4379 | #endif |
| 4380 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4381 | case MBEDTLS_MD_SHA512: |
| 4382 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 4383 | #endif |
| 4384 | default: |
| 4385 | return( MBEDTLS_SSL_HASH_NONE ); |
| 4386 | } |
| 4387 | } |
| 4388 | |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4389 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 4390 | * 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] | 4391 | * 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] | 4392 | */ |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4393 | 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] | 4394 | { |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4395 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4396 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4397 | if( group_list == NULL ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4398 | return( -1 ); |
| 4399 | |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4400 | for( ; *group_list != 0; group_list++ ) |
| 4401 | { |
| 4402 | if( *group_list == tls_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4403 | return( 0 ); |
Brett Warren | e0edc84 | 2021-08-17 09:53:13 +0100 | [diff] [blame] | 4404 | } |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4405 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4406 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4407 | } |
Manuel Pégourié-Gonnard | 0d63b84 | 2022-01-18 13:10:56 +0100 | [diff] [blame] | 4408 | |
| 4409 | #if defined(MBEDTLS_ECP_C) |
| 4410 | /* |
| 4411 | * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id. |
| 4412 | */ |
| 4413 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
| 4414 | { |
Manuel Pégourié-Gonnard | 422370d | 2022-02-07 11:55:21 +0100 | [diff] [blame] | 4415 | 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] | 4416 | return mbedtls_ssl_check_curve_tls_id( ssl, tls_id ); |
| 4417 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4418 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4420 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 4421 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 4422 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4423 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 4424 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4425 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4426 | int ret = 0; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4427 | int usage = 0; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4428 | const char *ext_oid; |
| 4429 | size_t ext_len; |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4430 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4431 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4432 | { |
| 4433 | /* Server part of the key exchange */ |
| 4434 | switch( ciphersuite->key_exchange ) |
| 4435 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4436 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 4437 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4438 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4439 | break; |
| 4440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4441 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 4442 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 4443 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 4444 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4445 | break; |
| 4446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4447 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 4448 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4449 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4450 | break; |
| 4451 | |
| 4452 | /* 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] | 4453 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 4454 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 4455 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 4456 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 557535d | 2015-09-15 17:53:32 +0200 | [diff] [blame] | 4457 | case MBEDTLS_KEY_EXCHANGE_ECJPAKE: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4458 | usage = 0; |
| 4459 | } |
| 4460 | } |
| 4461 | else |
| 4462 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4463 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 4464 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4465 | } |
| 4466 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4467 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4468 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4469 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4470 | ret = -1; |
| 4471 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4473 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4474 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4475 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 4476 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4477 | } |
| 4478 | else |
| 4479 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4480 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 4481 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4482 | } |
| 4483 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4484 | 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] | 4485 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 4486 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4487 | ret = -1; |
| 4488 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 4489 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4490 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4491 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4492 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 4493 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4494 | int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md ) |
| 4495 | { |
| 4496 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4497 | if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 4498 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4499 | |
| 4500 | switch( md ) |
| 4501 | { |
Mateusz Starzyk | c6d94ab | 2021-05-19 13:31:59 +0200 | [diff] [blame] | 4502 | #if defined(MBEDTLS_SHA384_C) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4503 | case MBEDTLS_SSL_HASH_SHA384: |
| 4504 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 4505 | break; |
| 4506 | #endif |
| 4507 | #if defined(MBEDTLS_SHA256_C) |
| 4508 | case MBEDTLS_SSL_HASH_SHA256: |
| 4509 | ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 4510 | break; |
| 4511 | #endif |
| 4512 | default: |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 4513 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | return 0; |
| 4517 | #else /* !MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4518 | (void) ssl; |
| 4519 | (void) md; |
| 4520 | |
Hanno Becker | 541af85 | 2021-05-14 16:49:01 +0100 | [diff] [blame] | 4521 | return( -1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4522 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4523 | } |
| 4524 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4525 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4526 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 4527 | const mbedtls_md_type_t md, |
| 4528 | unsigned char *dst, |
| 4529 | size_t dst_len, |
| 4530 | size_t *olen ) |
| 4531 | { |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4532 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 4533 | psa_hash_operation_t *hash_operation_to_clone; |
| 4534 | psa_hash_operation_t hash_operation = psa_hash_operation_init(); |
| 4535 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4536 | *olen = 0; |
Ronald Cron | f6893e1 | 2022-01-07 22:09:01 +0100 | [diff] [blame] | 4537 | |
| 4538 | switch( md ) |
| 4539 | { |
| 4540 | #if defined(MBEDTLS_SHA384_C) |
| 4541 | case MBEDTLS_MD_SHA384: |
| 4542 | hash_operation_to_clone = &ssl->handshake->fin_sha384_psa; |
| 4543 | break; |
| 4544 | #endif |
| 4545 | |
| 4546 | #if defined(MBEDTLS_SHA256_C) |
| 4547 | case MBEDTLS_MD_SHA256: |
| 4548 | hash_operation_to_clone = &ssl->handshake->fin_sha256_psa; |
| 4549 | break; |
| 4550 | #endif |
| 4551 | |
| 4552 | default: |
| 4553 | goto exit; |
| 4554 | } |
| 4555 | |
| 4556 | status = psa_hash_clone( hash_operation_to_clone, &hash_operation ); |
| 4557 | if( status != PSA_SUCCESS ) |
| 4558 | goto exit; |
| 4559 | |
| 4560 | status = psa_hash_finish( &hash_operation, dst, dst_len, olen ); |
| 4561 | if( status != PSA_SUCCESS ) |
| 4562 | goto exit; |
| 4563 | |
| 4564 | exit: |
Ronald Cron | b788c04 | 2022-02-15 09:14:15 +0100 | [diff] [blame] | 4565 | return( psa_ssl_status_to_mbedtls( status ) ); |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4566 | } |
| 4567 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 4568 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4569 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4570 | static int ssl_get_handshake_transcript_sha384( mbedtls_ssl_context *ssl, |
| 4571 | unsigned char *dst, |
| 4572 | size_t dst_len, |
| 4573 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4574 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4575 | int ret; |
| 4576 | mbedtls_sha512_context sha512; |
| 4577 | |
| 4578 | if( dst_len < 48 ) |
| 4579 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4580 | |
| 4581 | mbedtls_sha512_init( &sha512 ); |
| 4582 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 4583 | |
| 4584 | if( ( ret = mbedtls_sha512_finish( &sha512, dst ) ) != 0 ) |
| 4585 | { |
| 4586 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha512_finish", ret ); |
| 4587 | goto exit; |
| 4588 | } |
| 4589 | |
| 4590 | *olen = 48; |
| 4591 | |
| 4592 | exit: |
| 4593 | |
| 4594 | mbedtls_sha512_free( &sha512 ); |
| 4595 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4596 | } |
| 4597 | #endif /* MBEDTLS_SHA384_C */ |
| 4598 | |
| 4599 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4600 | static int ssl_get_handshake_transcript_sha256( mbedtls_ssl_context *ssl, |
| 4601 | unsigned char *dst, |
| 4602 | size_t dst_len, |
| 4603 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4604 | { |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4605 | int ret; |
| 4606 | mbedtls_sha256_context sha256; |
| 4607 | |
| 4608 | if( dst_len < 32 ) |
| 4609 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4610 | |
| 4611 | mbedtls_sha256_init( &sha256 ); |
| 4612 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4613 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4614 | if( ( ret = mbedtls_sha256_finish( &sha256, dst ) ) != 0 ) |
| 4615 | { |
| 4616 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha256_finish", ret ); |
| 4617 | goto exit; |
| 4618 | } |
| 4619 | |
| 4620 | *olen = 32; |
| 4621 | |
| 4622 | exit: |
| 4623 | |
| 4624 | mbedtls_sha256_free( &sha256 ); |
| 4625 | return( ret ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4626 | } |
| 4627 | #endif /* MBEDTLS_SHA256_C */ |
| 4628 | |
Jerry Yu | 000f976 | 2021-09-14 11:12:51 +0800 | [diff] [blame] | 4629 | int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl, |
| 4630 | const mbedtls_md_type_t md, |
| 4631 | unsigned char *dst, |
| 4632 | size_t dst_len, |
| 4633 | size_t *olen ) |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4634 | { |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4635 | switch( md ) |
| 4636 | { |
| 4637 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4638 | #if defined(MBEDTLS_SHA384_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4639 | case MBEDTLS_MD_SHA384: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4640 | return( ssl_get_handshake_transcript_sha384( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4641 | #endif /* MBEDTLS_SHA384_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4642 | |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4643 | #if defined(MBEDTLS_SHA256_C) |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4644 | case MBEDTLS_MD_SHA256: |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4645 | return( ssl_get_handshake_transcript_sha256( ssl, dst, dst_len, olen ) ); |
Jerry Yu | c10f6b4 | 2021-12-23 17:16:42 +0800 | [diff] [blame] | 4646 | #endif /* MBEDTLS_SHA256_C */ |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 4647 | |
| 4648 | default: |
| 4649 | break; |
| 4650 | } |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4651 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4652 | } |
XiaokangQian | 647719a | 2021-12-07 09:16:29 +0000 | [diff] [blame] | 4653 | |
Jerry Yu | 148165c | 2021-09-24 23:20:59 +0800 | [diff] [blame] | 4654 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Jerry Yu | 24c0ec3 | 2021-09-09 14:21:07 +0800 | [diff] [blame] | 4655 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4656 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) || \ |
| 4657 | defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \ |
| 4658 | defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4659 | /* |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4660 | * Function for writing a supported groups (TLS 1.3) or supported elliptic |
| 4661 | * curves (TLS 1.2) extension. |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4662 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4663 | * The "extension_data" field of a supported groups extension contains a |
| 4664 | * "NamedGroupList" value (TLS 1.3 RFC8446): |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4665 | * enum { |
| 4666 | * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019), |
| 4667 | * x25519(0x001D), x448(0x001E), |
| 4668 | * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102), |
| 4669 | * ffdhe6144(0x0103), ffdhe8192(0x0104), |
| 4670 | * ffdhe_private_use(0x01FC..0x01FF), |
| 4671 | * ecdhe_private_use(0xFE00..0xFEFF), |
| 4672 | * (0xFFFF) |
| 4673 | * } NamedGroup; |
| 4674 | * struct { |
| 4675 | * NamedGroup named_group_list<2..2^16-1>; |
| 4676 | * } NamedGroupList; |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4677 | * |
| 4678 | * The "extension_data" field of a supported elliptic curves extension contains |
| 4679 | * a "NamedCurveList" value (TLS 1.2 RFC 8422): |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4680 | * enum { |
| 4681 | * deprecated(1..22), |
| 4682 | * secp256r1 (23), secp384r1 (24), secp521r1 (25), |
| 4683 | * x25519(29), x448(30), |
| 4684 | * reserved (0xFE00..0xFEFF), |
| 4685 | * deprecated(0xFF01..0xFF02), |
| 4686 | * (0xFFFF) |
| 4687 | * } NamedCurve; |
| 4688 | * struct { |
| 4689 | * NamedCurve named_curve_list<2..2^16-1> |
| 4690 | * } NamedCurveList; |
| 4691 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4692 | * The TLS 1.3 supported groups extension was defined to be a compatible |
| 4693 | * generalization of the TLS 1.2 supported elliptic curves extension. They both |
| 4694 | * share the same extension identifier. |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4695 | * |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4696 | * DHE groups are not supported yet. |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4697 | */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4698 | int mbedtls_ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, |
| 4699 | unsigned char *buf, |
Jerry Yu | 1753261 | 2021-12-20 22:32:09 +0800 | [diff] [blame] | 4700 | const unsigned char *end, |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4701 | size_t *out_len ) |
| 4702 | { |
| 4703 | unsigned char *p = buf ; |
| 4704 | unsigned char *named_group_list; /* Start of named_group_list */ |
| 4705 | size_t named_group_list_len; /* Length of named_group_list */ |
| 4706 | const uint16_t *group_list = mbedtls_ssl_get_groups( ssl ); |
| 4707 | |
| 4708 | *out_len = 0; |
Jerry Yu | f46b016 | 2022-01-11 16:28:00 +0800 | [diff] [blame] | 4709 | |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4710 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) ); |
| 4711 | |
| 4712 | /* Check if we have space for header and length fields: |
| 4713 | * - extension_type (2 bytes) |
| 4714 | * - extension_data_length (2 bytes) |
| 4715 | * - named_group_list_length (2 bytes) |
| 4716 | */ |
| 4717 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 4718 | p += 6; |
| 4719 | |
| 4720 | named_group_list = p; |
| 4721 | |
| 4722 | if( group_list == NULL ) |
| 4723 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 4724 | |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4725 | for( ; *group_list != 0; group_list++ ) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4726 | { |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4727 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got supported group(%04x)", *group_list ) ); |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4728 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4729 | #if defined(MBEDTLS_ECP_C) |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4730 | if( ( mbedtls_ssl_conf_is_tls13_enabled( ssl->conf ) && |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4731 | mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) ) || |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4732 | ( mbedtls_ssl_conf_is_tls12_enabled( ssl->conf ) && |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4733 | mbedtls_ssl_tls12_named_group_is_ecdhe( *group_list ) ) ) |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4734 | { |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4735 | const mbedtls_ecp_curve_info *curve_info; |
| 4736 | curve_info = mbedtls_ecp_curve_info_from_tls_id( *group_list ); |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4737 | if( curve_info == NULL ) |
Jerry Yu | 3ad14ac | 2022-01-11 17:13:16 +0800 | [diff] [blame] | 4738 | continue; |
Jerry Yu | b925f21 | 2022-01-12 11:17:02 +0800 | [diff] [blame] | 4739 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 4740 | MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 ); |
| 4741 | p += 2; |
| 4742 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )", |
| 4743 | curve_info->name, *group_list ) ); |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4744 | } |
Jerry Yu | 63282b4 | 2022-01-11 15:43:53 +0800 | [diff] [blame] | 4745 | #endif /* MBEDTLS_ECP_C */ |
| 4746 | /* Add DHE groups here */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4747 | |
| 4748 | } |
| 4749 | |
Jerry Yu | 1510cea | 2022-01-12 10:56:49 +0800 | [diff] [blame] | 4750 | /* Length of named_group_list */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4751 | named_group_list_len = p - named_group_list; |
| 4752 | if( named_group_list_len == 0 ) |
| 4753 | { |
| 4754 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) ); |
| 4755 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4756 | } |
| 4757 | |
| 4758 | /* Write extension_type */ |
| 4759 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 ); |
| 4760 | /* Write extension_data_length */ |
| 4761 | MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 ); |
| 4762 | /* Write length of named_group_list */ |
| 4763 | MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 ); |
| 4764 | |
Jerry Yu | 7f029d8 | 2022-01-11 11:08:53 +0800 | [diff] [blame] | 4765 | MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", |
| 4766 | buf + 4, named_group_list_len + 2 ); |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4767 | |
| 4768 | *out_len = p - buf; |
| 4769 | |
| 4770 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4771 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS; |
| 4772 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4773 | |
| 4774 | return( 0 ); |
| 4775 | } |
| 4776 | |
Jerry Yu | 1ea9d10 | 2021-12-21 13:41:49 +0800 | [diff] [blame] | 4777 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED || |
| 4778 | MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || |
| 4779 | MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Jerry Yu | ba07342 | 2021-12-20 22:22:15 +0800 | [diff] [blame] | 4780 | |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4781 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 4782 | /* |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4783 | * Function for writing a signature algorithm extension. |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4784 | * |
Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 4785 | * The `extension_data` field of signature algorithm contains a `SignatureSchemeList` |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4786 | * value (TLS 1.3 RFC8446): |
| 4787 | * enum { |
| 4788 | * .... |
| 4789 | * ecdsa_secp256r1_sha256( 0x0403 ), |
| 4790 | * ecdsa_secp384r1_sha384( 0x0503 ), |
| 4791 | * ecdsa_secp521r1_sha512( 0x0603 ), |
| 4792 | * .... |
| 4793 | * } SignatureScheme; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4794 | * |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4795 | * struct { |
| 4796 | * SignatureScheme supported_signature_algorithms<2..2^16-2>; |
| 4797 | * } SignatureSchemeList; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4798 | * |
Jerry Yu | 8afd6e4 | 2022-01-20 15:54:26 +0800 | [diff] [blame] | 4799 | * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm` |
| 4800 | * value (TLS 1.2 RFC5246): |
Jerry Yu | 7ddc38c | 2022-01-19 11:08:05 +0800 | [diff] [blame] | 4801 | * enum { |
| 4802 | * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5), |
| 4803 | * sha512(6), (255) |
| 4804 | * } HashAlgorithm; |
| 4805 | * |
| 4806 | * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) } |
| 4807 | * SignatureAlgorithm; |
| 4808 | * |
| 4809 | * struct { |
| 4810 | * HashAlgorithm hash; |
| 4811 | * SignatureAlgorithm signature; |
| 4812 | * } SignatureAndHashAlgorithm; |
| 4813 | * |
| 4814 | * SignatureAndHashAlgorithm |
| 4815 | * supported_signature_algorithms<2..2^16-2>; |
| 4816 | * |
| 4817 | * The TLS 1.3 signature algorithm extension was defined to be a compatible |
| 4818 | * generalization of the TLS 1.2 signature algorithm extension. |
| 4819 | * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by |
| 4820 | * `SignatureScheme` field of TLS 1.3 |
| 4821 | * |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4822 | */ |
| 4823 | int mbedtls_ssl_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 4824 | const unsigned char *end, size_t *out_len ) |
| 4825 | { |
| 4826 | unsigned char *p = buf; |
| 4827 | unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */ |
| 4828 | size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */ |
| 4829 | |
| 4830 | *out_len = 0; |
| 4831 | |
| 4832 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) ); |
| 4833 | |
| 4834 | /* Check if we have space for header and length field: |
| 4835 | * - extension_type (2 bytes) |
| 4836 | * - extension_data_length (2 bytes) |
| 4837 | * - supported_signature_algorithms_length (2 bytes) |
| 4838 | */ |
| 4839 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); |
| 4840 | p += 6; |
| 4841 | |
| 4842 | /* |
| 4843 | * Write supported_signature_algorithms |
| 4844 | */ |
| 4845 | supported_sig_alg = p; |
Jerry Yu | 6106fdc | 2022-01-12 16:36:14 +0800 | [diff] [blame] | 4846 | const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs( ssl ); |
| 4847 | if( sig_alg == NULL ) |
| 4848 | return( MBEDTLS_ERR_SSL_BAD_CONFIG ); |
| 4849 | |
| 4850 | for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++ ) |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4851 | { |
Jerry Yu | 1bab301 | 2022-01-19 17:43:22 +0800 | [diff] [blame] | 4852 | if( ! mbedtls_ssl_sig_alg_is_supported( ssl, *sig_alg ) ) |
| 4853 | continue; |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4854 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); |
| 4855 | MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 ); |
| 4856 | p += 2; |
| 4857 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) ); |
| 4858 | } |
| 4859 | |
| 4860 | /* Length of supported_signature_algorithms */ |
| 4861 | supported_sig_alg_len = p - supported_sig_alg; |
| 4862 | if( supported_sig_alg_len == 0 ) |
| 4863 | { |
| 4864 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) ); |
| 4865 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4866 | } |
| 4867 | |
| 4868 | /* Write extension_type */ |
| 4869 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 ); |
| 4870 | /* Write extension_data_length */ |
| 4871 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 ); |
| 4872 | /* Write length of supported_signature_algorithms */ |
| 4873 | MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 ); |
| 4874 | |
| 4875 | /* Output the total length of signature algorithms extension. */ |
| 4876 | *out_len = p - buf; |
| 4877 | |
| 4878 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4879 | ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG; |
| 4880 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4881 | return( 0 ); |
| 4882 | } |
| 4883 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
Jerry Yu | c73c618 | 2022-02-08 20:29:25 +0800 | [diff] [blame] | 4884 | |
Jerry Yu | c5aef88 | 2021-12-23 20:15:02 +0800 | [diff] [blame] | 4885 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4886 | int mbedtls_ssl_write_hostname_ext( mbedtls_ssl_context *ssl, |
| 4887 | unsigned char *buf, |
| 4888 | const unsigned char *end, |
| 4889 | size_t *olen ) |
| 4890 | { |
| 4891 | unsigned char *p = buf; |
| 4892 | size_t hostname_len; |
| 4893 | |
| 4894 | *olen = 0; |
| 4895 | |
| 4896 | if( ssl->hostname == NULL ) |
| 4897 | return( 0 ); |
| 4898 | |
| 4899 | MBEDTLS_SSL_DEBUG_MSG( 3, |
| 4900 | ( "client hello, adding server name extension: %s", |
| 4901 | ssl->hostname ) ); |
| 4902 | |
| 4903 | hostname_len = strlen( ssl->hostname ); |
| 4904 | |
| 4905 | MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 ); |
| 4906 | |
| 4907 | /* |
| 4908 | * Sect. 3, RFC 6066 (TLS Extensions Definitions) |
| 4909 | * |
| 4910 | * In order to provide any of the server names, clients MAY include an |
| 4911 | * extension of type "server_name" in the (extended) client hello. The |
| 4912 | * "extension_data" field of this extension SHALL contain |
| 4913 | * "ServerNameList" where: |
| 4914 | * |
| 4915 | * struct { |
| 4916 | * NameType name_type; |
| 4917 | * select (name_type) { |
| 4918 | * case host_name: HostName; |
| 4919 | * } name; |
| 4920 | * } ServerName; |
| 4921 | * |
| 4922 | * enum { |
| 4923 | * host_name(0), (255) |
| 4924 | * } NameType; |
| 4925 | * |
| 4926 | * opaque HostName<1..2^16-1>; |
| 4927 | * |
| 4928 | * struct { |
| 4929 | * ServerName server_name_list<1..2^16-1> |
| 4930 | * } ServerNameList; |
| 4931 | * |
| 4932 | */ |
| 4933 | MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 ); |
| 4934 | p += 2; |
| 4935 | |
| 4936 | MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 ); |
| 4937 | p += 2; |
| 4938 | |
| 4939 | MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 ); |
| 4940 | p += 2; |
| 4941 | |
| 4942 | *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); |
| 4943 | |
| 4944 | MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); |
| 4945 | p += 2; |
| 4946 | |
| 4947 | memcpy( p, ssl->hostname, hostname_len ); |
| 4948 | |
| 4949 | *olen = hostname_len + 9; |
| 4950 | |
| 4951 | return( 0 ); |
| 4952 | } |
| 4953 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Jerry Yu | f017ee4 | 2022-01-12 15:49:48 +0800 | [diff] [blame] | 4954 | |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 4955 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4956 | |
| 4957 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4958 | |
| 4959 | static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation, |
| 4960 | mbedtls_svc_key_id_t key, |
| 4961 | psa_algorithm_t alg, |
| 4962 | const unsigned char* seed, size_t seed_length, |
| 4963 | const unsigned char* label, size_t label_length, |
| 4964 | size_t capacity ) |
| 4965 | { |
| 4966 | psa_status_t status; |
| 4967 | |
| 4968 | status = psa_key_derivation_setup( derivation, alg ); |
| 4969 | if( status != PSA_SUCCESS ) |
| 4970 | return( status ); |
| 4971 | |
| 4972 | if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 4973 | { |
| 4974 | status = psa_key_derivation_input_bytes( derivation, |
| 4975 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 4976 | seed, seed_length ); |
| 4977 | if( status != PSA_SUCCESS ) |
| 4978 | return( status ); |
| 4979 | |
| 4980 | if( mbedtls_svc_key_id_is_null( key ) ) |
| 4981 | { |
| 4982 | status = psa_key_derivation_input_bytes( |
| 4983 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 4984 | NULL, 0 ); |
| 4985 | } |
| 4986 | else |
| 4987 | { |
| 4988 | status = psa_key_derivation_input_key( |
| 4989 | derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key ); |
| 4990 | } |
| 4991 | if( status != PSA_SUCCESS ) |
| 4992 | return( status ); |
| 4993 | |
| 4994 | status = psa_key_derivation_input_bytes( derivation, |
| 4995 | PSA_KEY_DERIVATION_INPUT_LABEL, |
| 4996 | label, label_length ); |
| 4997 | if( status != PSA_SUCCESS ) |
| 4998 | return( status ); |
| 4999 | } |
| 5000 | else |
| 5001 | { |
| 5002 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 5003 | } |
| 5004 | |
| 5005 | status = psa_key_derivation_set_capacity( derivation, capacity ); |
| 5006 | if( status != PSA_SUCCESS ) |
| 5007 | return( status ); |
| 5008 | |
| 5009 | return( PSA_SUCCESS ); |
| 5010 | } |
| 5011 | |
| 5012 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5013 | const unsigned char *secret, size_t slen, |
| 5014 | const char *label, |
| 5015 | const unsigned char *random, size_t rlen, |
| 5016 | unsigned char *dstbuf, size_t dlen ) |
| 5017 | { |
| 5018 | psa_status_t status; |
| 5019 | psa_algorithm_t alg; |
| 5020 | mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5021 | psa_key_derivation_operation_t derivation = |
| 5022 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5023 | |
| 5024 | if( md_type == MBEDTLS_MD_SHA384 ) |
| 5025 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); |
| 5026 | else |
| 5027 | alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); |
| 5028 | |
| 5029 | /* Normally a "secret" should be long enough to be impossible to |
| 5030 | * find by brute force, and in particular should not be empty. But |
| 5031 | * this PRF is also used to derive an IV, in particular in EAP-TLS, |
| 5032 | * and for this use case it makes sense to have a 0-length "secret". |
| 5033 | * Since the key API doesn't allow importing a key of length 0, |
| 5034 | * keep master_key=0, which setup_psa_key_derivation() understands |
| 5035 | * to mean a 0-length "secret" input. */ |
| 5036 | if( slen != 0 ) |
| 5037 | { |
| 5038 | psa_key_attributes_t key_attributes = psa_key_attributes_init(); |
| 5039 | psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); |
| 5040 | psa_set_key_algorithm( &key_attributes, alg ); |
| 5041 | psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE ); |
| 5042 | |
| 5043 | status = psa_import_key( &key_attributes, secret, slen, &master_key ); |
| 5044 | if( status != PSA_SUCCESS ) |
| 5045 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5046 | } |
| 5047 | |
| 5048 | status = setup_psa_key_derivation( &derivation, |
| 5049 | master_key, alg, |
| 5050 | random, rlen, |
| 5051 | (unsigned char const *) label, |
| 5052 | (size_t) strlen( label ), |
| 5053 | dlen ); |
| 5054 | if( status != PSA_SUCCESS ) |
| 5055 | { |
| 5056 | psa_key_derivation_abort( &derivation ); |
| 5057 | psa_destroy_key( master_key ); |
| 5058 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5059 | } |
| 5060 | |
| 5061 | status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen ); |
| 5062 | if( status != PSA_SUCCESS ) |
| 5063 | { |
| 5064 | psa_key_derivation_abort( &derivation ); |
| 5065 | psa_destroy_key( master_key ); |
| 5066 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5067 | } |
| 5068 | |
| 5069 | status = psa_key_derivation_abort( &derivation ); |
| 5070 | if( status != PSA_SUCCESS ) |
| 5071 | { |
| 5072 | psa_destroy_key( master_key ); |
| 5073 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5074 | } |
| 5075 | |
| 5076 | if( ! mbedtls_svc_key_id_is_null( master_key ) ) |
| 5077 | status = psa_destroy_key( master_key ); |
| 5078 | if( status != PSA_SUCCESS ) |
| 5079 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5080 | |
| 5081 | return( 0 ); |
| 5082 | } |
| 5083 | |
| 5084 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5085 | |
| 5086 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
| 5087 | const unsigned char *secret, size_t slen, |
| 5088 | const char *label, |
| 5089 | const unsigned char *random, size_t rlen, |
| 5090 | unsigned char *dstbuf, size_t dlen ) |
| 5091 | { |
| 5092 | size_t nb; |
| 5093 | size_t i, j, k, md_len; |
| 5094 | unsigned char *tmp; |
| 5095 | size_t tmp_len = 0; |
| 5096 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 5097 | const mbedtls_md_info_t *md_info; |
| 5098 | mbedtls_md_context_t md_ctx; |
| 5099 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5100 | |
| 5101 | mbedtls_md_init( &md_ctx ); |
| 5102 | |
| 5103 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 5104 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5105 | |
| 5106 | md_len = mbedtls_md_get_size( md_info ); |
| 5107 | |
| 5108 | tmp_len = md_len + strlen( label ) + rlen; |
| 5109 | tmp = mbedtls_calloc( 1, tmp_len ); |
| 5110 | if( tmp == NULL ) |
| 5111 | { |
| 5112 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 5113 | goto exit; |
| 5114 | } |
| 5115 | |
| 5116 | nb = strlen( label ); |
| 5117 | memcpy( tmp + md_len, label, nb ); |
| 5118 | memcpy( tmp + md_len + nb, random, rlen ); |
| 5119 | nb += rlen; |
| 5120 | |
| 5121 | /* |
| 5122 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 5123 | */ |
| 5124 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
| 5125 | goto exit; |
| 5126 | |
| 5127 | ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 5128 | if( ret != 0 ) |
| 5129 | goto exit; |
| 5130 | ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 5131 | if( ret != 0 ) |
| 5132 | goto exit; |
| 5133 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5134 | if( ret != 0 ) |
| 5135 | goto exit; |
| 5136 | |
| 5137 | for( i = 0; i < dlen; i += md_len ) |
| 5138 | { |
| 5139 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5140 | if( ret != 0 ) |
| 5141 | goto exit; |
| 5142 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 5143 | if( ret != 0 ) |
| 5144 | goto exit; |
| 5145 | ret = mbedtls_md_hmac_finish( &md_ctx, h_i ); |
| 5146 | if( ret != 0 ) |
| 5147 | goto exit; |
| 5148 | |
| 5149 | ret = mbedtls_md_hmac_reset ( &md_ctx ); |
| 5150 | if( ret != 0 ) |
| 5151 | goto exit; |
| 5152 | ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 5153 | if( ret != 0 ) |
| 5154 | goto exit; |
| 5155 | ret = mbedtls_md_hmac_finish( &md_ctx, tmp ); |
| 5156 | if( ret != 0 ) |
| 5157 | goto exit; |
| 5158 | |
| 5159 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
| 5160 | |
| 5161 | for( j = 0; j < k; j++ ) |
| 5162 | dstbuf[i + j] = h_i[j]; |
| 5163 | } |
| 5164 | |
| 5165 | exit: |
| 5166 | mbedtls_md_free( &md_ctx ); |
| 5167 | |
| 5168 | mbedtls_platform_zeroize( tmp, tmp_len ); |
| 5169 | mbedtls_platform_zeroize( h_i, sizeof( h_i ) ); |
| 5170 | |
| 5171 | mbedtls_free( tmp ); |
| 5172 | |
| 5173 | return( ret ); |
| 5174 | } |
| 5175 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5176 | |
| 5177 | #if defined(MBEDTLS_SHA256_C) |
| 5178 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 5179 | const char *label, |
| 5180 | const unsigned char *random, size_t rlen, |
| 5181 | unsigned char *dstbuf, size_t dlen ) |
| 5182 | { |
| 5183 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
| 5184 | label, random, rlen, dstbuf, dlen ) ); |
| 5185 | } |
| 5186 | #endif /* MBEDTLS_SHA256_C */ |
| 5187 | |
| 5188 | #if defined(MBEDTLS_SHA384_C) |
| 5189 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 5190 | const char *label, |
| 5191 | const unsigned char *random, size_t rlen, |
| 5192 | unsigned char *dstbuf, size_t dlen ) |
| 5193 | { |
| 5194 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
| 5195 | label, random, rlen, dstbuf, dlen ) ); |
| 5196 | } |
| 5197 | #endif /* MBEDTLS_SHA384_C */ |
| 5198 | |
Jerry Yu | f009d86 | 2022-02-17 14:01:37 +0800 | [diff] [blame] | 5199 | /* |
| 5200 | * Set appropriate PRF function and other SSL / TLS1.2 functions |
| 5201 | * |
| 5202 | * Inputs: |
| 5203 | * - SSL/TLS minor version |
| 5204 | * - hash associated with the ciphersuite (only used by TLS 1.2) |
| 5205 | * |
| 5206 | * Outputs: |
| 5207 | * - the tls_prf, calc_verify and calc_finished members of handshake structure |
| 5208 | */ |
| 5209 | static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake, |
| 5210 | int minor_ver, |
| 5211 | mbedtls_md_type_t hash ) |
| 5212 | { |
| 5213 | |
| 5214 | #if defined(MBEDTLS_SHA384_C) |
| 5215 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 5216 | hash == MBEDTLS_MD_SHA384 ) |
| 5217 | { |
| 5218 | handshake->tls_prf = tls_prf_sha384; |
| 5219 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 5220 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
| 5221 | } |
| 5222 | else |
| 5223 | #endif |
| 5224 | #if defined(MBEDTLS_SHA256_C) |
| 5225 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 5226 | { |
| 5227 | handshake->tls_prf = tls_prf_sha256; |
| 5228 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 5229 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
| 5230 | } |
| 5231 | else |
| 5232 | #endif |
| 5233 | { |
| 5234 | (void) hash; |
| 5235 | (void) minor_ver; |
| 5236 | (void) handshake; |
| 5237 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5238 | } |
| 5239 | |
| 5240 | return( 0 ); |
| 5241 | } |
Jerry Yu | d6ab235 | 2022-02-17 14:03:43 +0800 | [diff] [blame] | 5242 | |
| 5243 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \ |
| 5244 | defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5245 | static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl ) |
| 5246 | { |
| 5247 | if( ssl->conf->f_psk != NULL ) |
| 5248 | { |
| 5249 | /* If we've used a callback to select the PSK, |
| 5250 | * the static configuration is irrelevant. */ |
| 5251 | if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) ) |
| 5252 | return( 1 ); |
| 5253 | |
| 5254 | return( 0 ); |
| 5255 | } |
| 5256 | |
| 5257 | if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) ) |
| 5258 | return( 1 ); |
| 5259 | |
| 5260 | return( 0 ); |
| 5261 | } |
| 5262 | #endif /* MBEDTLS_USE_PSA_CRYPTO && |
| 5263 | MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 5264 | |
Jerry Yu | 2a7b5ac | 2022-02-17 14:07:00 +0800 | [diff] [blame] | 5265 | /* |
| 5266 | * Compute master secret if needed |
| 5267 | * |
| 5268 | * Parameters: |
| 5269 | * [in/out] handshake |
| 5270 | * [in] resume, premaster, extended_ms, calc_verify, tls_prf |
| 5271 | * (PSA-PSK) ciphersuite_info, psk_opaque |
| 5272 | * [out] premaster (cleared) |
| 5273 | * [out] master |
| 5274 | * [in] ssl: optionally used for debugging, EMS and PSA-PSK |
| 5275 | * debug: conf->f_dbg, conf->p_dbg |
| 5276 | * EMS: passed to calc_verify (debug + session_negotiate) |
| 5277 | * PSA-PSA: minor_ver, conf |
| 5278 | */ |
| 5279 | static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake, |
| 5280 | unsigned char *master, |
| 5281 | const mbedtls_ssl_context *ssl ) |
| 5282 | { |
| 5283 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5284 | |
| 5285 | /* cf. RFC 5246, Section 8.1: |
| 5286 | * "The master secret is always exactly 48 bytes in length." */ |
| 5287 | size_t const master_secret_len = 48; |
| 5288 | |
| 5289 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5290 | unsigned char session_hash[48]; |
| 5291 | #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */ |
| 5292 | |
| 5293 | /* The label for the KDF used for key expansion. |
| 5294 | * This is either "master secret" or "extended master secret" |
| 5295 | * depending on whether the Extended Master Secret extension |
| 5296 | * is used. */ |
| 5297 | char const *lbl = "master secret"; |
| 5298 | |
| 5299 | /* The salt for the KDF used for key expansion. |
| 5300 | * - If the Extended Master Secret extension is not used, |
| 5301 | * this is ClientHello.Random + ServerHello.Random |
| 5302 | * (see Sect. 8.1 in RFC 5246). |
| 5303 | * - If the Extended Master Secret extension is used, |
| 5304 | * this is the transcript of the handshake so far. |
| 5305 | * (see Sect. 4 in RFC 7627). */ |
| 5306 | unsigned char const *salt = handshake->randbytes; |
| 5307 | size_t salt_len = 64; |
| 5308 | |
| 5309 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 5310 | !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \ |
| 5311 | !(defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5312 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)) |
| 5313 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 5314 | (void) ssl; |
| 5315 | #endif |
| 5316 | |
| 5317 | if( handshake->resume != 0 ) |
| 5318 | { |
| 5319 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
| 5320 | return( 0 ); |
| 5321 | } |
| 5322 | |
| 5323 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 5324 | if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
| 5325 | { |
| 5326 | lbl = "extended master secret"; |
| 5327 | salt = session_hash; |
| 5328 | handshake->calc_verify( ssl, session_hash, &salt_len ); |
| 5329 | |
| 5330 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret", |
| 5331 | session_hash, salt_len ); |
| 5332 | } |
| 5333 | #endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */ |
| 5334 | |
| 5335 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 5336 | defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 5337 | if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK && |
| 5338 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 5339 | ssl_use_opaque_psk( ssl ) == 1 ) |
| 5340 | { |
| 5341 | /* Perform PSK-to-MS expansion in a single step. */ |
| 5342 | psa_status_t status; |
| 5343 | psa_algorithm_t alg; |
| 5344 | mbedtls_svc_key_id_t psk; |
| 5345 | psa_key_derivation_operation_t derivation = |
| 5346 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 5347 | mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac; |
| 5348 | |
| 5349 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) ); |
| 5350 | |
| 5351 | psk = mbedtls_ssl_get_opaque_psk( ssl ); |
| 5352 | |
| 5353 | if( hash_alg == MBEDTLS_MD_SHA384 ) |
| 5354 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384); |
| 5355 | else |
| 5356 | alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256); |
| 5357 | |
| 5358 | status = setup_psa_key_derivation( &derivation, psk, alg, |
| 5359 | salt, salt_len, |
| 5360 | (unsigned char const *) lbl, |
| 5361 | (size_t) strlen( lbl ), |
| 5362 | master_secret_len ); |
| 5363 | if( status != PSA_SUCCESS ) |
| 5364 | { |
| 5365 | psa_key_derivation_abort( &derivation ); |
| 5366 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5367 | } |
| 5368 | |
| 5369 | status = psa_key_derivation_output_bytes( &derivation, |
| 5370 | master, |
| 5371 | master_secret_len ); |
| 5372 | if( status != PSA_SUCCESS ) |
| 5373 | { |
| 5374 | psa_key_derivation_abort( &derivation ); |
| 5375 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5376 | } |
| 5377 | |
| 5378 | status = psa_key_derivation_abort( &derivation ); |
| 5379 | if( status != PSA_SUCCESS ) |
| 5380 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
| 5381 | } |
| 5382 | else |
| 5383 | #endif |
| 5384 | { |
| 5385 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 5386 | lbl, salt, salt_len, |
| 5387 | master, |
| 5388 | master_secret_len ); |
| 5389 | if( ret != 0 ) |
| 5390 | { |
| 5391 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 5392 | return( ret ); |
| 5393 | } |
| 5394 | |
| 5395 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", |
| 5396 | handshake->premaster, |
| 5397 | handshake->pmslen ); |
| 5398 | |
| 5399 | mbedtls_platform_zeroize( handshake->premaster, |
| 5400 | sizeof(handshake->premaster) ); |
| 5401 | } |
| 5402 | |
| 5403 | return( 0 ); |
| 5404 | } |
| 5405 | |
Jerry Yu | d62f87e | 2022-02-17 14:09:02 +0800 | [diff] [blame] | 5406 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
| 5407 | { |
| 5408 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5409 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 5410 | ssl->handshake->ciphersuite_info; |
| 5411 | |
| 5412 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
| 5413 | |
| 5414 | /* Set PRF, calc_verify and calc_finished function pointers */ |
| 5415 | ret = ssl_set_handshake_prfs( ssl->handshake, |
| 5416 | ssl->minor_ver, |
| 5417 | ciphersuite_info->mac ); |
| 5418 | if( ret != 0 ) |
| 5419 | { |
| 5420 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret ); |
| 5421 | return( ret ); |
| 5422 | } |
| 5423 | |
| 5424 | /* Compute master secret if needed */ |
| 5425 | ret = ssl_compute_master( ssl->handshake, |
| 5426 | ssl->session_negotiate->master, |
| 5427 | ssl ); |
| 5428 | if( ret != 0 ) |
| 5429 | { |
| 5430 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret ); |
| 5431 | return( ret ); |
| 5432 | } |
| 5433 | |
| 5434 | /* Swap the client and server random values: |
| 5435 | * - MS derivation wanted client+server (RFC 5246 8.1) |
| 5436 | * - key derivation wants server+client (RFC 5246 6.3) */ |
| 5437 | { |
| 5438 | unsigned char tmp[64]; |
| 5439 | memcpy( tmp, ssl->handshake->randbytes, 64 ); |
| 5440 | memcpy( ssl->handshake->randbytes, tmp + 32, 32 ); |
| 5441 | memcpy( ssl->handshake->randbytes + 32, tmp, 32 ); |
| 5442 | mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); |
| 5443 | } |
| 5444 | |
| 5445 | /* Populate transform structure */ |
| 5446 | ret = ssl_tls12_populate_transform( ssl->transform_negotiate, |
| 5447 | ssl->session_negotiate->ciphersuite, |
| 5448 | ssl->session_negotiate->master, |
| 5449 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 5450 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 5451 | ssl->session_negotiate->encrypt_then_mac, |
| 5452 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 5453 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 5454 | ssl->handshake->tls_prf, |
| 5455 | ssl->handshake->randbytes, |
| 5456 | ssl->minor_ver, |
| 5457 | ssl->conf->endpoint, |
| 5458 | ssl ); |
| 5459 | if( ret != 0 ) |
| 5460 | { |
| 5461 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls12_populate_transform", ret ); |
| 5462 | return( ret ); |
| 5463 | } |
| 5464 | |
| 5465 | /* We no longer need Server/ClientHello.random values */ |
| 5466 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 5467 | sizeof( ssl->handshake->randbytes ) ); |
| 5468 | |
| 5469 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
| 5470 | |
| 5471 | return( 0 ); |
| 5472 | } |
Jerry Yu | 8392e0d | 2022-02-17 14:10:24 +0800 | [diff] [blame] | 5473 | |
| 5474 | #if defined(MBEDTLS_SHA256_C) |
| 5475 | void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl, |
| 5476 | unsigned char *hash, |
| 5477 | size_t *hlen ) |
| 5478 | { |
| 5479 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5480 | size_t hash_size; |
| 5481 | psa_status_t status; |
| 5482 | psa_hash_operation_t sha256_psa = psa_hash_operation_init(); |
| 5483 | |
| 5484 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); |
| 5485 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 5486 | if( status != PSA_SUCCESS ) |
| 5487 | { |
| 5488 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5489 | return; |
| 5490 | } |
| 5491 | |
| 5492 | status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); |
| 5493 | if( status != PSA_SUCCESS ) |
| 5494 | { |
| 5495 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5496 | return; |
| 5497 | } |
| 5498 | |
| 5499 | *hlen = 32; |
| 5500 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5501 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5502 | #else |
| 5503 | mbedtls_sha256_context sha256; |
| 5504 | |
| 5505 | mbedtls_sha256_init( &sha256 ); |
| 5506 | |
| 5507 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) ); |
| 5508 | |
| 5509 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 5510 | mbedtls_sha256_finish( &sha256, hash ); |
| 5511 | |
| 5512 | *hlen = 32; |
| 5513 | |
| 5514 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5515 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5516 | |
| 5517 | mbedtls_sha256_free( &sha256 ); |
| 5518 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5519 | return; |
| 5520 | } |
| 5521 | #endif /* MBEDTLS_SHA256_C */ |
| 5522 | |
Jerry Yu | c1cb384 | 2022-02-17 14:13:48 +0800 | [diff] [blame] | 5523 | #if defined(MBEDTLS_SHA384_C) |
| 5524 | void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl, |
| 5525 | unsigned char *hash, |
| 5526 | size_t *hlen ) |
| 5527 | { |
| 5528 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5529 | size_t hash_size; |
| 5530 | psa_status_t status; |
| 5531 | psa_hash_operation_t sha384_psa = psa_hash_operation_init(); |
| 5532 | |
| 5533 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); |
| 5534 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 5535 | if( status != PSA_SUCCESS ) |
| 5536 | { |
| 5537 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 5538 | return; |
| 5539 | } |
| 5540 | |
| 5541 | status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); |
| 5542 | if( status != PSA_SUCCESS ) |
| 5543 | { |
| 5544 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 5545 | return; |
| 5546 | } |
| 5547 | |
| 5548 | *hlen = 48; |
| 5549 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen ); |
| 5550 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); |
| 5551 | #else |
| 5552 | mbedtls_sha512_context sha512; |
| 5553 | |
| 5554 | mbedtls_sha512_init( &sha512 ); |
| 5555 | |
| 5556 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
| 5557 | |
| 5558 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 5559 | mbedtls_sha512_finish( &sha512, hash ); |
| 5560 | |
| 5561 | *hlen = 48; |
| 5562 | |
| 5563 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen ); |
| 5564 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
| 5565 | |
| 5566 | mbedtls_sha512_free( &sha512 ); |
| 5567 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5568 | return; |
| 5569 | } |
| 5570 | #endif /* MBEDTLS_SHA384_C */ |
| 5571 | |
Jerry Yu | ce3dca4 | 2022-02-17 14:16:37 +0800 | [diff] [blame] | 5572 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 5573 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
| 5574 | { |
| 5575 | unsigned char *p = ssl->handshake->premaster; |
| 5576 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
| 5577 | const unsigned char *psk = NULL; |
| 5578 | size_t psk_len = 0; |
| 5579 | |
| 5580 | if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len ) |
| 5581 | == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED ) |
| 5582 | { |
| 5583 | /* |
| 5584 | * This should never happen because the existence of a PSK is always |
| 5585 | * checked before calling this function |
| 5586 | */ |
| 5587 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5588 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5589 | } |
| 5590 | |
| 5591 | /* |
| 5592 | * PMS = struct { |
| 5593 | * opaque other_secret<0..2^16-1>; |
| 5594 | * opaque psk<0..2^16-1>; |
| 5595 | * }; |
| 5596 | * with "other_secret" depending on the particular key exchange |
| 5597 | */ |
| 5598 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 5599 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
| 5600 | { |
| 5601 | if( end - p < 2 ) |
| 5602 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5603 | |
| 5604 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5605 | p += 2; |
| 5606 | |
| 5607 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5608 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5609 | |
| 5610 | memset( p, 0, psk_len ); |
| 5611 | p += psk_len; |
| 5612 | } |
| 5613 | else |
| 5614 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 5615 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 5616 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 5617 | { |
| 5618 | /* |
| 5619 | * other_secret already set by the ClientKeyExchange message, |
| 5620 | * and is 48 bytes long |
| 5621 | */ |
| 5622 | if( end - p < 2 ) |
| 5623 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5624 | |
| 5625 | *p++ = 0; |
| 5626 | *p++ = 48; |
| 5627 | p += 48; |
| 5628 | } |
| 5629 | else |
| 5630 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 5631 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 5632 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
| 5633 | { |
| 5634 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5635 | size_t len; |
| 5636 | |
| 5637 | /* Write length only when we know the actual value */ |
| 5638 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
| 5639 | p + 2, end - ( p + 2 ), &len, |
| 5640 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5641 | { |
| 5642 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
| 5643 | return( ret ); |
| 5644 | } |
| 5645 | MBEDTLS_PUT_UINT16_BE( len, p, 0 ); |
| 5646 | p += 2 + len; |
| 5647 | |
| 5648 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
| 5649 | } |
| 5650 | else |
| 5651 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 5652 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 5653 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
| 5654 | { |
| 5655 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5656 | size_t zlen; |
| 5657 | |
| 5658 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
| 5659 | p + 2, end - ( p + 2 ), |
| 5660 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
| 5661 | { |
| 5662 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
| 5663 | return( ret ); |
| 5664 | } |
| 5665 | |
| 5666 | MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); |
| 5667 | p += 2 + zlen; |
| 5668 | |
| 5669 | MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, |
| 5670 | MBEDTLS_DEBUG_ECDH_Z ); |
| 5671 | } |
| 5672 | else |
| 5673 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
| 5674 | { |
| 5675 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5676 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5677 | } |
| 5678 | |
| 5679 | /* opaque psk<0..2^16-1>; */ |
| 5680 | if( end - p < 2 ) |
| 5681 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5682 | |
| 5683 | MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); |
| 5684 | p += 2; |
| 5685 | |
| 5686 | if( end < p || (size_t)( end - p ) < psk_len ) |
| 5687 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5688 | |
| 5689 | memcpy( p, psk, psk_len ); |
| 5690 | p += psk_len; |
| 5691 | |
| 5692 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 5693 | |
| 5694 | return( 0 ); |
| 5695 | } |
| 5696 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Jerry Yu | c2c673d | 2022-02-17 14:20:39 +0800 | [diff] [blame] | 5697 | |
| 5698 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5699 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
| 5700 | |
| 5701 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5702 | int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
| 5703 | { |
| 5704 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 5705 | * timeout if we were using the usual handshake doubling scheme */ |
| 5706 | if( ssl->conf->renego_max_records < 0 ) |
| 5707 | { |
| 5708 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
| 5709 | unsigned char doublings = 1; |
| 5710 | |
| 5711 | while( ratio != 0 ) |
| 5712 | { |
| 5713 | ++doublings; |
| 5714 | ratio >>= 1; |
| 5715 | } |
| 5716 | |
| 5717 | if( ++ssl->renego_records_seen > doublings ) |
| 5718 | { |
| 5719 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) ); |
| 5720 | return( 0 ); |
| 5721 | } |
| 5722 | } |
| 5723 | |
| 5724 | return( ssl_write_hello_request( ssl ) ); |
| 5725 | } |
| 5726 | #endif |
| 5727 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5728 | |
Jerry Yu | d952669 | 2022-02-17 14:23:47 +0800 | [diff] [blame] | 5729 | /* |
| 5730 | * Handshake functions |
| 5731 | */ |
| 5732 | #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 5733 | /* No certificate support -> dummy functions */ |
| 5734 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 5735 | { |
| 5736 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5737 | ssl->handshake->ciphersuite_info; |
| 5738 | |
| 5739 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 5740 | |
| 5741 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5742 | { |
| 5743 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5744 | ssl->state++; |
| 5745 | return( 0 ); |
| 5746 | } |
| 5747 | |
| 5748 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5749 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5750 | } |
| 5751 | |
| 5752 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 5753 | { |
| 5754 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5755 | ssl->handshake->ciphersuite_info; |
| 5756 | |
| 5757 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 5758 | |
| 5759 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5760 | { |
| 5761 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 5762 | ssl->state++; |
| 5763 | return( 0 ); |
| 5764 | } |
| 5765 | |
| 5766 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 5767 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5768 | } |
| 5769 | |
| 5770 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 5771 | /* Some certificate support -> implement write and parse */ |
| 5772 | |
| 5773 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
| 5774 | { |
| 5775 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 5776 | size_t i, n; |
| 5777 | const mbedtls_x509_crt *crt; |
| 5778 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 5779 | ssl->handshake->ciphersuite_info; |
| 5780 | |
| 5781 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
| 5782 | |
| 5783 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 5784 | { |
| 5785 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5786 | ssl->state++; |
| 5787 | return( 0 ); |
| 5788 | } |
| 5789 | |
| 5790 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5791 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5792 | { |
| 5793 | if( ssl->handshake->client_auth == 0 ) |
| 5794 | { |
| 5795 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
| 5796 | ssl->state++; |
| 5797 | return( 0 ); |
| 5798 | } |
| 5799 | } |
| 5800 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 5801 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5802 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 5803 | { |
| 5804 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
| 5805 | { |
| 5806 | /* Should never happen because we shouldn't have picked the |
| 5807 | * ciphersuite if we don't have a certificate. */ |
| 5808 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 5809 | } |
| 5810 | } |
| 5811 | #endif |
| 5812 | |
| 5813 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
| 5814 | |
| 5815 | /* |
| 5816 | * 0 . 0 handshake type |
| 5817 | * 1 . 3 handshake length |
| 5818 | * 4 . 6 length of all certs |
| 5819 | * 7 . 9 length of cert. 1 |
| 5820 | * 10 . n-1 peer certificate |
| 5821 | * n . n+2 length of cert. 2 |
| 5822 | * n+3 . ... upper level cert, etc. |
| 5823 | */ |
| 5824 | i = 7; |
| 5825 | crt = mbedtls_ssl_own_cert( ssl ); |
| 5826 | |
| 5827 | while( crt != NULL ) |
| 5828 | { |
| 5829 | n = crt->raw.len; |
| 5830 | if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i ) |
| 5831 | { |
| 5832 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %" MBEDTLS_PRINTF_SIZET |
| 5833 | " > %" MBEDTLS_PRINTF_SIZET, |
| 5834 | i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
| 5835 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 5836 | } |
| 5837 | |
| 5838 | ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); |
| 5839 | ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); |
| 5840 | ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); |
| 5841 | |
| 5842 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 5843 | i += n; crt = crt->next; |
| 5844 | } |
| 5845 | |
| 5846 | ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); |
| 5847 | ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); |
| 5848 | ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); |
| 5849 | |
| 5850 | ssl->out_msglen = i; |
| 5851 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5852 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
| 5853 | |
| 5854 | ssl->state++; |
| 5855 | |
| 5856 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 5857 | { |
| 5858 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 5859 | return( ret ); |
| 5860 | } |
| 5861 | |
| 5862 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
| 5863 | |
| 5864 | return( ret ); |
| 5865 | } |
| 5866 | |
| 5867 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5868 | |
| 5869 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 5870 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 5871 | unsigned char *crt_buf, |
| 5872 | size_t crt_buf_len ) |
| 5873 | { |
| 5874 | mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert; |
| 5875 | |
| 5876 | if( peer_crt == NULL ) |
| 5877 | return( -1 ); |
| 5878 | |
| 5879 | if( peer_crt->raw.len != crt_buf_len ) |
| 5880 | return( -1 ); |
| 5881 | |
| 5882 | return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) ); |
| 5883 | } |
| 5884 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5885 | static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl, |
| 5886 | unsigned char *crt_buf, |
| 5887 | size_t crt_buf_len ) |
| 5888 | { |
| 5889 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5890 | unsigned char const * const peer_cert_digest = |
| 5891 | ssl->session->peer_cert_digest; |
| 5892 | mbedtls_md_type_t const peer_cert_digest_type = |
| 5893 | ssl->session->peer_cert_digest_type; |
| 5894 | mbedtls_md_info_t const * const digest_info = |
| 5895 | mbedtls_md_info_from_type( peer_cert_digest_type ); |
| 5896 | unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN]; |
| 5897 | size_t digest_len; |
| 5898 | |
| 5899 | if( peer_cert_digest == NULL || digest_info == NULL ) |
| 5900 | return( -1 ); |
| 5901 | |
| 5902 | digest_len = mbedtls_md_get_size( digest_info ); |
| 5903 | if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN ) |
| 5904 | return( -1 ); |
| 5905 | |
| 5906 | ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest ); |
| 5907 | if( ret != 0 ) |
| 5908 | return( -1 ); |
| 5909 | |
| 5910 | return( memcmp( tmp_digest, peer_cert_digest, digest_len ) ); |
| 5911 | } |
| 5912 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 5913 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 5914 | |
| 5915 | /* |
| 5916 | * Once the certificate message is read, parse it into a cert chain and |
| 5917 | * perform basic checks, but leave actual verification to the caller |
| 5918 | */ |
| 5919 | static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl, |
| 5920 | mbedtls_x509_crt *chain ) |
| 5921 | { |
| 5922 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 5923 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 5924 | int crt_cnt=0; |
| 5925 | #endif |
| 5926 | size_t i, n; |
| 5927 | uint8_t alert; |
| 5928 | |
| 5929 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 5930 | { |
| 5931 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5932 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5933 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 5934 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5935 | } |
| 5936 | |
| 5937 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ) |
| 5938 | { |
| 5939 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5940 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 5941 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5942 | } |
| 5943 | |
| 5944 | if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
| 5945 | { |
| 5946 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5947 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5948 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5949 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5950 | } |
| 5951 | |
| 5952 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
| 5953 | |
| 5954 | /* |
| 5955 | * Same message structure as in mbedtls_ssl_write_certificate() |
| 5956 | */ |
| 5957 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
| 5958 | |
| 5959 | if( ssl->in_msg[i] != 0 || |
| 5960 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
| 5961 | { |
| 5962 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5963 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5964 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5965 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5966 | } |
| 5967 | |
| 5968 | /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */ |
| 5969 | i += 3; |
| 5970 | |
| 5971 | /* Iterate through and parse the CRTs in the provided chain. */ |
| 5972 | while( i < ssl->in_hslen ) |
| 5973 | { |
| 5974 | /* Check that there's room for the next CRT's length fields. */ |
| 5975 | if ( i + 3 > ssl->in_hslen ) { |
| 5976 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5977 | mbedtls_ssl_send_alert_message( ssl, |
| 5978 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5979 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 5980 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 5981 | } |
| 5982 | /* In theory, the CRT can be up to 2**24 Bytes, but we don't support |
| 5983 | * anything beyond 2**16 ~ 64K. */ |
| 5984 | if( ssl->in_msg[i] != 0 ) |
| 5985 | { |
| 5986 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 5987 | mbedtls_ssl_send_alert_message( ssl, |
| 5988 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5989 | MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT ); |
| 5990 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 5991 | } |
| 5992 | |
| 5993 | /* Read length of the next CRT in the chain. */ |
| 5994 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 5995 | | (unsigned int) ssl->in_msg[i + 2]; |
| 5996 | i += 3; |
| 5997 | |
| 5998 | if( n < 128 || i + n > ssl->in_hslen ) |
| 5999 | { |
| 6000 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 6001 | mbedtls_ssl_send_alert_message( ssl, |
| 6002 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6003 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6004 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
| 6005 | } |
| 6006 | |
| 6007 | /* Check if we're handling the first CRT in the chain. */ |
| 6008 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
| 6009 | if( crt_cnt++ == 0 && |
| 6010 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 6011 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6012 | { |
| 6013 | /* During client-side renegotiation, check that the server's |
| 6014 | * end-CRTs hasn't changed compared to the initial handshake, |
| 6015 | * mitigating the triple handshake attack. On success, reuse |
| 6016 | * the original end-CRT instead of parsing it again. */ |
| 6017 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) ); |
| 6018 | if( ssl_check_peer_crt_unchanged( ssl, |
| 6019 | &ssl->in_msg[i], |
| 6020 | n ) != 0 ) |
| 6021 | { |
| 6022 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 6023 | mbedtls_ssl_send_alert_message( ssl, |
| 6024 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6025 | MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED ); |
| 6026 | return( MBEDTLS_ERR_SSL_BAD_CERTIFICATE ); |
| 6027 | } |
| 6028 | |
| 6029 | /* Now we can safely free the original chain. */ |
| 6030 | ssl_clear_peer_cert( ssl->session ); |
| 6031 | } |
| 6032 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
| 6033 | |
| 6034 | /* Parse the next certificate in the chain. */ |
| 6035 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6036 | ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n ); |
| 6037 | #else |
| 6038 | /* If we don't need to store the CRT chain permanently, parse |
| 6039 | * it in-place from the input buffer instead of making a copy. */ |
| 6040 | ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n ); |
| 6041 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6042 | switch( ret ) |
| 6043 | { |
| 6044 | case 0: /*ok*/ |
| 6045 | case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND: |
| 6046 | /* Ignore certificate with an unknown algorithm: maybe a |
| 6047 | prior certificate was already trusted. */ |
| 6048 | break; |
| 6049 | |
| 6050 | case MBEDTLS_ERR_X509_ALLOC_FAILED: |
| 6051 | alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR; |
| 6052 | goto crt_parse_der_failed; |
| 6053 | |
| 6054 | case MBEDTLS_ERR_X509_UNKNOWN_VERSION: |
| 6055 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6056 | goto crt_parse_der_failed; |
| 6057 | |
| 6058 | default: |
| 6059 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6060 | crt_parse_der_failed: |
| 6061 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert ); |
| 6062 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
| 6063 | return( ret ); |
| 6064 | } |
| 6065 | |
| 6066 | i += n; |
| 6067 | } |
| 6068 | |
| 6069 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain ); |
| 6070 | return( 0 ); |
| 6071 | } |
| 6072 | |
| 6073 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6074 | static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl ) |
| 6075 | { |
| 6076 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6077 | return( -1 ); |
| 6078 | |
| 6079 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 6080 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 6081 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 6082 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
| 6083 | { |
| 6084 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
| 6085 | return( 0 ); |
| 6086 | } |
| 6087 | return( -1 ); |
| 6088 | } |
| 6089 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6090 | |
| 6091 | /* Check if a certificate message is expected. |
| 6092 | * Return either |
| 6093 | * - SSL_CERTIFICATE_EXPECTED, or |
| 6094 | * - SSL_CERTIFICATE_SKIP |
| 6095 | * indicating whether a Certificate message is expected or not. |
| 6096 | */ |
| 6097 | #define SSL_CERTIFICATE_EXPECTED 0 |
| 6098 | #define SSL_CERTIFICATE_SKIP 1 |
| 6099 | static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl, |
| 6100 | int authmode ) |
| 6101 | { |
| 6102 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6103 | ssl->handshake->ciphersuite_info; |
| 6104 | |
| 6105 | if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) ) |
| 6106 | return( SSL_CERTIFICATE_SKIP ); |
| 6107 | |
| 6108 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6109 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6110 | { |
| 6111 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 6112 | return( SSL_CERTIFICATE_SKIP ); |
| 6113 | |
| 6114 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6115 | { |
| 6116 | ssl->session_negotiate->verify_result = |
| 6117 | MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
| 6118 | return( SSL_CERTIFICATE_SKIP ); |
| 6119 | } |
| 6120 | } |
| 6121 | #else |
| 6122 | ((void) authmode); |
| 6123 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6124 | |
| 6125 | return( SSL_CERTIFICATE_EXPECTED ); |
| 6126 | } |
| 6127 | |
| 6128 | static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl, |
| 6129 | int authmode, |
| 6130 | mbedtls_x509_crt *chain, |
| 6131 | void *rs_ctx ) |
| 6132 | { |
| 6133 | int ret = 0; |
| 6134 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = |
| 6135 | ssl->handshake->ciphersuite_info; |
| 6136 | int have_ca_chain = 0; |
| 6137 | |
| 6138 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *); |
| 6139 | void *p_vrfy; |
| 6140 | |
| 6141 | if( authmode == MBEDTLS_SSL_VERIFY_NONE ) |
| 6142 | return( 0 ); |
| 6143 | |
| 6144 | if( ssl->f_vrfy != NULL ) |
| 6145 | { |
| 6146 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) ); |
| 6147 | f_vrfy = ssl->f_vrfy; |
| 6148 | p_vrfy = ssl->p_vrfy; |
| 6149 | } |
| 6150 | else |
| 6151 | { |
| 6152 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) ); |
| 6153 | f_vrfy = ssl->conf->f_vrfy; |
| 6154 | p_vrfy = ssl->conf->p_vrfy; |
| 6155 | } |
| 6156 | |
| 6157 | /* |
| 6158 | * Main check: verify certificate |
| 6159 | */ |
| 6160 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 6161 | if( ssl->conf->f_ca_cb != NULL ) |
| 6162 | { |
| 6163 | ((void) rs_ctx); |
| 6164 | have_ca_chain = 1; |
| 6165 | |
| 6166 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) ); |
| 6167 | ret = mbedtls_x509_crt_verify_with_ca_cb( |
| 6168 | chain, |
| 6169 | ssl->conf->f_ca_cb, |
| 6170 | ssl->conf->p_ca_cb, |
| 6171 | ssl->conf->cert_profile, |
| 6172 | ssl->hostname, |
| 6173 | &ssl->session_negotiate->verify_result, |
| 6174 | f_vrfy, p_vrfy ); |
| 6175 | } |
| 6176 | else |
| 6177 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 6178 | { |
| 6179 | mbedtls_x509_crt *ca_chain; |
| 6180 | mbedtls_x509_crl *ca_crl; |
| 6181 | |
| 6182 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6183 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 6184 | { |
| 6185 | ca_chain = ssl->handshake->sni_ca_chain; |
| 6186 | ca_crl = ssl->handshake->sni_ca_crl; |
| 6187 | } |
| 6188 | else |
| 6189 | #endif |
| 6190 | { |
| 6191 | ca_chain = ssl->conf->ca_chain; |
| 6192 | ca_crl = ssl->conf->ca_crl; |
| 6193 | } |
| 6194 | |
| 6195 | if( ca_chain != NULL ) |
| 6196 | have_ca_chain = 1; |
| 6197 | |
| 6198 | ret = mbedtls_x509_crt_verify_restartable( |
| 6199 | chain, |
| 6200 | ca_chain, ca_crl, |
| 6201 | ssl->conf->cert_profile, |
| 6202 | ssl->hostname, |
| 6203 | &ssl->session_negotiate->verify_result, |
| 6204 | f_vrfy, p_vrfy, rs_ctx ); |
| 6205 | } |
| 6206 | |
| 6207 | if( ret != 0 ) |
| 6208 | { |
| 6209 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
| 6210 | } |
| 6211 | |
| 6212 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6213 | if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) |
| 6214 | return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ); |
| 6215 | #endif |
| 6216 | |
| 6217 | /* |
| 6218 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 6219 | */ |
| 6220 | |
| 6221 | #if defined(MBEDTLS_ECP_C) |
| 6222 | { |
| 6223 | const mbedtls_pk_context *pk = &chain->pk; |
| 6224 | |
| 6225 | /* If certificate uses an EC key, make sure the curve is OK */ |
| 6226 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
| 6227 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
| 6228 | { |
| 6229 | ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY; |
| 6230 | |
| 6231 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
| 6232 | if( ret == 0 ) |
| 6233 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6234 | } |
| 6235 | } |
| 6236 | #endif /* MBEDTLS_ECP_C */ |
| 6237 | |
| 6238 | if( mbedtls_ssl_check_cert_usage( chain, |
| 6239 | ciphersuite_info, |
| 6240 | ! ssl->conf->endpoint, |
| 6241 | &ssl->session_negotiate->verify_result ) != 0 ) |
| 6242 | { |
| 6243 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
| 6244 | if( ret == 0 ) |
| 6245 | ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE; |
| 6246 | } |
| 6247 | |
| 6248 | /* mbedtls_x509_crt_verify_with_profile is supposed to report a |
| 6249 | * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, |
| 6250 | * with details encoded in the verification flags. All other kinds |
| 6251 | * of error codes, including those from the user provided f_vrfy |
| 6252 | * functions, are treated as fatal and lead to a failure of |
| 6253 | * ssl_parse_certificate even if verification was optional. */ |
| 6254 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL && |
| 6255 | ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED || |
| 6256 | ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) ) |
| 6257 | { |
| 6258 | ret = 0; |
| 6259 | } |
| 6260 | |
| 6261 | if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED ) |
| 6262 | { |
| 6263 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 6264 | ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED; |
| 6265 | } |
| 6266 | |
| 6267 | if( ret != 0 ) |
| 6268 | { |
| 6269 | uint8_t alert; |
| 6270 | |
| 6271 | /* The certificate may have been rejected for several reasons. |
| 6272 | Pick one and send the corresponding alert. Which alert to send |
| 6273 | may be a subject of debate in some cases. */ |
| 6274 | if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER ) |
| 6275 | alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED; |
| 6276 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH ) |
| 6277 | alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT; |
| 6278 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE ) |
| 6279 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6280 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE ) |
| 6281 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6282 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE ) |
| 6283 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6284 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK ) |
| 6285 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6286 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY ) |
| 6287 | alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT; |
| 6288 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED ) |
| 6289 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED; |
| 6290 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED ) |
| 6291 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED; |
| 6292 | else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) |
| 6293 | alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA; |
| 6294 | else |
| 6295 | alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN; |
| 6296 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6297 | alert ); |
| 6298 | } |
| 6299 | |
| 6300 | #if defined(MBEDTLS_DEBUG_C) |
| 6301 | if( ssl->session_negotiate->verify_result != 0 ) |
| 6302 | { |
| 6303 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x", |
| 6304 | (unsigned int) ssl->session_negotiate->verify_result ) ); |
| 6305 | } |
| 6306 | else |
| 6307 | { |
| 6308 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) ); |
| 6309 | } |
| 6310 | #endif /* MBEDTLS_DEBUG_C */ |
| 6311 | |
| 6312 | return( ret ); |
| 6313 | } |
| 6314 | |
| 6315 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6316 | static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl, |
| 6317 | unsigned char *start, size_t len ) |
| 6318 | { |
| 6319 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6320 | /* Remember digest of the peer's end-CRT. */ |
| 6321 | ssl->session_negotiate->peer_cert_digest = |
| 6322 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 6323 | if( ssl->session_negotiate->peer_cert_digest == NULL ) |
| 6324 | { |
| 6325 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
| 6326 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ); |
| 6327 | mbedtls_ssl_send_alert_message( ssl, |
| 6328 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6329 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6330 | |
| 6331 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 6332 | } |
| 6333 | |
| 6334 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 6335 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 6336 | start, len, |
| 6337 | ssl->session_negotiate->peer_cert_digest ); |
| 6338 | |
| 6339 | ssl->session_negotiate->peer_cert_digest_type = |
| 6340 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 6341 | ssl->session_negotiate->peer_cert_digest_len = |
| 6342 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 6343 | |
| 6344 | return( ret ); |
| 6345 | } |
| 6346 | |
| 6347 | static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl, |
| 6348 | unsigned char *start, size_t len ) |
| 6349 | { |
| 6350 | unsigned char *end = start + len; |
| 6351 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6352 | |
| 6353 | /* Make a copy of the peer's raw public key. */ |
| 6354 | mbedtls_pk_init( &ssl->handshake->peer_pubkey ); |
| 6355 | ret = mbedtls_pk_parse_subpubkey( &start, end, |
| 6356 | &ssl->handshake->peer_pubkey ); |
| 6357 | if( ret != 0 ) |
| 6358 | { |
| 6359 | /* We should have parsed the public key before. */ |
| 6360 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 6361 | } |
| 6362 | |
| 6363 | return( 0 ); |
| 6364 | } |
| 6365 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6366 | |
| 6367 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
| 6368 | { |
| 6369 | int ret = 0; |
| 6370 | int crt_expected; |
| 6371 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 6372 | const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET |
| 6373 | ? ssl->handshake->sni_authmode |
| 6374 | : ssl->conf->authmode; |
| 6375 | #else |
| 6376 | const int authmode = ssl->conf->authmode; |
| 6377 | #endif |
| 6378 | void *rs_ctx = NULL; |
| 6379 | mbedtls_x509_crt *chain = NULL; |
| 6380 | |
| 6381 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
| 6382 | |
| 6383 | crt_expected = ssl_parse_certificate_coordinate( ssl, authmode ); |
| 6384 | if( crt_expected == SSL_CERTIFICATE_SKIP ) |
| 6385 | { |
| 6386 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 6387 | goto exit; |
| 6388 | } |
| 6389 | |
| 6390 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6391 | if( ssl->handshake->ecrs_enabled && |
| 6392 | ssl->handshake->ecrs_state == ssl_ecrs_crt_verify ) |
| 6393 | { |
| 6394 | chain = ssl->handshake->ecrs_peer_cert; |
| 6395 | ssl->handshake->ecrs_peer_cert = NULL; |
| 6396 | goto crt_verify; |
| 6397 | } |
| 6398 | #endif |
| 6399 | |
| 6400 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6401 | { |
| 6402 | /* mbedtls_ssl_read_record may have sent an alert already. We |
| 6403 | let it decide whether to alert. */ |
| 6404 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6405 | goto exit; |
| 6406 | } |
| 6407 | |
| 6408 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6409 | if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 ) |
| 6410 | { |
| 6411 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
| 6412 | |
| 6413 | if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL ) |
| 6414 | ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE; |
| 6415 | |
| 6416 | goto exit; |
| 6417 | } |
| 6418 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 6419 | |
| 6420 | /* Clear existing peer CRT structure in case we tried to |
| 6421 | * reuse a session but it failed, and allocate a new one. */ |
| 6422 | ssl_clear_peer_cert( ssl->session_negotiate ); |
| 6423 | |
| 6424 | chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 6425 | if( chain == NULL ) |
| 6426 | { |
| 6427 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", |
| 6428 | sizeof( mbedtls_x509_crt ) ) ); |
| 6429 | mbedtls_ssl_send_alert_message( ssl, |
| 6430 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6431 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 6432 | |
| 6433 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 6434 | goto exit; |
| 6435 | } |
| 6436 | mbedtls_x509_crt_init( chain ); |
| 6437 | |
| 6438 | ret = ssl_parse_certificate_chain( ssl, chain ); |
| 6439 | if( ret != 0 ) |
| 6440 | goto exit; |
| 6441 | |
| 6442 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6443 | if( ssl->handshake->ecrs_enabled) |
| 6444 | ssl->handshake->ecrs_state = ssl_ecrs_crt_verify; |
| 6445 | |
| 6446 | crt_verify: |
| 6447 | if( ssl->handshake->ecrs_enabled) |
| 6448 | rs_ctx = &ssl->handshake->ecrs_ctx; |
| 6449 | #endif |
| 6450 | |
| 6451 | ret = ssl_parse_certificate_verify( ssl, authmode, |
| 6452 | chain, rs_ctx ); |
| 6453 | if( ret != 0 ) |
| 6454 | goto exit; |
| 6455 | |
| 6456 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 6457 | { |
| 6458 | unsigned char *crt_start, *pk_start; |
| 6459 | size_t crt_len, pk_len; |
| 6460 | |
| 6461 | /* We parse the CRT chain without copying, so |
| 6462 | * these pointers point into the input buffer, |
| 6463 | * and are hence still valid after freeing the |
| 6464 | * CRT chain. */ |
| 6465 | |
| 6466 | crt_start = chain->raw.p; |
| 6467 | crt_len = chain->raw.len; |
| 6468 | |
| 6469 | pk_start = chain->pk_raw.p; |
| 6470 | pk_len = chain->pk_raw.len; |
| 6471 | |
| 6472 | /* Free the CRT structures before computing |
| 6473 | * digest and copying the peer's public key. */ |
| 6474 | mbedtls_x509_crt_free( chain ); |
| 6475 | mbedtls_free( chain ); |
| 6476 | chain = NULL; |
| 6477 | |
| 6478 | ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len ); |
| 6479 | if( ret != 0 ) |
| 6480 | goto exit; |
| 6481 | |
| 6482 | ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len ); |
| 6483 | if( ret != 0 ) |
| 6484 | goto exit; |
| 6485 | } |
| 6486 | #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6487 | /* Pass ownership to session structure. */ |
| 6488 | ssl->session_negotiate->peer_cert = chain; |
| 6489 | chain = NULL; |
| 6490 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 6491 | |
| 6492 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
| 6493 | |
| 6494 | exit: |
| 6495 | |
| 6496 | if( ret == 0 ) |
| 6497 | ssl->state++; |
| 6498 | |
| 6499 | #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED) |
| 6500 | if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS ) |
| 6501 | { |
| 6502 | ssl->handshake->ecrs_peer_cert = chain; |
| 6503 | chain = NULL; |
| 6504 | } |
| 6505 | #endif |
| 6506 | |
| 6507 | if( chain != NULL ) |
| 6508 | { |
| 6509 | mbedtls_x509_crt_free( chain ); |
| 6510 | mbedtls_free( chain ); |
| 6511 | } |
| 6512 | |
| 6513 | return( ret ); |
| 6514 | } |
| 6515 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 6516 | |
Jerry Yu | 615bd6f | 2022-02-17 14:25:15 +0800 | [diff] [blame] | 6517 | #if defined(MBEDTLS_SHA256_C) |
| 6518 | static void ssl_calc_finished_tls_sha256( |
| 6519 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6520 | { |
| 6521 | int len = 12; |
| 6522 | const char *sender; |
| 6523 | unsigned char padbuf[32]; |
| 6524 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6525 | size_t hash_size; |
| 6526 | psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; |
| 6527 | psa_status_t status; |
| 6528 | #else |
| 6529 | mbedtls_sha256_context sha256; |
| 6530 | #endif |
| 6531 | |
| 6532 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6533 | if( !session ) |
| 6534 | session = ssl->session; |
| 6535 | |
| 6536 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6537 | ? "client finished" |
| 6538 | : "server finished"; |
| 6539 | |
| 6540 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6541 | sha256_psa = psa_hash_operation_init(); |
| 6542 | |
| 6543 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); |
| 6544 | |
| 6545 | status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); |
| 6546 | if( status != PSA_SUCCESS ) |
| 6547 | { |
| 6548 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6549 | return; |
| 6550 | } |
| 6551 | |
| 6552 | status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6553 | if( status != PSA_SUCCESS ) |
| 6554 | { |
| 6555 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6556 | return; |
| 6557 | } |
| 6558 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); |
| 6559 | #else |
| 6560 | |
| 6561 | mbedtls_sha256_init( &sha256 ); |
| 6562 | |
| 6563 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) ); |
| 6564 | |
| 6565 | mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 ); |
| 6566 | |
| 6567 | /* |
| 6568 | * TLSv1.2: |
| 6569 | * hash = PRF( master, finished_label, |
| 6570 | * Hash( handshake ) )[0.11] |
| 6571 | */ |
| 6572 | |
| 6573 | #if !defined(MBEDTLS_SHA256_ALT) |
| 6574 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 6575 | sha256.state, sizeof( sha256.state ) ); |
| 6576 | #endif |
| 6577 | |
| 6578 | mbedtls_sha256_finish( &sha256, padbuf ); |
| 6579 | mbedtls_sha256_free( &sha256 ); |
| 6580 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 6581 | |
| 6582 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6583 | padbuf, 32, buf, len ); |
| 6584 | |
| 6585 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6586 | |
| 6587 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6588 | |
| 6589 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6590 | } |
| 6591 | #endif /* MBEDTLS_SHA256_C */ |
| 6592 | |
Jerry Yu | b7ba49e | 2022-02-17 14:25:53 +0800 | [diff] [blame] | 6593 | |
| 6594 | #if defined(MBEDTLS_SHA384_C) |
| 6595 | |
| 6596 | static void ssl_calc_finished_tls_sha384( |
| 6597 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
| 6598 | { |
| 6599 | int len = 12; |
| 6600 | const char *sender; |
| 6601 | unsigned char padbuf[48]; |
| 6602 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6603 | size_t hash_size; |
| 6604 | psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT; |
| 6605 | psa_status_t status; |
| 6606 | #else |
| 6607 | mbedtls_sha512_context sha512; |
| 6608 | #endif |
| 6609 | |
| 6610 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 6611 | if( !session ) |
| 6612 | session = ssl->session; |
| 6613 | |
| 6614 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
| 6615 | ? "client finished" |
| 6616 | : "server finished"; |
| 6617 | |
| 6618 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 6619 | sha384_psa = psa_hash_operation_init(); |
| 6620 | |
| 6621 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); |
| 6622 | |
| 6623 | status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); |
| 6624 | if( status != PSA_SUCCESS ) |
| 6625 | { |
| 6626 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); |
| 6627 | return; |
| 6628 | } |
| 6629 | |
| 6630 | status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); |
| 6631 | if( status != PSA_SUCCESS ) |
| 6632 | { |
| 6633 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); |
| 6634 | return; |
| 6635 | } |
| 6636 | MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); |
| 6637 | #else |
| 6638 | mbedtls_sha512_init( &sha512 ); |
| 6639 | |
| 6640 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
| 6641 | |
| 6642 | mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 ); |
| 6643 | |
| 6644 | /* |
| 6645 | * TLSv1.2: |
| 6646 | * hash = PRF( master, finished_label, |
| 6647 | * Hash( handshake ) )[0.11] |
| 6648 | */ |
| 6649 | |
| 6650 | #if !defined(MBEDTLS_SHA512_ALT) |
| 6651 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *) |
| 6652 | sha512.state, sizeof( sha512.state ) ); |
| 6653 | #endif |
| 6654 | mbedtls_sha512_finish( &sha512, padbuf ); |
| 6655 | |
| 6656 | mbedtls_sha512_free( &sha512 ); |
| 6657 | #endif |
| 6658 | |
| 6659 | ssl->handshake->tls_prf( session->master, 48, sender, |
| 6660 | padbuf, 48, buf, len ); |
| 6661 | |
| 6662 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
| 6663 | |
| 6664 | mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) ); |
| 6665 | |
| 6666 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
| 6667 | } |
| 6668 | #endif /* MBEDTLS_SHA384_C */ |
| 6669 | |
Jerry Yu | aef0015 | 2022-02-17 14:27:31 +0800 | [diff] [blame] | 6670 | void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
| 6671 | { |
| 6672 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
| 6673 | |
| 6674 | /* |
| 6675 | * Free our handshake params |
| 6676 | */ |
| 6677 | mbedtls_ssl_handshake_free( ssl ); |
| 6678 | mbedtls_free( ssl->handshake ); |
| 6679 | ssl->handshake = NULL; |
| 6680 | |
| 6681 | /* |
| 6682 | * Free the previous transform and swith in the current one |
| 6683 | */ |
| 6684 | if( ssl->transform ) |
| 6685 | { |
| 6686 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6687 | mbedtls_free( ssl->transform ); |
| 6688 | } |
| 6689 | ssl->transform = ssl->transform_negotiate; |
| 6690 | ssl->transform_negotiate = NULL; |
| 6691 | |
| 6692 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
| 6693 | } |
| 6694 | |
Jerry Yu | 2a9fff5 | 2022-02-17 14:28:51 +0800 | [diff] [blame] | 6695 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
| 6696 | { |
| 6697 | int resume = ssl->handshake->resume; |
| 6698 | |
| 6699 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
| 6700 | |
| 6701 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6702 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
| 6703 | { |
| 6704 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
| 6705 | ssl->renego_records_seen = 0; |
| 6706 | } |
| 6707 | #endif |
| 6708 | |
| 6709 | /* |
| 6710 | * Free the previous session and switch in the current one |
| 6711 | */ |
| 6712 | if( ssl->session ) |
| 6713 | { |
| 6714 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6715 | /* RFC 7366 3.1: keep the EtM state */ |
| 6716 | ssl->session_negotiate->encrypt_then_mac = |
| 6717 | ssl->session->encrypt_then_mac; |
| 6718 | #endif |
| 6719 | |
| 6720 | mbedtls_ssl_session_free( ssl->session ); |
| 6721 | mbedtls_free( ssl->session ); |
| 6722 | } |
| 6723 | ssl->session = ssl->session_negotiate; |
| 6724 | ssl->session_negotiate = NULL; |
| 6725 | |
| 6726 | /* |
| 6727 | * Add cache entry |
| 6728 | */ |
| 6729 | if( ssl->conf->f_set_cache != NULL && |
| 6730 | ssl->session->id_len != 0 && |
| 6731 | resume == 0 ) |
| 6732 | { |
| 6733 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, |
| 6734 | ssl->session->id, |
| 6735 | ssl->session->id_len, |
| 6736 | ssl->session ) != 0 ) |
| 6737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
| 6738 | } |
| 6739 | |
| 6740 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6741 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6742 | ssl->handshake->flight != NULL ) |
| 6743 | { |
| 6744 | /* Cancel handshake timer */ |
| 6745 | mbedtls_ssl_set_timer( ssl, 0 ); |
| 6746 | |
| 6747 | /* Keep last flight around in case we need to resend it: |
| 6748 | * we need the handshake and transform structures for that */ |
| 6749 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
| 6750 | } |
| 6751 | else |
| 6752 | #endif |
| 6753 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 6754 | |
| 6755 | ssl->state++; |
| 6756 | |
| 6757 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
| 6758 | } |
| 6759 | |
Jerry Yu | 3c8e47b | 2022-02-17 14:30:01 +0800 | [diff] [blame] | 6760 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
| 6761 | { |
| 6762 | int ret, hash_len; |
| 6763 | |
| 6764 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
| 6765 | |
| 6766 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate ); |
| 6767 | |
| 6768 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
| 6769 | |
| 6770 | /* |
| 6771 | * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites |
| 6772 | * may define some other value. Currently (early 2016), no defined |
| 6773 | * ciphersuite does this (and this is unlikely to change as activity has |
| 6774 | * moved to TLS 1.3 now) so we can keep the hardcoded 12 here. |
| 6775 | */ |
| 6776 | hash_len = 12; |
| 6777 | |
| 6778 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6779 | ssl->verify_data_len = hash_len; |
| 6780 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
| 6781 | #endif |
| 6782 | |
| 6783 | ssl->out_msglen = 4 + hash_len; |
| 6784 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 6785 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
| 6786 | |
| 6787 | /* |
| 6788 | * In case of session resuming, invert the client and server |
| 6789 | * ChangeCipherSpec messages order. |
| 6790 | */ |
| 6791 | if( ssl->handshake->resume != 0 ) |
| 6792 | { |
| 6793 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6794 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6795 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 6796 | #endif |
| 6797 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6798 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6799 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 6800 | #endif |
| 6801 | } |
| 6802 | else |
| 6803 | ssl->state++; |
| 6804 | |
| 6805 | /* |
| 6806 | * Switch to our negotiated transform and session parameters for outbound |
| 6807 | * data. |
| 6808 | */ |
| 6809 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
| 6810 | |
| 6811 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6812 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6813 | { |
| 6814 | unsigned char i; |
| 6815 | |
| 6816 | /* Remember current epoch settings for resending */ |
| 6817 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 6818 | memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, |
| 6819 | sizeof( ssl->handshake->alt_out_ctr ) ); |
| 6820 | |
| 6821 | /* Set sequence_number to zero */ |
| 6822 | memset( &ssl->cur_out_ctr[2], 0, sizeof( ssl->cur_out_ctr ) - 2 ); |
| 6823 | |
| 6824 | |
| 6825 | /* Increment epoch */ |
| 6826 | for( i = 2; i > 0; i-- ) |
| 6827 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 6828 | break; |
| 6829 | |
| 6830 | /* The loop goes to its end iff the counter is wrapping */ |
| 6831 | if( i == 0 ) |
| 6832 | { |
| 6833 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 6834 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 6835 | } |
| 6836 | } |
| 6837 | else |
| 6838 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6839 | memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) ); |
| 6840 | |
| 6841 | ssl->transform_out = ssl->transform_negotiate; |
| 6842 | ssl->session_out = ssl->session_negotiate; |
| 6843 | |
| 6844 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6845 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6846 | mbedtls_ssl_send_flight_completed( ssl ); |
| 6847 | #endif |
| 6848 | |
| 6849 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
| 6850 | { |
| 6851 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
| 6852 | return( ret ); |
| 6853 | } |
| 6854 | |
| 6855 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6856 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6857 | ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
| 6858 | { |
| 6859 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret ); |
| 6860 | return( ret ); |
| 6861 | } |
| 6862 | #endif |
| 6863 | |
| 6864 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
| 6865 | |
| 6866 | return( 0 ); |
| 6867 | } |
| 6868 | |
Jerry Yu | 0b3d7c1 | 2022-02-17 14:30:51 +0800 | [diff] [blame] | 6869 | #define SSL_MAX_HASH_LEN 12 |
| 6870 | |
| 6871 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
| 6872 | { |
| 6873 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6874 | unsigned int hash_len = 12; |
| 6875 | unsigned char buf[SSL_MAX_HASH_LEN]; |
| 6876 | |
| 6877 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
| 6878 | |
| 6879 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
| 6880 | |
| 6881 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
| 6882 | { |
| 6883 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 6884 | goto exit; |
| 6885 | } |
| 6886 | |
| 6887 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
| 6888 | { |
| 6889 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6890 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6891 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6892 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 6893 | goto exit; |
| 6894 | } |
| 6895 | |
| 6896 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ) |
| 6897 | { |
| 6898 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6899 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
| 6900 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 6901 | goto exit; |
| 6902 | } |
| 6903 | |
| 6904 | if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
| 6905 | { |
| 6906 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6907 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6908 | MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR ); |
| 6909 | ret = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 6910 | goto exit; |
| 6911 | } |
| 6912 | |
| 6913 | if( mbedtls_ct_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
| 6914 | buf, hash_len ) != 0 ) |
| 6915 | { |
| 6916 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 6917 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6918 | MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR ); |
| 6919 | ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; |
| 6920 | goto exit; |
| 6921 | } |
| 6922 | |
| 6923 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6924 | ssl->verify_data_len = hash_len; |
| 6925 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
| 6926 | #endif |
| 6927 | |
| 6928 | if( ssl->handshake->resume != 0 ) |
| 6929 | { |
| 6930 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6931 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6932 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
| 6933 | #endif |
| 6934 | #if defined(MBEDTLS_SSL_SRV_C) |
| 6935 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6936 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
| 6937 | #endif |
| 6938 | } |
| 6939 | else |
| 6940 | ssl->state++; |
| 6941 | |
| 6942 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6943 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6944 | mbedtls_ssl_recv_flight_completed( ssl ); |
| 6945 | #endif |
| 6946 | |
| 6947 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
| 6948 | |
| 6949 | exit: |
| 6950 | mbedtls_platform_zeroize( buf, hash_len ); |
| 6951 | return( ret ); |
| 6952 | } |
| 6953 | |
Jerry Yu | 392112c | 2022-02-17 14:34:10 +0800 | [diff] [blame] | 6954 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 6955 | /* |
| 6956 | * Helper to get TLS 1.2 PRF from ciphersuite |
| 6957 | * (Duplicates bits of logic from ssl_set_handshake_prfs().) |
| 6958 | */ |
| 6959 | static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id ) |
| 6960 | { |
| 6961 | #if defined(MBEDTLS_SHA384_C) |
| 6962 | const mbedtls_ssl_ciphersuite_t * const ciphersuite_info = |
| 6963 | mbedtls_ssl_ciphersuite_from_id( ciphersuite_id ); |
| 6964 | |
| 6965 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
| 6966 | return( tls_prf_sha384 ); |
| 6967 | #else |
| 6968 | (void) ciphersuite_id; |
| 6969 | #endif |
| 6970 | return( tls_prf_sha256 ); |
| 6971 | } |
| 6972 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Jerry Yu | e93ffcd | 2022-02-17 14:37:06 +0800 | [diff] [blame] | 6973 | |
| 6974 | static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf ) |
| 6975 | { |
| 6976 | ((void) tls_prf); |
| 6977 | #if defined(MBEDTLS_SHA384_C) |
| 6978 | if( tls_prf == tls_prf_sha384 ) |
| 6979 | { |
| 6980 | return( MBEDTLS_SSL_TLS_PRF_SHA384 ); |
| 6981 | } |
| 6982 | else |
| 6983 | #endif |
| 6984 | #if defined(MBEDTLS_SHA256_C) |
| 6985 | if( tls_prf == tls_prf_sha256 ) |
| 6986 | { |
| 6987 | return( MBEDTLS_SSL_TLS_PRF_SHA256 ); |
| 6988 | } |
| 6989 | else |
| 6990 | #endif |
| 6991 | return( MBEDTLS_SSL_TLS_PRF_NONE ); |
| 6992 | } |
| 6993 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 6994 | /* |
| 6995 | * Populate a transform structure with session keys and all the other |
| 6996 | * necessary information. |
| 6997 | * |
| 6998 | * Parameters: |
| 6999 | * - [in/out]: transform: structure to populate |
| 7000 | * [in] must be just initialised with mbedtls_ssl_transform_init() |
| 7001 | * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf() |
| 7002 | * - [in] ciphersuite |
| 7003 | * - [in] master |
| 7004 | * - [in] encrypt_then_mac |
| 7005 | * - [in] compression |
| 7006 | * - [in] tls_prf: pointer to PRF to use for key derivation |
| 7007 | * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random |
| 7008 | * - [in] minor_ver: SSL/TLS minor version |
| 7009 | * - [in] endpoint: client or server |
| 7010 | * - [in] ssl: used for: |
| 7011 | * - ssl->conf->{f,p}_export_keys |
| 7012 | * [in] optionally used for: |
| 7013 | * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg |
| 7014 | */ |
| 7015 | static int ssl_tls12_populate_transform( mbedtls_ssl_transform *transform, |
| 7016 | int ciphersuite, |
| 7017 | const unsigned char master[48], |
| 7018 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) && \ |
| 7019 | defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7020 | int encrypt_then_mac, |
| 7021 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC && |
| 7022 | MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7023 | ssl_tls_prf_t tls_prf, |
| 7024 | const unsigned char randbytes[64], |
| 7025 | int minor_ver, |
| 7026 | unsigned endpoint, |
| 7027 | const mbedtls_ssl_context *ssl ) |
| 7028 | { |
| 7029 | int ret = 0; |
| 7030 | unsigned char keyblk[256]; |
| 7031 | unsigned char *key1; |
| 7032 | unsigned char *key2; |
| 7033 | unsigned char *mac_enc; |
| 7034 | unsigned char *mac_dec; |
| 7035 | size_t mac_key_len = 0; |
| 7036 | size_t iv_copy_len; |
| 7037 | size_t keylen; |
| 7038 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 7039 | const mbedtls_cipher_info_t *cipher_info; |
| 7040 | const mbedtls_md_info_t *md_info; |
| 7041 | |
| 7042 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7043 | psa_key_type_t key_type; |
| 7044 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7045 | psa_algorithm_t alg; |
| 7046 | size_t key_bits; |
| 7047 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 7048 | #endif |
| 7049 | |
| 7050 | #if !defined(MBEDTLS_DEBUG_C) && \ |
| 7051 | !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7052 | if( ssl->f_export_keys == NULL ) |
| 7053 | { |
| 7054 | ssl = NULL; /* make sure we don't use it except for these cases */ |
| 7055 | (void) ssl; |
| 7056 | } |
| 7057 | #endif |
| 7058 | |
| 7059 | /* |
| 7060 | * Some data just needs copying into the structure |
| 7061 | */ |
| 7062 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 7063 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7064 | transform->encrypt_then_mac = encrypt_then_mac; |
| 7065 | #endif |
| 7066 | transform->minor_ver = minor_ver; |
| 7067 | |
| 7068 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 7069 | memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) ); |
| 7070 | #endif |
| 7071 | |
| 7072 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 7073 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 7074 | { |
| 7075 | /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform |
| 7076 | * generation separate. This should never happen. */ |
| 7077 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7078 | } |
| 7079 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 7080 | |
| 7081 | /* |
| 7082 | * Get various info structures |
| 7083 | */ |
| 7084 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
| 7085 | if( ciphersuite_info == NULL ) |
| 7086 | { |
| 7087 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 7088 | ciphersuite ) ); |
| 7089 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7090 | } |
| 7091 | |
| 7092 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 7093 | if( cipher_info == NULL ) |
| 7094 | { |
| 7095 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 7096 | ciphersuite_info->cipher ) ); |
| 7097 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7098 | } |
| 7099 | |
| 7100 | md_info = mbedtls_md_info_from_type( ciphersuite_info->mac ); |
| 7101 | if( md_info == NULL ) |
| 7102 | { |
| 7103 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %u not found", |
| 7104 | (unsigned) ciphersuite_info->mac ) ); |
| 7105 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7106 | } |
| 7107 | |
| 7108 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 7109 | /* Copy own and peer's CID if the use of the CID |
| 7110 | * extension has been negotiated. */ |
| 7111 | if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED ) |
| 7112 | { |
| 7113 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) ); |
| 7114 | |
| 7115 | transform->in_cid_len = ssl->own_cid_len; |
| 7116 | memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); |
| 7117 | MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid, |
| 7118 | transform->in_cid_len ); |
| 7119 | |
| 7120 | transform->out_cid_len = ssl->handshake->peer_cid_len; |
| 7121 | memcpy( transform->out_cid, ssl->handshake->peer_cid, |
| 7122 | ssl->handshake->peer_cid_len ); |
| 7123 | MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid, |
| 7124 | transform->out_cid_len ); |
| 7125 | } |
| 7126 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 7127 | |
| 7128 | /* |
| 7129 | * Compute key block using the PRF |
| 7130 | */ |
| 7131 | ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 ); |
| 7132 | if( ret != 0 ) |
| 7133 | { |
| 7134 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
| 7135 | return( ret ); |
| 7136 | } |
| 7137 | |
| 7138 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 7139 | mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) ); |
| 7140 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 ); |
| 7141 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 ); |
| 7142 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
| 7143 | |
| 7144 | /* |
| 7145 | * Determine the appropriate key, IV and MAC length. |
| 7146 | */ |
| 7147 | |
| 7148 | keylen = mbedtls_cipher_info_get_key_bitlen( cipher_info ) / 8; |
| 7149 | |
| 7150 | #if defined(MBEDTLS_GCM_C) || \ |
| 7151 | defined(MBEDTLS_CCM_C) || \ |
| 7152 | defined(MBEDTLS_CHACHAPOLY_C) |
| 7153 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_GCM || |
| 7154 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CCM || |
| 7155 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
| 7156 | { |
| 7157 | size_t explicit_ivlen; |
| 7158 | |
| 7159 | transform->maclen = 0; |
| 7160 | mac_key_len = 0; |
| 7161 | transform->taglen = |
| 7162 | ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
| 7163 | |
| 7164 | /* All modes haves 96-bit IVs, but the length of the static parts vary |
| 7165 | * with mode and version: |
| 7166 | * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes |
| 7167 | * (to be concatenated with a dynamically chosen IV of 8 Bytes) |
| 7168 | * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's |
| 7169 | * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record |
| 7170 | * sequence number). |
| 7171 | */ |
| 7172 | transform->ivlen = 12; |
| 7173 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CHACHAPOLY ) |
| 7174 | transform->fixed_ivlen = 12; |
| 7175 | else |
| 7176 | transform->fixed_ivlen = 4; |
| 7177 | |
| 7178 | /* Minimum length of encrypted record */ |
| 7179 | explicit_ivlen = transform->ivlen - transform->fixed_ivlen; |
| 7180 | transform->minlen = explicit_ivlen + transform->taglen; |
| 7181 | } |
| 7182 | else |
| 7183 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 7184 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 7185 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM || |
| 7186 | mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7187 | { |
| 7188 | /* Initialize HMAC contexts */ |
| 7189 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 7190 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
| 7191 | { |
| 7192 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7193 | goto end; |
| 7194 | } |
| 7195 | |
| 7196 | /* Get MAC length */ |
| 7197 | mac_key_len = mbedtls_md_get_size( md_info ); |
| 7198 | transform->maclen = mac_key_len; |
| 7199 | |
| 7200 | /* IV length */ |
| 7201 | transform->ivlen = cipher_info->iv_size; |
| 7202 | |
| 7203 | /* Minimum length */ |
| 7204 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_STREAM ) |
| 7205 | transform->minlen = transform->maclen; |
| 7206 | else |
| 7207 | { |
| 7208 | /* |
| 7209 | * GenericBlockCipher: |
| 7210 | * 1. if EtM is in use: one block plus MAC |
| 7211 | * otherwise: * first multiple of blocklen greater than maclen |
| 7212 | * 2. IV |
| 7213 | */ |
| 7214 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7215 | if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
| 7216 | { |
| 7217 | transform->minlen = transform->maclen |
| 7218 | + cipher_info->block_size; |
| 7219 | } |
| 7220 | else |
| 7221 | #endif |
| 7222 | { |
| 7223 | transform->minlen = transform->maclen |
| 7224 | + cipher_info->block_size |
| 7225 | - transform->maclen % cipher_info->block_size; |
| 7226 | } |
| 7227 | |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7228 | if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
| 7229 | { |
| 7230 | transform->minlen += transform->ivlen; |
| 7231 | } |
| 7232 | else |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7233 | { |
| 7234 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7235 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7236 | goto end; |
| 7237 | } |
| 7238 | } |
| 7239 | } |
| 7240 | else |
| 7241 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7242 | { |
| 7243 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7244 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 7245 | } |
| 7246 | |
| 7247 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u", |
| 7248 | (unsigned) keylen, |
| 7249 | (unsigned) transform->minlen, |
| 7250 | (unsigned) transform->ivlen, |
| 7251 | (unsigned) transform->maclen ) ); |
| 7252 | |
| 7253 | /* |
| 7254 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 7255 | */ |
| 7256 | #if defined(MBEDTLS_SSL_CLI_C) |
| 7257 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 7258 | { |
| 7259 | key1 = keyblk + mac_key_len * 2; |
| 7260 | key2 = keyblk + mac_key_len * 2 + keylen; |
| 7261 | |
| 7262 | mac_enc = keyblk; |
| 7263 | mac_dec = keyblk + mac_key_len; |
| 7264 | |
| 7265 | /* |
| 7266 | * This is not used in TLS v1.1. |
| 7267 | */ |
| 7268 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7269 | transform->fixed_ivlen : transform->ivlen; |
| 7270 | memcpy( transform->iv_enc, key2 + keylen, iv_copy_len ); |
| 7271 | memcpy( transform->iv_dec, key2 + keylen + iv_copy_len, |
| 7272 | iv_copy_len ); |
| 7273 | } |
| 7274 | else |
| 7275 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 7276 | #if defined(MBEDTLS_SSL_SRV_C) |
| 7277 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 7278 | { |
| 7279 | key1 = keyblk + mac_key_len * 2 + keylen; |
| 7280 | key2 = keyblk + mac_key_len * 2; |
| 7281 | |
| 7282 | mac_enc = keyblk + mac_key_len; |
| 7283 | mac_dec = keyblk; |
| 7284 | |
| 7285 | /* |
| 7286 | * This is not used in TLS v1.1. |
| 7287 | */ |
| 7288 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 7289 | transform->fixed_ivlen : transform->ivlen; |
| 7290 | memcpy( transform->iv_dec, key1 + keylen, iv_copy_len ); |
| 7291 | memcpy( transform->iv_enc, key1 + keylen + iv_copy_len, |
| 7292 | iv_copy_len ); |
| 7293 | } |
| 7294 | else |
| 7295 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 7296 | { |
| 7297 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 7298 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 7299 | goto end; |
| 7300 | } |
| 7301 | |
| 7302 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7303 | /* For HMAC-based ciphersuites, initialize the HMAC transforms. |
| 7304 | For AEAD-based ciphersuites, there is nothing to do here. */ |
| 7305 | if( mac_key_len != 0 ) |
| 7306 | { |
| 7307 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len ); |
| 7308 | if( ret != 0 ) |
| 7309 | goto end; |
| 7310 | ret = mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len ); |
| 7311 | if( ret != 0 ) |
| 7312 | goto end; |
| 7313 | } |
Jerry Yu | 9bccc4c | 2022-02-17 14:38:28 +0800 | [diff] [blame] | 7314 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 7315 | |
| 7316 | ((void) mac_dec); |
| 7317 | ((void) mac_enc); |
| 7318 | |
| 7319 | if( ssl != NULL && ssl->f_export_keys != NULL ) |
| 7320 | { |
| 7321 | ssl->f_export_keys( ssl->p_export_keys, |
| 7322 | MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET, |
| 7323 | master, 48, |
| 7324 | randbytes + 32, |
| 7325 | randbytes, |
| 7326 | tls_prf_get_type( tls_prf ) ); |
| 7327 | } |
| 7328 | |
| 7329 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7330 | if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type, |
| 7331 | transform->taglen, |
| 7332 | &alg, |
| 7333 | &key_type, |
| 7334 | &key_bits ) ) != PSA_SUCCESS ) |
| 7335 | { |
| 7336 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7337 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", ret ); |
| 7338 | goto end; |
| 7339 | } |
| 7340 | |
| 7341 | transform->psa_alg = alg; |
| 7342 | |
| 7343 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 7344 | { |
| 7345 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7346 | psa_set_key_algorithm( &attributes, alg ); |
| 7347 | psa_set_key_type( &attributes, key_type ); |
| 7348 | |
| 7349 | if( ( status = psa_import_key( &attributes, |
| 7350 | key1, |
| 7351 | PSA_BITS_TO_BYTES( key_bits ), |
| 7352 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 7353 | { |
| 7354 | MBEDTLS_SSL_DEBUG_RET( 3, "psa_import_key", (int)status ); |
| 7355 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7356 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7357 | goto end; |
| 7358 | } |
| 7359 | |
| 7360 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7361 | |
| 7362 | if( ( status = psa_import_key( &attributes, |
| 7363 | key2, |
| 7364 | PSA_BITS_TO_BYTES( key_bits ), |
| 7365 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 7366 | { |
| 7367 | ret = psa_ssl_status_to_mbedtls( status ); |
| 7368 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", ret ); |
| 7369 | goto end; |
| 7370 | } |
| 7371 | } |
| 7372 | #else |
| 7373 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 7374 | cipher_info ) ) != 0 ) |
| 7375 | { |
| 7376 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7377 | goto end; |
| 7378 | } |
| 7379 | |
| 7380 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 7381 | cipher_info ) ) != 0 ) |
| 7382 | { |
| 7383 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 7384 | goto end; |
| 7385 | } |
| 7386 | |
| 7387 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
| 7388 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7389 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 7390 | { |
| 7391 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7392 | goto end; |
| 7393 | } |
| 7394 | |
| 7395 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
| 7396 | (int) mbedtls_cipher_info_get_key_bitlen( cipher_info ), |
| 7397 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 7398 | { |
| 7399 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 7400 | goto end; |
| 7401 | } |
| 7402 | |
| 7403 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 7404 | if( mbedtls_cipher_info_get_mode( cipher_info ) == MBEDTLS_MODE_CBC ) |
| 7405 | { |
| 7406 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 7407 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7408 | { |
| 7409 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7410 | goto end; |
| 7411 | } |
| 7412 | |
| 7413 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 7414 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
| 7415 | { |
| 7416 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
| 7417 | goto end; |
| 7418 | } |
| 7419 | } |
| 7420 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 7421 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7422 | |
| 7423 | end: |
| 7424 | mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) ); |
| 7425 | return( ret ); |
| 7426 | } |
| 7427 | |
Jerry Yu | ee40f9d | 2022-02-17 14:55:16 +0800 | [diff] [blame] | 7428 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 7429 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7430 | unsigned char *hash, size_t *hashlen, |
| 7431 | unsigned char *data, size_t data_len, |
| 7432 | mbedtls_md_type_t md_alg ) |
| 7433 | { |
| 7434 | psa_status_t status; |
| 7435 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
| 7436 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); |
| 7437 | |
| 7438 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); |
| 7439 | |
| 7440 | if( ( status = psa_hash_setup( &hash_operation, |
| 7441 | hash_alg ) ) != PSA_SUCCESS ) |
| 7442 | { |
| 7443 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); |
| 7444 | goto exit; |
| 7445 | } |
| 7446 | |
| 7447 | if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, |
| 7448 | 64 ) ) != PSA_SUCCESS ) |
| 7449 | { |
| 7450 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7451 | goto exit; |
| 7452 | } |
| 7453 | |
| 7454 | if( ( status = psa_hash_update( &hash_operation, |
| 7455 | data, data_len ) ) != PSA_SUCCESS ) |
| 7456 | { |
| 7457 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); |
| 7458 | goto exit; |
| 7459 | } |
| 7460 | |
| 7461 | if( ( status = psa_hash_finish( &hash_operation, hash, PSA_HASH_MAX_SIZE, |
| 7462 | hashlen ) ) != PSA_SUCCESS ) |
| 7463 | { |
| 7464 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); |
| 7465 | goto exit; |
| 7466 | } |
| 7467 | |
| 7468 | exit: |
| 7469 | if( status != PSA_SUCCESS ) |
| 7470 | { |
| 7471 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7472 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7473 | switch( status ) |
| 7474 | { |
| 7475 | case PSA_ERROR_NOT_SUPPORTED: |
| 7476 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 7477 | case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ |
| 7478 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 7479 | return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 7480 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 7481 | return( MBEDTLS_ERR_MD_ALLOC_FAILED ); |
| 7482 | default: |
| 7483 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 7484 | } |
| 7485 | } |
| 7486 | return( 0 ); |
| 7487 | } |
| 7488 | |
| 7489 | #else |
| 7490 | |
| 7491 | int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, |
| 7492 | unsigned char *hash, size_t *hashlen, |
| 7493 | unsigned char *data, size_t data_len, |
| 7494 | mbedtls_md_type_t md_alg ) |
| 7495 | { |
| 7496 | int ret = 0; |
| 7497 | mbedtls_md_context_t ctx; |
| 7498 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 7499 | *hashlen = mbedtls_md_get_size( md_info ); |
| 7500 | |
| 7501 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); |
| 7502 | |
| 7503 | mbedtls_md_init( &ctx ); |
| 7504 | |
| 7505 | /* |
| 7506 | * digitally-signed struct { |
| 7507 | * opaque client_random[32]; |
| 7508 | * opaque server_random[32]; |
| 7509 | * ServerDHParams params; |
| 7510 | * }; |
| 7511 | */ |
| 7512 | if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) |
| 7513 | { |
| 7514 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
| 7515 | goto exit; |
| 7516 | } |
| 7517 | if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 ) |
| 7518 | { |
| 7519 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret ); |
| 7520 | goto exit; |
| 7521 | } |
| 7522 | if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 ) |
| 7523 | { |
| 7524 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7525 | goto exit; |
| 7526 | } |
| 7527 | if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 ) |
| 7528 | { |
| 7529 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret ); |
| 7530 | goto exit; |
| 7531 | } |
| 7532 | if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 ) |
| 7533 | { |
| 7534 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret ); |
| 7535 | goto exit; |
| 7536 | } |
| 7537 | |
| 7538 | exit: |
| 7539 | mbedtls_md_free( &ctx ); |
| 7540 | |
| 7541 | if( ret != 0 ) |
| 7542 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 7543 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); |
| 7544 | |
| 7545 | return( ret ); |
| 7546 | } |
| 7547 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 7548 | |
Jerry Yu | d9d91da | 2022-02-17 14:57:06 +0800 | [diff] [blame] | 7549 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) |
| 7550 | |
| 7551 | /* Find an entry in a signature-hash set matching a given hash algorithm. */ |
| 7552 | mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set, |
| 7553 | mbedtls_pk_type_t sig_alg ) |
| 7554 | { |
| 7555 | switch( sig_alg ) |
| 7556 | { |
| 7557 | case MBEDTLS_PK_RSA: |
| 7558 | return( set->rsa ); |
| 7559 | case MBEDTLS_PK_ECDSA: |
| 7560 | return( set->ecdsa ); |
| 7561 | default: |
| 7562 | return( MBEDTLS_MD_NONE ); |
| 7563 | } |
| 7564 | } |
| 7565 | |
| 7566 | /* Add a signature-hash-pair to a signature-hash set */ |
| 7567 | void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set, |
| 7568 | mbedtls_pk_type_t sig_alg, |
| 7569 | mbedtls_md_type_t md_alg ) |
| 7570 | { |
| 7571 | switch( sig_alg ) |
| 7572 | { |
| 7573 | case MBEDTLS_PK_RSA: |
| 7574 | if( set->rsa == MBEDTLS_MD_NONE ) |
| 7575 | set->rsa = md_alg; |
| 7576 | break; |
| 7577 | |
| 7578 | case MBEDTLS_PK_ECDSA: |
| 7579 | if( set->ecdsa == MBEDTLS_MD_NONE ) |
| 7580 | set->ecdsa = md_alg; |
| 7581 | break; |
| 7582 | |
| 7583 | default: |
| 7584 | break; |
| 7585 | } |
| 7586 | } |
| 7587 | |
| 7588 | /* Allow exactly one hash algorithm for each signature. */ |
| 7589 | void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set, |
| 7590 | mbedtls_md_type_t md_alg ) |
| 7591 | { |
| 7592 | set->rsa = md_alg; |
| 7593 | set->ecdsa = md_alg; |
| 7594 | } |
| 7595 | |
| 7596 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ |
| 7597 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7598 | /* Serialization of TLS 1.2 sessions: |
| 7599 | * |
| 7600 | * struct { |
| 7601 | * uint64 start_time; |
| 7602 | * uint8 ciphersuite[2]; // defined by the standard |
| 7603 | * uint8 compression; // 0 or 1 |
| 7604 | * uint8 session_id_len; // at most 32 |
| 7605 | * opaque session_id[32]; |
| 7606 | * opaque master[48]; // fixed length in the standard |
| 7607 | * uint32 verify_result; |
| 7608 | * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert |
| 7609 | * opaque ticket<0..2^24-1>; // length 0 means no ticket |
| 7610 | * uint32 ticket_lifetime; |
| 7611 | * uint8 mfl_code; // up to 255 according to standard |
| 7612 | * uint8 encrypt_then_mac; // 0 or 1 |
| 7613 | * } serialized_session_tls12; |
| 7614 | * |
| 7615 | */ |
| 7616 | static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, |
| 7617 | unsigned char *buf, |
| 7618 | size_t buf_len ) |
| 7619 | { |
| 7620 | unsigned char *p = buf; |
| 7621 | size_t used = 0; |
| 7622 | |
| 7623 | #if defined(MBEDTLS_HAVE_TIME) |
| 7624 | uint64_t start; |
| 7625 | #endif |
| 7626 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7627 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7628 | size_t cert_len; |
| 7629 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7630 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7631 | |
| 7632 | /* |
| 7633 | * Time |
| 7634 | */ |
| 7635 | #if defined(MBEDTLS_HAVE_TIME) |
| 7636 | used += 8; |
| 7637 | |
| 7638 | if( used <= buf_len ) |
| 7639 | { |
| 7640 | start = (uint64_t) session->start; |
| 7641 | |
| 7642 | MBEDTLS_PUT_UINT64_BE( start, p, 0 ); |
| 7643 | p += 8; |
| 7644 | } |
| 7645 | #endif /* MBEDTLS_HAVE_TIME */ |
| 7646 | |
| 7647 | /* |
| 7648 | * Basic mandatory fields |
| 7649 | */ |
| 7650 | used += 2 /* ciphersuite */ |
| 7651 | + 1 /* compression */ |
| 7652 | + 1 /* id_len */ |
| 7653 | + sizeof( session->id ) |
| 7654 | + sizeof( session->master ) |
| 7655 | + 4; /* verify_result */ |
| 7656 | |
| 7657 | if( used <= buf_len ) |
| 7658 | { |
| 7659 | MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); |
| 7660 | p += 2; |
| 7661 | |
| 7662 | *p++ = MBEDTLS_BYTE_0( session->compression ); |
| 7663 | |
| 7664 | *p++ = MBEDTLS_BYTE_0( session->id_len ); |
| 7665 | memcpy( p, session->id, 32 ); |
| 7666 | p += 32; |
| 7667 | |
| 7668 | memcpy( p, session->master, 48 ); |
| 7669 | p += 48; |
| 7670 | |
| 7671 | MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); |
| 7672 | p += 4; |
| 7673 | } |
| 7674 | |
| 7675 | /* |
| 7676 | * Peer's end-entity certificate |
| 7677 | */ |
| 7678 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7679 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7680 | if( session->peer_cert == NULL ) |
| 7681 | cert_len = 0; |
| 7682 | else |
| 7683 | cert_len = session->peer_cert->raw.len; |
| 7684 | |
| 7685 | used += 3 + cert_len; |
| 7686 | |
| 7687 | if( used <= buf_len ) |
| 7688 | { |
| 7689 | *p++ = MBEDTLS_BYTE_2( cert_len ); |
| 7690 | *p++ = MBEDTLS_BYTE_1( cert_len ); |
| 7691 | *p++ = MBEDTLS_BYTE_0( cert_len ); |
| 7692 | |
| 7693 | if( session->peer_cert != NULL ) |
| 7694 | { |
| 7695 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 7696 | p += cert_len; |
| 7697 | } |
| 7698 | } |
| 7699 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7700 | if( session->peer_cert_digest != NULL ) |
| 7701 | { |
| 7702 | used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len; |
| 7703 | if( used <= buf_len ) |
| 7704 | { |
| 7705 | *p++ = (unsigned char) session->peer_cert_digest_type; |
| 7706 | *p++ = (unsigned char) session->peer_cert_digest_len; |
| 7707 | memcpy( p, session->peer_cert_digest, |
| 7708 | session->peer_cert_digest_len ); |
| 7709 | p += session->peer_cert_digest_len; |
| 7710 | } |
| 7711 | } |
| 7712 | else |
| 7713 | { |
| 7714 | used += 2; |
| 7715 | if( used <= buf_len ) |
| 7716 | { |
| 7717 | *p++ = (unsigned char) MBEDTLS_MD_NONE; |
| 7718 | *p++ = 0; |
| 7719 | } |
| 7720 | } |
| 7721 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7722 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7723 | |
| 7724 | /* |
| 7725 | * Session ticket if any, plus associated data |
| 7726 | */ |
| 7727 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7728 | used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */ |
| 7729 | |
| 7730 | if( used <= buf_len ) |
| 7731 | { |
| 7732 | *p++ = MBEDTLS_BYTE_2( session->ticket_len ); |
| 7733 | *p++ = MBEDTLS_BYTE_1( session->ticket_len ); |
| 7734 | *p++ = MBEDTLS_BYTE_0( session->ticket_len ); |
| 7735 | |
| 7736 | if( session->ticket != NULL ) |
| 7737 | { |
| 7738 | memcpy( p, session->ticket, session->ticket_len ); |
| 7739 | p += session->ticket_len; |
| 7740 | } |
| 7741 | |
| 7742 | MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); |
| 7743 | p += 4; |
| 7744 | } |
| 7745 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7746 | |
| 7747 | /* |
| 7748 | * Misc extension-related info |
| 7749 | */ |
| 7750 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 7751 | used += 1; |
| 7752 | |
| 7753 | if( used <= buf_len ) |
| 7754 | *p++ = session->mfl_code; |
| 7755 | #endif |
| 7756 | |
| 7757 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7758 | used += 1; |
| 7759 | |
| 7760 | if( used <= buf_len ) |
| 7761 | *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); |
| 7762 | #endif |
| 7763 | |
| 7764 | return( used ); |
| 7765 | } |
| 7766 | |
Jerry Yu | 4f9e3ef | 2022-02-17 14:58:27 +0800 | [diff] [blame] | 7767 | static int ssl_session_load_tls12( mbedtls_ssl_session *session, |
| 7768 | const unsigned char *buf, |
| 7769 | size_t len ) |
| 7770 | { |
| 7771 | #if defined(MBEDTLS_HAVE_TIME) |
| 7772 | uint64_t start; |
| 7773 | #endif |
| 7774 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7775 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7776 | size_t cert_len; |
| 7777 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7778 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7779 | |
| 7780 | const unsigned char *p = buf; |
| 7781 | const unsigned char * const end = buf + len; |
| 7782 | |
| 7783 | /* |
| 7784 | * Time |
| 7785 | */ |
| 7786 | #if defined(MBEDTLS_HAVE_TIME) |
| 7787 | if( 8 > (size_t)( end - p ) ) |
| 7788 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7789 | |
| 7790 | start = ( (uint64_t) p[0] << 56 ) | |
| 7791 | ( (uint64_t) p[1] << 48 ) | |
| 7792 | ( (uint64_t) p[2] << 40 ) | |
| 7793 | ( (uint64_t) p[3] << 32 ) | |
| 7794 | ( (uint64_t) p[4] << 24 ) | |
| 7795 | ( (uint64_t) p[5] << 16 ) | |
| 7796 | ( (uint64_t) p[6] << 8 ) | |
| 7797 | ( (uint64_t) p[7] ); |
| 7798 | p += 8; |
| 7799 | |
| 7800 | session->start = (time_t) start; |
| 7801 | #endif /* MBEDTLS_HAVE_TIME */ |
| 7802 | |
| 7803 | /* |
| 7804 | * Basic mandatory fields |
| 7805 | */ |
| 7806 | if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) ) |
| 7807 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7808 | |
| 7809 | session->ciphersuite = ( p[0] << 8 ) | p[1]; |
| 7810 | p += 2; |
| 7811 | |
| 7812 | session->compression = *p++; |
| 7813 | |
| 7814 | session->id_len = *p++; |
| 7815 | memcpy( session->id, p, 32 ); |
| 7816 | p += 32; |
| 7817 | |
| 7818 | memcpy( session->master, p, 48 ); |
| 7819 | p += 48; |
| 7820 | |
| 7821 | session->verify_result = ( (uint32_t) p[0] << 24 ) | |
| 7822 | ( (uint32_t) p[1] << 16 ) | |
| 7823 | ( (uint32_t) p[2] << 8 ) | |
| 7824 | ( (uint32_t) p[3] ); |
| 7825 | p += 4; |
| 7826 | |
| 7827 | /* Immediately clear invalid pointer values that have been read, in case |
| 7828 | * we exit early before we replaced them with valid ones. */ |
| 7829 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7830 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7831 | session->peer_cert = NULL; |
| 7832 | #else |
| 7833 | session->peer_cert_digest = NULL; |
| 7834 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7835 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7836 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7837 | session->ticket = NULL; |
| 7838 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7839 | |
| 7840 | /* |
| 7841 | * Peer certificate |
| 7842 | */ |
| 7843 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 7844 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 7845 | /* Deserialize CRT from the end of the ticket. */ |
| 7846 | if( 3 > (size_t)( end - p ) ) |
| 7847 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7848 | |
| 7849 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 7850 | p += 3; |
| 7851 | |
| 7852 | if( cert_len != 0 ) |
| 7853 | { |
| 7854 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 7855 | |
| 7856 | if( cert_len > (size_t)( end - p ) ) |
| 7857 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7858 | |
| 7859 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 7860 | |
| 7861 | if( session->peer_cert == NULL ) |
| 7862 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7863 | |
| 7864 | mbedtls_x509_crt_init( session->peer_cert ); |
| 7865 | |
| 7866 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 7867 | p, cert_len ) ) != 0 ) |
| 7868 | { |
| 7869 | mbedtls_x509_crt_free( session->peer_cert ); |
| 7870 | mbedtls_free( session->peer_cert ); |
| 7871 | session->peer_cert = NULL; |
| 7872 | return( ret ); |
| 7873 | } |
| 7874 | |
| 7875 | p += cert_len; |
| 7876 | } |
| 7877 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7878 | /* Deserialize CRT digest from the end of the ticket. */ |
| 7879 | if( 2 > (size_t)( end - p ) ) |
| 7880 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7881 | |
| 7882 | session->peer_cert_digest_type = (mbedtls_md_type_t) *p++; |
| 7883 | session->peer_cert_digest_len = (size_t) *p++; |
| 7884 | |
| 7885 | if( session->peer_cert_digest_len != 0 ) |
| 7886 | { |
| 7887 | const mbedtls_md_info_t *md_info = |
| 7888 | mbedtls_md_info_from_type( session->peer_cert_digest_type ); |
| 7889 | if( md_info == NULL ) |
| 7890 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7891 | if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) ) |
| 7892 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7893 | |
| 7894 | if( session->peer_cert_digest_len > (size_t)( end - p ) ) |
| 7895 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7896 | |
| 7897 | session->peer_cert_digest = |
| 7898 | mbedtls_calloc( 1, session->peer_cert_digest_len ); |
| 7899 | if( session->peer_cert_digest == NULL ) |
| 7900 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7901 | |
| 7902 | memcpy( session->peer_cert_digest, p, |
| 7903 | session->peer_cert_digest_len ); |
| 7904 | p += session->peer_cert_digest_len; |
| 7905 | } |
| 7906 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 7907 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 7908 | |
| 7909 | /* |
| 7910 | * Session ticket and associated data |
| 7911 | */ |
| 7912 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 7913 | if( 3 > (size_t)( end - p ) ) |
| 7914 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7915 | |
| 7916 | session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 7917 | p += 3; |
| 7918 | |
| 7919 | if( session->ticket_len != 0 ) |
| 7920 | { |
| 7921 | if( session->ticket_len > (size_t)( end - p ) ) |
| 7922 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7923 | |
| 7924 | session->ticket = mbedtls_calloc( 1, session->ticket_len ); |
| 7925 | if( session->ticket == NULL ) |
| 7926 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 7927 | |
| 7928 | memcpy( session->ticket, p, session->ticket_len ); |
| 7929 | p += session->ticket_len; |
| 7930 | } |
| 7931 | |
| 7932 | if( 4 > (size_t)( end - p ) ) |
| 7933 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7934 | |
| 7935 | session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) | |
| 7936 | ( (uint32_t) p[1] << 16 ) | |
| 7937 | ( (uint32_t) p[2] << 8 ) | |
| 7938 | ( (uint32_t) p[3] ); |
| 7939 | p += 4; |
| 7940 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 7941 | |
| 7942 | /* |
| 7943 | * Misc extension-related info |
| 7944 | */ |
| 7945 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 7946 | if( 1 > (size_t)( end - p ) ) |
| 7947 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7948 | |
| 7949 | session->mfl_code = *p++; |
| 7950 | #endif |
| 7951 | |
| 7952 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 7953 | if( 1 > (size_t)( end - p ) ) |
| 7954 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7955 | |
| 7956 | session->encrypt_then_mac = *p++; |
| 7957 | #endif |
| 7958 | |
| 7959 | /* Done, should have consumed entire buffer */ |
| 7960 | if( p != end ) |
| 7961 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 7962 | |
| 7963 | return( 0 ); |
| 7964 | } |
Jerry Yu | dc7bd17 | 2022-02-17 13:44:15 +0800 | [diff] [blame] | 7965 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 7966 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7967 | #endif /* MBEDTLS_SSL_TLS_C */ |